Buggy code:
public class TestIndexFileDeleter extends LuceneTestCase { public void testDeleteLeftoverFiles ( ) throws IOException { MockDirectoryWrapper dir = newDirectory ( ) ; dir . setPreventDoubleWrite ( false ) ; LogMergePolicy mergePolicy = newLogMergePolicy ( true , 10 ) ; mergePolicy . setNoCFSRatio ( 1 ) ; IndexWriter writer = new IndexWriter ( dir , newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ( ) ) ) . setMaxBufferedDocs ( 10 ) . setMergePolicy ( mergePolicy ) ) ; int i ; for ( i = 0 ; i < 35 ; i ++ ) { addDoc ( writer , i ) ; } <START_BUG> mergePolicy . setUseCompoundFile ( false ) ; <END_BUG> for ( ; i < 45 ; i ++ ) { addDoc ( writer , i ) ; } writer . close ( ) ; writer = new IndexWriter ( dir , newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ( ) ) ) . setMergePolicy ( NO_COMPOUND_FILES ) ) ; Term searchTerm = new Term ( "id" , "7" ) ; writer . deleteDocuments ( searchTerm ) ; writer . close ( ) ; String [ ] files = dir . listAll ( ) ; String ext = ( Codec . getDefault ( ) . getName ( ) . equals ( "SimpleText" ) ) ? ".liv" : ".del" ; copyFile ( dir , ( "_0_1" + ext ) , ( "_0_2" + ext ) ) ; copyFile ( dir , ( "_0_1" + ext ) , ( "_1_1" + ext ) ) ; copyFile ( dir , ( "_0_1" + ext ) , ( "_188_1" + ext ) ) ; copyFile ( dir , "_0.cfs" , "_188.cfs" ) ; copyFile ( dir , "_0.cfs" , "_0.fnm" ) ; copyFile ( dir , "segments_2" , "segments" ) ; copyFile ( dir , "segments_2" , "segments_1" ) ; assertTrue ( ( ( dir . fileExists ( "_3.fdt" ) ) || ( dir . fileExists ( "_3.fld" ) ) ) ) ; assertTrue ( ( ! ( dir . fileExists ( "_3.cfs" ) ) ) ) ; copyFile ( dir , "_1.cfs" , "_3.cfs" ) ; String [ ] filesPre = dir . listAll ( ) ; writer = new IndexWriter ( dir , newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ( ) ) ) . setOpenMode ( APPEND ) ) ; writer . close ( ) ; String [ ] files2 = dir . listAll ( ) ; dir . close ( ) ; Arrays . sort ( files ) ; Arrays . sort ( files2 ) ; Set < String > 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 ) ) ) + "\ndiff:<seq2seq4repair_space>" ) + dif ) ) ; } } private static Set < String > 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:
( ( LogMergePolicy ) ( writer . getConfig ( ) . getMergePolicy ( ) ) ) . setUseCompoundFile ( false ) ;