From a37fca20f3ad92cc020c77c3f2ec9396dec3e947 Mon Sep 17 00:00:00 2001 From: "Juan J. Garcia 01" <juanjo.garcia@gmail.com> Date: Tue, 18 Oct 2016 08:30:38 -0600 Subject: [PATCH 1/3] Fix unit test, that gives array out of bounds --- src/main/java/difflib/DiffRowGenerator.java | 15 ++- .../java/diffutils/DiffRowGeneratorTest.java | 107 +++++++++++++++++- 2 files changed, 115 insertions(+), 7 deletions(-) diff --git a/src/main/java/difflib/DiffRowGenerator.java b/src/main/java/difflib/DiffRowGenerator.java index a341ff3..8d37612 100644 --- a/src/main/java/difflib/DiffRowGenerator.java +++ b/src/main/java/difflib/DiffRowGenerator.java @@ -457,8 +457,19 @@ private void addInlineDiffs(Delta<String> delta) { } } - delta.getOriginal().setLines(Arrays.asList(Utils.join(origList, "").split(NEW_LINE))); - delta.getRevised().setLines(Arrays.asList(Utils.join(revList, "").split(NEW_LINE))); + delta.getOriginal().setLines(addMissingLines(origList, orig.size())); + delta.getRevised().setLines(addMissingLines(revList, rev.size())); + } + + private List<String> addMissingLines(final List<String> lines, final int targetSize) { + List<String> tempList = Arrays.asList(Utils.join(lines, "").split(NEW_LINE)); + if (tempList.size() < targetSize) { + tempList = new ArrayList<>(tempList); + while (tempList.size() < targetSize) { + tempList.add(""); + } + } + return tempList; } private static final LinkedList<String> charArrayToStringList(char[] cs) { diff --git a/src/test/java/diffutils/DiffRowGeneratorTest.java b/src/test/java/diffutils/DiffRowGeneratorTest.java index de209d4..8e2ac89 100644 --- a/src/test/java/diffutils/DiffRowGeneratorTest.java +++ b/src/test/java/diffutils/DiffRowGeneratorTest.java @@ -1,13 +1,15 @@ package diffutils; -import java.util.Arrays; -import java.util.List; - import difflib.DiffRow; import difflib.DiffRowGenerator; - import junit.framework.TestCase; +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + public class DiffRowGeneratorTest extends TestCase { public void testGenerator_Default() { @@ -35,7 +37,7 @@ public void testGenerator_InlineDiff() { print(rows); assertEquals(3, rows.size()); - assertTrue(rows.get(0).getOldLine().indexOf("<span") > 0); + assertTrue(rows.get(0).getOldLine().indexOf("<del>") > 0); } public void testGenerator_IgnoreWhitespaces() { @@ -56,6 +58,101 @@ public void testGenerator_IgnoreWhitespaces() { assertEquals(rows.get(3).getTag(), DiffRow.Tag.CHANGE); } + public void testChangeToEmptyLine() { + String first = "Test \n \no\n"; + String second ="Test\n\no\n"; + + DiffRowGenerator generator = new DiffRowGenerator.Builder() + .showInlineDiffs(true) + .columnWidth(Integer.MAX_VALUE) // do not wrap + .build(); + List<DiffRow> rows = generator.generateDiffRows(split(first), split(second)); + print(rows); + + assertEquals(3, rows.size()); + assertThat(rows.size(), is(3)); + assertThat(rows.get(0).getTag(), is(DiffRow.Tag.CHANGE)); + assertThat(rows.get(0).getOldLine().indexOf("<del>"), is(4)); + assertThat(rows.get(1).getTag(), is(DiffRow.Tag.CHANGE)); + assertThat(rows.get(1).getOldLine().indexOf("<del>"), is(0)); + assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL)); + } + + public void testChangeToTwoEmptyLine() { + String first = "One\n \nTwo\n \nThree\n"; + String second ="One\n\nTwo\n\nThree\n"; + + DiffRowGenerator generator = new DiffRowGenerator.Builder() + .showInlineDiffs(true) + .columnWidth(Integer.MAX_VALUE) // do not wrap + .build(); + List<DiffRow> rows = generator.generateDiffRows(split(first), split(second)); + print(rows); + + assertEquals(5, rows.size()); + assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL)); + + assertThat(rows.get(1).getTag(), is(DiffRow.Tag.CHANGE)); + assertThat(rows.get(1).getOldLine().indexOf("<del>"), is(0)); + + assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL)); + + assertThat(rows.get(3).getTag(), is(DiffRow.Tag.CHANGE)); + assertThat(rows.get(3).getOldLine().indexOf("<del>"), is(0)); + + } + + public void testDeleteLine() { + String first ="Equal Line\nDeleted Line\nEqual Line 2\n"; + String second = "Equal Line\nEqual Line 2\n"; + + DiffRowGenerator generator = new DiffRowGenerator.Builder() + .showInlineDiffs(true) + .columnWidth(Integer.MAX_VALUE) // do not wrap + .build(); + List<DiffRow> rows = generator.generateDiffRows(split(first), split(second)); + print(rows); + + assertThat(rows.size(), is(3)); + assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL)); + assertThat(rows.get(1).getTag(), is(DiffRow.Tag.DELETE)); + assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL)); + } + + public void testInsertedLine() { + String first = "Equal Line\nEqual Line 2\n"; + String second = "Equal Line\nDeleted Line\nEqual Line 2\n"; + + DiffRowGenerator generator = new DiffRowGenerator.Builder() + .showInlineDiffs(true) + .columnWidth(Integer.MAX_VALUE) // do not wrap + .build(); + List<DiffRow> rows = generator.generateDiffRows(split(first), split(second)); + print(rows); + + assertThat(rows.size(), is(3)); + assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL)); + assertThat(rows.get(1).getTag(), is(DiffRow.Tag.INSERT)); + assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL)); + } + + public void testChangedLine() { + String first = "Equal Line\nLine to be changed\nEqual Line 2\n"; + String second = "Equal Line\nLine changed test\nEqual Line 2\n"; + + DiffRowGenerator generator = new DiffRowGenerator.Builder() + .showInlineDiffs(true) + .columnWidth(Integer.MAX_VALUE) // do not wrap + .build(); + List<DiffRow> rows = generator.generateDiffRows(split(first), split(second)); + print(rows); + + assertThat(rows.size(), is(3)); + assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL)); + assertThat(rows.get(1).getTag(), is(DiffRow.Tag.CHANGE)); + assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL)); + } + private List<String> split(String content) { return Arrays.asList(content.split("\n")); } From 6807ca3dcd514e5324037d9141203a198c4bfb6c Mon Sep 17 00:00:00 2001 From: "Juan J. Garcia 01" <juanjo.garcia@gmail.com> Date: Tue, 18 Oct 2016 20:32:57 -0600 Subject: [PATCH 2/3] Add documentation --- CONTRIBUTING.md | 11 +++++++++++ README.md | 46 +++++++++++++++++++++++++++++++++++++--------- build.gradle | 2 +- 3 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..0dfe689 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,11 @@ +# Contributing + +To contribute do the following: +1. Check existing open issues. +2. If not found, file an issue with any relevant information (versions, steps to reproduce, stacktrace, screenshots, etc). +3. Fork repository. +4. Create a branch on your fork and add the issue # to he name. Like feature/issue-23 or bug/issue-65. +5. Try to match the style code. +6. Create a pull request to develop branch. + +Feel free to send any question. \ No newline at end of file diff --git a/README.md b/README.md index b7bd5ac..1d7b8f4 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,41 @@ -A fork of [java-diff-utils](https://code.google.com/p/java-diff-utils/) +# java-diff-utils -# Changelog +The java-diff-utils library is for computing diffs, applying patches, generation side-by-side view in Java. -## 2.1.0 +It is an OpenSource library for performing the comparison operations between texts: computing diffs, applying patches, generating unified diffs or parsing them, generating diff output for easy future displaying (like side-by-side view) and so on. -- Removes the dependency on Guava +Main reason to build this library was the lack of easy-to-use libraries with all the usual stuff you need while working with diff files. Originally it was inspired by JRCS library and it's nice design of diff module. -## 2.0.0 +**Original code and docs were forked from:** [java-diff-utils](https://code.google.com/p/java-diff-utils/) + +## Main Features + +* computing the difference between two texts. +* capable to hand more than plain ascci. Arrays or List of any type that implements hashCode() and equals() correctly can be subject to differencing using this library +* patch and unpatch the text with the given patch +* parsing the unified diff format +* producing human-readable differences + +## Algorithms + +This library implements Myer's diff algorithm. But it can easily replaced by any other which is better for handing your texts. + +# Tutorial + +* In Spanish: [Comparar Ficheros java-diff-utils](https://www.adictosaltrabajo.com/tutoriales/comparar-ficheros-java-diff-utils/) + +## Changelog + +### 2.1.1 + +- Bugfix: Fix issue showing inline diffs. +- Added some unit tests. + +### 2.1.0 + +- Removes the dependency on Guavatime + +### 2.0.0 - Change groupId and artifactId to prevent conflict with origin library: now 'com.github.java-diff-utils:java-diff-utils' instead of 'jp.skypencil.java-diff-utils:diffutils' - Adds the ability to differentiate the inserted and deleted tags and class-names in inline-diff @@ -18,18 +47,17 @@ A fork of [java-diff-utils](https://code.google.com/p/java-diff-utils/) - fix imbrication tag bug in lineDiff (when inline is on a multi-line chunk) - Adds tha ability to skip data -## 1.5.0 +### 1.5.0 - make Equalizer configurable. ([pull #1](https://github.com/eller86/java-diff-utils/pull/1)) -## 1.4.1 +### 1.4.1 - bugfix: parse method should be public -## 1.4.0 +### 1.4.0 - switch from JDK5 to JDK7 - add Guava to dependency - let user uses other string to represent line which does not exist - implement event based parser like SAX (in difflib.event package) - diff --git a/build.gradle b/build.gradle index 713a208..6f20a51 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'java' apply plugin: 'maven' group = 'com.github.java-diff-utils' -version = '2.1.0-SNAPSHOT' +version = '2.1.1-SNAPSHOT' description = """java-diff-utils""" From cfc00cf5beccd0e42977b8c9556090bd73b8486b Mon Sep 17 00:00:00 2001 From: "Juan J. Garcia 01" <juanjo.garcia@gmail.com> Date: Tue, 18 Oct 2016 20:37:30 -0600 Subject: [PATCH 3/3] Prepare version 2.1.1 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 6f20a51..b671ea0 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'java' apply plugin: 'maven' group = 'com.github.java-diff-utils' -version = '2.1.1-SNAPSHOT' +version = '2.1.1' description = """java-diff-utils"""