diff options
author | Szymon Brandys | 2009-02-23 16:23:00 +0000 |
---|---|---|
committer | Szymon Brandys | 2009-02-23 16:23:00 +0000 |
commit | df0ee1529ea69c24c39e3216563655d5ff10f172 (patch) | |
tree | 81ba06ca591f97869429be3973c217c8ee2618b9 | |
parent | 2e5a99a647b8bf8d54d418cbc469e87292761de6 (diff) | |
download | eclipse.platform.team-df0ee1529ea69c24c39e3216563655d5ff10f172.tar.gz eclipse.platform.team-df0ee1529ea69c24c39e3216563655d5ff10f172.tar.xz eclipse.platform.team-df0ee1529ea69c24c39e3216563655d5ff10f172.zip |
Bug 183226 - [Apply Patch] API to instantiate and apply arbitrary IHunks
16 files changed, 890 insertions, 136 deletions
diff --git a/tests/org.eclipse.compare.tests/patchdata/context_full.txt b/tests/org.eclipse.compare.tests/patchdata/context_full.txt new file mode 100644 index 000000000..ff06fcb44 --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/context_full.txt @@ -0,0 +1,52 @@ +[a]
+[b]
+[c]
+[d]
+[e]
+[f]
+[g]
+[h]
+[i]
+[j]
+[k]
+[l]
+[m]
+[n]
+[o]
+[p]
+[q]
+[r]
+[s]
+[t]
+[u]
+[v]
+[w]
+[x]
+[y]
+[z]
+[A]
+[B]
+[C]
+[D]
+[E]
+[F]
+[G]
+[H]
+[I]
+[J]
+[K]
+[L]
+[M]
+[N]
+[O]
+[P]
+[Q]
+[R]
+[S]
+[T]
+[U]
+[V]
+[W]
+[X]
+[Y]
+[Z]
\ No newline at end of file diff --git a/tests/org.eclipse.compare.tests/patchdata/exp_addHunks.txt b/tests/org.eclipse.compare.tests/patchdata/exp_addHunks.txt new file mode 100644 index 000000000..3d09ef1ad --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/exp_addHunks.txt @@ -0,0 +1,59 @@ +[a1]
+[a2]
+[a]
+[b]
+[c]
+[d]
+[d1]
+[d2]
+[d3]
+[d4]
+[e]
+[f]
+[g]
+[h]
+[i]
+[j]
+[k]
+[l]
+[m]
+[n]
+[o]
+[p]
+[q]
+[r]
+[s]
+[t]
+[u]
+[v]
+[y]
+[z]
+[A]
+[B]
+[C]
+[D]
+[E]
+[F]
+[G]
+[H]
+[I]
+[J]
+[K]
+[L]
+[N]
+[N1]
+[N2]
+[O]
+[P]
+[Q]
+[R]
+[S]
+[T]
+[U]
+[V]
+[W]
+[W1]
+[W2]
+[W3]
+[Y]
+[Z]
\ No newline at end of file diff --git a/tests/org.eclipse.compare.tests/patchdata/exp_createFilePatch.txt b/tests/org.eclipse.compare.tests/patchdata/exp_createFilePatch.txt new file mode 100644 index 000000000..7d137a8e3 --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/exp_createFilePatch.txt @@ -0,0 +1,27 @@ +[a1]
+[a2]
+[a3]
+[a]
+[b]
+[c]
+[f]
+[g]
+[h]
+[h1]
+[i]
+[j]
+[j1]
+[j2]
+[k]
+[l]
+[m]
+[n]
+[o]
+[p]
+[q]
+[r]
+[s]
+[t]
+[u]
+[v]
+[w]
diff --git a/tests/org.eclipse.compare.tests/patchdata/exp_modifyHunks.txt b/tests/org.eclipse.compare.tests/patchdata/exp_modifyHunks.txt new file mode 100644 index 000000000..c3e64d74b --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/exp_modifyHunks.txt @@ -0,0 +1,28 @@ +[a]
+[b]
+[c]
+[d]
+[d1]
+[d2]
+[d3]
+[d4]
+[e]
+[f]
+[g]
+[h]
+[i1]
+[j]
+[k]
+[l]
+[m]
+[n]
+[o]
+[p]
+[q]
+[r]
+[s]
+[s1]
+[t]
+[u]
+[v]
+[w]
diff --git a/tests/org.eclipse.compare.tests/patchdata/exp_removeHunks.txt b/tests/org.eclipse.compare.tests/patchdata/exp_removeHunks.txt new file mode 100644 index 000000000..68fc86fcd --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/exp_removeHunks.txt @@ -0,0 +1,51 @@ +[a]
+[b]
+[c]
+[d]
+[e]
+[f]
+[g]
+[h]
+[i]
+[j]
+[k]
+[l]
+[m]
+[n]
+[o]
+[p]
+[q]
+[r]
+[s]
+[t]
+[u]
+[v]
+[y]
+[z]
+[A]
+[B]
+[C]
+[D]
+[E]
+[F]
+[I]
+[J]
+[J1]
+[K]
+[L]
+[M]
+[N]
+[O]
+[P]
+[Q]
+[R]
+[S]
+[T]
+[U]
+[V]
+[W]
+[W1]
+[W2]
+[W3]
+[Y]
+[Z]
\ No newline at end of file diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_addHunks.txt b/tests/org.eclipse.compare.tests/patchdata/patch_addHunks.txt new file mode 100644 index 000000000..4c75b3d6e --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/patch_addHunks.txt @@ -0,0 +1,34 @@ +### Eclipse Workspace Patch 1.0
+#P Bug202944
+Index: tmp/context_full.txt
+===================================================================
+RCS file: /TEST/Bug202944/tmp/context_full.txt,v
+retrieving revision 1.1
+diff -u -r1.1 context_full.txt
+--- tmp/context_full.txt 23 Feb 2009 09:39:56 -0000 1.1
++++ tmp/context_full.txt 23 Feb 2009 09:40:51 -0000
+@@ -1,3 +1,5 @@
++[a1]
++[a2]
+ [a]
+ [b]
+ [c]
+@@ -20,8 +22,6 @@
+ [t]
+ [u]
+ [v]
+-[w]
+-[x]
+ [y]
+ [z]
+ [A]
+@@ -47,6 +47,8 @@
+ [U]
+ [V]
+ [W]
+-[X]
++[W1]
++[W2]
++[W3]
+ [Y]
+ [Z]
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_createHunk0.txt b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk0.txt new file mode 100644 index 000000000..f1de92aaf --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk0.txt @@ -0,0 +1,19 @@ +### Eclipse Workspace Patch 1.0
+#P Bug202944
+Index: tmp/list.txt
+===================================================================
+RCS file: /TEST/Bug202944/tmp/list.txt,v
+retrieving revision 1.2
+diff -u -r1.2 list.txt
+--- tmp/list.txt 20 Feb 2009 11:02:27 -0000 1.2
++++ tmp/list.txt 20 Feb 2009 11:03:29 -0000
+@@ -1,6 +1,8 @@
++[a1]
++[a2]
++[a3]
+ [a]
+ [b]
+-[c]
+ [d]
+ [e]
+ [f]
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_createHunk1.txt b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk1.txt new file mode 100644 index 000000000..7b0fb2930 --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk1.txt @@ -0,0 +1,26 @@ +### Eclipse Workspace Patch 1.0
+#P Bug202944
+Index: tmp/list.txt
+===================================================================
+RCS file: /TEST/Bug202944/tmp/list.txt,v
+retrieving revision 1.2
+diff -u -r1.2 list.txt
+--- tmp/list.txt 20 Feb 2009 11:02:27 -0000 1.2
++++ tmp/list.txt 20 Feb 2009 11:32:47 -0000
+@@ -1,13 +1,14 @@
+ [a]
+ [b]
+-[c]
+ [d]
+-[e]
+ [f]
+ [g]
+ [h]
++[h1]
+ [i]
+ [j]
++[j1]
++[j2]
+ [k]
+ [l]
+ [m]
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_createHunk2.txt b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk2.txt new file mode 100644 index 000000000..2f64be799 --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk2.txt @@ -0,0 +1,12 @@ +### Eclipse Workspace Patch 1.0
+#P Bug202944
+Index: tmp/a.txt
+===================================================================
+RCS file: tmp/a.txt
+diff -N tmp/a.txt
+--- /dev/null 1 Jan 1970 00:00:00 -0000
++++ tmp/a.txt 1 Jan 1970 00:00:00 -0000
+@@ -0,0 +1,3 @@
++[aa]
++[bb]
++[cc]
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_createHunk3.txt b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk3.txt new file mode 100644 index 000000000..0bec1a205 --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/patch_createHunk3.txt @@ -0,0 +1,14 @@ +### Eclipse Workspace Patch 1.0
+#P Bug202944
+Index: tmp/a.txt
+===================================================================
+RCS file: /TEST/Bug202944/tmp/a.txt,v
+retrieving revision 1.1
+diff -u -r1.1 a.txt
+--- tmp/a.txt 20 Feb 2009 11:37:35 -0000 1.1
++++ tmp/a.txt 20 Feb 2009 11:38:40 -0000
+@@ -1,4 +0,0 @@
+-[aa]
+-[bb]
+-[cc]
+-[dd]
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_modifyHunks.txt b/tests/org.eclipse.compare.tests/patchdata/patch_modifyHunks.txt new file mode 100644 index 000000000..917b62b9b --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/patch_modifyHunks.txt @@ -0,0 +1,25 @@ +--- old.txt 2005-05-07 00:16:20.000000000 +0200
++++ new.txt 2005-05-07 00:16:32.000000000 +0200
+@@ -3,2 +3,4 @@
+ [c]
++[c1]
++[c2]
+ [d]
+@@ -8,3 +10,3 @@
+ [h]
+-[i]
++[i1]
+ [j]
+@@ -14,3 +16,2 @@
+ [n]
+-[o]
+ [p]
+@@ -19,2 +20,3 @@
+ [s]
++[s1]
+ [t]
+@@ -29,2 +31,3 @@
+ [yy]
++[xx]
+ [yy]
+
\ No newline at end of file diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_removeHunks.txt b/tests/org.eclipse.compare.tests/patchdata/patch_removeHunks.txt new file mode 100644 index 000000000..860e39ad7 --- /dev/null +++ b/tests/org.eclipse.compare.tests/patchdata/patch_removeHunks.txt @@ -0,0 +1,55 @@ +### Eclipse Workspace Patch 1.0
+#P Bug202944
+Index: tmp/context_full.txt
+===================================================================
+RCS file: /TEST/Bug202944/tmp/context_full.txt,v
+retrieving revision 1.1
+diff -u -r1.1 context_full.txt
+--- tmp/context_full.txt 23 Feb 2009 09:39:56 -0000 1.1
++++ tmp/context_full.txt 23 Feb 2009 10:03:51 -0000
+@@ -1,3 +1,5 @@
++[a1]
++[a2]
+ [a]
+ [b]
+ [c]
+@@ -11,6 +13,8 @@
+ [k]
+ [l]
+ [m]
++[m1]
++[m2]
+ [n]
+ [o]
+ [p]
+@@ -20,8 +24,6 @@
+ [t]
+ [u]
+ [v]
+-[w]
+-[x]
+ [y]
+ [z]
+ [A]
+@@ -30,10 +32,9 @@
+ [D]
+ [E]
+ [F]
+-[G]
+-[H]
+ [I]
+ [J]
++[J1]
+ [K]
+ [L]
+ [M]
+@@ -47,6 +48,8 @@
+ [U]
+ [V]
+ [W]
+-[X]
++[W1]
++[W2]
++[W3]
+ [Y]
+ [Z]
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 b7c52c859..e3e11d56b 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 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 @@ -28,6 +28,7 @@ public class AllTests { suite.addTestSuite(DocLineComparatorTest.class); suite.addTestSuite(FilterTest.class); suite.addTestSuite(PatchTest.class); + suite.addTestSuite(PatchBuilderTest.class); suite.addTestSuite(AsyncExecTests.class); suite.addTestSuite(DiffTest.class); suite.addTestSuite(FileDiffResultTest.class); diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java new file mode 100644 index 000000000..177f82030 --- /dev/null +++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java @@ -0,0 +1,296 @@ +/******************************************************************************* + * 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.IOException; +import java.io.InputStream; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.compare.internal.core.patch.FileDiff; +import org.eclipse.compare.internal.core.patch.Hunk; +import org.eclipse.compare.internal.core.patch.LineReader; +import org.eclipse.compare.internal.patch.FileDiffWrapper; +import org.eclipse.compare.patch.ApplyPatchOperation; +import org.eclipse.compare.patch.IFilePatch; +import org.eclipse.compare.patch.IFilePatch2; +import org.eclipse.compare.patch.IFilePatchResult; +import org.eclipse.compare.patch.IHunk; +import org.eclipse.compare.patch.PatchBuilder; +import org.eclipse.compare.patch.PatchConfiguration; +import org.eclipse.compare.tests.PatchUtils.StringStorage; +import org.eclipse.core.resources.IStorage; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; + +public class PatchBuilderTest extends TestCase { + + public PatchBuilderTest() { + + } + + protected void setUp() throws Exception { + // Nothing to do + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testModifyHunks() throws CoreException, IOException { + IStorage patchStorage = new StringStorage("patch_modifyHunks.txt"); + IStorage contextStorage = new StringStorage("context.txt"); + IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); + assertEquals(1, patches.length); + IHunk[] hunksBefore = patches[0].getHunks(); + assertEquals(5, hunksBefore.length); + + String[] lines = new String[] { " [d]", "+[d1]", "+[d2]", "+[d3]", + "+[d4]", " [e]" }; + addLineDelimiters(lines); + IHunk[] toAdd = new IHunk[] { PatchBuilder.createHunk(3, lines) }; + IFilePatch2 filePatch = PatchBuilder.addHunks(patches[0], toAdd); + + IHunk[] toRemove = new IHunk[] { hunksBefore[0], hunksBefore[2] }; + filePatch = PatchBuilder.removeHunks(filePatch, toRemove); + + IHunk[] hunksAfter = filePatch.getHunks(); + assertEquals(4, hunksAfter.length); + assertEquals(3, ((Hunk) hunksAfter[0]).getStart(false)); + assertEquals(3, ((Hunk) hunksAfter[0]).getStart(true)); + assertEquals(7, ((Hunk) hunksAfter[1]).getStart(false)); + assertEquals(11, ((Hunk) hunksAfter[1]).getStart(true)); + assertEquals(18, ((Hunk) hunksAfter[2]).getStart(false)); + assertEquals(22, ((Hunk) hunksAfter[2]).getStart(true)); + assertEquals(28, ((Hunk) hunksAfter[3]).getStart(false)); + assertEquals(33, ((Hunk) hunksAfter[3]).getStart(true)); + + FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); + IFilePatchResult result = wrapper.apply(contextStorage, + new PatchConfiguration(), new NullProgressMonitor()); + + IHunk[] rejects = result.getRejects(); + assertEquals(1, rejects.length); + + InputStream actual = result.getPatchedContents(); + + LineReader lr = new LineReader(PatchUtils + .getReader("exp_modifyHunks.txt")); + List inLines = lr.readLines(); + String expected = LineReader.createString(false, inLines); + + assertEquals(expected, PatchUtils.asString(actual)); + } + + public void testAddHunks() throws CoreException, IOException { + IStorage patchStorage = new StringStorage("patch_addHunks.txt"); + IStorage contextStorage = new StringStorage("context_full.txt"); + IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); + assertEquals(1, patches.length); + IHunk[] hunksBefore = patches[0].getHunks(); + assertEquals(3, hunksBefore.length); + + String[] lines0 = new String[] { " [d]", "+[d1]", "+[d2]", "+[d3]", + "+[d4]", " [e]" }; + addLineDelimiters(lines0); + IHunk hunk0 = PatchBuilder.createHunk(3, lines0); + + String[] lines1 = new String[] { " [K]", " [L]", "-[M]", " [N]", + "+[N1]", "+[N2]", " [O]", " [P]" }; + addLineDelimiters(lines1); + IHunk hunk1 = PatchBuilder.createHunk(36, lines1); + + IHunk[] toAdd = new IHunk[] { hunk0, hunk1 }; + IFilePatch2 filePatch = PatchBuilder.addHunks(patches[0], toAdd); + + IHunk[] hunksAfter = filePatch.getHunks(); + assertEquals(5, hunksAfter.length); + assertEquals(0, ((Hunk) hunksAfter[0]).getStart(false)); + assertEquals(0, ((Hunk) hunksAfter[0]).getStart(true)); + assertEquals(3, ((Hunk) hunksAfter[1]).getStart(false)); + assertEquals(5, ((Hunk) hunksAfter[1]).getStart(true)); + assertEquals(19, ((Hunk) hunksAfter[2]).getStart(false)); + assertEquals(25, ((Hunk) hunksAfter[2]).getStart(true)); + assertEquals(36, ((Hunk) hunksAfter[3]).getStart(false)); + assertEquals(40, ((Hunk) hunksAfter[3]).getStart(true)); + assertEquals(46, ((Hunk) hunksAfter[4]).getStart(false)); + assertEquals(51, ((Hunk) hunksAfter[4]).getStart(true)); + + FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); + IFilePatchResult result = wrapper.apply(contextStorage, + new PatchConfiguration(), new NullProgressMonitor()); + + IHunk[] rejects = result.getRejects(); + assertEquals(0, rejects.length); + + InputStream actual = result.getPatchedContents(); + + LineReader lr = new LineReader(PatchUtils.getReader("exp_addHunks.txt")); + List inLines = lr.readLines(); + String expected = LineReader.createString(false, inLines); + + assertEquals(expected, PatchUtils.asString(actual)); + } + + public void testRemoveHunks() throws CoreException, IOException { + IStorage patchStorage = new StringStorage("patch_removeHunks.txt"); + IStorage contextStorage = new StringStorage("context_full.txt"); + IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); + assertEquals(1, patches.length); + IHunk[] hunksBefore = patches[0].getHunks(); + assertEquals(5, hunksBefore.length); + + IHunk[] toRemove = new IHunk[] { hunksBefore[0], hunksBefore[1] }; + IFilePatch2 filePatch = PatchBuilder.removeHunks(patches[0], toRemove); + + IHunk[] hunksAfter = filePatch.getHunks(); + assertEquals(3, hunksAfter.length); + assertEquals(19, ((Hunk) hunksAfter[0]).getStart(false)); + assertEquals(19, ((Hunk) hunksAfter[0]).getStart(true)); + assertEquals(29, ((Hunk) hunksAfter[1]).getStart(false)); + assertEquals(27, ((Hunk) hunksAfter[1]).getStart(true)); + assertEquals(46, ((Hunk) hunksAfter[2]).getStart(false)); + assertEquals(43, ((Hunk) hunksAfter[2]).getStart(true)); + + FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); + IFilePatchResult result = wrapper.apply(contextStorage, + new PatchConfiguration(), new NullProgressMonitor()); + + IHunk[] rejects = result.getRejects(); + assertEquals(0, rejects.length); + + InputStream actual = result.getPatchedContents(); + + LineReader lr = new LineReader(PatchUtils + .getReader("exp_removeHunks.txt")); + List inLines = lr.readLines(); + String expected = LineReader.createString(false, inLines); + + assertEquals(expected, PatchUtils.asString(actual)); + } + + public void testCreateFilePatch() throws CoreException, IOException { + IStorage contextStorage = new StringStorage("context.txt"); + + String[] lines0 = new String[] { "+[a1]", "+[a2]", "+[a3]", " [a]" }; + addLineDelimiters(lines0); + Hunk hunk0 = (Hunk) PatchBuilder.createHunk(0, lines0); + + String[] lines1 = new String[] { " [b]", " [c]", "-[d]", "-[e]", + " [f]", " [g]", " [h]", "+[h1]", " [i]", " [j]", "+[j1]", + "+[j2]", " [k]", " [l]" }; + addLineDelimiters(lines1); + Hunk hunk1 = (Hunk) PatchBuilder.createHunk(1, lines1); + + IHunk[] hunks = new IHunk[] { hunk1, hunk0 }; + + IFilePatch2 filePatch = PatchBuilder.createFilePatch(new Path(""), + IFilePatch2.DATE_UNKNOWN, new Path(""), + IFilePatch2.DATE_UNKNOWN, hunks); + + assertEquals(2, filePatch.getHunks().length); + assertEquals(hunk0, filePatch.getHunks()[0]); + assertEquals(hunk1, filePatch.getHunks()[1]); + + FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); + IFilePatchResult result = wrapper.apply(contextStorage, + new PatchConfiguration(), new NullProgressMonitor()); + + InputStream actual = result.getPatchedContents(); + + LineReader lr = new LineReader(PatchUtils + .getReader("exp_createFilePatch.txt")); + List inLines = lr.readLines(); + String expected = LineReader.createString(false, inLines); + + assertEquals(expected, PatchUtils.asString(actual)); + } + + public void testCreateHunk0() throws CoreException { + IStorage patch = new StringStorage("patch_createHunk0.txt"); + IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); + assertEquals(1, filePatches.length); + assertEquals(1, filePatches[0].getHunks().length); + + String[] lines = new String[] { "+[a1]", "+[a2]", "+[a3]", " [a]", + " [b]", "-[c]", " [d]", " [e]", " [f]" }; + addLineDelimiters(lines); + Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + + assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); + } + + public void testCreateHunk1() throws CoreException { + IStorage patch = new StringStorage("patch_createHunk1.txt"); + IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); + assertEquals(1, filePatches.length); + assertEquals(1, filePatches[0].getHunks().length); + + String[] lines = new String[] { " [a]", " [b]", "-[c]", " [d]", "-[e]", + " [f]", " [g]", " [h]", "+[h1]", " [i]", " [j]", "+[j1]", + "+[j2]", " [k]", " [l]", " [m]" }; + addLineDelimiters(lines); + Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + + assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); + } + + public void testCreateHunk2() throws CoreException { + IStorage patch = new StringStorage("patch_createHunk2.txt"); + IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); + assertEquals(1, filePatches.length); + assertEquals(1, filePatches[0].getHunks().length); + + String[] lines = new String[] { "+[aa]", "+[bb]", "+[cc]" }; + addLineDelimiters(lines); + Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + + assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); + } + + public void testCreateHunk3() throws CoreException { + IStorage patch = new StringStorage("patch_createHunk3.txt"); + IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); + assertEquals(1, filePatches.length); + assertEquals(1, filePatches[0].getHunks().length); + + String[] lines = new String[] { "-[aa]", "-[bb]", "-[cc]", "-[dd]" }; + addLineDelimiters(lines); + Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + + assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); + } + + private void assertHunkEquals(Hunk h1, Hunk h2) { + String[] l1 = h1.getLines(); + String[] l2 = h2.getLines(); + assertEquals(l1.length, l2.length); + for (int i = 0; i < l1.length; i++) { + assertFalse(l1[i] == null && l2[i] != null); + assertEquals(l1[i], (l2[i])); + } + assertEquals(h1.getStart(false), h2.getStart(false)); + assertEquals(h1.getStart(true), h2.getStart(true)); + assertEquals(h1.getLength(false), h2.getLength(false)); + assertEquals(h1.getLength(true), h2.getLength(true)); + assertEquals(h1.getHunkType(false), h2.getHunkType(false)); + assertEquals(h1.getHunkType(true), h2.getHunkType(true)); + } + + private void addLineDelimiters(String[] lines) { + for (int i = 0; i < lines.length; i++) { + lines[i] = lines[i] + "\r\n"; + } + } + +} diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchTest.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchTest.java index 8791ef8f6..db17b2189 100644 --- a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchTest.java +++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchTest.java @@ -10,13 +10,11 @@ *******************************************************************************/ package org.eclipse.compare.tests; -import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -38,7 +36,6 @@ import junit.framework.Assert; import junit.framework.AssertionFailedError; import junit.framework.TestCase; -import org.eclipse.compare.internal.Utilities; import org.eclipse.compare.internal.core.patch.FileDiff; import org.eclipse.compare.internal.core.patch.FileDiffResult; import org.eclipse.compare.internal.core.patch.LineReader; @@ -49,8 +46,10 @@ import org.eclipse.compare.patch.IFilePatchResult; import org.eclipse.compare.patch.IHunk; import org.eclipse.compare.patch.IHunkFilter; import org.eclipse.compare.patch.PatchConfiguration; +import org.eclipse.compare.tests.PatchUtils.JarEntryStorage; +import org.eclipse.compare.tests.PatchUtils.PatchTestConfiguration; +import org.eclipse.compare.tests.PatchUtils.StringStorage; import org.eclipse.core.resources.IStorage; -import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; @@ -59,103 +58,13 @@ import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; -import org.osgi.framework.Bundle; public class PatchTest extends TestCase { - private static final String PATCHDATA = "patchdata"; private static final String PATCH_CONFIGURATION = "patchConfiguration.properties"; Properties defaultPatchProperties; - class StringStorage implements IStorage { - String fileName; - public StringStorage(String old) { - fileName = old; - } - public Object getAdapter(Class adapter) { - return null; - } - public boolean isReadOnly() { - return false; - } - public String getName() { - return fileName; - } - public IPath getFullPath() { - return null; - } - public InputStream getContents() throws CoreException { - return new BufferedInputStream(asInputStream(fileName)); - } - } - - class FileStorage implements IStorage { - File file; - public FileStorage(File file) { - this.file = file; - } - public InputStream getContents() throws CoreException { - try { - return new FileInputStream(file); - } catch (FileNotFoundException e) { - // ignore, should never happen - } - return null; - } - public IPath getFullPath() { - return new Path(file.getAbsolutePath()); - } - public String getName() { - return file.getName(); - } - public boolean isReadOnly() { - return true; - } - public Object getAdapter(Class adapter) { - return null; - } - } - - class JarEntryStorage implements IStorage { - JarEntry jarEntry; - JarFile jarFile; - public JarEntryStorage(JarEntry jarEntry, JarFile jarFile) { - this.jarEntry = jarEntry; - this.jarFile = jarFile; - } - public InputStream getContents() throws CoreException { - try { - return jarFile.getInputStream(jarEntry); - } catch (IOException e) { - // ignore, should never happen - } - return null; - } - public IPath getFullPath() { - return new Path(jarFile.getName()); - } - public String getName() { - return jarEntry.getName(); - } - public boolean isReadOnly() { - return true; - } - public Object getAdapter(Class adapter) { - return null; - } - } - - class PatchTestConfiguration { - String subfolderName; - PatchConfiguration pc; - String patchFileName; - String[] originalFileNames; - String[] expectedFileNames; - String[] actualFileNames; - // TODO: getters, setters - } - public PatchTest(String name) { super(name); defaultPatchProperties = new Properties(); @@ -217,11 +126,11 @@ public class PatchTest extends TestCase { InputStream actual = result.getPatchedContents(); - LineReader lr = new LineReader(getReader("exp_hunkFilter.txt")); + LineReader lr = new LineReader(PatchUtils.getReader("exp_hunkFilter.txt")); List inLines = lr.readLines(); String expected = LineReader.createString(false, inLines); - assertEquals(expected, asString(actual)); + assertEquals(expected, PatchUtils.asString(actual)); } public void testContext3PatchWithHeader() throws CoreException, IOException { @@ -298,7 +207,8 @@ public class PatchTest extends TestCase { private List failures = new ArrayList(); public void testPatchdataSubfolders() throws IOException, CoreException { - URL patchdataUrl = new URL(getBundle().getEntry("/"), new Path(PATCHDATA).toString()); + URL patchdataUrl = new URL(PatchUtils.getBundle().getEntry("/"), + new Path(PatchUtils.PATCHDATA).toString()); patchdataUrl = FileLocator.resolve(patchdataUrl); Map map = null; @@ -321,7 +231,8 @@ public class PatchTest extends TestCase { PatchConfiguration pc = ptc.pc; // create a message to distinguish tests from different subfolders - String msg = "Test for subfolder [" + PATCHDATA + "/" + sf + "] failed."; + String msg = "Test for subfolder [" + PatchUtils.PATCHDATA + "/" + + sf + "] failed."; try { // test with expected result @@ -339,8 +250,8 @@ public class PatchTest extends TestCase { continue; // continue with a next subfolder } failures.add(new AssertionFailedError( - "\npatchWorkspace should fail for folder [" + PATCHDATA - + "/" + sf + "].")); + "\npatchWorkspace should fail for folder [" + + PatchUtils.PATCHDATA + "/" + sf + "].")); } } @@ -391,7 +302,7 @@ public class PatchTest extends TestCase { while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); String entryName = entry.getName(); - if (entryName.endsWith("/" + PATCHDATA + "/")) { + if (entryName.endsWith("/" + PatchUtils.PATCHDATA + "/")) { patchdataName = entryName; break; } @@ -512,32 +423,13 @@ public class PatchTest extends TestCase { result.put(subfolderName, tpc); } - - // Test changing - private BufferedReader getReader(String name) { - InputStream resourceAsStream = asInputStream(name); - InputStreamReader reader2= new InputStreamReader(resourceAsStream); - return new BufferedReader(reader2); - } - - private InputStream asInputStream(String name) { - IPath path= new Path(PATCHDATA).append(name); - try { - URL url= new URL(getBundle().getEntry("/"), path.toString()); - return url.openStream(); - } catch (IOException e) { - fail("Failed while reading " + name); - return null; // never reached - } - } - private void patch(final String old, String patch, String expt) throws CoreException, IOException { patcherPatch(old, patch, expt); filePatch(old, patch, expt); } private void filePatch(final String old, String patch, String expt) throws CoreException, IOException { - LineReader lr= new LineReader(getReader(expt)); + LineReader lr= new LineReader(PatchUtils.getReader(expt)); List inLines= lr.readLines(); String expected = LineReader.createString(false, inLines); @@ -549,21 +441,17 @@ public class PatchTest extends TestCase { assertTrue(result.hasMatches()); assertFalse(result.hasRejects()); InputStream actualStream = result.getPatchedContents(); - String actual = asString(actualStream); + String actual = PatchUtils.asString(actualStream); assertEquals(expected, actual); } - private String asString(InputStream exptStream) throws IOException { - return Utilities.readString(exptStream, ResourcesPlugin.getEncoding()); - } - private void patcherPatch(String old, String patch, String expt) { - LineReader lr= new LineReader(getReader(old)); + LineReader lr= new LineReader(PatchUtils.getReader(old)); List inLines= lr.readLines(); WorkspacePatcher patcher= new WorkspacePatcher(); try { - patcher.parse(getReader(patch)); + patcher.parse(PatchUtils.getReader(patch)); } catch (IOException e) { e.printStackTrace(); } @@ -574,7 +462,7 @@ public class PatchTest extends TestCase { FileDiffResult diffResult = patcher.getDiffResult(diffs[0]); diffResult.patch(inLines, null); - LineReader expectedContents= new LineReader(getReader(expt)); + LineReader expectedContents= new LineReader(PatchUtils.getReader(expt)); List expectedLines= expectedContents.readLines(); Object[] expected= expectedLines.toArray(); @@ -618,7 +506,7 @@ public class PatchTest extends TestCase { patcher.getConfiguration().setFuzz(patchConfiguration.getFuzz()); patcher.getConfiguration().setIgnoreWhitespace(patchConfiguration.isIgnoreWhitespace()); patcher.getConfiguration().setPrefixSegmentStripCount(patchConfiguration.getPrefixSegmentStripCount()); - patcher.parse(getReader(patch)); + patcher.parse(PatchUtils.getReader(patch)); patcher.setReversed(patchConfiguration.isReversed()); } catch (IOException e) { e.printStackTrace(); @@ -630,13 +518,13 @@ public class PatchTest extends TestCase { //Iterate through all of the original files, apply the diffs that belong to the file and compare //with the corresponding outcome file for (int i = 0; i < originalFiles.length; i++) { - LineReader lr= new LineReader(getReader(originalFiles[i])); + LineReader lr= new LineReader(PatchUtils.getReader(originalFiles[i])); List inLines= lr.readLines(); FileDiffResult diffResult = patcher.getDiffResult(diffs[i]); diffResult.patch(inLines, null); - LineReader expectedContents= new LineReader(getReader(expectedOutcomeFiles[i])); + LineReader expectedContents= new LineReader(PatchUtils.getReader(expectedOutcomeFiles[i])); List expectedLines= expectedContents.readLines(); Object[] expected= expectedLines.toArray(); @@ -652,7 +540,4 @@ public class PatchTest extends TestCase { } } - private Bundle getBundle() { - return CompareTestPlugin.getDefault().getBundle(); - } } diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchUtils.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchUtils.java new file mode 100644 index 000000000..1dbd16c74 --- /dev/null +++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchUtils.java @@ -0,0 +1,170 @@ +/******************************************************************************* + * 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.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import junit.framework.Assert; + +import org.eclipse.compare.internal.Utilities; +import org.eclipse.compare.patch.PatchConfiguration; +import org.eclipse.core.resources.IStorage; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.osgi.framework.Bundle; + +public class PatchUtils { + + public static final String PATCHDATA = "patchdata"; + + public static class PatchTestConfiguration { + String subfolderName; + PatchConfiguration pc; + String patchFileName; + String[] originalFileNames; + String[] expectedFileNames; + String[] actualFileNames; + } + + public static class StringStorage implements IStorage { + String fileName; + + public StringStorage(String old) { + fileName = old; + } + + public Object getAdapter(Class adapter) { + return null; + } + + public boolean isReadOnly() { + return false; + } + + public String getName() { + return fileName; + } + + public IPath getFullPath() { + return null; + } + + public InputStream getContents() throws CoreException { + return new BufferedInputStream(asInputStream(fileName)); + } + } + + public static class FileStorage implements IStorage { + File file; + + public FileStorage(File file) { + this.file = file; + } + + public InputStream getContents() throws CoreException { + try { + return new FileInputStream(file); + } catch (FileNotFoundException e) { + // ignore, should never happen + } + return null; + } + + public IPath getFullPath() { + return new Path(file.getAbsolutePath()); + } + + public String getName() { + return file.getName(); + } + + public boolean isReadOnly() { + return true; + } + + public Object getAdapter(Class adapter) { + return null; + } + } + + public static class JarEntryStorage implements IStorage { + JarEntry jarEntry; + JarFile jarFile; + + public JarEntryStorage(JarEntry jarEntry, JarFile jarFile) { + this.jarEntry = jarEntry; + this.jarFile = jarFile; + } + + public InputStream getContents() throws CoreException { + try { + return jarFile.getInputStream(jarEntry); + } catch (IOException e) { + // ignore, should never happen + } + return null; + } + + public IPath getFullPath() { + return new Path(jarFile.getName()); + } + + public String getName() { + return jarEntry.getName(); + } + + public boolean isReadOnly() { + return true; + } + + public Object getAdapter(Class adapter) { + return null; + } + } + + public static String asString(InputStream exptStream) throws IOException { + return Utilities.readString(exptStream, ResourcesPlugin.getEncoding()); + } + + public static InputStream asInputStream(String name) { + IPath path = new Path(PATCHDATA).append(name); + try { + URL url = new URL(getBundle().getEntry("/"), path.toString()); + return url.openStream(); + } catch (IOException e) { + Assert.fail("Failed while reading " + name); + return null; // never reached + } + } + + public static BufferedReader getReader(String name) { + InputStream resourceAsStream = PatchUtils.asInputStream(name); + InputStreamReader reader2 = new InputStreamReader(resourceAsStream); + return new BufferedReader(reader2); + } + + public static Bundle getBundle() { + return CompareTestPlugin.getDefault().getBundle(); + } + +} |