### Eclipse Workspace Patch 1.0 #P org.eclipse.compare.tests Index: patchdata/patch_additionA.txt =================================================================== RCS file: patchdata/patch_additionA.txt diff -N patchdata/patch_additionA.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionA.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,13 @@ +--- addition.txt 2005-05-09 12:14:04.000000000 +0200 ++++ exp_addition.txt 2005-05-09 13:15:34.000000000 +0200 +@@ -0,0 +0,10 @@ ++[1] ++[2] ++[3] ++[4] ++[5] ++[6] ++[7] ++[8] ++[9] ++ Index: patchdata/patch_addition2A.txt =================================================================== RCS file: patchdata/patch_addition2A.txt diff -N patchdata/patch_addition2A.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_addition2A.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,12 @@ +--- addition.txt 2005-05-09 12:14:04.000000000 +0200 ++++ exp_addition.txt 2005-05-09 12:29:24.000000000 +0200 +@@ -0,0 +0,9 @@ ++[1] ++[2] ++[3] ++[4] ++[5] ++[6] ++[7] ++[8] ++[9] Index: patchdata/patchAddition3.txt =================================================================== RCS file: patchdata/patchAddition3.txt diff -N patchdata/patchAddition3.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patchAddition3.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,30 @@ +--- exp_context.txt 20 Dec 2008 02:59:19 -0000 ++++ exp_context.txt 20 Dec 2008 02:59:19 -0000 +@@ -1,6 +1,8 @@ + [b] + [c] ++[c1] ++[c2] + [d] + [e] + [f] +@@ -8,14 +10,16 @@ + [g] + [h] +-[i] ++[i1] + [j] + [k] + [l] +@@ -16,26 +18,28 @@ + [m] + [n] +-[o] + [p] + [q] + [r] + [s] ++[s1] + [t] + [u] + [v] Index: patchdata/testPatch.txt =================================================================== RCS file: patchdata/testPatch.txt diff -N patchdata/testPatch.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/testPatch.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,32 @@ +Index: patchdata/testPatch.txt +=================================================================== +--- patchdata/testPatch.txt 20 Dec 2008 03:06:21 -0000 ++++ patchdata/testPatch.txt 20 Dec 2008 03:06:21 -0000 +@@ -1,6 +1,8 @@ + [b] + [c] ++[c1] ++[c2] + [d] + [e] + [f] +@@ -8,14 +10,16 @@ + [g] + [h] +-[i] ++[i1] + [j] + [k] + [l] +@@ -16,26 +18,28 @@ + [m] + [n] +-[o] + [p] + [q] + [r] + [s] ++[s1] + [t] + [u] + [v] Index: patchdata/exp_additionA.txt =================================================================== RCS file: patchdata/exp_additionA.txt diff -N patchdata/exp_additionA.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/exp_additionA.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,9 @@ +[1] +[2] +[3] +[4] +[5] +[6] +[7] +[8] +[9] Index: src/org/eclipse/compare/tests/UnifiedDiffFormatterTest.java =================================================================== RCS file: src/org/eclipse/compare/tests/UnifiedDiffFormatterTest.java diff -N src/org/eclipse/compare/tests/UnifiedDiffFormatterTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/compare/tests/UnifiedDiffFormatterTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,201 @@ +/******************************************************************************* + * Copyright (c) 2000, 2008 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: + * Krzysztof Poglodzinski (intuicje@gmail.com) - initial API and implementation + * Mariusz Tanski (mariusztanski@gmail.com) - initial API and implementation + * Kacper Zdanowicz (kacper.zdanowicz@gmail.com) - initial API and implementation + *******************************************************************************/ +package org.eclipse.compare.tests; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; + +import org.eclipse.compare.CompareConfiguration; +import org.eclipse.compare.contentmergeviewer.ITokenComparator; +import org.eclipse.compare.contentmergeviewer.TokenComparator; +import org.eclipse.compare.internal.UnifiedDiffFormatter; +import org.eclipse.compare.internal.merge.DocumentMerger; +import org.eclipse.compare.internal.merge.DocumentMerger.IDocumentMergerInput; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; +import org.osgi.framework.Bundle; + +import junit.framework.TestCase; + +public class UnifiedDiffFormatterTest extends TestCase { + + private static final String PATCHDATA = "patchdata"; + private static final String TESTPATCHPATH = "patchdata/testPatch.txt"; + private static final String TESTPATCHFILE = "testPatch.txt"; + public static final char RIGHT_CONTRIBUTOR = 'R'; + public static final char LEFT_CONTRIBUTOR = 'L'; + + protected void setUp() throws Exception { + // empty + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testCreatePatch() throws CoreException, IOException { + patch("addition.txt", "exp_addition.txt", "patch_additionA.txt"); + } + + public void testUnterminatedCreatePatch() throws CoreException, IOException { + patch("addition.txt", "exp_addition2.txt", "patch_addition2A.txt"); + } + + public void testCreate2Patch()throws CoreException, IOException { + patch("context.txt", "exp_context.txt", "patchAddition3.txt"); + } + + private void patch(String fromFilePath, String toFilePath, String expectedPatch) throws CoreException, IOException{ + String fromFileContent = readFileToString(fromFilePath); + String toFileContent = readFileToString(toFilePath); + + final Document fromDoc = new Document(fromFileContent); + final Document toDoc = new Document(toFileContent); + + DocumentMerger merger = new DocumentMerger(getDocumentMergerInput(fromDoc, toDoc)); + + merger.doDiff(); + + UnifiedDiffFormatter formatter = new UnifiedDiffFormatter(merger, + fromDoc, toDoc, TESTPATCHPATH, false); + + File file = getFile(TESTPATCHPATH); + + formatter.generateDiff(file); + + String patchContent = readFileToString(TESTPATCHFILE); + String expectedContent = readFileToString(expectedPatch); + + String[] patchContents = patchContent.split("\n"); + String[] expectedContents = expectedContent.split("\n"); + patchContent = new String(); + expectedContent = new String(); + + for (int i = 4; i < patchContents.length; i++) { + patchContent += patchContents[i]; + } + for (int i = 2; i < expectedContents.length; i++) { + expectedContent += expectedContents[i]; + } + + + assertEquals(patchContent.length(), expectedContent.length()); + + assertEquals(patchContent, expectedContent); + + } + + private File getFile(String path) throws IOException { + File result = new File(path); + if(!result.exists()) { + result.createNewFile(); + } + return result; + } + + private IDocumentMergerInput getDocumentMergerInput(final Document fromDoc, final Document toDoc) { + return new IDocumentMergerInput() { + + public ITokenComparator createTokenComparator(String line) { + return new TokenComparator(line); + } + public CompareConfiguration getCompareConfiguration() { + return new CompareConfiguration(); + } + public IDocument getDocument(char contributor) { + switch (contributor) { + case LEFT_CONTRIBUTOR: + return fromDoc; + case RIGHT_CONTRIBUTOR: + return toDoc; + default: + return null; + } + } + public int getHunkStart() { + return 0; + } + public Position getRegion(char contributor) { + switch (contributor) { + case LEFT_CONTRIBUTOR: + return new Position(0, fromDoc.getLength()); + case RIGHT_CONTRIBUTOR: + return new Position(0, toDoc.getLength()); + } + return null; + } + public boolean isHunkOnLeft() { + return false; + } + public boolean isIgnoreAncestor() { + return true; + } + public boolean isPatchHunk() { + return false; + } + + public boolean isShowPseudoConflicts() { + return false; + } + public boolean isThreeWay() { + // return TextMergeViewer.this.isThreeWay(); + return false; + } + public boolean isPatchHunkOk() { + return false; + } + + }; + } + + public String readFileToString(String path) throws IOException { + BufferedReader reader = getReader(path); + StringBuffer str = new StringBuffer(); + int c; + + while (((c = reader.read()) != -1)){ + str.append((char)c); + } + reader.close(); + return str.toString(); + + } + + 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 Bundle getBundle() { + return CompareTestPlugin.getDefault().getBundle(); + } +}