### Question:
WriteCoocurrencesToLoadfile2 extends JCasAnnotator_ImplBase { public static String snippet(JCas jCas, int snippetBegin, int snippetEnd, Annotation a1, Annotation a2) { final String T_BEGIN = "<strong class=\""; final String T_END = "</strong>"; String s = jCas.getDocumentText().substring(snippetBegin, snippetEnd); String sError = s; try { int relA1Begin = a1.getBegin() - snippetBegin; int relA1End = a1.getEnd() - snippetBegin; int relA2Begin = a2.getBegin() - snippetBegin; int relA2End = a2.getEnd() - snippetBegin; Position position = BlueCasUtil.isBefore(a1, a2); if (position == Position.before) { s = s.substring(0, relA2End) + T_END + s.substring(relA2End); s = s.substring(0, relA2Begin) + T_BEGIN + a2.getClass().getSimpleName() + "\">" + s.substring(relA2Begin); s = s.substring(0, relA1End) + T_END + s.substring(relA1End); s = s.substring(0, relA1Begin) + T_BEGIN + a1.getClass().getSimpleName() + "\">" + s.substring(relA1Begin); } else if (position == Position.after) { s = s.substring(0, relA1End) + T_END + s.substring(relA1End); s = s.substring(0, relA1Begin) + T_BEGIN + a1.getClass().getSimpleName() + "\">" + s.substring(relA1Begin); s = s.substring(0, relA2End) + T_END + s.substring(relA2End); s = s.substring(0, relA2Begin) + T_BEGIN + a2.getClass().getSimpleName() + "\">" + s.substring(relA2Begin); } else { LOG.warn( "don't know how to highlight snippet that overlaps '{}' and '{}', pmid:{}" + getHeaderDocId(jCas), a1.getCoveredText(), a2.getCoveredText()); } return s.replaceAll("[\r\t\n]", ""); } catch (Exception e) { LOG.warn( "Could not extract snippet on pmid {} beg {} end {} a1 {} a2 {}", new Object[] { getHeaderDocId(jCas), snippetBegin, snippetEnd, To.string(a1), To.string(a2) }); return sError; } }  @Override void initialize(UimaContext context); void process(JCas jCas); static String snippet(JCas jCas, int snippetBegin, int snippetEnd,
            Annotation a1, Annotation a2); @Override void collectionProcessComplete();  }### Answer:
@Test public void testSnippet() throws Exception { JCas jCas = getTokenizedTestCas("Test sentence for snippetting great"); Protein a1 = createAnnot(jCas, Protein.class, 5, 13); Protein a2 = createAnnot(jCas, Protein.class, 18, 29); String snippet = snippet(jCas, 2, 32, a1, a2); assertEquals( "st <strong class=\"Protein\">sentence</strong> for <strong class=\"Protein\">snippetting</strong> gr", snippet); snippet = snippet(jCas, 2, 32, a2, a1); assertEquals( "a2 then a1", "st <strong class=\"Protein\">sentence</strong> for <strong class=\"Protein\">snippetting</strong> gr", snippet); }