Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Zarna2009-03-17 15:50:43 +0000
committerTomasz Zarna2009-03-17 15:50:43 +0000
commit7c3249df449182ebb6af893f2b2ded0c8b7a79ad (patch)
tree1eba6134c63611482a98e27b33013dcbcad4050e /tests/org.eclipse.compare.tests
parent402b8a1a46870a4fe800ff757f29de3db6df370e (diff)
downloadeclipse.platform.team-7c3249df449182ebb6af893f2b2ded0c8b7a79ad.tar.gz
eclipse.platform.team-7c3249df449182ebb6af893f2b2ded0c8b7a79ad.tar.xz
eclipse.platform.team-7c3249df449182ebb6af893f2b2ded0c8b7a79ad.zip
bug 228000: [Tests] Add tests for the "Count lines in the patch" feature
Diffstat (limited to 'tests/org.eclipse.compare.tests')
-rw-r--r--tests/org.eclipse.compare.tests/patchdata/autofuzz-f1/addition.txt9
-rw-r--r--tests/org.eclipse.compare.tests/patchdata/patch_addition_context.txt14
-rw-r--r--tests/org.eclipse.compare.tests/patchdata/patch_context3_context.txt51
-rw-r--r--tests/org.eclipse.compare.tests/patchdata/patch_oneline_context.txt7
-rw-r--r--tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java8
-rw-r--r--tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchLinesTest.java172
6 files changed, 257 insertions, 4 deletions
diff --git a/tests/org.eclipse.compare.tests/patchdata/autofuzz-f1/addition.txt b/tests/org.eclipse.compare.tests/patchdata/autofuzz-f1/addition.txt
new file mode 100644
index 000000000..e58329b43
--- /dev/null
+++ b/tests/org.eclipse.compare.tests/patchdata/autofuzz-f1/addition.txt
@@ -0,0 +1,9 @@
+[1]
+[2]
+[3]
+[4]
+[5]
+[6]
+[7]
+[8]
+[9] \ No newline at end of file
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_addition_context.txt b/tests/org.eclipse.compare.tests/patchdata/patch_addition_context.txt
new file mode 100644
index 000000000..3b6317351
--- /dev/null
+++ b/tests/org.eclipse.compare.tests/patchdata/patch_addition_context.txt
@@ -0,0 +1,14 @@
+*** /dev/null 1 Jan 1970 00:00:00 -0000
+--- addition.txt 1 Jan 1970 00:00:00 -0000
+***************
+*** 0 ****
+--- 1,9 ----
++ [1]
++ [2]
++ [3]
++ [4]
++ [5]
++ [6]
++ [7]
++ [8]
++ [9] \ No newline at end of file
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_context3_context.txt b/tests/org.eclipse.compare.tests/patchdata/patch_context3_context.txt
new file mode 100644
index 000000000..b9d747daf
--- /dev/null
+++ b/tests/org.eclipse.compare.tests/patchdata/patch_context3_context.txt
@@ -0,0 +1,51 @@
+*** old.txt 18 Apr 2008 15:48:57 -0000 1.1
+--- old.txt 18 Apr 2008 15:50:03 -0000
+***************
+*** 1,22 ****
+ [a]
+ [b]
+ [c]
+ [d]
+ [e]
+ [f]
+ [g]
+ [h]
+! [i]
+ [j]
+ [k]
+ [l]
+ [m]
+ [n]
+- [o]
+ [p]
+ [q]
+ [r]
+ [s]
+ [t]
+ [u]
+ [v]
+--- 1,24 ----
+ [a]
+ [b]
+ [c]
++ [c1]
++ [c2]
+ [d]
+ [e]
+ [f]
+ [g]
+ [h]
+! [i1]
+ [j]
+ [k]
+ [l]
+ [m]
+ [n]
+ [p]
+ [q]
+ [r]
+ [s]
++ [s1]
+ [t]
+ [u]
+ [v]
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_oneline_context.txt b/tests/org.eclipse.compare.tests/patchdata/patch_oneline_context.txt
new file mode 100644
index 000000000..6736515bd
--- /dev/null
+++ b/tests/org.eclipse.compare.tests/patchdata/patch_oneline_context.txt
@@ -0,0 +1,7 @@
+*** addition.txt 9 May 2005 11:25:30 -0000 1.1
+--- addition.txt 18 Apr 2008 15:35:17 -0000
+***************
+*** 0 ****
+--- 1 ----
++ one line
+\ No newline at end of file
diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java
index e3e11d56b..0c43c9e7a 100644
--- a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java
+++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java
@@ -13,15 +13,14 @@ package org.eclipse.compare.tests;
import junit.framework.Test;
import junit.framework.TestSuite;
-
/**
* Test some non-UI areas of the compare plugin.
*/
public class AllTests {
public static Test suite() {
- TestSuite suite= new TestSuite("Test for org.eclipse.compare.tests"); //$NON-NLS-1$
- //$JUnit-BEGIN$
+ TestSuite suite = new TestSuite("Test for org.eclipse.compare.tests"); //$NON-NLS-1$
+ // $JUnit-BEGIN$
suite.addTestSuite(TextMergeViewerTest.class);
suite.addTestSuite(LineReaderTest.class);
suite.addTestSuite(StreamMergerTest.class);
@@ -33,7 +32,8 @@ public class AllTests {
suite.addTestSuite(DiffTest.class);
suite.addTestSuite(FileDiffResultTest.class);
suite.addTestSuite(ContentMergeViewerTest.class);
- //$JUnit-END$
+ suite.addTestSuite(PatchLinesTest.class);
+ // $JUnit-END$
return suite;
}
}
diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchLinesTest.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchLinesTest.java
new file mode 100644
index 000000000..bd41c5864
--- /dev/null
+++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchLinesTest.java
@@ -0,0 +1,172 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.compare.tests;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.lang.reflect.Field;
+
+import junit.framework.TestCase;
+
+import org.eclipse.compare.internal.core.patch.FilePatch2;
+import org.eclipse.compare.internal.patch.WorkspacePatcher;
+import org.eclipse.compare.patch.IHunk;
+
+public class PatchLinesTest extends TestCase {
+
+ public PatchLinesTest(String name) {
+ super(name);
+ }
+
+ // unified diff format
+
+ public void test_196847() throws Exception {
+ int[] lines = parsePatch("196847/stuff_patch.txt");
+ assertEquals(6, lines[0]);
+ assertEquals(5, lines[1]);
+ }
+
+ public void test_deletion_autofuzz() throws Exception {
+ int[] lines = parsePatch("deletion_autofuzz/patch.txt");
+ assertEquals(0, lines[0]);
+ assertEquals(1, lines[1]);
+ }
+
+ public void test_patch_addition() throws Exception {
+ int[] lines = parsePatch("patch_addition.txt");
+ assertEquals(9, lines[0]);
+ assertEquals(0, lines[1]);
+ }
+
+ public void test_patch_context1() throws Exception {
+ int[] lines = parsePatch("patch_context1.txt");
+ assertEquals(4, lines[0]);
+ assertEquals(2, lines[1]);
+ }
+
+ public void test_patch_context3() throws Exception {
+ int[] lines = parsePatch("patch_context3.txt");
+ assertEquals(4, lines[0]);
+ assertEquals(2, lines[1]);
+ }
+
+ public void test_patch_workspacePatchAddition() throws Exception {
+ int[] lines = parsePatch("patch_workspacePatchAddition.txt");
+ assertEquals(31, lines[0]);
+ assertEquals(0, lines[1]);
+ }
+
+ public void test_patch_workspacePatchDelete() throws Exception {
+ int[] lines = parsePatch("patch_workspacePatchDelete.txt");
+ assertEquals(29, lines[0]);
+ assertEquals(46, lines[1]);
+ }
+
+ public void test_patch_workspacePatchMod() throws Exception {
+ int[] lines = parsePatch("patch_workspacePatchMod.txt");
+ assertEquals(33, lines[0]);
+ assertEquals(8, lines[1]);
+ }
+
+ // context diff format
+
+ public void test_patch_addition_context() throws Exception {
+ int[] lines = parsePatch("patch_addition_context.txt");
+ assertEquals(9, lines[0]);
+ assertEquals(0, lines[1]);
+ }
+
+ public void test_patch_oneline_context() throws Exception {
+ int[] lines = parsePatch("patch_oneline_context.txt");
+ assertEquals(1, lines[0]);
+ assertEquals(0, lines[1]);
+ }
+
+ public void test_patch_context3_context() throws Exception {
+ int[] lines = parsePatch("patch_context3_context.txt");
+ assertEquals(4, lines[0]);
+ assertEquals(2, lines[1]);
+ }
+
+ private int[] parsePatch(String patch) {
+ WorkspacePatcher patcher = new WorkspacePatcher();
+ try {
+ patcher.parse(getReader(patch));
+ patcher.countLines();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ int[] result = new int[] { 0, 0 };
+ int hunksLengthSum = 0;
+ int fileDiffSum = 0;
+
+ FilePatch2[] diffs = patcher.getDiffs();
+ for (int i = 0; i < diffs.length; i++) {
+ FilePatch2 fileDiff = diffs[i];
+ IHunk[] hunks = fileDiff.getHunks();
+ for (int j = 0; j < hunks.length; j++) {
+ IHunk hunk = hunks[j];
+ hunksLengthSum += getNewLength(hunk);
+ hunksLengthSum -= getOldLength(hunk);
+ }
+ result[0] += fileDiff.getAddedLines();
+ result[1] += fileDiff.getRemovedLines();
+ fileDiffSum += fileDiff.getAddedLines();
+ fileDiffSum -= fileDiff.getRemovedLines();
+ }
+
+ assertEquals(hunksLengthSum, fileDiffSum);
+ return result;
+ }
+
+ private BufferedReader getReader(String name) {
+ return PatchUtils.getReader(name);
+ }
+
+ private int getNewLength(IHunk hunk) {
+ Class cls = hunk.getClass();
+ try {
+ Field fld = cls.getDeclaredField("fNewLength");
+ fld.setAccessible(true);
+ return fld.getInt(hunk);
+ } catch (SecurityException e) {
+ fail(e.getMessage());
+ } catch (NoSuchFieldException e) {
+ fail(e.getMessage());
+ } catch (IllegalArgumentException e) {
+ fail(e.getMessage());
+ } catch (IllegalAccessException e) {
+ fail(e.getMessage());
+ }
+ fail();
+ return -1;
+ }
+
+ private int getOldLength(IHunk hunk) {
+ Class cls = hunk.getClass();
+ try {
+ Field fld = cls.getDeclaredField("fOldLength");
+ fld.setAccessible(true);
+ return fld.getInt(hunk);
+ } catch (SecurityException e) {
+ fail(e.getMessage());
+ } catch (NoSuchFieldException e) {
+ fail(e.getMessage());
+ } catch (IllegalArgumentException e) {
+ fail(e.getMessage());
+ } catch (IllegalAccessException e) {
+ fail(e.getMessage());
+ }
+ fail();
+ return -1;
+ }
+}

Back to the top