diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/CacheTestSupport.java lucene4-directory/src/test/java/org/infinispan/lucene/CacheTestSupport.java
*** lucene-directory/src/test/java/org/infinispan/lucene/CacheTestSupport.java	2013-01-07 11:38:20.838581067 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/CacheTestSupport.java	2013-01-07 16:06:30.686429731 +0000
***************
*** 158,189 ****
           // can happen
        } finally {
           if (writer != null) {
              writer.close();
              log.info("IndexWriter was closed");
           }
        }
     }
  
     protected static void doReadOperation(Directory d) throws Exception {
        IndexSearcher search = null;
        try {
!          IndexReader indexReader = IndexReader.open(d);
           // this is a read
           search = new IndexSearcher(indexReader);
           // dummy query that probably won't return anything
           Term term = new Term( "path", "good" );
           TermQuery termQuery = new TermQuery(term);
           search.search(termQuery, null, 1);
        } finally {
           if (search != null) {
!             search.close();
           }
        }
     }
  
     public static void initializeDirectory(Directory directory) throws IOException {
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(LuceneSettings.LUCENE_VERSION, LuceneSettings.analyzer);
        IndexWriter iwriter = new IndexWriter(directory, indexWriterConfig);
        iwriter.commit();
        iwriter.close();
        //reopen to check for index
--- 158,190 ----
           // can happen
        } finally {
           if (writer != null) {
              writer.close();
              log.info("IndexWriter was closed");
           }
        }
     }
  
     protected static void doReadOperation(Directory d) throws Exception {
+    	IndexReader indexReader = null;
     	IndexSearcher search = null;
        try {
!          indexReader = IndexReader.open(d);
           // this is a read
           search = new IndexSearcher(indexReader);
           // dummy query that probably won't return anything
           Term term = new Term( "path", "good" );
           TermQuery termQuery = new TermQuery(term);
           search.search(termQuery, null, 1);
        } finally {
           if (search != null) {
!          	indexReader.close();
           }
        }
     }
  
     public static void initializeDirectory(Directory directory) throws IOException {
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(LuceneSettings.LUCENE_VERSION, LuceneSettings.analyzer);
        IndexWriter iwriter = new IndexWriter(directory, indexWriterConfig);
        iwriter.commit();
        iwriter.close();
        //reopen to check for index
***************
*** 222,242 ****
        TopDocs docs = searcher.search(query, null, expectedResults + 1);
        assert docs.totalHits == expectedResults;
        for (ScoreDoc scoreDoc : docs.scoreDocs) {
           int docId = scoreDoc.doc;
           Document document = searcher.doc(docId);
           String idString = document.get("id");
           assert idString != null;
           Integer idFoundElement = Integer.valueOf(idString);
           assert expectedDocumendIds.contains(idFoundElement);
        }
-       searcher.close();
        reader.close();
     }
  
     /**
      * Used in test to add a new Document to an Index; two fields are created: id and body 
      * 
      * @param dir The Directory containing the Index to modify
      * @param id a sequential number to identify this document (id field)
      * @param text Some text to add to the body field
      * @throws IOException
--- 223,242 ----
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryIOTest.java lucene4-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryIOTest.java
*** lucene-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryIOTest.java	2013-01-07 11:38:20.838581067 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryIOTest.java	2013-01-07 16:08:39.876452372 +0000
***************
*** 24,43 ****
--- 24,44 ----
  
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.IOException;
  import java.util.Arrays;
  import java.util.HashSet;
  import java.util.Random;
  import java.util.Set;
  
  import org.apache.lucene.store.Directory;
+ import org.apache.lucene.store.IOContext;
  import org.apache.lucene.store.IndexInput;
  import org.apache.lucene.store.IndexOutput;
  import org.apache.lucene.store.RAMDirectory;
  import org.infinispan.Cache;
  import org.infinispan.lucene.testutils.RepeatableLongByteSequence;
  import org.infinispan.manager.CacheContainer;
  import org.infinispan.test.TestingUtil;
  import org.testng.Assert;
  import org.testng.annotations.AfterMethod;
  import org.testng.annotations.AfterTest;
***************
*** 76,96 ****
     }
  
     @Test
     public void testWriteUsingSeekMethod() throws IOException {
        final int BUFFER_SIZE = 64;
        
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, BUFFER_SIZE);
        
        String fileName = "SomeText.txt";
!       IndexOutput io = dir.createOutput(fileName);
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
        //It writes repeatable text
        final int REPEATABLE_BUFFER_SIZE = 1501;
        for (int i = 0; i < REPEATABLE_BUFFER_SIZE; i++) {
           io.writeByte(bytesGenerator.nextByte());
        }
        io.flush();
        assert io.length() == REPEATABLE_BUFFER_SIZE;
        
        //Text to write on file with repeatable text
--- 77,97 ----
     }
  
     @Test
     public void testWriteUsingSeekMethod() throws IOException {
        final int BUFFER_SIZE = 64;
        
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, BUFFER_SIZE);
        
        String fileName = "SomeText.txt";
!       IndexOutput io = dir.createOutput(fileName, IOContext.DEFAULT);
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
        //It writes repeatable text
        final int REPEATABLE_BUFFER_SIZE = 1501;
        for (int i = 0; i < REPEATABLE_BUFFER_SIZE; i++) {
           io.writeByte(bytesGenerator.nextByte());
        }
        io.flush();
        assert io.length() == REPEATABLE_BUFFER_SIZE;
        
        //Text to write on file with repeatable text
***************
*** 190,210 ****
     public void testReadRandomSampleFile() throws IOException {
        final int BUFFER_SIZE = 64;
  
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, BUFFER_SIZE);
  
        final int FILE_SIZE = 1000;
        assert BUFFER_SIZE < FILE_SIZE;
        createFileWithRepeatableContent(dir, "RandomSampleFile.txt", FILE_SIZE);
  
!       IndexInput indexInput = dir.openInput("RandomSampleFile.txt");
        assert indexInput.length() == FILE_SIZE;
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
  
        Random r = new Random();
        long seekPoint = 0;
        // Now it reads some random byte and it compares to the expected byte
        for (int i = 0; i < FILE_SIZE; i++) {
           if (seekPoint == i) {
              byte expectedByte = bytesGenerator.nextByte();
              byte actualByte = indexInput.readByte();
--- 191,211 ----
     public void testReadRandomSampleFile() throws IOException {
        final int BUFFER_SIZE = 64;
  
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, BUFFER_SIZE);
  
        final int FILE_SIZE = 1000;
        assert BUFFER_SIZE < FILE_SIZE;
        createFileWithRepeatableContent(dir, "RandomSampleFile.txt", FILE_SIZE);
  
!       IndexInput indexInput = dir.openInput("RandomSampleFile.txt", IOContext.DEFAULT);
        assert indexInput.length() == FILE_SIZE;
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
  
        Random r = new Random();
        long seekPoint = 0;
        // Now it reads some random byte and it compares to the expected byte
        for (int i = 0; i < FILE_SIZE; i++) {
           if (seekPoint == i) {
              byte expectedByte = bytesGenerator.nextByte();
              byte actualByte = indexInput.readByte();
***************
*** 230,250 ****
      * @param fileName
      *           The file name to read
      * @param contentFileSizeExpected
      *           The size content file expected
      * @param arrayLengthToRead
      *           The length of byte array to read
      * @throws IOException
      */
     private void assertReadBytesWorkingCorrectly(InfinispanDirectory dir, String fileName,
              final int contentFileSizeExpected, final int arrayLengthToRead) throws IOException {
!       IndexInput indexInput = dir.openInput(fileName);
        assert indexInput.length() == contentFileSizeExpected;
  
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
  
        byte[] readBytes = new byte[arrayLengthToRead];
        byte[] expectedBytes = new byte[arrayLengthToRead];
  
        long toRead = contentFileSizeExpected;
        while (toRead > 0) {
           // the condition is satisfied when the file is close to the end
--- 231,251 ----
      * @param fileName
      *           The file name to read
      * @param contentFileSizeExpected
      *           The size content file expected
      * @param arrayLengthToRead
      *           The length of byte array to read
      * @throws IOException
      */
     private void assertReadBytesWorkingCorrectly(InfinispanDirectory dir, String fileName,
              final int contentFileSizeExpected, final int arrayLengthToRead) throws IOException {
!       IndexInput indexInput = dir.openInput(fileName, IOContext.DEFAULT);
        assert indexInput.length() == contentFileSizeExpected;
  
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
  
        byte[] readBytes = new byte[arrayLengthToRead];
        byte[] expectedBytes = new byte[arrayLengthToRead];
  
        long toRead = contentFileSizeExpected;
        while (toRead > 0) {
           // the condition is satisfied when the file is close to the end
***************
*** 272,292 ****
      * @param dir
      *           The Directory containing the file to verify
      * @param fileName
      *           The file name to read
      * @param contentFileSizeExpected
      *           The size content file expected
      * @throws IOException
      */
     private void assertReadByteWorkingCorrectly(InfinispanDirectory dir, String fileName,
              final int contentFileSizeExpected) throws IOException {
!       IndexInput indexInput = dir.openInput(fileName);
        assert indexInput.length() == contentFileSizeExpected;
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
  
        for (int i = 0; i < contentFileSizeExpected; i++) {
           assert bytesGenerator.nextByte() == indexInput.readByte();
        }
        indexInput.close();
     }
  
     /**
--- 273,293 ----
      * @param dir
      *           The Directory containing the file to verify
      * @param fileName
      *           The file name to read
      * @param contentFileSizeExpected
      *           The size content file expected
      * @throws IOException
      */
     private void assertReadByteWorkingCorrectly(InfinispanDirectory dir, String fileName,
              final int contentFileSizeExpected) throws IOException {
!       IndexInput indexInput = dir.openInput(fileName, IOContext.DEFAULT);
        assert indexInput.length() == contentFileSizeExpected;
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
  
        for (int i = 0; i < contentFileSizeExpected; i++) {
           assert bytesGenerator.nextByte() == indexInput.readByte();
        }
        indexInput.close();
     }
  
     /**
***************
*** 308,328 ****
      * 
      * @param dir
      *           The Directory containing the file to create
      * @param fileName
      *           The file name to create
      * @param contentFileSize
      *           The size content file to create
      * @throws IOException
      */
     private void createFileWithRepeatableContent(InfinispanDirectory dir, String fileName, final int contentFileSize) throws IOException {
!       IndexOutput indexOutput = dir.createOutput(fileName);
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
        for (int i = 0; i < contentFileSize; i++) {
           indexOutput.writeByte(bytesGenerator.nextByte());
        }
        indexOutput.close();
     }
  
     
     @Test(enabled = false)
     public void testReadChunks() throws Exception {
--- 309,329 ----
      * 
      * @param dir
      *           The Directory containing the file to create
      * @param fileName
      *           The file name to create
      * @param contentFileSize
      *           The size content file to create
      * @throws IOException
      */
     private void createFileWithRepeatableContent(InfinispanDirectory dir, String fileName, final int contentFileSize) throws IOException {
!       IndexOutput indexOutput = dir.createOutput(fileName, IOContext.DEFAULT);
        RepeatableLongByteSequence bytesGenerator = new RepeatableLongByteSequence();
        for (int i = 0; i < contentFileSize; i++) {
           indexOutput.writeByte(bytesGenerator.nextByte());
        }
        indexOutput.close();
     }
  
     
     @Test(enabled = false)
     public void testReadChunks() throws Exception {
***************
*** 366,429 ****
        file2.setSize(worldText.length());
  
        Set<String> s = new HashSet<String>();
        s.add("Hello.txt");
        s.add("World.txt");
        Set other = new HashSet(Arrays.asList(dir.list()));
  
        // ok, file listing works.
        assert s.equals(other);
  
!       IndexInput ii = dir.openInput("Hello.txt");
  
        assert ii.length() == helloText.length();
  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
        for (int i = 0; i < ii.length(); i++) {
           baos.write(ii.readByte());
        }
  
        assert new String(baos.toByteArray()).equals(helloText);
  
!       ii = dir.openInput("World.txt");
  
        assert ii.length() == worldText.length();
  
        baos = new ByteArrayOutputStream();
  
        for (int i = 0; i < ii.length(); i++) {
           baos.write(ii.readByte());
        }
  
        assert new String(baos.toByteArray()).equals(worldText);
  
        // now with buffered reading
  
!       ii = dir.openInput("Hello.txt");
  
        assert ii.length() == helloText.length();
  
        baos = new ByteArrayOutputStream();
  
        long toRead = ii.length();
        while (toRead > 0) {
           buf = new byte[19]; // suitably arbitrary
           int bytesRead = (int) Math.min(toRead, 19);
           ii.readBytes(buf, 0, bytesRead);
           toRead = toRead - bytesRead;
           baos.write(buf, 0, bytesRead);
        }
  
        assert new String(baos.toByteArray()).equals(helloText);
  
!       ii = dir.openInput("World.txt");
  
        assert ii.length() == worldText.length();
  
        baos = new ByteArrayOutputStream();
  
        toRead = ii.length();
        while (toRead > 0) {
           buf = new byte[19]; // suitably arbitrary
           int bytesRead = (int) Math.min(toRead, 19);
           ii.readBytes(buf, 0, bytesRead);
--- 367,430 ----
        file2.setSize(worldText.length());
  
        Set<String> s = new HashSet<String>();
        s.add("Hello.txt");
        s.add("World.txt");
        Set other = new HashSet(Arrays.asList(dir.list()));
  
        // ok, file listing works.
        assert s.equals(other);
  
!       IndexInput ii = dir.openInput("Hello.txt", IOContext.DEFAULT);
  
        assert ii.length() == helloText.length();
  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
        for (int i = 0; i < ii.length(); i++) {
           baos.write(ii.readByte());
        }
  
        assert new String(baos.toByteArray()).equals(helloText);
  
!       ii = dir.openInput("World.txt", IOContext.DEFAULT);
  
        assert ii.length() == worldText.length();
  
        baos = new ByteArrayOutputStream();
  
        for (int i = 0; i < ii.length(); i++) {
           baos.write(ii.readByte());
        }
  
        assert new String(baos.toByteArray()).equals(worldText);
  
        // now with buffered reading
  
!       ii = dir.openInput("Hello.txt", IOContext.DEFAULT);
  
        assert ii.length() == helloText.length();
  
        baos = new ByteArrayOutputStream();
  
        long toRead = ii.length();
        while (toRead > 0) {
           buf = new byte[19]; // suitably arbitrary
           int bytesRead = (int) Math.min(toRead, 19);
           ii.readBytes(buf, 0, bytesRead);
           toRead = toRead - bytesRead;
           baos.write(buf, 0, bytesRead);
        }
  
        assert new String(baos.toByteArray()).equals(helloText);
  
!       ii = dir.openInput("World.txt", IOContext.DEFAULT);
  
        assert ii.length() == worldText.length();
  
        baos = new ByteArrayOutputStream();
  
        toRead = ii.length();
        while (toRead > 0) {
           buf = new byte[19]; // suitably arbitrary
           int bytesRead = (int) Math.min(toRead, 19);
           ii.readBytes(buf, 0, bytesRead);
***************
*** 444,464 ****
        dir.renameFile("World.txt", "HelloWorld.txt");
        assert null == cache.get(new FileCacheKey(INDEXNAME, "Hello.txt"));
        assert null == cache.get(new ChunkCacheKey(INDEXNAME, "Hello.txt", 0, BUFFER_SIZE));
        assert null == cache.get(new ChunkCacheKey(INDEXNAME, "Hello.txt", 1, BUFFER_SIZE));
  
        assert cache.get(new FileCacheKey(INDEXNAME, "HelloWorld.txt")).equals(ob1);
        assert cache.get(new ChunkCacheKey(INDEXNAME, "HelloWorld.txt", 0, BUFFER_SIZE)).equals(ob2);
        assert cache.get(new ChunkCacheKey(INDEXNAME, "HelloWorld.txt", 1, BUFFER_SIZE)).equals(ob3);
  
        // test that contents survives a move
!       ii = dir.openInput("HelloWorld.txt");
  
        assert ii.length() == worldText.length();
  
        baos = new ByteArrayOutputStream();
  
        toRead = ii.length();
        while (toRead > 0) {
           buf = new byte[19]; // suitably arbitrary
           int bytesRead = (int) Math.min(toRead, 19);
           ii.readBytes(buf, 0, bytesRead);
--- 445,465 ----
        dir.renameFile("World.txt", "HelloWorld.txt");
        assert null == cache.get(new FileCacheKey(INDEXNAME, "Hello.txt"));
        assert null == cache.get(new ChunkCacheKey(INDEXNAME, "Hello.txt", 0, BUFFER_SIZE));
        assert null == cache.get(new ChunkCacheKey(INDEXNAME, "Hello.txt", 1, BUFFER_SIZE));
  
        assert cache.get(new FileCacheKey(INDEXNAME, "HelloWorld.txt")).equals(ob1);
        assert cache.get(new ChunkCacheKey(INDEXNAME, "HelloWorld.txt", 0, BUFFER_SIZE)).equals(ob2);
        assert cache.get(new ChunkCacheKey(INDEXNAME, "HelloWorld.txt", 1, BUFFER_SIZE)).equals(ob3);
  
        // test that contents survives a move
!       ii = dir.openInput("HelloWorld.txt", IOContext.DEFAULT);
  
        assert ii.length() == worldText.length();
  
        baos = new ByteArrayOutputStream();
  
        toRead = ii.length();
        while (toRead > 0) {
           buf = new byte[19]; // suitably arbitrary
           int bytesRead = (int) Math.min(toRead, 19);
           ii.readBytes(buf, 0, bytesRead);
***************
*** 471,511 ****
        dir.close();
        DirectoryIntegrityCheck.verifyDirectoryStructure(cache, INDEXNAME);
     }
  
     public void testWriteChunks() throws Exception {
        final int BUFFER_SIZE = 64;
  
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, BUFFER_SIZE);
  
!       IndexOutput io = dir.createOutput("MyNewFile.txt");
  
        io.writeByte((byte) 66);
        io.writeByte((byte) 69);
  
        io.flush();
        io.close();
  
        assert dir.fileExists("MyNewFile.txt");
        assert null != cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, BUFFER_SIZE));
  
        // test contents by reading:
        byte[] buf = new byte[9];
!       IndexInput ii = dir.openInput("MyNewFile.txt");
        ii.readBytes(buf, 0, (int) ii.length());
        ii.close();
  
        assert new String(new byte[] { 66, 69 }).equals(new String(buf).trim());
  
        String testText = "This is some rubbish again that will span more than one chunk - one hopes.  Who knows, maybe even three or four chunks.";
!       io = dir.createOutput("MyNewFile.txt");
        io.seek(0);
        io.writeBytes(testText.getBytes(), 0, testText.length());
        io.close();
        // now compare.
        byte[] chunk1 = (byte[]) cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, BUFFER_SIZE));
        byte[] chunk2 = (byte[]) cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 1, BUFFER_SIZE));
        assert null != chunk1;
        assert null != chunk2;
  
        assert testText.equals(new String(chunk1) + new String(chunk2).trim());
--- 472,512 ----
        dir.close();
        DirectoryIntegrityCheck.verifyDirectoryStructure(cache, INDEXNAME);
     }
  
     public void testWriteChunks() throws Exception {
        final int BUFFER_SIZE = 64;
  
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, BUFFER_SIZE);
  
!       IndexOutput io = dir.createOutput("MyNewFile.txt", IOContext.DEFAULT);
  
        io.writeByte((byte) 66);
        io.writeByte((byte) 69);
  
        io.flush();
        io.close();
  
        assert dir.fileExists("MyNewFile.txt");
        assert null != cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, BUFFER_SIZE));
  
        // test contents by reading:
        byte[] buf = new byte[9];
