Buggy code:
public class TestIndexFileDeleter extends LuceneTestCase { public void testDeleteLeftoverFiles ( ) throws IOException { Directory dir = new RAMDirectory ( ) ; IndexWriter writer = new IndexWriter ( dir , new WhitespaceAnalyzer ( ) , true , MaxFieldLength . LIMITED ) ; writer . setMaxBufferedDocs ( 10 ) ; int i ; for ( i = 0 ; i < 35 ; i ++ ) { addDoc ( writer , i ) ; } writer . setUseCompoundFile ( false ) ; for ( ; i < 45 ; i ++ ) { addDoc ( writer , i ) ; } writer . close ( ) ; <START_BUG> IndexReader reader = IndexReader . open ( dir ) ; <END_BUG> Term searchTerm = new Term ( "id" , "7" ) ; int delCount = reader . deleteDocuments ( searchTerm ) ; assertEquals ( "didn't<seq2seq4repair_space>delete<seq2seq4repair_space>the<seq2seq4repair_space>right<seq2seq4repair_space>number<seq2seq4repair_space>of<seq2seq4repair_space>documents" , 1 , delCount ) ; reader . setNorm ( 21 , "content" , ( ( float ) ( 1.5 ) ) ) ; reader . close ( ) ; String [ ] files = dir . listAll ( ) ; CompoundFileReader cfsReader = new CompoundFileReader ( dir , "_2.cfs" ) ; FieldInfos fieldInfos = new FieldInfos ( cfsReader , "_2.fnm" ) ; int contentFieldIndex = - 1 ; for ( i = 0 ; i < ( fieldInfos . size ( ) ) ; i ++ ) { FieldInfo fi = fieldInfos . fieldInfo ( i ) ; if ( fi . name . equals ( "content" ) ) { contentFieldIndex = i ; break ; } } cfsReader . close ( ) ; assertTrue ( "could<seq2seq4repair_space>not<seq2seq4repair_space>locate<seq2seq4repair_space>the<seq2seq4repair_space>'content'<seq2seq4repair_space>field<seq2seq4repair_space>number<seq2seq4repair_space>in<seq2seq4repair_space>the<seq2seq4repair_space>_2.cfs<seq2seq4repair_space>segment" , ( contentFieldIndex != ( - 1 ) ) ) ; String normSuffix = "s" + contentFieldIndex ; copyFile ( dir , ( "_2_1." + normSuffix ) , ( "_2_2." + normSuffix ) ) ; copyFile ( dir , ( "_2_1." + normSuffix ) , ( "_2_2.f" + contentFieldIndex ) ) ; copyFile ( dir , ( "_2_1." + normSuffix ) , ( "_1_1." + normSuffix ) ) ; copyFile ( dir , ( "_2_1." + normSuffix ) , ( "_1_1.f" + contentFieldIndex ) ) ; copyFile ( dir , "_0_1.del" , "_0_2.del" ) ; copyFile ( dir , "_0_1.del" , "_1_1.del" ) ; copyFile ( dir , "_0_1.del" , "_188_1.del" ) ; copyFile ( dir , "_0.cfs" , "_188.cfs" ) ; copyFile ( dir , "_0.cfs" , "_0.fnm" ) ; copyFile ( dir , "_0.cfs" , "deletable" ) ; copyFile ( dir , "segments_3" , "segments" ) ; copyFile ( dir , "segments_3" , "segments_2" ) ; copyFile ( dir , "_2.cfs" , "_3.cfs" ) ; String [ ] filesPre = dir . listAll ( ) ; writer = new IndexWriter ( dir , new WhitespaceAnalyzer ( ) , false , MaxFieldLength . LIMITED ) ; writer . close ( ) ; String [ ] files2 = dir . listAll ( ) ; dir . close ( ) ; Arrays . sort ( files ) ; Arrays . sort ( files2 ) ; Set dif = TestIndexFileDeleter . difFiles ( files , files2 ) ; if ( ! ( Arrays . equals ( files , files2 ) ) ) { fail ( ( ( ( ( ( ( ( ( ( "IndexFileDeleter<seq2seq4repair_space>failed<seq2seq4repair_space>to<seq2seq4repair_space>delete<seq2seq4repair_space>unreferenced<seq2seq4repair_space>extra<seq2seq4repair_space>files:<seq2seq4repair_space>should<seq2seq4repair_space>have<seq2seq4repair_space>deleted<seq2seq4repair_space>" + ( ( filesPre . length ) - ( files . length ) ) ) + "<seq2seq4repair_space>files<seq2seq4repair_space>but<seq2seq4repair_space>only<seq2seq4repair_space>deleted<seq2seq4repair_space>" ) + ( ( filesPre . length ) - ( files2 . length ) ) ) + ";<seq2seq4repair_space>expected<seq2seq4repair_space>files:\n<seq2seq4repair_space>" ) + ( asString ( files ) ) ) + "\n<seq2seq4repair_space>actual<seq2seq4repair_space>files:\n<seq2seq4repair_space>" ) + ( asString ( files2 ) ) ) + "\ndif:<seq2seq4repair_space>" ) + dif ) ) ; } } private static Set difFiles ( String [ ] files1 , String [ ] files2 ) { } private String asString ( String [ ] l ) { } public void copyFile ( Directory dir , String src , String dest ) throws IOException { } private void addDoc ( IndexWriter writer , int id ) throws IOException { } }

Fix:
IndexReader reader = IndexReader . open ( dir , false ) ;