### Eclipse Workspace Patch 1.0 #P org.eclipse.compare.tests Index: src/org/eclipse/compare/tests/AllTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java,v retrieving revision 1.15 diff -u -r1.15 AllTests.java --- src/org/eclipse/compare/tests/AllTests.java 3 Jun 2008 13:36:11 -0000 1.15 +++ src/org/eclipse/compare/tests/AllTests.java 8 Jan 2009 20:49:44 -0000 @@ -32,6 +32,7 @@ suite.addTestSuite(DiffTest.class); suite.addTestSuite(FileDiffResultTest.class); suite.addTestSuite(ContentMergeViewerTest.class); + suite.addTestSuite(UnifiedDiffFormatterTest.class); //$JUnit-END$ return suite; } Index: patchdata/patch_additionA1.txt =================================================================== RCS file: patchdata/patch_additionA1.txt diff -N patchdata/patch_additionA1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionA1.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_additionB2.txt =================================================================== RCS file: patchdata/patch_additionB2.txt diff -N patchdata/patch_additionB2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionB2.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,12 @@ +--- exp_addition.txt 2005-05-09 12:14:04.000000000 +0200 ++++ addition.txt 2005-05-09 13:15:34.000000000 +0200 +@@ -1,9 +0,0 @@ +-[1] +-[2] +-[3] +-[4] +-[5] +-[6] +-[7] +-[8] +-[9] Index: patchdata/no_newline.txt =================================================================== RCS file: patchdata/no_newline.txt diff -N patchdata/no_newline.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/no_newline.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,6 @@ +[a] +[b] +[c] +[d] +[e] +[f] \ No newline at end of file Index: patchdata/patch_empty.txt =================================================================== RCS file: patchdata/patch_empty.txt diff -N patchdata/patch_empty.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_empty.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +--- empty1.txt 24 Dec 2008 12:56:00 -0000 ++++ empty2.txt 24 Dec 2008 12:56:00 -0000 +@@ -0,0 +0,0 @@ Index: patchdata/patch_additionA2.txt =================================================================== RCS file: patchdata/patch_additionA2.txt diff -N patchdata/patch_additionA2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionA2.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 13:15:34.000000000 +0200 +@@ -0,0 +1,9 @@ ++[1] ++[2] ++[3] ++[4] ++[5] ++[6] ++[7] ++[8] ++[9] 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,15 @@ +Index: no_newline.txt +=================================================================== +--- no_newline.txt 2009-01-08 21:48:55 -0000 ++++ exp_no_newline.txt 2009-01-08 21:48:55 -0000 +@@ -1,6 +1,6 @@ + [a] +-[b] +-[c] +-[d] ++[b1] ++[c1] ++[d1] + [e] + [f] +\ No newline at end of file Index: patchdata/exp_no_newline.txt =================================================================== RCS file: patchdata/exp_no_newline.txt diff -N patchdata/exp_no_newline.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/exp_no_newline.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,6 @@ +[a] +[b1] +[c1] +[d1] +[e] +[f] \ No newline at end of file Index: patchdata/patch_no_newline.txt =================================================================== RCS file: patchdata/patch_no_newline.txt diff -N patchdata/patch_no_newline.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_no_newline.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,13 @@ +--- no_newline.txt 2005-05-09 12:14:04.000000000 +0200 ++++ exp_no_newline.txt 2005-05-09 12:14:04.000000000 +0200 +@@ -1,6 +1,6 @@ + [a] +-[b] +-[c] +-[d] ++[b1] ++[c1] ++[d1] + [e] + [f] +\ No newline at end of file Index: patchdata/patch_additionB1.txt =================================================================== RCS file: patchdata/patch_additionB1.txt diff -N patchdata/patch_additionB1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionB1.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,13 @@ +--- exp_addition.txt 2005-05-09 12:14:04.000000000 +0200 ++++ addition.txt 2005-05-09 13:15:34.000000000 +0200 +@@ -0,10 +0,0 @@ +-[1] +-[2] +-[3] +-[4] +-[5] +-[6] +-[7] +-[8] +-[9] +- Index: patchdata/patch_additionD2.txt =================================================================== RCS file: patchdata/patch_additionD2.txt diff -N patchdata/patch_additionD2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionD2.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,29 @@ +--- context.txt 20 Dec 2008 02:59:19 -0000 ++++ exp_context.txt 20 Dec 2008 02:59:19 -0000 +@@ -1,22 +1,24 @@ + [a] + [b] + [c] ++[c1] ++[c2] + [d] + [e] + [f] + [g] + [h] +-[i] ++[i1] + [j] + [k] + [l] + [m] + [n] +-[o] + [p] + [q] + [r] + [s] ++[s1] + [t] + [u] + [v] Index: patchdata/patch_additionC2.txt =================================================================== RCS file: patchdata/patch_additionC2.txt diff -N patchdata/patch_additionC2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionC2.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,13 @@ +--- addition.txt 2005-05-09 12:14:04.000000000 +0200 ++++ exp_addition2.txt 2005-05-09 12:29:24.000000000 +0200 +@@ -0,0 +1,9 @@ ++[1] ++[2] ++[3] ++[4] ++[5] ++[6] ++[7] ++[8] ++[9] +\ No newline at end of file Index: patchdata/patch_additionC1.txt =================================================================== RCS file: patchdata/patch_additionC1.txt diff -N patchdata/patch_additionC1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionC1.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,12 @@ +--- addition.txt 2005-05-09 12:14:04.000000000 +0200 ++++ exp_addition2.txt 2005-05-09 12:29:24.000000000 +0200 +@@ -0,0 +0,9 @@ ++[1] ++[2] ++[3] ++[4] ++[5] ++[6] ++[7] ++[8] ++[9] 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: patchdata/patch_additionD1.txt =================================================================== RCS file: patchdata/patch_additionD1.txt diff -N patchdata/patch_additionD1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/patch_additionD1.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,30 @@ +--- 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: 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,221 @@ +/******************************************************************************* + * 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.MergeViewerContentProvider; +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"; + + class FormatterTestDocMerger implements IDocumentMergerInput { + + private Document fromDoc; + private Document toDoc; + + public FormatterTestDocMerger(Document fromDoc, Document toDoc) { + this.fromDoc = fromDoc; + this.toDoc = toDoc; + } + + public ITokenComparator createTokenComparator(String line) { + return new TokenComparator(line); + } + public CompareConfiguration getCompareConfiguration() { + return new CompareConfiguration(); + } + public IDocument getDocument(char contributor) { + switch (contributor) { + case MergeViewerContentProvider.LEFT_CONTRIBUTOR: + return fromDoc; + case MergeViewerContentProvider.RIGHT_CONTRIBUTOR: + return toDoc; + default: + return null; + } + } + public int getHunkStart() { + return 0; + } + public Position getRegion(char contributor) { + switch (contributor) { + case MergeViewerContentProvider.LEFT_CONTRIBUTOR: + return new Position(0, fromDoc.getLength()); + case MergeViewerContentProvider.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; + } + + } + + protected void setUp() throws Exception { + // empty + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testLeftEmptyPatch() throws CoreException, IOException { + patch("addition.txt", "exp_addition.txt", "patch_additionA2.txt"); + } + + public void testRightEmptyPatch() throws CoreException, IOException { + patch("exp_addition.txt", "addition.txt", "patch_additionB2.txt"); + } + + public void testEmptyFilesPatch() throws CoreException, IOException { + patch("empty1.txt", "empty2.txt", "patch_empty.txt"); + } + + public void testUnterminatedCreatePatch() throws CoreException, IOException { + patch("addition.txt", "exp_addition2.txt", "patch_additionC2.txt"); + } + + public void testCreateExamplePatch()throws CoreException, IOException { + patch("context.txt", "exp_context.txt", "patch_additionD2.txt"); + } + + public void testBothFilesWithoutEndingNewlinePatch()throws CoreException, IOException { + patch("no_newline.txt", "exp_no_newline.txt", "patch_no_newline.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(new FormatterTestDocMerger(fromDoc, toDoc)); + merger.doDiff(); + + UnifiedDiffFormatter formatter = new UnifiedDiffFormatter(merger, + fromDoc, toDoc, fromFilePath, toFilePath, 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 = getContentFromLines(patchContents); + expectedContent = getContentFromLines(expectedContents); + System.out.println(patchContent); + System.out.println(expectedContent); +// 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; + } + + 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(); + } + + private String getContentFromLines(String [] patchContents){ + String patchContent = new String(); + for (int i=0;i< patchContents.length;i++){ + String line =patchContents[i]; + if (line.startsWith(UnifiedDiffFormatter.NEW_FILE_PREFIX)||line.startsWith(UnifiedDiffFormatter.OLD_FILE_PREFIX)){ + String[] line_split = line.split("\t"); + patchContent += line_split[0]; + } else if (line.startsWith(UnifiedDiffFormatter.NEW_LINE_PREFIX)||line.startsWith(UnifiedDiffFormatter.OLD_LINE_PREFIX)||line.startsWith(UnifiedDiffFormatter.RANGE_INFORMATION_PREFIX)){ + patchContent +=line; + } else if(!line.startsWith("Index: ") && !line.startsWith("==========")){ + patchContent +=line; + } + } + return patchContent; + } +}