!       IndexInput ii = dir.openInput("MyNewFile.txt", IOContext.DEFAULT);
        ii.readBytes(buf, 0, (int) ii.length());
        ii.close();
  
        assert new String(new byte[] { 66, 69 }).equals(new String(buf).trim());
  
        String testText = "This is some rubbish again that will span more than one chunk - one hopes.  Who knows, maybe even three or four chunks.";
!       io = dir.createOutput("MyNewFile.txt", IOContext.DEFAULT);
        io.seek(0);
        io.writeBytes(testText.getBytes(), 0, testText.length());
        io.close();
        // now compare.
        byte[] chunk1 = (byte[]) cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, BUFFER_SIZE));
        byte[] chunk2 = (byte[]) cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 1, BUFFER_SIZE));
        assert null != chunk1;
        assert null != chunk2;
  
        assert testText.equals(new String(chunk1) + new String(chunk2).trim());
***************
*** 515,551 ****
     }
  
     @Test
     public void testWriteChunksDefaultChunks() throws Exception {
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, INDEXNAME);
  
        final String testText = "This is some rubbish";
        final byte[] testTextAsBytes = testText.getBytes();
  
!       IndexOutput io = dir.createOutput("MyNewFile.txt");
  
        io.writeByte((byte) 1);
        io.writeByte((byte) 2);
        io.writeByte((byte) 3);
        io.writeBytes(testTextAsBytes, testTextAsBytes.length);
        io.close();
        DirectoryIntegrityCheck.verifyDirectoryStructure(cache, INDEXNAME);
  
        FileCacheKey fileCacheKey = new FileCacheKey(INDEXNAME, "MyNewFile.txt");
        assert null != cache.get(fileCacheKey);
        FileMetadata metadata = (FileMetadata) cache.get(fileCacheKey);
        Assert.assertEquals(testTextAsBytes.length + 3, metadata.getSize());
        assert null != cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, InfinispanDirectory.DEFAULT_BUFFER_SIZE));
  
        // test contents by reading:
