Buggy code:
public class TestExternalCodecs extends LuceneTestCase { private static final class CustomPerFieldCodec extends Lucene40Codec { private final PostingsFormat ramFormat = PostingsFormat . forName ( "RAMOnly" ) ; private final PostingsFormat defaultFormat = PostingsFormat . forName ( "Lucene40" ) ; private final PostingsFormat pulsingFormat = PostingsFormat . forName ( "Pulsing40" ) ; @ Override public PostingsFormat getPostingsFormatForField ( String field ) { } } public void testPerFieldCodec ( ) throws Exception { final int NUM_DOCS = atLeast ( 173 ) ; if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>NUM_DOCS=" + NUM_DOCS ) ) ; } <START_BUG> MockDirectoryWrapper dir = newDirectory ( ) ; <END_BUG> dir . setCheckIndexOnClose ( false ) ; IndexWriter w = new IndexWriter ( dir , newIndexWriterConfig ( TEST_VERSION_CURRENT , new MockAnalyzer ( random ( ) ) ) . setCodec ( new TestExternalCodecs . CustomPerFieldCodec ( ) ) . setMergePolicy ( newLogMergePolicy ( 3 ) ) ) ; Document doc = new Document ( ) ; doc . add ( newTextField ( "field1" , "this<seq2seq4repair_space>field<seq2seq4repair_space>uses<seq2seq4repair_space>the<seq2seq4repair_space>standard<seq2seq4repair_space>codec<seq2seq4repair_space>as<seq2seq4repair_space>the<seq2seq4repair_space>test" , NO ) ) ; Field field2 = newTextField ( "field2" , "this<seq2seq4repair_space>field<seq2seq4repair_space>uses<seq2seq4repair_space>the<seq2seq4repair_space>pulsing<seq2seq4repair_space>codec<seq2seq4repair_space>as<seq2seq4repair_space>the<seq2seq4repair_space>test" , NO ) ; doc . add ( field2 ) ; Field idField = newStringField ( "id" , "" , NO ) ; doc . add ( idField ) ; for ( int i = 0 ; i < NUM_DOCS ; i ++ ) { idField . setStringValue ( ( "" + i ) ) ; w . addDocument ( doc ) ; if ( ( ( i + 1 ) % 10 ) == 0 ) { w . commit ( ) ; } } if ( VERBOSE ) { System . out . println ( "TEST:<seq2seq4repair_space>now<seq2seq4repair_space>delete<seq2seq4repair_space>id=77" ) ; } w . deleteDocuments ( new Term ( "id" , "77" ) ) ; IndexReader r = DirectoryReader . open ( w , true ) ; assertEquals ( ( NUM_DOCS - 1 ) , r . numDocs ( ) ) ; IndexSearcher s = newSearcher ( r ) ; assertEquals ( ( NUM_DOCS - 1 ) , s . search ( new TermQuery ( new Term ( "field1" , "standard" ) ) , 1 ) . totalHits ) ; assertEquals ( ( NUM_DOCS - 1 ) , s . search ( new TermQuery ( new Term ( "field2" , "pulsing" ) ) , 1 ) . totalHits ) ; r . close ( ) ; if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>now<seq2seq4repair_space>delete<seq2seq4repair_space>2nd<seq2seq4repair_space>doc" ) ; } w . deleteDocuments ( new Term ( "id" , "44" ) ) ; if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>now<seq2seq4repair_space>force<seq2seq4repair_space>merge" ) ; } w . forceMerge ( 1 ) ; if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>now<seq2seq4repair_space>open<seq2seq4repair_space>reader" ) ; } r = DirectoryReader . open ( w , true ) ; assertEquals ( ( NUM_DOCS - 2 ) , r . maxDoc ( ) ) ; assertEquals ( ( NUM_DOCS - 2 ) , r . numDocs ( ) ) ; s = newSearcher ( r ) ; assertEquals ( ( NUM_DOCS - 2 ) , s . search ( new TermQuery ( new Term ( "field1" , "standard" ) ) , 1 ) . totalHits ) ; assertEquals ( ( NUM_DOCS - 2 ) , s . search ( new TermQuery ( new Term ( "field2" , "pulsing" ) ) , 1 ) . totalHits ) ; assertEquals ( 1 , s . search ( new TermQuery ( new Term ( "id" , "76" ) ) , 1 ) . totalHits ) ; assertEquals ( 0 , s . search ( new TermQuery ( new Term ( "id" , "77" ) ) , 1 ) . totalHits ) ; assertEquals ( 0 , s . search ( new TermQuery ( new Term ( "id" , "44" ) ) , 1 ) . totalHits ) ; if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>now<seq2seq4repair_space>close<seq2seq4repair_space>NRT<seq2seq4repair_space>reader" ) ; } r . close ( ) ; w . close ( ) ; dir . close ( ) ; } }

Fix:
BaseDirectoryWrapper dir = newDirectory ( ) ;