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 ) ) ; } MockDirectoryWrapper dir = newDirectory ( ) ; 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 ( newField ( "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" , TYPE_UNSTORED ) ) ; Field field2 = newField ( "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" , TYPE_UNSTORED ) ; doc . add ( field2 ) ; Field idField = newField ( "id" , "" , StringField . TYPE_UNSTORED ) ; doc . add ( idField ) ; for ( int i = 0 ; i < NUM_DOCS ; i ++ ) { <START_BUG> idField . setValue ( ( "" + i ) ) ; <END_BUG> 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 = IndexReader . 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 = IndexReader . 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:
idField . setStringValue ( ( "" + i ) ) ;