Skip to content
This repository was archived by the owner on Nov 12, 2019. It is now read-only.

Commit 76fa0b4

Browse files
committed
Introduced generic type to some tests. Close IO streams in tests.
1 parent a4292fd commit 76fa0b4

File tree

6 files changed

+56
-38
lines changed

6 files changed

+56
-38
lines changed

src/main/java/difflib/Chunk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public boolean equals(Object obj) {
139139
return false;
140140
if (getClass() != obj.getClass())
141141
return false;
142-
Chunk other = (Chunk) obj;
142+
Chunk<T> other = (Chunk) obj;
143143
if (lines == null) {
144144
if (other.lines != null)
145145
return false;

src/main/java/difflib/DiffRowGenerator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ private List<String> removeBlankLines(List<String> lines) {
209209
* @param patch the given patch
210210
* @return the DiffRows between original and revised texts
211211
*/
212-
@SuppressWarnings("unchecked")
213212
public List<DiffRow> generateDiffRows(List<String> original, List<String> revised, Patch<String> patch) {
214213
// normalize the lines (expand tabs, escape html entities)
215214
original = StringUtills.normalize(original);
@@ -291,7 +290,6 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
291290
* Add the inline diffs for given delta
292291
* @param delta the given delta
293292
*/
294-
@SuppressWarnings("unchecked")
295293
private void addInlineDiffs(Delta<String> delta) {
296294
List<String> orig = (List<String>) delta.getOriginal().getLines();
297295
List<String> rev = (List<String>) delta.getRevised().getLines();

src/test/java/diffutils/GenerateUnifiedDiffTest.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
package diffutils;
22

3-
import difflib.DiffUtils;
4-
import difflib.Patch;
5-
import difflib.PatchFailedException;
6-
import junit.framework.TestCase;
7-
83
import java.io.BufferedReader;
9-
import java.io.File;
104
import java.io.FileReader;
115
import java.io.IOException;
126
import java.util.ArrayList;
137
import java.util.Arrays;
148
import java.util.LinkedList;
159
import java.util.List;
1610

11+
import junit.framework.TestCase;
12+
import difflib.DiffUtils;
13+
import difflib.Patch;
14+
import difflib.PatchFailedException;
15+
1716
public class GenerateUnifiedDiffTest extends TestCase {
1817

1918

2019
public List<String> fileToLines(String filename) {
2120
List<String> lines = new LinkedList<String>();
2221
String line = "";
22+
BufferedReader in = null;
2323
try {
24-
BufferedReader in = new BufferedReader(new FileReader(filename));
24+
in = new BufferedReader(new FileReader(filename));
2525
while ((line = in.readLine()) != null) {
2626
lines.add(line);
2727
}
2828
} catch (IOException e) {
2929
e.printStackTrace();
3030
fail(e.getMessage());
31-
}
31+
} finally {
32+
if (in != null) {
33+
try {
34+
in.close();
35+
} catch (IOException e) {
36+
// ignore ... any errors should already have been
37+
// reported via an IOException from the final flush.
38+
}
39+
}
40+
}
3241
return lines;
3342
}
3443

@@ -48,14 +57,14 @@ public void testGenerateUnifiedWithOneDelta() {
4857

4958
public void testGenerateUnifiedDiffWithoutAnyDeltas() {
5059
List<String> test = Arrays.asList("abc");
51-
Patch patch = DiffUtils.diff(test, test);
60+
Patch<String> patch = DiffUtils.diff(test, test);
5261
DiffUtils.generateUnifiedDiff("abc", "abc", test, patch, 0);
5362
}
5463

5564
public void testDiff_Issue10() {
5665
final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
5766
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
58-
final Patch p = DiffUtils.parseUnifiedDiff(patchLines);
67+
final Patch<String> p = DiffUtils.parseUnifiedDiff(patchLines);
5968
try {
6069
DiffUtils.patch(baseLines, p);
6170
} catch (PatchFailedException e) {
@@ -96,20 +105,19 @@ public void testDiffWithHeaderLineInText() {
96105
revised.add("test line 4");
97106
revised.add("test line 5");
98107

99-
Patch patch = DiffUtils.diff(original, revised);
108+
Patch<String> patch = DiffUtils.diff(original, revised);
100109
List<String> udiff = DiffUtils.generateUnifiedDiff("original", "revised",
101110
original, patch, 10);
102111
DiffUtils.parseUnifiedDiff(udiff);
103112
}
104113

105-
@SuppressWarnings("unchecked")
106114
private void verify(List<String> origLines, List<String> revLines,
107115
String originalFile, String revisedFile) {
108-
Patch patch = DiffUtils.diff(origLines, revLines);
116+
Patch<String> patch = DiffUtils.diff(origLines, revLines);
109117
List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(originalFile, revisedFile,
110118
origLines, patch, 10);
111119

112-
Patch fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
120+
Patch<String> fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
113121
List<String> patchedLines;
114122
try {
115123
patchedLines = (List<String>) fromUnifiedPatch.applyTo(origLines);

src/test/java/diffutils/PatchTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void testPatch_Insert() {
1414
final List<String> insertTest_from = Arrays.asList("hhh");
1515
final List<String> insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
1616

17-
final Patch patch = DiffUtils.diff(insertTest_from, insertTest_to);
17+
final Patch<String> patch = DiffUtils.diff(insertTest_from, insertTest_to);
1818
try {
1919
assertEquals(insertTest_to, DiffUtils.patch(insertTest_from, patch));
2020
} catch (PatchFailedException e) {
@@ -26,7 +26,7 @@ public void testPatch_Delete() {
2626
final List<String> deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh");
2727
final List<String> deleteTest_to = Arrays.asList("ggg");
2828

29-
final Patch patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
29+
final Patch<String> patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
3030
try {
3131
assertEquals(deleteTest_to, DiffUtils.patch(deleteTest_from, patch));
3232
} catch (PatchFailedException e) {
@@ -38,7 +38,7 @@ public void testPatch_Change() {
3838
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
3939
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
4040

41-
final Patch patch = DiffUtils.diff(changeTest_from, changeTest_to);
41+
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
4242
try {
4343
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, patch));
4444
} catch (PatchFailedException e) {

src/test/java/diffutils/examples/ApplyPatch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public static void main(String[] args) throws PatchFailedException {
1717
List<String> patched = fileToLines(PATCH);
1818

1919
// At first, parse the unified diff file and get the patch
20-
Patch patch = DiffUtils.parseUnifiedDiff(patched);
20+
Patch<String> patch = DiffUtils.parseUnifiedDiff(patched);
2121

2222
// Then apply the computed patch to the given text
23-
List result = DiffUtils.patch(original, patch);
23+
List<String> result = DiffUtils.patch(original, patch);
2424
System.out.println(result);
2525
// / Or we can call patch.applyTo(original). There is no difference.
2626
}

src/test/java/diffutils/examples/Example.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,40 @@
88
import java.util.List;
99

1010
public abstract class Example {
11-
11+
1212
/** File separator. */
1313
protected static final String FS = File.separator;
1414
/** The base resource path. */
1515
protected static String BASE_PATH = "src" + FS + "test" + FS + "resources";
16-
16+
1717
/**
1818
* Tries to read the file and split it into a list of lines.
19-
* @param filename The filename as path.
19+
*
20+
* @param filename
21+
* The filename as path.
2022
* @return A list of lines.
2123
*/
22-
public static List<String> fileToLines(String filename) {
23-
List<String> lines = new LinkedList<String>();
24-
String line = "";
25-
try {
26-
BufferedReader in = new BufferedReader(new FileReader(filename));
27-
while ((line = in.readLine()) != null) {
28-
lines.add(line);
29-
}
30-
} catch (IOException e) {
31-
e.printStackTrace();
32-
}
33-
return lines;
34-
}
24+
public static List<String> fileToLines(String filename) {
25+
List<String> lines = new LinkedList<String>();
26+
String line = "";
27+
BufferedReader in = null;
28+
try {
29+
in = new BufferedReader(new FileReader(filename));
30+
while ((line = in.readLine()) != null) {
31+
lines.add(line);
32+
}
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
} finally {
36+
if (in != null) {
37+
try {
38+
in.close();
39+
} catch (IOException e) {
40+
// ignore ... any errors should already have been
41+
// reported via an IOException from the final flush.
42+
}
43+
}
44+
}
45+
return lines;
46+
}
3547
}

0 commit comments

Comments
 (0)