com.github.difflib.patch.Patch Java Examples

The following examples show how to use com.github.difflib.patch.Patch. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Client.java    From batfish with Apache License 2.0 7 votes vote down vote up
/**
 * Computes a unified diff of the input strings, returning the empty string if the {@code
 * expected} and {@code actual} are equal.
 */
@Nonnull
@VisibleForTesting
static String getPatch(
    String expected, String actual, String expectedFileName, String actualFileName)
    throws DiffException {
  List<String> referenceLines = Arrays.asList(expected.split("\n"));
  List<String> testLines = Arrays.asList(actual.split("\n"));
  Patch<String> patch = DiffUtils.diff(referenceLines, testLines);
  if (patch.getDeltas().isEmpty()) {
    return "";
  } else {
    List<String> patchLines =
        UnifiedDiffUtils.generateUnifiedDiff(
            expectedFileName, actualFileName, referenceLines, patch, 3);
    return StringUtils.join(patchLines, "\n");
  }
}
 
Example #2
Source File: GenerateUnifiedDiffTest.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue 19
 */
@Test
public void testDiffWithHeaderLineInText() {
    List<String> original = new ArrayList<>();
    List<String> revised = new ArrayList<>();

    original.add("test line1");
    original.add("test line2");
    original.add("test line 4");
    original.add("test line 5");

    revised.add("test line1");
    revised.add("test line2");
    revised.add("@@ -2,6 +2,7 @@");
    revised.add("test line 4");
    revised.add("test line 5");

    Patch<String> patch = DiffUtils.diff(original, revised);
    List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff("original", "revised",
            original, patch, 10);
    UnifiedDiffUtils.parseUnifiedDiff(udiff);
}
 
Example #3
Source File: MyersDiffTest.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiffMyersExample1ForwardWithListener() {
    List<String> original = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revised = Arrays.asList("C", "B", "A", "B", "A", "C");
    
    List<String> logdata = new ArrayList<>();
    final Patch<String> patch = Patch.generate(original, revised, 
            new MyersDiff<String>().computeDiff(original, revised, new DiffAlgorithmListener() {
        @Override
        public void diffStart() {
            logdata.add("start");
        }

        @Override
        public void diffStep(int value, int max) {
            logdata.add(value + " - " + max);
        }

        @Override
        public void diffEnd() {
            logdata.add("end");
        }
    }));
    assertNotNull(patch);
    assertEquals(4, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString());
    System.out.println(logdata);
    assertEquals(8, logdata.size());
}
 
Example #4
Source File: UnifiedDiffRoundTripTest.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiff_Issue10() throws IOException {
    final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
    final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");

    UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(
            new ByteArrayInputStream(patchLines.stream().collect(joining("\n")).getBytes())
    );

    final Patch<String> p = unifiedDiff.getFiles().get(0).getPatch();
    try {
        DiffUtils.patch(baseLines, p);
    } catch (PatchFailedException e) {
        fail(e.getMessage());
    }
}
 
Example #5
Source File: GenerateUnifiedDiffTest.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
private void verify(List<String> origLines, List<String> revLines,
        String originalFile, String revisedFile) {
    Patch<String> patch = DiffUtils.diff(origLines, revLines);
    List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
            origLines, patch, 10);

    System.out.println(unifiedDiff.stream().collect(joining("\n")));

    Patch<String> fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
    List<String> patchedLines;
    try {
        patchedLines = fromUnifiedPatch.applyTo(origLines);
        assertEquals(revLines.size(), patchedLines.size());
        for (int i = 0; i < revLines.size(); i++) {
            String l1 = revLines.get(i);
            String l2 = patchedLines.get(i);
            if (!l1.equals(l2)) {
                fail("Line " + (i + 1) + " of the patched file did not match the revised original");
            }
        }
    } catch (PatchFailedException e) {
        fail(e.getMessage());
    }
}
 
