Buggy code:
public class HighlighterTest extends BaseTokenStreamTestCase implements Formatter { private IndexReader reader ; static final String FIELD_NAME = "contents" ; private static final String NUMERIC_FIELD_NAME = "nfield" ; private Query query ; Directory ramDir ; public IndexSearcher searcher = null ; int numHighlights = 0 ; Analyzer analyzer ; TopDocs hits ; String [ ] texts = new String [ ] { "Hello<seq2seq4repair_space>this<seq2seq4repair_space>is<seq2seq4repair_space>a<seq2seq4repair_space>piece<seq2seq4repair_space>of<seq2seq4repair_space>text<seq2seq4repair_space>that<seq2seq4repair_space>is<seq2seq4repair_space>very<seq2seq4repair_space>long<seq2seq4repair_space>and<seq2seq4repair_space>contains<seq2seq4repair_space>too<seq2seq4repair_space>much<seq2seq4repair_space>preamble<seq2seq4repair_space>and<seq2seq4repair_space>the<seq2seq4repair_space>meat<seq2seq4repair_space>is<seq2seq4repair_space>really<seq2seq4repair_space>here<seq2seq4repair_space>which<seq2seq4repair_space>says<seq2seq4repair_space>kennedy<seq2seq4repair_space>has<seq2seq4repair_space>been<seq2seq4repair_space>shot" , "This<seq2seq4repair_space>piece<seq2seq4repair_space>of<seq2seq4repair_space>text<seq2seq4repair_space>refers<seq2seq4repair_space>to<seq2seq4repair_space>Kennedy<seq2seq4repair_space>at<seq2seq4repair_space>the<seq2seq4repair_space>beginning<seq2seq4repair_space>then<seq2seq4repair_space>has<seq2seq4repair_space>a<seq2seq4repair_space>longer<seq2seq4repair_space>piece<seq2seq4repair_space>of<seq2seq4repair_space>text<seq2seq4repair_space>that<seq2seq4repair_space>is<seq2seq4repair_space>very<seq2seq4repair_space>long<seq2seq4repair_space>in<seq2seq4repair_space>the<seq2seq4repair_space>middle<seq2seq4repair_space>and<seq2seq4repair_space>finally<seq2seq4repair_space>ends<seq2seq4repair_space>with<seq2seq4repair_space>another<seq2seq4repair_space>reference<seq2seq4repair_space>to<seq2seq4repair_space>Kennedy" , "JFK<seq2seq4repair_space>has<seq2seq4repair_space>been<seq2seq4repair_space>shot" , "John<seq2seq4repair_space>Kennedy<seq2seq4repair_space>has<seq2seq4repair_space>been<seq2seq4repair_space>shot" , "This<seq2seq4repair_space>text<seq2seq4repair_space>has<seq2seq4repair_space>a<seq2seq4repair_space>typo<seq2seq4repair_space>in<seq2seq4repair_space>referring<seq2seq4repair_space>to<seq2seq4repair_space>Keneddy" , "wordx<seq2seq4repair_space>wordy<seq2seq4repair_space>wordz<seq2seq4repair_space>wordx<seq2seq4repair_space>wordy<seq2seq4repair_space>wordx<seq2seq4repair_space>worda<seq2seq4repair_space>wordb<seq2seq4repair_space>wordy<seq2seq4repair_space>wordc" , "y<seq2seq4repair_space>z<seq2seq4repair_space>x<seq2seq4repair_space>y<seq2seq4repair_space>z<seq2seq4repair_space>a<seq2seq4repair_space>b" , "lets<seq2seq4repair_space>is<seq2seq4repair_space>a<seq2seq4repair_space>the<seq2seq4repair_space>lets<seq2seq4repair_space>is<seq2seq4repair_space>a<seq2seq4repair_space>the<seq2seq4repair_space>lets<seq2seq4repair_space>is<seq2seq4repair_space>a<seq2seq4repair_space>the<seq2seq4repair_space>lets" } ; public void testQueryScorerHits ( ) throws Exception { } public void testHighlightingWithDefaultField ( ) throws Exception { } private String highlightField ( Query query , String fieldName , String text ) throws IOException , InvalidTokenOffsetsException { } public void testSimpleSpanHighlighter ( ) throws Exception { } public void testRepeatingTermsInMultBooleans ( ) throws Exception { } public void testSimpleQueryScorerPhraseHighlighting ( ) throws Exception { } public void testSpanRegexQuery ( ) throws Exception { } public void testRegexQuery ( ) throws Exception { } public void testNumericRangeQuery ( ) throws Exception { } public void testSimpleQueryScorerPhraseHighlighting2 ( ) throws Exception { } public void testSimpleQueryScorerPhraseHighlighting3 ( ) throws Exception { } public void testSimpleSpanFragmenter ( ) throws Exception { } public void testPosTermStdTerm ( ) throws Exception { } public void testQueryScorerMultiPhraseQueryHighlighting ( ) throws Exception { } public void testQueryScorerMultiPhraseQueryHighlightingWithGap ( ) throws Exception { } public void testNearSpanSimpleQuery ( ) throws Exception { } public void testSimpleQueryTermScorerHighlighter ( ) throws Exception { } public void testSpanHighlighting ( ) throws Exception { } public void testNotSpanSimpleQuery ( ) throws Exception { } public void testGetBestFragmentsSimpleQuery ( ) throws Exception { } public void testGetFuzzyFragments ( ) throws Exception { } public void testGetWildCardFragments ( ) throws Exception { } public void testGetMidWildCardFragments ( ) throws Exception { } public void testGetRangeFragments ( ) throws Exception { } public void testConstantScoreMultiTermQuery ( ) throws Exception { } public void testGetBestFragmentsPhrase ( ) throws Exception { } public void testGetBestFragmentsQueryScorer ( ) throws Exception { } public void testOffByOne ( ) throws Exception { } public void testGetBestFragmentsFilteredQuery ( ) throws Exception { } public void testGetBestFragmentsFilteredPhraseQuery ( ) throws Exception { } public void testGetBestFragmentsMultiTerm ( ) throws Exception { } public void testGetBestFragmentsWithOr ( ) throws Exception { } public void testGetBestSingleFragment ( ) throws Exception { } public void testGetBestSingleFragmentWithWeights ( ) throws Exception { } public void testOverlapAnalyzer ( ) throws Exception { } public void testGetSimpleHighlight ( ) throws Exception { } public void testGetTextFragments ( ) throws Exception { } public void testMaxSizeHighlight ( ) throws Exception { } public void testMaxSizeHighlightTruncates ( ) throws Exception { } public void testMaxSizeEndHighlight ( ) throws Exception { } public void testUnRewrittenQuery ( ) throws Exception { } public void testNoFragments ( ) throws Exception { } public void testEncoding ( ) throws Exception { String rawDocContent = "\"Smith<seq2seq4repair_space>&<seq2seq4repair_space>sons\'<seq2seq4repair_space>prices<seq2seq4repair_space><<seq2seq4repair_space>3<seq2seq4repair_space>and<seq2seq4repair_space>>4\"<seq2seq4repair_space>claims<seq2seq4repair_space>article" ; Highlighter highlighter = new Highlighter ( this , new SimpleHTMLEncoder ( ) , new Scorer ( ) { public void startFragment ( TextFragment newFragment ) { } public float getTokenScore ( ) { return 0 ; } public float getFragmentScore ( ) { return 1 ; } public TokenStream init ( TokenStream tokenStream ) { return null ; } } ) ; highlighter . setTextFragmenter ( new SimpleFragmenter ( 2000 ) ) ; TokenStream tokenStream = analyzer . tokenStream ( HighlighterTest . FIELD_NAME , new StringReader ( rawDocContent ) ) ; String encodedSnippet = highlighter . getBestFragments ( tokenStream , rawDocContent , 1 , "" ) ; String xhtml = ( ( ( ( "<?xml<seq2seq4repair_space>version=\"1.0\"<seq2seq4repair_space>encoding=\"UTF-8\"?>\n" + ( ( ( ( ( "<html<seq2seq4repair_space>xmlns=\"http:////www.w3.org//1999//xhtml\"<seq2seq4repair_space>xml:lang=\"en\"<seq2seq4repair_space>lang=\"en\">\n" + "<head>\n" ) + "<title>My<seq2seq4repair_space>Test<seq2seq4repair_space>HTML<seq2seq4repair_space>Document<//title>\n" ) + "<//head>\n" ) + "<body>\n" ) + "<h2>" ) ) + encodedSnippet ) + "<//h2>\n" ) + "<//body>\n" ) + "<//html>" ; DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ( ) ; DocumentBuilder db = dbf . newDocumentBuilder ( ) ; <START_BUG> Document doc = db . parse ( new ByteArrayInputStream ( xhtml . getBytes ( ) ) ) ; <END_BUG> Element root = doc . getDocumentElement ( ) ; NodeList nodes = root . getElementsByTagName ( "body" ) ; Element body = ( ( Element ) ( nodes . item ( 0 ) ) ) ; nodes = body . getElementsByTagName ( "h2" ) ; Element h2 = ( ( Element ) ( nodes . item ( 0 ) ) ) ; String decodedSnippet = h2 . getFirstChild ( ) . getNodeValue ( ) ; assertEquals ( "XHTML<seq2seq4repair_space>Encoding<seq2seq4repair_space>should<seq2seq4repair_space>have<seq2seq4repair_space>worked:" , rawDocContent , decodedSnippet ) ; } public void testFieldSpecificHighlighting ( ) throws Exception { } protected TokenStream getTS2 ( ) { } protected TokenStream getTS2a ( ) { } public void testOverlapAnalyzer2 ( ) throws Exception { } private Directory dir ; private Analyzer a ; public void testWeightedTermsWithDeletes ( ) throws IOException , InvalidTokenOffsetsException { } private org . apache . lucene . document . Document doc ( String f , String v ) { } private void makeIndex ( ) throws IOException { } private void deleteDocument ( ) throws IOException { } private void searchIndex ( ) throws IOException , InvalidTokenOffsetsException { } public String highlightTerm ( String originalText , TokenGroup group ) { } public void doSearching ( Query unReWrittenQuery ) throws Exception { } public void assertExpectedHighlightCount ( final int maxNumFragmentsRequired , final int expectedHighlights ) throws Exception { } @ Override public void setUp ( ) throws Exception { } @ Override public void tearDown ( ) throws Exception { } private void addDoc ( IndexWriter writer , String text ) throws IOException { } private static Token createToken ( String term , int start , int offset ) { } }

Fix:
Document doc = db . parse ( new ByteArrayInputStream ( xhtml . getBytes ( "UTF-8" ) ) ) ;