Introduced generic type to some tests. Close IO streams in tests.

This commit is contained in:
simon.mittermueller@gmail.com
2013-03-04 11:19:43 +00:00
parent 66aa2c02f2
commit 882f3f7e1e
6 changed files with 56 additions and 38 deletions

View File

@@ -139,7 +139,7 @@ public class Chunk<T> {
return false;
if (getClass() != obj.getClass())
return false;
Chunk other = (Chunk) obj;
Chunk<T> other = (Chunk) obj;
if (lines == null) {
if (other.lines != null)
return false;

View File

@@ -209,7 +209,6 @@ public class DiffRowGenerator {
* @param patch the given patch
* @return the DiffRows between original and revised texts
*/
@SuppressWarnings("unchecked")
public List<DiffRow> generateDiffRows(List<String> original, List<String> revised, Patch<String> patch) {
// normalize the lines (expand tabs, escape html entities)
original = StringUtills.normalize(original);
@@ -291,7 +290,6 @@ public class DiffRowGenerator {
* Add the inline diffs for given delta
* @param delta the given delta
*/
@SuppressWarnings("unchecked")
private void addInlineDiffs(Delta<String> delta) {
List<String> orig = (List<String>) delta.getOriginal().getLines();
List<String> rev = (List<String>) delta.getRevised().getLines();

View File

@@ -1,12 +1,6 @@
package diffutils;
import difflib.DiffUtils;
import difflib.Patch;
import difflib.PatchFailedException;
import junit.framework.TestCase;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
@@ -14,21 +8,36 @@ import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import junit.framework.TestCase;
import difflib.DiffUtils;
import difflib.Patch;
import difflib.PatchFailedException;
public class GenerateUnifiedDiffTest extends TestCase {
public List<String> fileToLines(String filename) {
List<String> lines = new LinkedList<String>();
String line = "";
BufferedReader in = null;
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
in = new BufferedReader(new FileReader(filename));
while ((line = in.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore ... any errors should already have been
// reported via an IOException from the final flush.
}
}
}
return lines;
}
@@ -48,14 +57,14 @@ public class GenerateUnifiedDiffTest extends TestCase {
public void testGenerateUnifiedDiffWithoutAnyDeltas() {
List<String> test = Arrays.asList("abc");
Patch patch = DiffUtils.diff(test, test);
Patch<String> patch = DiffUtils.diff(test, test);
DiffUtils.generateUnifiedDiff("abc", "abc", test, patch, 0);
}
public void testDiff_Issue10() {
final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
final Patch p = DiffUtils.parseUnifiedDiff(patchLines);
final Patch<String> p = DiffUtils.parseUnifiedDiff(patchLines);
try {
DiffUtils.patch(baseLines, p);
} catch (PatchFailedException e) {
@@ -96,20 +105,19 @@ public class GenerateUnifiedDiffTest extends TestCase {
revised.add("test line 4");
revised.add("test line 5");
Patch patch = DiffUtils.diff(original, revised);
Patch<String> patch = DiffUtils.diff(original, revised);
List<String> udiff = DiffUtils.generateUnifiedDiff("original", "revised",
original, patch, 10);
DiffUtils.parseUnifiedDiff(udiff);
}
@SuppressWarnings("unchecked")
private void verify(List<String> origLines, List<String> revLines,
String originalFile, String revisedFile) {
Patch patch = DiffUtils.diff(origLines, revLines);
Patch<String> patch = DiffUtils.diff(origLines, revLines);
List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(originalFile, revisedFile,
origLines, patch, 10);
Patch fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
Patch<String> fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
List<String> patchedLines;
try {
patchedLines = (List<String>) fromUnifiedPatch.applyTo(origLines);

View File

@@ -14,7 +14,7 @@ public class PatchTest extends TestCase {
final List<String> insertTest_from = Arrays.asList("hhh");
final List<String> insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
final Patch patch = DiffUtils.diff(insertTest_from, insertTest_to);
final Patch<String> patch = DiffUtils.diff(insertTest_from, insertTest_to);
try {
assertEquals(insertTest_to, DiffUtils.patch(insertTest_from, patch));
} catch (PatchFailedException e) {
@@ -26,7 +26,7 @@ public class PatchTest extends TestCase {
final List<String> deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh");
final List<String> deleteTest_to = Arrays.asList("ggg");
final Patch patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
final Patch<String> patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
try {
assertEquals(deleteTest_to, DiffUtils.patch(deleteTest_from, patch));
} catch (PatchFailedException e) {
@@ -38,7 +38,7 @@ public class PatchTest extends TestCase {
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
final Patch patch = DiffUtils.diff(changeTest_from, changeTest_to);
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
try {
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, patch));
} catch (PatchFailedException e) {

View File

@@ -17,10 +17,10 @@ public class ApplyPatch extends Example {
List<String> patched = fileToLines(PATCH);
// At first, parse the unified diff file and get the patch
Patch patch = DiffUtils.parseUnifiedDiff(patched);
Patch<String> patch = DiffUtils.parseUnifiedDiff(patched);
// Then apply the computed patch to the given text
List result = DiffUtils.patch(original, patch);
List<String> result = DiffUtils.patch(original, patch);
System.out.println(result);
// / Or we can call patch.applyTo(original). There is no difference.
}

View File

@@ -8,28 +8,40 @@ import java.util.LinkedList;
import java.util.List;
public abstract class Example {
/** File separator. */
protected static final String FS = File.separator;
/** The base resource path. */
protected static String BASE_PATH = "src" + FS + "test" + FS + "resources";
/**
* Tries to read the file and split it into a list of lines.
* @param filename The filename as path.
*
* @param filename
* The filename as path.
* @return A list of lines.
*/
public static List<String> fileToLines(String filename) {
List<String> lines = new LinkedList<String>();
String line = "";
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
while ((line = in.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
public static List<String> fileToLines(String filename) {
List<String> lines = new LinkedList<String>();
String line = "";
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(filename));
while ((line = in.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore ... any errors should already have been
// reported via an IOException from the final flush.
}
}
}
return lines;
}
}