Example #6
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiff_InsertWithEqual() {
    final Patch<String> patch = DiffUtils.diff(Arrays.asList("hhh"), Arrays.
            asList("hhh", "jjj", "kkk"), true);
    assertNotNull(patch);
    assertEquals(2, patch.getDeltas().size());

    AbstractDelta<String> delta = patch.getDeltas().get(0);
    assertTrue(delta instanceof EqualDelta);
    assertEquals(new Chunk<>(0, Arrays.asList("hhh")), delta.getSource());
    assertEquals(new Chunk<>(0, Arrays.asList("hhh")), delta.getTarget());

    delta = patch.getDeltas().get(1);
    assertTrue(delta instanceof InsertDelta);
    assertEquals(new Chunk<>(1, Collections.<String>emptyList()), delta.getSource());
    assertEquals(new Chunk<>(1, Arrays.asList("jjj", "kkk")), delta.getTarget());
}
 
Example #7
Source File: UnifiedDiffUtils.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
private static void processLinesInPrevChunk(List<String[]> rawChunk, Patch<String> patch, int old_ln, int new_ln) {
    String tag;
    String rest;
    if (!rawChunk.isEmpty()) {
        List<String> oldChunkLines = new ArrayList<>();
        List<String> newChunkLines = new ArrayList<>();

        for (String[] raw_line : rawChunk) {
            tag = raw_line[0];
            rest = raw_line[1];
            if (" ".equals(tag) || "-".equals(tag)) {
                oldChunkLines.add(rest);
            }
            if (" ".equals(tag) || "+".equals(tag)) {
                newChunkLines.add(rest);
            }
        }
        patch.addDelta(new ChangeDelta<>(new Chunk<>(
                old_ln - 1, oldChunkLines), new Chunk<>(
                new_ln - 1, newChunkLines)));
        rawChunk.clear();
    }
}
 
Example #8
Source File: GenerateUnifiedDiffTest.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue 47
 */
@Test
public void testNewFileCreation() {
    List<String> original = new ArrayList<>();
    List<String> revised = new ArrayList<>();

    revised.add("line1");
    revised.add("line2");

    Patch<String> patch = DiffUtils.diff(original, revised);
    List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff(null, "revised",
            original, patch, 10);
    
    assertEquals("--- ", udiff.get(0));
    assertEquals("+++ revised", udiff.get(1));
    assertEquals("@@ -0,0 +1,2 @@", udiff.get(2));
    
    UnifiedDiffUtils.parseUnifiedDiff(udiff);
}
 
Example #9
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiff_ProblemIssue42() {
    final Patch<String> patch = DiffUtils.diff(
            Arrays.asList("The", "dog", "is", "brown"),
            Arrays.asList("The", "fox", "is", "down"), true);

    System.out.println(patch);
    assertNotNull(patch);
    assertEquals(4, patch.getDeltas().size());

    
    assertThat(patch.getDeltas()).extracting(d -> d.getType().name())
            .containsExactly("EQUAL", "CHANGE", "EQUAL", "CHANGE");
    
    AbstractDelta<String> delta = patch.getDeltas().get(0);
    assertTrue(delta instanceof EqualDelta);
    assertEquals(new Chunk<>(0, Arrays.asList("The")), delta.getSource());
    assertEquals(new Chunk<>(0, Arrays.asList("The")), delta.getTarget());

    delta = patch.getDeltas().get(1);
    assertTrue(delta instanceof ChangeDelta);
    assertEquals(new Chunk<>(1, Arrays.asList("dog")), delta.getSource());
    assertEquals(new Chunk<>(1, Arrays.asList("fox")), delta.getTarget());
    
    delta = patch.getDeltas().get(2);
    assertTrue(delta instanceof EqualDelta);
    assertEquals(new Chunk<>(2, Arrays.asList("is")), delta.getSource());
    assertEquals(new Chunk<>(2, Arrays.asList("is")), delta.getTarget());
    
    delta = patch.getDeltas().get(3);
    assertTrue(delta instanceof ChangeDelta);
    assertEquals(new Chunk<>(3, Arrays.asList("brown")), delta.getSource());
    assertEquals(new Chunk<>(3, Arrays.asList("down")), delta.getTarget());
}
 