!       IndexInput ii = dir.openInput("MyNewFile.txt");
        assert ii.readByte() == 1;
        assert ii.readByte() == 2;
        assert ii.readByte() == 3;
        byte[] buf = new byte[testTextAsBytes.length];
  
        ii.readBytes(buf, 0, testTextAsBytes.length);
        ii.close();
  
        assert testText.equals(new String(buf).trim());
  
--- 516,552 ----
     }
  
     @Test
     public void testWriteChunksDefaultChunks() throws Exception {
        Cache cache = cacheManager.getCache();
        InfinispanDirectory dir = new InfinispanDirectory(cache, INDEXNAME);
  
        final String testText = "This is some rubbish";
        final byte[] testTextAsBytes = testText.getBytes();
  
!       IndexOutput io = dir.createOutput("MyNewFile.txt", IOContext.DEFAULT);
  
        io.writeByte((byte) 1);
        io.writeByte((byte) 2);
        io.writeByte((byte) 3);
        io.writeBytes(testTextAsBytes, testTextAsBytes.length);
        io.close();
        DirectoryIntegrityCheck.verifyDirectoryStructure(cache, INDEXNAME);
  
        FileCacheKey fileCacheKey = new FileCacheKey(INDEXNAME, "MyNewFile.txt");
        assert null != cache.get(fileCacheKey);
        FileMetadata metadata = (FileMetadata) cache.get(fileCacheKey);
        Assert.assertEquals(testTextAsBytes.length + 3, metadata.getSize());
        assert null != cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, InfinispanDirectory.DEFAULT_BUFFER_SIZE));
  
        // test contents by reading:
!       IndexInput ii = dir.openInput("MyNewFile.txt", IOContext.DEFAULT);
        assert ii.readByte() == 1;
        assert ii.readByte() == 2;
        assert ii.readByte() == 3;
        byte[] buf = new byte[testTextAsBytes.length];
  
        ii.readBytes(buf, 0, testTextAsBytes.length);
        ii.close();
  
        assert testText.equals(new String(buf).trim());
  
***************
*** 608,664 ****
        testOn(dir, 40 ,39, cache);
        testOn(dir, 39 ,40, cache);
        
        // quite bigger
        testOn(dir, 600, 600, cache);
     }
  
     private void testOn(Directory dir, int writeSize, int readSize, Cache cache) throws IOException {
        if (cache != null) cache.clear();//needed to make sure no chunks are left over in case of Infinispan implementation
        final String filename = "chunkTest";
!       IndexOutput indexOutput = dir.createOutput(filename);
        byte[] toWrite = fillBytes(writeSize);
        indexOutput.writeBytes(toWrite, writeSize);
        indexOutput.close();
        if (cache != null) {
           assert writeSize == DirectoryIntegrityCheck.deepCountFileSize(new FileCacheKey(INDEXNAME,filename), cache);
        }
        assert indexOutput.length() == writeSize;
        byte[] results = new byte[readSize];
!       IndexInput openInput = dir.openInput(filename);
        try {
           openInput.readBytes(results, 0, readSize);
           for (int i = 0; i < writeSize && i < readSize; i++) {
              assert toWrite[i] == results[i];
           }
           if (readSize > writeSize)
              assert false : "should have thrown an IOException for reading past EOF";
        } catch (IOException ioe) {
           if (readSize <= writeSize)
              assert false :"should not have thrown an IOException" + ioe.getMessage();
        }
     }
     
     public void multipleFlushTest() throws IOException {
        final String filename = "longFile.writtenInMultipleFlushes";
        final int bufferSize = 300;
        Cache cache = cacheManager.getCache();
        cache.clear();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, 13);
        byte[] manyBytes = fillBytes(bufferSize);
