Buggy code:
@ Seed ( "AFD1E7E84B35D2B1" ) public class TestPhraseQuery extends LuceneTestCase { public static final float SCORE_COMP_THRESH = 1.0E-6F ; private static IndexSearcher searcher ; private static IndexReader reader ; private PhraseQuery query ; private static Directory directory ; @ BeforeClass public static void beforeClass ( ) throws Exception { } @ Override public void setUp ( ) throws Exception { } @ AfterClass public static void afterClass ( ) throws Exception { } public void testNotCloseEnough ( ) throws Exception { } public void testBarelyCloseEnough ( ) throws Exception { } public void testExact ( ) throws Exception { } public void testSlop1 ( ) throws Exception { } public void testOrderDoesntMatter ( ) throws Exception { } public void testMulipleTerms ( ) throws Exception { } public void testPhraseQueryWithStopAnalyzer ( ) throws Exception { } public void testPhraseQueryInConjunctionScorer ( ) throws Exception { } public void testSlopScoring ( ) throws IOException { } public void testToString ( ) throws Exception { } public void testWrappedPhrase ( ) throws IOException { } public void testNonExistingPhrase ( ) throws IOException { } public void testPalyndrome2 ( ) throws Exception { } public void testPalyndrome3 ( ) throws Exception { } public void testEmptyPhraseQuery ( ) throws Throwable { } public void testRewrite ( ) throws IOException { } public void testRandomPhrases ( ) throws Exception { Directory dir = newDirectory ( ) ; Analyzer analyzer = new MockAnalyzer ( random ( ) ) ; RandomIndexWriter w = new RandomIndexWriter ( random ( ) , dir , newIndexWriterConfig ( TEST_VERSION_CURRENT , analyzer ) . setMergePolicy ( newLogMergePolicy ( ) ) ) ; List < List < String > > docs = new ArrayList < List < String > > ( ) ; Document d = new Document ( ) ; Field f = newTextField ( "f" , "" , NO ) ; d . add ( f ) ; Random r = random ( ) ; int NUM_DOCS = atLeast ( 10 ) ; for ( int i = 0 ; i < NUM_DOCS ; i ++ ) { int termCount = _TestUtil . nextInt ( random ( ) , 4097 , 8200 ) ; List < String > doc = new ArrayList < String > ( ) ; StringBuilder sb = new StringBuilder ( ) ; while ( ( doc . size ( ) ) < termCount ) { if ( ( ( r . nextInt ( 5 ) ) == 1 ) || ( ( docs . size ( ) ) == 0 ) ) { String term ; while ( true ) { term = _TestUtil . randomUnicodeString ( r ) ; if ( ( term . length ( ) ) > 0 ) { break ; } } <START_BUG> TokenStream ts = analyzer . tokenStream ( "ignore" , new StringReader ( term ) ) ; <END_BUG> CharTermAttribute termAttr = ts . addAttribute ( CharTermAttribute . class ) ; ts . reset ( ) ; while ( ts . incrementToken ( ) ) { String text = termAttr . toString ( ) ; doc . add ( text ) ; sb . append ( text ) . append ( '<seq2seq4repair_space>' ) ; } ts . end ( ) ; ts . close ( ) ; } else { List < String > lastDoc = docs . get ( r . nextInt ( docs . size ( ) ) ) ; int len = _TestUtil . nextInt ( r , 1 , 10 ) ; int start = r . nextInt ( ( ( lastDoc . size ( ) ) - len ) ) ; for ( int k = start ; k < ( start + len ) ; k ++ ) { String t = lastDoc . get ( k ) ; doc . add ( t ) ; sb . append ( t ) . append ( '<seq2seq4repair_space>' ) ; } } } docs . add ( doc ) ; f . setStringValue ( sb . toString ( ) ) ; w . addDocument ( d ) ; } IndexReader reader = w . getReader ( ) ; IndexSearcher s = newSearcher ( reader ) ; w . close ( ) ; int num = atLeast ( 10 ) ; for ( int i = 0 ; i < num ; i ++ ) { int docID = r . nextInt ( docs . size ( ) ) ; List < String > doc = docs . get ( docID ) ; final int numTerm = _TestUtil . nextInt ( r , 2 , 20 ) ; final int start = r . nextInt ( ( ( doc . size ( ) ) - numTerm ) ) ; PhraseQuery pq = new PhraseQuery ( ) ; StringBuilder sb = new StringBuilder ( ) ; for ( int t = start ; t < ( start + numTerm ) ; t ++ ) { pq . add ( new Term ( "f" , doc . get ( t ) ) ) ; sb . append ( doc . get ( t ) ) . append ( '<seq2seq4repair_space>' ) ; } TopDocs hits = s . search ( pq , NUM_DOCS ) ; boolean found = false ; for ( int j = 0 ; j < ( hits . scoreDocs . length ) ; j ++ ) { if ( ( hits . scoreDocs [ j ] . doc ) == docID ) { found = true ; break ; } } assertTrue ( ( ( ( "phrase<seq2seq4repair_space>'" + sb ) + "'<seq2seq4repair_space>not<seq2seq4repair_space>found;<seq2seq4repair_space>start=" ) + start ) , found ) ; } reader . close ( ) ; dir . close ( ) ; } }

Fix:
TokenStream ts = analyzer . tokenStream ( "ignore" , term ) ;