Example #10
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiff_Equal() {
    final Patch<String> patch = DiffUtils.diff(
            Arrays.asList("hhh", "jjj", "kkk"),
            Arrays.asList("hhh", "jjj", "kkk"), true);
    assertNotNull(patch);
    assertEquals(1, patch.getDeltas().size());
    final AbstractDelta<String> delta = patch.getDeltas().get(0);
    assertTrue(delta instanceof EqualDelta);
    assertEquals(new Chunk<>(0, Arrays.asList("hhh", "jjj", "kkk")), delta.getSource());
    assertEquals(new Chunk<>(0, Arrays.asList("hhh", "jjj", "kkk")), delta.getTarget());
}
 
Example #11
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffMyersExample1() {
    final Patch<String> patch = DiffUtils.diff(Arrays.asList("A", "B", "C", "A", "B", "B", "A"), Arrays.asList("C", "B", "A", "B", "A", "C"));
    assertNotNull(patch);
    assertEquals(4, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString());
}
 
Example #12
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
/**
 * To test this, the greedy meyer algorithm is not suitable.
 */
@Test
@Disabled
public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException {
    ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip");

    Patch<String> patch = DiffUtils.diff(
            readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))),
            readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))));

    assertEquals(1, patch.getDeltas().size());
}
 
Example #13
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffMissesChangeForkDnaumenkoIssue31() {
    List<String> original = Arrays.asList("line1", "line2", "line3");
    List<String> revised = Arrays.asList("line1", "line2-2", "line4");

    Patch<String> patch = DiffUtils.diff(original, revised);
    assertEquals(1, patch.getDeltas().size());
    assertEquals("[ChangeDelta, position: 1, lines: [line2, line3] to [line2-2, line4]]", patch.getDeltas().get(0).toString());
}
 
Example #14
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffIntegerList() {
    List<Integer> original = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> revised = Arrays.asList(2, 3, 4, 6);

    final Patch<Integer> patch = DiffUtils.diff(original, revised);

    for (AbstractDelta delta : patch.getDeltas()) {
        System.out.println(delta);
    }

    assertEquals(2, patch.getDeltas().size());
    assertEquals("[DeleteDelta, position: 0, lines: [1]]", patch.getDeltas().get(0).toString());
    assertEquals("[ChangeDelta, position: 4, lines: [5] to [6]]", patch.getDeltas().get(1).toString());
}
 
Example #15
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffInline2() {
    final Patch<String> patch = DiffUtils.diffInline("es", "fest");
    assertEquals(2, patch.getDeltas().size());
    assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
    assertEquals(0, patch.getDeltas().get(0).getSource().getPosition());
    assertEquals(2, patch.getDeltas().get(1).getSource().getPosition());
    assertEquals(0, patch.getDeltas().get(0).getSource().getLines().size());
    assertEquals(0, patch.getDeltas().get(1).getSource().getLines().size());
    assertEquals("f", patch.getDeltas().get(0).getTarget().getLines().get(0));
    assertEquals("t", patch.getDeltas().get(1).getTarget().getLines().get(0));
}
 
Example #16
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffInline() {
    final Patch<String> patch = DiffUtils.diffInline("", "test");
    assertEquals(1, patch.getDeltas().size());
    assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
    assertEquals(0, patch.getDeltas().get(0).getSource().getPosition());
    assertEquals(0, patch.getDeltas().get(0).getSource().getLines().size());
    assertEquals("test", patch.getDeltas().get(0).getTarget().getLines().get(0));
}
 
Example #17
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiff_EmptyListWithNonEmpty() {
    final Patch<String> patch = DiffUtils.diff(new ArrayList<>(), Arrays.asList("aaa"));
    assertNotNull(patch);
    assertEquals(1, patch.getDeltas().size());
    final AbstractDelta<String> delta = patch.getDeltas().get(0);
    assertTrue(delta instanceof InsertDelta);
}
 
Example #18
Source File: GenerateUnifiedDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiff_Issue10() throws IOException {
    final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
    final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
    final Patch<String> p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
    try {
        DiffUtils.patch(baseLines, p);
    } catch (PatchFailedException e) {
        fail(e.getMessage());
    }
}
 
Example #19
Source File: TestHelper.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Render the expected template and compare it with the given actual file.
 * The actual file must exist and be identical to the rendered template,
 * otherwise assert errors will be thrown.
 *
 * @param outputdir the output directory where to render the expected template
 * @param expectedTpl the template used for comparing the actual file
 * @param actual the rendered file to be compared
 * @throws Exception if an error occurred
 */