!       IndexOutput indexOutput = dir.createOutput(filename);
        for (int i = 0; i < 10; i++) {
           indexOutput.writeBytes(manyBytes, bufferSize);
           indexOutput.flush();
        }
        indexOutput.close();
!       IndexInput input = dir.openInput(filename);
        final int finalSize = (10 * bufferSize);
        assert input.length() == finalSize;
        final byte[] resultingBuffer = new byte[finalSize];
        input.readBytes(resultingBuffer, 0, finalSize);
        int index = 0;
        for (int i = 0; i < 10; i++) {
           for (int j = 0; j < bufferSize; j++)
              assert resultingBuffer[index++] == manyBytes[j];
        }
     }
--- 609,665 ----
        testOn(dir, 40 ,39, cache);
        testOn(dir, 39 ,40, cache);
        
        // quite bigger
        testOn(dir, 600, 600, cache);
     }
  
     private void testOn(Directory dir, int writeSize, int readSize, Cache cache) throws IOException {
        if (cache != null) cache.clear();//needed to make sure no chunks are left over in case of Infinispan implementation
        final String filename = "chunkTest";
!       IndexOutput indexOutput = dir.createOutput(filename, IOContext.DEFAULT);
        byte[] toWrite = fillBytes(writeSize);
        indexOutput.writeBytes(toWrite, writeSize);
        indexOutput.close();
        if (cache != null) {
           assert writeSize == DirectoryIntegrityCheck.deepCountFileSize(new FileCacheKey(INDEXNAME,filename), cache);
        }
        assert indexOutput.length() == writeSize;
        byte[] results = new byte[readSize];
!       IndexInput openInput = dir.openInput(filename, IOContext.DEFAULT);
        try {
           openInput.readBytes(results, 0, readSize);
           for (int i = 0; i < writeSize && i < readSize; i++) {
              assert toWrite[i] == results[i];
           }
           if (readSize > writeSize)
              assert false : "should have thrown an IOException for reading past EOF";
        } catch (IOException ioe) {
           if (readSize <= writeSize)
              assert false :"should not have thrown an IOException" + ioe.getMessage();
        }
     }
     
     public void multipleFlushTest() throws IOException {
        final String filename = "longFile.writtenInMultipleFlushes";
        final int bufferSize = 300;
        Cache cache = cacheManager.getCache();
        cache.clear();
        InfinispanDirectory dir = new InfinispanDirectory(cache, cache, cache, INDEXNAME, 13);
        byte[] manyBytes = fillBytes(bufferSize);
!       IndexOutput indexOutput = dir.createOutput(filename, IOContext.DEFAULT);
        for (int i = 0; i < 10; i++) {
           indexOutput.writeBytes(manyBytes, bufferSize);
           indexOutput.flush();
        }
        indexOutput.close();
!       IndexInput input = dir.openInput(filename, IOContext.DEFAULT);
        final int finalSize = (10 * bufferSize);
        assert input.length() == finalSize;
        final byte[] resultingBuffer = new byte[finalSize];
        input.readBytes(resultingBuffer, 0, finalSize);
        int index = 0;
        for (int i = 0; i < 10; i++) {
           for (int j = 0; j < bufferSize; j++)
              assert resultingBuffer[index++] == manyBytes[j];
        }
     }
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryStressTest.java lucene4-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryStressTest.java
*** lucene-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryStressTest.java	2013-01-07 11:38:20.838581067 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/InfinispanDirectoryStressTest.java	2013-01-07 16:09:02.906455927 +0000
***************
*** 70,90 ****
           CacheTestSupport.doWriteOperation(directory, document);
           CacheTestSupport.doReadOperation(directory);
        }
  
        IndexReader ir = IndexReader.open(directory);
        IndexSearcher search = new IndexSearcher(ir);
        Term t = new Term("info", "good");
        Query query = new TermQuery(t);
        TopDocs hits = search.search(query, 1);
  
-       search.close();
        ir.close();
  
        assert OPERATIONS == hits.totalHits;
  
        directory.close();
        cacheContainer.stop();
     }
  
     public void testDirectoryWithMultipleThreads() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
--- 70,89 ----
***************
*** 127,152 ****
           }
        }
  
        IndexReader indexReader1 = IndexReader.open(directory1);
        IndexSearcher search = new IndexSearcher(indexReader1);
        Term t = new Term("info", "good");
        Query query = new TermQuery(t);
        int expectedDocs = writeCount.get();
        TopDocs hits = search.search(query, 1);
  
-       search.close();
        indexReader1.close();
  
        assert expectedDocs == hits.totalHits;
  
