|
7 | 7 | import difflib.patch.Delta; |
8 | 8 | import difflib.patch.InsertDelta; |
9 | 9 | import difflib.patch.Patch; |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.io.InputStreamReader; |
| 14 | +import java.nio.charset.Charset; |
| 15 | +import java.nio.charset.StandardCharsets; |
10 | 16 | import java.util.ArrayList; |
11 | 17 | import java.util.Arrays; |
12 | 18 | import java.util.Collections; |
13 | 19 | import java.util.List; |
| 20 | +import static java.util.stream.Collectors.toList; |
| 21 | +import java.util.zip.ZipFile; |
14 | 22 | import static org.junit.Assert.assertEquals; |
15 | 23 | import static org.junit.Assert.assertNotNull; |
16 | 24 | import static org.junit.Assert.assertTrue; |
| 25 | +import org.junit.Ignore; |
17 | 26 | import org.junit.Test; |
18 | 27 |
|
19 | 28 | public class DiffUtilsTest { |
@@ -101,13 +110,46 @@ public void testDiffIntegerList() throws DiffException { |
101 | 110 | List<Integer> revised = Arrays.asList(2, 3, 4, 6); |
102 | 111 |
|
103 | 112 | final Patch<Integer> patch = DiffUtils.diff(original, revised); |
104 | | - |
| 113 | + |
105 | 114 | for (Delta delta : patch.getDeltas()) { |
106 | 115 | System.out.println(delta); |
107 | 116 | } |
108 | | - |
| 117 | + |
109 | 118 | assertEquals(2, patch.getDeltas().size()); |
110 | 119 | assertEquals("[DeleteDelta, position: 0, lines: [1]]", patch.getDeltas().get(0).toString()); |
111 | 120 | assertEquals("[ChangeDelta, position: 4, lines: [5] to [6]]", patch.getDeltas().get(1).toString()); |
112 | 121 | } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testDiffMissesChangeForkDnaumenkoIssue31() throws DiffException { |
| 125 | + List<String> original = Arrays.asList("line1", "line2", "line3"); |
| 126 | + List<String> revised = Arrays.asList("line1", "line2-2", "line4"); |
| 127 | + |
| 128 | + Patch<String> patch = DiffUtils.diff(original, revised); |
| 129 | + assertEquals(1, patch.getDeltas().size()); |
| 130 | + assertEquals("[ChangeDelta, position: 1, lines: [line2, line3] to [line2-2, line4]]", patch.getDeltas().get(0).toString()); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * To test this, the greedy meyer algorithm is not suitable. |
| 135 | + */ |
| 136 | + @Test |
| 137 | + @Ignore |
| 138 | + public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException { |
| 139 | + ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip"); |
| 140 | + |
| 141 | + Patch<String> patch = DiffUtils.diff( |
| 142 | + readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))), |
| 143 | + readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")))); |
| 144 | + |
| 145 | + assertEquals(1, patch.getDeltas().size()); |
| 146 | + } |
| 147 | + |
| 148 | + private static List<String> readStringListFromInputStream(InputStream is) throws IOException { |
| 149 | + try (BufferedReader reader = new BufferedReader( |
| 150 | + new InputStreamReader(is, Charset.forName(StandardCharsets.UTF_8.name())))) { |
| 151 | + |
| 152 | + return reader.lines().collect(toList()); |
| 153 | + } |
| 154 | + } |
113 | 155 | } |
0 commit comments