public static void assertRendering(File outputdir, File expectedTpl, File actual)
        throws Exception {

    assertTrue(actual.exists(), actual.getAbsolutePath() + " does not exist");

    // render expected
    FileTemplateLoader ftl = new FileTemplateLoader(expectedTpl.getParentFile());
    Configuration config = new Configuration(Configuration.VERSION_2_3_23);
    config.setTemplateLoader(ftl);
    Template template = config.getTemplate(expectedTpl.getName());
    File expected = new File(outputdir, "expected_" + actual.getName());
    FileWriter writer = new FileWriter(expected);
    Map<String, Object> model = new HashMap<>();
    model.put("basedir", getBasedirPath());
    template.process(model, writer);

    // diff expected and rendered
    List<String> expectedLines = Files.readAllLines(expected.toPath());
    List<String> actualLines = Files.readAllLines(actual.toPath());

    // compare expected and rendered
    Patch<String> patch = DiffUtils.diff(expectedLines, actualLines);
    if (patch.getDeltas().size() > 0) {
        fail("rendered file " + actual.getAbsolutePath() + " differs from expected: " + patch.toString());
    }
}
 
Example #20
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiff_Change() {
    final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc");
    final List<String> changeTest_to = Arrays.asList("aaa", "zzz", "ccc");

    final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
    assertNotNull(patch);
    assertEquals(1, patch.getDeltas().size());
    final AbstractDelta<String> delta = patch.getDeltas().get(0);
    assertTrue(delta instanceof ChangeDelta);
    assertEquals(new Chunk<>(1, Arrays.asList("bbb")), delta.getSource());
    assertEquals(new Chunk<>(1, Arrays.asList("zzz")), delta.getTarget());
}
 
Example #21
Source File: DiffUtilsTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiff_Delete() {
    final Patch<String> patch = DiffUtils.diff(Arrays.asList("ddd", "fff", "ggg"), Arrays.
            asList("ggg"));
    assertNotNull(patch);
    assertEquals(1, patch.getDeltas().size());
    final AbstractDelta<String> delta = patch.getDeltas().get(0);
    assertTrue(delta instanceof DeleteDelta);
    assertEquals(new Chunk<>(0, Arrays.asList("ddd", "fff")), delta.getSource());
    assertEquals(new Chunk<>(0, Collections.<String>emptyList()), delta.getTarget());
}
 
Example #22
Source File: UnifiedDiffRoundTripTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
private void verify(List<String> origLines, List<String> revLines,
            String originalFile, String revisedFile) throws IOException {
        Patch<String> patch = DiffUtils.diff(origLines, revLines);

        StringWriter writer = new StringWriter();
        UnifiedDiffWriter.write(
                UnifiedDiff.from("header", "tail", UnifiedDiffFile.from(originalFile, revisedFile, patch)),
                name -> origLines,
                writer, 10);

        System.out.println(writer.toString());

        UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(writer.toString().getBytes()));

        List<String> patchedLines;
        try {
//            if (unifiedDiff.getFiles().isEmpty()) {
//                patchedLines = new ArrayList<>(origLines);
//            } else {
//                Patch<String> fromUnifiedPatch = unifiedDiff.getFiles().get(0).getPatch();
//                patchedLines = fromUnifiedPatch.applyTo(origLines);
//            }
            patchedLines = unifiedDiff.spplyPatchTo(file -> originalFile.equals(file), origLines);
            assertEquals(revLines.size(), patchedLines.size());
            for (int i = 0; i < revLines.size(); i++) {
                String l1 = revLines.get(i);
                String l2 = patchedLines.get(i);
                if (!l1.equals(l2)) {
                    fail("Line " + (i + 1) + " of the patched file did not match the revised original");
                }
            }
        } catch (PatchFailedException e) {
            fail(e.getMessage());
        }
    }
 