-       search.close();
        directory1.close();
        directory2.close();
        cache.getCacheManager().stop();
        cache2.getCacheManager().stop();
     }
  
     class InfinispanDirectoryThread extends Thread {
        Exception e;
        CountDownLatch latch;
        File document;
--- 126,149 ----
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/profiling/DynamicTopologyStressTest.java lucene4-directory/src/test/java/org/infinispan/lucene/profiling/DynamicTopologyStressTest.java
*** lucene-directory/src/test/java/org/infinispan/lucene/profiling/DynamicTopologyStressTest.java	2013-01-07 11:38:20.841914402 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/profiling/DynamicTopologyStressTest.java	2013-01-07 16:10:13.339800068 +0000
***************
*** 185,205 ****
        public ConstantReadingThread(Directory masterDirectory, SharedState sharedIndexState) {
           this.masterDirectory = masterDirectory;
        }
  
        @Override
        public void run() {
           while (lastWrittenTermId < (SECONDS_TOTAL * WRITES_PER_SECOND) && failed.get() == false) {
              IndexReader indexReader = null;
              try {
                 int i = lastWrittenTermId;
!                indexReader = IndexReader.open(masterDirectory, true);
                 IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                 if (i==0) continue; // nothing written yet
                 String termValue = "HA" + i;
                 Query query = new TermQuery(new Term( FIELDNAME, termValue));
                 TopDocs docs = indexSearcher.search(query, null, 1);
                 if (docs.totalHits != 1) {
                    failed.set(true);
                    log.error("String '" + termValue + "' should exist but was not found in index");
                 }
                 if (VISUAL_PROGRESS_FEEDBACK) System.out.print(".");
--- 185,205 ----
        public ConstantReadingThread(Directory masterDirectory, SharedState sharedIndexState) {
           this.masterDirectory = masterDirectory;
        }
  
        @Override
        public void run() {
           while (lastWrittenTermId < (SECONDS_TOTAL * WRITES_PER_SECOND) && failed.get() == false) {
              IndexReader indexReader = null;
              try {
                 int i = lastWrittenTermId;
!                indexReader = IndexReader.open(masterDirectory);
                 IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                 if (i==0) continue; // nothing written yet
                 String termValue = "HA" + i;
                 Query query = new TermQuery(new Term( FIELDNAME, termValue));
                 TopDocs docs = indexSearcher.search(query, null, 1);
                 if (docs.totalHits != 1) {
                    failed.set(true);
                    log.error("String '" + termValue + "' should exist but was not found in index");
                 }
                 if (VISUAL_PROGRESS_FEEDBACK) System.out.print(".");
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/profiling/IndexReadingStressTest.java lucene4-directory/src/test/java/org/infinispan/lucene/profiling/IndexReadingStressTest.java
*** lucene-directory/src/test/java/org/infinispan/lucene/profiling/IndexReadingStressTest.java	2013-01-07 11:38:20.841914402 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/profiling/IndexReadingStressTest.java	2013-01-07 16:13:30.423160984 +0000
***************
*** 183,203 ****
           this.increment = increment;
           this.max = max;
           indexReader = IndexReader.open(directory);
           searcher = new IndexSearcher(indexReader);
        }
        
        @Override
        protected void testLoop() throws IOException {
           Term t = new Term("main", "0");
           for (int i = startValue; i <= max && state.needToQuit() == false; i += increment) {
!             Term termToQuery = t.createTerm(Integer.toString(i));
              Query query = new TermQuery(termToQuery);
              TopDocs docs = searcher.search(query, null, 1);
              if (i % 2 == 0 && docs.totalHits != 1) {
                 //Even values should be found in the index
                 throw new RuntimeException("String '" + String.valueOf(i) + "' should exist but was not found in index");
              } else if (i % 2 == 1 && docs.totalHits != 0) {
                 //Uneven values should not be found
                 throw new RuntimeException("String '" + String.valueOf(i) + "' should NOT exist but was found in index");
              }
              state.incrementIndexSearchesCount(1);
--- 183,203 ----
           this.increment = increment;
           this.max = max;
           indexReader = IndexReader.open(directory);
           searcher = new IndexSearcher(indexReader);
        }
        
        @Override
        protected void testLoop() throws IOException {
           Term t = new Term("main", "0");
           for (int i = startValue; i <= max && state.needToQuit() == false; i += increment) {
!             Term termToQuery = new Term("main", Integer.toString(i));
              Query query = new TermQuery(termToQuery);
              TopDocs docs = searcher.search(query, null, 1);
              if (i % 2 == 0 && docs.totalHits != 1) {
                 //Even values should be found in the index
                 throw new RuntimeException("String '" + String.valueOf(i) + "' should exist but was not found in index");
              } else if (i % 2 == 1 && docs.totalHits != 0) {
                 //Uneven values should not be found
                 throw new RuntimeException("String '" + String.valueOf(i) + "' should NOT exist but was found in index");
              }
              state.incrementIndexSearchesCount(1);
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java lucene4-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java
*** lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java	2013-01-07 11:38:20.841914402 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java	2013-01-07 16:19:34.056544294 +0000
***************
*** 20,60 ****
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.infinispan.lucene.profiling;
  
  import java.io.IOException;
  import java.util.HashSet;
  import java.util.Set;
  
  import org.apache.lucene.index.CorruptIndexException;
  import org.apache.lucene.index.IndexReader;
  import org.apache.lucene.index.Term;
  import org.apache.lucene.search.IndexSearcher;
  import org.apache.lucene.search.Query;
  import org.apache.lucene.search.TermQuery;
  import org.apache.lucene.search.TopDocs;
  import org.apache.lucene.store.Directory;
  
  /**
   * LuceneReaderThread is going to perform searches on the Directory until it's interrupted.
   * Good for performance comparisons and stress tests.
   * It needs a SharedState object to be shared with other readers and writers on the same directory to
   * be able to throw exceptions in case it's able to detect an illegal state.
   * 
   * @author Sanne Grinovero
   * @since 4.0
   */
  public class LuceneReaderThread extends LuceneUserThread {
  
     protected IndexSearcher searcher;
!    protected IndexReader indexReader;
  
     LuceneReaderThread(Directory dir, SharedState state) {
        super(dir, state);
     }
  
     @Override
     protected void testLoop() throws IOException {
        // take ownership of some strings, so that no other thread will change status for these:
        Set<String> strings = new HashSet<String>();
        int numElements = state.stringsInIndex.drainTo(strings, 50);
--- 20,61 ----
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.infinispan.lucene.profiling;
  
  import java.io.IOException;
  import java.util.HashSet;
  import java.util.Set;
  
  import org.apache.lucene.index.CorruptIndexException;
+ import org.apache.lucene.index.DirectoryReader;
  import org.apache.lucene.index.IndexReader;
  import org.apache.lucene.index.Term;
  import org.apache.lucene.search.IndexSearcher;
  import org.apache.lucene.search.Query;
  import org.apache.lucene.search.TermQuery;
  import org.apache.lucene.search.TopDocs;
  import org.apache.lucene.store.Directory;
  
  /**
   * LuceneReaderThread is going to perform searches on the Directory until it's interrupted.
   * Good for performance comparisons and stress tests.
   * It needs a SharedState object to be shared with other readers and writers on the same directory to
   * be able to throw exceptions in case it's able to detect an illegal state.
   * 
   * @author Sanne Grinovero
   * @since 4.0
   */
  public class LuceneReaderThread extends LuceneUserThread {
  
     protected IndexSearcher searcher;
!    protected DirectoryReader indexReader;
  
     LuceneReaderThread(Directory dir, SharedState state) {
        super(dir, state);
     }
  
     @Override
     protected void testLoop() throws IOException {
        // take ownership of some strings, so that no other thread will change status for these:
        Set<String> strings = new HashSet<String>();
        int numElements = state.stringsInIndex.drainTo(strings, 50);
***************
*** 66,97 ****
              throw new RuntimeException("String '" + term + "' should exist but was not found in index");
           }
        }
        // put the strings back at their place:
        state.stringsInIndex.addAll(strings);
        state.incrementIndexSearchesCount(numElements);
     }
  
     protected void refreshIndexReader() throws CorruptIndexException, IOException {
        if (indexReader == null) {
!          indexReader = IndexReader.open(directory);
        }
        else {
           IndexReader before = indexReader;
!          indexReader = indexReader.reopen();
           if (before != indexReader) {
              before.close();
           }
        }
!       if (searcher != null) {
!          searcher.close();
!       }
        searcher = new IndexSearcher(indexReader);
     }
     
     @Override
     protected void cleanup() throws IOException {
        if (indexReader != null)
           indexReader.close();
     }
  
  }
--- 67,96 ----
              throw new RuntimeException("String '" + term + "' should exist but was not found in index");
           }
        }
        // put the strings back at their place:
        state.stringsInIndex.addAll(strings);
        state.incrementIndexSearchesCount(numElements);
     }
  
     protected void refreshIndexReader() throws CorruptIndexException, IOException {
        if (indexReader == null) {
!          indexReader = DirectoryReader.open(directory);
        }
        else {
           IndexReader before = indexReader;
!          indexReader = DirectoryReader.openIfChanged(indexReader);
           if (before != indexReader) {
              before.close();
           }
        }
!       
        searcher = new IndexSearcher(indexReader);
     }
     
     @Override
     protected void cleanup() throws IOException {
        if (indexReader != null)
           indexReader.close();
     }
  
  }
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/readlocks/DistributedSegmentReadLockerTest.java lucene4-directory/src/test/java/org/infinispan/lucene/readlocks/DistributedSegmentReadLockerTest.java
*** lucene-directory/src/test/java/org/infinispan/lucene/readlocks/DistributedSegmentReadLockerTest.java	2013-01-07 11:38:20.841914402 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/readlocks/DistributedSegmentReadLockerTest.java	2013-01-07 16:20:06.633216036 +0000
***************
*** 18,37 ****
--- 18,38 ----
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.infinispan.lucene.readlocks;
  
  import java.io.IOException;
  
  import org.apache.lucene.store.Directory;
+ import org.apache.lucene.store.IOContext;
  import org.apache.lucene.store.IndexInput;
  import org.apache.lucene.store.IndexOutput;
  import org.infinispan.Cache;
  import org.infinispan.configuration.cache.ConfigurationBuilder;
  import org.infinispan.lucene.CacheTestSupport;
  import org.infinispan.lucene.DirectoryIntegrityCheck;
  import org.infinispan.lucene.InfinispanDirectory;
  import org.infinispan.test.MultipleCacheManagersTest;
  import org.infinispan.transaction.TransactionMode;
  import org.testng.annotations.BeforeMethod;
***************
*** 73,98 ****
        cache0 = cache(0, CACHE_NAME);
        cache1 = cache(1, CACHE_NAME);
        dirA = createDirectory(cache0);
        dirB = createDirectory(cache1);
        CacheTestSupport.initializeDirectory(dirA);
     }
     
     @Test
     public void testIndexWritingAndFinding() throws IOException, InterruptedException {
        verifyBoth(cache0,cache1);
!       IndexOutput indexOutput = dirA.createOutput(filename);
        indexOutput.writeString("no need to write, nobody ever will read this");
        indexOutput.flush();
        indexOutput.close();
        assertFileExistsHavingRLCount(filename, 1, true);
!       IndexInput openInput = dirB.openInput(filename);
        assertFileExistsHavingRLCount(filename, 2, true);
        dirA.deleteFile(filename);
        assertFileExistsHavingRLCount(filename, 1, false);
        //Lucene does use clone() - lock implementation ignores it as a clone is
        //cast on locked segments and released before the close on the parent object
        IndexInput clone = (IndexInput) openInput.clone();
        assertFileExistsHavingRLCount(filename, 1, false);
        clone.close();
        assertFileExistsHavingRLCount(filename, 1, false);
        openInput.close();
--- 74,99 ----
        cache0 = cache(0, CACHE_NAME);
        cache1 = cache(1, CACHE_NAME);
        dirA = createDirectory(cache0);
        dirB = createDirectory(cache1);
        CacheTestSupport.initializeDirectory(dirA);
     }
     
     @Test
     public void testIndexWritingAndFinding() throws IOException, InterruptedException {
        verifyBoth(cache0,cache1);
!       IndexOutput indexOutput = dirA.createOutput(filename, IOContext.DEFAULT);
        indexOutput.writeString("no need to write, nobody ever will read this");
        indexOutput.flush();
        indexOutput.close();
        assertFileExistsHavingRLCount(filename, 1, true);
!       IndexInput openInput = dirB.openInput(filename, IOContext.DEFAULT);
        assertFileExistsHavingRLCount(filename, 2, true);
        dirA.deleteFile(filename);
        assertFileExistsHavingRLCount(filename, 1, false);
        //Lucene does use clone() - lock implementation ignores it as a clone is
        //cast on locked segments and released before the close on the parent object
        IndexInput clone = (IndexInput) openInput.clone();
        assertFileExistsHavingRLCount(filename, 1, false);
        clone.close();
        assertFileExistsHavingRLCount(filename, 1, false);
        openInput.close();
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/readlocks/LocalLockMergingSegmentReadLockerTest.java lucene4-directory/src/test/java/org/infinispan/lucene/readlocks/LocalLockMergingSegmentReadLockerTest.java
*** lucene-directory/src/test/java/org/infinispan/lucene/readlocks/LocalLockMergingSegmentReadLockerTest.java	2013-01-07 11:38:20.841914402 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/readlocks/LocalLockMergingSegmentReadLockerTest.java	2013-01-07 16:20:21.256552186 +0000
***************
*** 18,75 ****
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.infinispan.lucene.readlocks;
  
  import java.io.IOException;
  
  import org.apache.lucene.store.Directory;
  import org.apache.lucene.store.IndexInput;
  import org.apache.lucene.store.IndexOutput;
  import org.infinispan.Cache;
  import org.infinispan.lucene.InfinispanDirectory;
  import org.testng.annotations.Test;
  
  /**
   * LocalLockMergingSegmentReadLockerTest represents a quick check on the functionality
   * of {@link org.infinispan.lucene.readlocks.LocalLockMergingSegmentReadLocker}
   * 
   * @author Sanne Grinovero
   * @since 4.1
   */
  @SuppressWarnings("unchecked")
  @Test(groups = "functional", testName = "lucene.readlocks.LocalLockMergingSegmentReadLockerTest")
  public class LocalLockMergingSegmentReadLockerTest extends DistributedSegmentReadLockerTest {
     
     @Test @Override
     public void testIndexWritingAndFinding() throws IOException, InterruptedException {
        verifyBoth(cache0,cache1);
!       IndexOutput indexOutput = dirA.createOutput(filename);
        indexOutput.writeString("no need to write, nobody ever will read this");
        indexOutput.flush();
        indexOutput.close();
        assertFileExistsHavingRLCount(filename, 1, true);
!       IndexInput firstOpenOnB = dirB.openInput(filename);
        assertFileExistsHavingRLCount(filename, 2, true);
        dirA.deleteFile(filename);
        assertFileExistsHavingRLCount(filename, 1, false);
        //Lucene does use clone() - lock implementation ignores it as a clone is
        //cast on locked segments and released before the close on the parent object
        IndexInput cloneOfFirstOpenOnB = (IndexInput) firstOpenOnB.clone();
        assertFileExistsHavingRLCount(filename, 1, false);
        cloneOfFirstOpenOnB.close();
        assertFileExistsHavingRLCount(filename, 1, false);
!       IndexInput firstOpenOnA = dirA.openInput(filename);
        assertFileExistsHavingRLCount(filename, 2, false);
!       IndexInput secondOpenOnA = dirA.openInput(filename);
        assertFileExistsHavingRLCount(filename, 2, false);
        firstOpenOnA.close();
        assertFileExistsHavingRLCount(filename, 2, false);
        secondOpenOnA.close();
        assertFileExistsHavingRLCount(filename, 1, false);
        firstOpenOnB.close();
        assertFileNotExists(filename);
        dirA.close();
        dirB.close();
        verifyBoth(cache0, cache1);
--- 18,76 ----
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.infinispan.lucene.readlocks;
  
  import java.io.IOException;
  
  import org.apache.lucene.store.Directory;
+ import org.apache.lucene.store.IOContext;
  import org.apache.lucene.store.IndexInput;
  import org.apache.lucene.store.IndexOutput;
  import org.infinispan.Cache;
  import org.infinispan.lucene.InfinispanDirectory;
  import org.testng.annotations.Test;
  
  /**
   * LocalLockMergingSegmentReadLockerTest represents a quick check on the functionality
   * of {@link org.infinispan.lucene.readlocks.LocalLockMergingSegmentReadLocker}
   * 
   * @author Sanne Grinovero
   * @since 4.1
   */
  @SuppressWarnings("unchecked")
  @Test(groups = "functional", testName = "lucene.readlocks.LocalLockMergingSegmentReadLockerTest")
  public class LocalLockMergingSegmentReadLockerTest extends DistributedSegmentReadLockerTest {
     
     @Test @Override
     public void testIndexWritingAndFinding() throws IOException, InterruptedException {
        verifyBoth(cache0,cache1);
!       IndexOutput indexOutput = dirA.createOutput(filename, IOContext.DEFAULT);
        indexOutput.writeString("no need to write, nobody ever will read this");
        indexOutput.flush();
        indexOutput.close();
        assertFileExistsHavingRLCount(filename, 1, true);
!       IndexInput firstOpenOnB = dirB.openInput(filename, IOContext.DEFAULT);
        assertFileExistsHavingRLCount(filename, 2, true);
        dirA.deleteFile(filename);
        assertFileExistsHavingRLCount(filename, 1, false);
        //Lucene does use clone() - lock implementation ignores it as a clone is
        //cast on locked segments and released before the close on the parent object
        IndexInput cloneOfFirstOpenOnB = (IndexInput) firstOpenOnB.clone();
        assertFileExistsHavingRLCount(filename, 1, false);
        cloneOfFirstOpenOnB.close();
        assertFileExistsHavingRLCount(filename, 1, false);
!       IndexInput firstOpenOnA = dirA.openInput(filename, IOContext.DEFAULT);
        assertFileExistsHavingRLCount(filename, 2, false);
!       IndexInput secondOpenOnA = dirA.openInput(filename, IOContext.DEFAULT);
        assertFileExistsHavingRLCount(filename, 2, false);
        firstOpenOnA.close();
        assertFileExistsHavingRLCount(filename, 2, false);
        secondOpenOnA.close();
        assertFileExistsHavingRLCount(filename, 1, false);
        firstOpenOnB.close();
        assertFileNotExists(filename);
        dirA.close();
        dirB.close();
        verifyBoth(cache0, cache1);
diff -b -C 10 -r lucene-directory/src/test/java/org/infinispan/lucene/testutils/LuceneSettings.java lucene4-directory/src/test/java/org/infinispan/lucene/testutils/LuceneSettings.java
*** lucene-directory/src/test/java/org/infinispan/lucene/testutils/LuceneSettings.java	2013-01-07 11:38:20.841914402 +0000
--- lucene4-directory/src/test/java/org/infinispan/lucene/testutils/LuceneSettings.java	2013-01-07 16:23:50.799919950 +0000
***************
*** 18,38 ****
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.infinispan.lucene.testutils;
  
  import java.io.IOException;
  
  import org.apache.lucene.analysis.Analyzer;
! import org.apache.lucene.analysis.SimpleAnalyzer;
  import org.apache.lucene.index.CorruptIndexException;
  import org.apache.lucene.index.IndexWriter;
  import org.apache.lucene.index.IndexWriterConfig;
  import org.apache.lucene.index.LogByteSizeMergePolicy;
  import org.apache.lucene.index.LogMergePolicy;
  import org.apache.lucene.index.MergeScheduler;
  import org.apache.lucene.index.SerialMergeScheduler;
  import org.apache.lucene.store.Directory;
  import org.apache.lucene.store.LockObtainFailedException;
  import org.apache.lucene.util.Version;
--- 18,38 ----
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.infinispan.lucene.testutils;
  
  import java.io.IOException;
  
  import org.apache.lucene.analysis.Analyzer;
! import org.apache.lucene.analysis.core.SimpleAnalyzer;
  import org.apache.lucene.index.CorruptIndexException;
  import org.apache.lucene.index.IndexWriter;
  import org.apache.lucene.index.IndexWriterConfig;
  import org.apache.lucene.index.LogByteSizeMergePolicy;
  import org.apache.lucene.index.LogMergePolicy;
  import org.apache.lucene.index.MergeScheduler;
  import org.apache.lucene.index.SerialMergeScheduler;
  import org.apache.lucene.store.Directory;
  import org.apache.lucene.store.LockObtainFailedException;
  import org.apache.lucene.util.Version;
