Buggy code:
public class TestRandomStoredFields extends LuceneTestCase { public void testRandomStoredFields ( ) throws IOException { Directory dir = newDirectory ( ) ; Random rand = random ; RandomIndexWriter w = new RandomIndexWriter ( rand , dir , newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ) ) . setMaxBufferedDocs ( _TestUtil . nextInt ( rand , 5 , 20 ) ) ) ; final int docCount = atLeast ( 200 ) ; final int fieldCount = _TestUtil . nextInt ( rand , 1 , 5 ) ; final List < Integer > fieldIDs = new ArrayList < Integer > ( ) ; FieldType customType = new FieldType ( TextField . TYPE_STORED ) ; customType . setTokenized ( false ) ; Field idField = newField ( "id" , "" , customType ) ; for ( int i = 0 ; i < fieldCount ; i ++ ) { fieldIDs . add ( i ) ; } final Map < String , Document > docs = new HashMap < String , Document > ( ) ; if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>build<seq2seq4repair_space>index<seq2seq4repair_space>docCount=" + docCount ) ) ; } FieldType customType2 = new FieldType ( ) ; customType2 . setStored ( true ) ; for ( int i = 0 ; i < docCount ; i ++ ) { Document doc = new Document ( ) ; doc . add ( idField ) ; final String id = "" + i ; <START_BUG> idField . setValue ( id ) ; <END_BUG> docs . put ( id , doc ) ; if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>add<seq2seq4repair_space>doc<seq2seq4repair_space>id=" + id ) ) ; } for ( int field : fieldIDs ) { final String s ; if ( ( rand . nextInt ( 4 ) ) != 3 ) { s = _TestUtil . randomUnicodeString ( rand , 1000 ) ; doc . add ( newField ( ( "f" + field ) , s , customType2 ) ) ; } else { s = null ; } } w . addDocument ( doc ) ; if ( ( rand . nextInt ( 50 ) ) == 17 ) { Collections . shuffle ( fieldIDs ) ; } if ( ( ( rand . nextInt ( 5 ) ) == 3 ) && ( i > 0 ) ) { final String delID = "" + ( rand . nextInt ( i ) ) ; if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>delete<seq2seq4repair_space>doc<seq2seq4repair_space>id=" + delID ) ) ; } w . deleteDocuments ( new Term ( "id" , delID ) ) ; docs . remove ( delID ) ; } } if ( VERBOSE ) { System . out . println ( ( ( "TEST:<seq2seq4repair_space>" + ( docs . size ( ) ) ) + "<seq2seq4repair_space>docs<seq2seq4repair_space>in<seq2seq4repair_space>index;<seq2seq4repair_space>now<seq2seq4repair_space>load<seq2seq4repair_space>fields" ) ) ; } if ( ( docs . size ( ) ) > 0 ) { String [ ] idsList = docs . keySet ( ) . toArray ( new String [ docs . size ( ) ] ) ; for ( int x = 0 ; x < 2 ; x ++ ) { IndexReader r = w . getReader ( ) ; IndexSearcher s = newSearcher ( r ) ; if ( VERBOSE ) { System . out . println ( ( ( ( "TEST:<seq2seq4repair_space>cycle<seq2seq4repair_space>x=" + x ) + "<seq2seq4repair_space>r=" ) + r ) ) ; } int num = atLeast ( 1000 ) ; for ( int iter = 0 ; iter < num ; iter ++ ) { String testID = idsList [ rand . nextInt ( idsList . length ) ] ; if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>test<seq2seq4repair_space>id=" + testID ) ) ; } TopDocs hits = s . search ( new org . apache . lucene . search . TermQuery ( new Term ( "id" , testID ) ) , 1 ) ; assertEquals ( 1 , hits . totalHits ) ; Document doc = r . document ( hits . scoreDocs [ 0 ] . doc ) ; Document docExp = docs . get ( testID ) ; for ( int i = 0 ; i < fieldCount ; i ++ ) { assertEquals ( ( ( ( ( "doc<seq2seq4repair_space>" + testID ) + ",<seq2seq4repair_space>field<seq2seq4repair_space>f" ) + fieldCount ) + "<seq2seq4repair_space>is<seq2seq4repair_space>wrong" ) , docExp . get ( ( "f" + i ) ) , doc . get ( ( "f" + i ) ) ) ; } } r . close ( ) ; w . forceMerge ( 1 ) ; } } w . close ( ) ; dir . close ( ) ; } }

Fix:
idField . setStringValue ( id ) ;