Example #23
Source File: MyersDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffMyersExample1Forward() {
    List<String> original = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revised = Arrays.asList("C", "B", "A", "B", "A", "C");
    final Patch<String> patch = Patch.generate(original, revised, new MyersDiff<String>().computeDiff(original, revised, null));
    assertNotNull(patch);
    assertEquals(4, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString());
}
 
Example #24
Source File: UnifiedDiffFile.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
public static UnifiedDiffFile from(String fromFile, String toFile, Patch<String> patch) {
    UnifiedDiffFile file = new UnifiedDiffFile();
    file.setFromFile(fromFile);
    file.setToFile(toFile);
    file.patch = patch;
    return file;
}
 
Example #25
Source File: PatchUtil.java    From bazel with Apache License 2.0 5 votes vote down vote up
public static List<String> applyTo(Patch<String> patch, List<String> target)
    throws PatchFailedException {
  List<AbstractDelta<String>> deltas = patch.getDeltas();
  List<String> result = new ArrayList<>(target);
  for (AbstractDelta<String> item : Lists.reverse(deltas)) {
    AbstractDelta<String> delta = item;
    applyTo(delta, result);
  }

  return result;
}
 
Example #26
Source File: TextDiffSubject.java    From nomulus with Apache License 2.0 5 votes vote down vote up
static String generateUnifiedDiff(
    ImmutableList<String> expectedContent, ImmutableList<String> actualContent) {
  Patch<String> diff;
  try {
    diff = DiffUtils.diff(expectedContent, actualContent);
  } catch (DiffException e) {
    throw new RuntimeException(e);
  }
  List<String> unifiedDiff =
      UnifiedDiffUtils.generateUnifiedDiff("expected", "actual", expectedContent, diff, 0);

  return Joiner.on('\n').join(unifiedDiff);
}
 
Example #27
Source File: DiffUtils.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the difference between the original and revised text.
 */
public static Patch<String> diff(String sourceText, String targetText,
        DiffAlgorithmListener progress) {
    return DiffUtils.diff(
            Arrays.asList(sourceText.split("\n")),
            Arrays.asList(targetText.split("\n")), progress);
}
 
Example #28
Source File: LRHistogramDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, PatchFailedException {
    ZipFile zip = new ZipFile("target/test-classes/mocks/large_dataset1.zip");
    List<String> original = readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta")));
    List<String> revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")));

    List<String> logdata = new ArrayList<>();
    Patch<String> patch = Patch.generate(original, revised, new HistogramDiff().computeDiff(original, revised, new DiffAlgorithmListener() {
        @Override
        public void diffStart() {
            logdata.add("start");
        }

        @Override
        public void diffStep(int value, int max) {
            logdata.add(value + " - " + max);
        }

        @Override
        public void diffEnd() {
            logdata.add("end");
        }
    }));

    assertEquals(34, patch.getDeltas().size());

    List<String> created = patch.applyTo(original);
    assertArrayEquals(revised.toArray(), created.toArray());
    
    assertEquals(246579, logdata.size());
}
 
Example #29
Source File: HistogramDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffWithListener() throws PatchFailedException {
    List<String> orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revList = Arrays.asList("C", "B", "A", "B", "A", "C");
    
    List<String> logdata = new ArrayList<>();
    final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff().computeDiff(orgList, revList, new DiffAlgorithmListener() {
        @Override
        public void diffStart() {
            logdata.add("start");
        }

        @Override
        public void diffStep(int value, int max) {
            logdata.add(value + " - " + max);
        }

        @Override
        public void diffEnd() {
            logdata.add("end");
        }
    }));
    System.out.println(patch);
    assertNotNull(patch);
    assertEquals(3, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString());

    List<String> patched = patch.applyTo(orgList);
    assertEquals(revList, patched);
    
    System.out.println(logdata);
    assertEquals(17, logdata.size());
}
 
Example #30
Source File: HistogramDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Test of diff method, of class HistogramDiff.
 */
@Test
public void testDiff() throws PatchFailedException {
    List<String> orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revList = Arrays.asList("C", "B", "A", "B", "A", "C");
    final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff().computeDiff(orgList, revList, null));
    System.out.println(patch);
    assertNotNull(patch);
    assertEquals(3, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString());

    List<String> patched = patch.applyTo(orgList);
    assertEquals(revList, patched);
}