DiffParser { protected ResultDelta processDelta(Delta delta) { String originalLines = StringUtils.join(delta.getOriginal().getLines(), NEW_LINE); String revisedLines = StringUtils.join(delta.getRevised().getLines(), NEW_LINE); String originalChunkHtml; String revisedChunkHtml; if (Delta.TYPE.CHANGE.equals(delta.getType())) { LinkedList<Diff> diffList = diffMatchPatch.diff_main(originalLines, revisedLines); diffMatchPatch.diff_cleanupSemantic(diffList); LinkedList<Diff> originalDiffList = Lists.newLinkedList(); LinkedList<Diff> revisedDiffList = Lists.newLinkedList(); for (Diff diff : diffList) { switch (diff.operation) { case DELETE: originalDiffList.add(diff); break; case EQUAL: originalDiffList.add(diff); revisedDiffList.add(diff); break; case INSERT: revisedDiffList.add(diff); break; default: break; } } originalChunkHtml = diffMatchPatch.diff_prettyHtml(originalDiffList); revisedChunkHtml = diffMatchPatch.diff_prettyHtml(revisedDiffList); } else { originalChunkHtml = StringEscapeUtils.escapeHtml4(originalLines); revisedChunkHtml = StringEscapeUtils.escapeHtml4(revisedLines); } return buildDelta(originalChunkHtml, revisedChunkHtml, delta); }  List<ResultDelta> generateDiffs(String pattern, String source, boolean trimmedLines);  }
@Test public void testProcessChangeDelta() { String[] originalLines = {"<html>", "Some text", "</html>"}; String[] revisedLines = {"<html>", "Another text", "</html>"}; Delta delta = new ChangeDelta(new Chunk(0, originalLines), new Chunk(0, revisedLines)); ResultDelta resultDelta = tested.processDelta(delta); assertThat( resultDelta.getOriginal().getPrettyHtml(), is("<span>&lt;html&gt;&para;<br></span><del style=\"background:#ffe6e6;\">Some</del><span> text&para;<br>&lt;/html&gt;</span>")); assertThat( resultDelta.getRevised().getPrettyHtml(), is("<span>&lt;html&gt;&para;<br></span><ins style=\"background:#e6ffe6;\">Another</ins><span> text&para;<br>&lt;/html&gt;</span>")); }
@Test public void testProcessDeleteDelta() { String[] originalLines = {"<html>", "Some text", "</html>"}; String[] revisedLines = {}; Delta delta = new DeleteDelta(new Chunk(0, originalLines), new Chunk(0, revisedLines)); ResultDelta resultDelta = tested.processDelta(delta); assertThat(resultDelta.getOriginal().getPrettyHtml(), is("&lt;html&gt;\nSome text\n&lt;/html&gt;")); assertThat(resultDelta.getRevised().getPrettyHtml(), is("<br><br><br>")); }
@Test public void testProcessInsertDelta() { String[] originalLines = {}; String[] revisedLines = {"<html>", "Another text", "</html>"}; Delta delta = new InsertDelta(new Chunk(0, originalLines), new Chunk(0, revisedLines)); ResultDelta resultDelta = tested.processDelta(delta); assertThat(resultDelta.getOriginal().getPrettyHtml(), is("<br><br><br>")); assertThat(resultDelta.getRevised().getPrettyHtml(), is("&lt;html&gt;\nAnother text\n&lt;/html&gt;")); }