### Eclipse Workspace Patch 1.0 #P org.eclipse.compare.tests Index: src/org/eclipse/compare/tests/PatchTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchTest.java,v retrieving revision 1.22 diff -u -r1.22 PatchTest.java --- src/org/eclipse/compare/tests/PatchTest.java 11 Dec 2008 13:39:35 -0000 1.22 +++ src/org/eclipse/compare/tests/PatchTest.java 13 Jan 2009 20:01:26 -0000 @@ -10,7 +10,6 @@ *******************************************************************************/ package org.eclipse.compare.tests; -import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; @@ -36,19 +35,15 @@ 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; import org.eclipse.compare.internal.patch.WorkspacePatcher; import org.eclipse.compare.patch.ApplyPatchOperation; import org.eclipse.compare.patch.IFilePatch; -import org.eclipse.compare.patch.IFilePatchResult; 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.FileLocator; import org.eclipse.core.runtime.IPath; @@ -56,37 +51,14 @@ 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 { +public class PatchTest extends AbstractPatchTest { + 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) { @@ -161,7 +133,11 @@ defaultPatchProperties.setProperty("expectedResultFile", "exp_context.txt"); defaultPatchProperties.setProperty("fuzzFactor", "-1"); } - + + protected String getWorkingFolder() { + return PATCHDATA; + } + protected void setUp() throws Exception { // empty } @@ -479,79 +455,6 @@ } - // 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)); - List inLines= lr.readLines(); - String expected = LineReader.createString(false, inLines); - - IStorage oldStorage = new StringStorage(old); - IStorage patchStorage = new StringStorage(patch); - IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); - assertTrue(patches.length == 1); - IFilePatchResult result = patches[0].apply(oldStorage, new PatchConfiguration(), null); - assertTrue(result.hasMatches()); - assertFalse(result.hasRejects()); - InputStream actualStream = result.getPatchedContents(); - String actual = 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)); - List inLines= lr.readLines(); - - WorkspacePatcher patcher= new WorkspacePatcher(); - try { - patcher.parse(getReader(patch)); - } catch (IOException e) { - e.printStackTrace(); - } - - FileDiff[] diffs= patcher.getDiffs(); - Assert.assertEquals(diffs.length, 1); - - FileDiffResult diffResult = patcher.getDiffResult(diffs[0]); - diffResult.patch(inLines, null); - - LineReader expectedContents= new LineReader(getReader(expt)); - List expectedLines= expectedContents.readLines(); - - Object[] expected= expectedLines.toArray(); - Object[] result= inLines.toArray(); - - Assert.assertEquals(expected.length, result.length); - - for (int i= 0; i < expected.length; i++) - Assert.assertEquals(expected[i], result[i]); - } - private void patchWorkspace(String[] originalFiles, String patch, String[] expectedOutcomeFiles, boolean reverse, int fuzzFactor) { @@ -616,9 +519,5 @@ for (int j= 0; j < expected.length; j++) Assert.assertEquals(msg, expected[j], result[j]); } - } - - private Bundle getBundle() { - return CompareTestPlugin.getDefault().getBundle(); } } 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 13 Jan 2009 20:01:26 -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/unifiedDiffFormatter/exp_no_newline.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/exp_no_newline.txt diff -N patchdata/unifiedDiffFormatter/exp_no_newline.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/patch_additionC2.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionC2.txt diff -N patchdata/unifiedDiffFormatter/patch_additionC2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/patch_additionA2.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionA2.txt diff -N patchdata/unifiedDiffFormatter/patch_additionA2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/patch_additionD2.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionD2.txt diff -N patchdata/unifiedDiffFormatter/patch_additionD2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/patch_additionC1.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionC1.txt diff -N patchdata/unifiedDiffFormatter/patch_additionC1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/exp_additionA.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/exp_additionA.txt diff -N patchdata/unifiedDiffFormatter/exp_additionA.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/patch_additionB1.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionB1.txt diff -N patchdata/unifiedDiffFormatter/patch_additionB1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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: src/org/eclipse/compare/tests/AbstractPatchTest.java =================================================================== RCS file: src/org/eclipse/compare/tests/AbstractPatchTest.java diff -N src/org/eclipse/compare/tests/AbstractPatchTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/compare/tests/AbstractPatchTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,159 @@ +/******************************************************************************* + * 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 - implementation + *******************************************************************************/ +package org.eclipse.compare.tests; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.List; + +import junit.framework.Assert; +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; +import org.eclipse.compare.internal.patch.WorkspacePatcher; +import org.eclipse.compare.patch.ApplyPatchOperation; +import org.eclipse.compare.patch.IFilePatch; +import org.eclipse.compare.patch.IFilePatchResult; +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 abstract class AbstractPatchTest extends TestCase { + + public AbstractPatchTest() { + super(); + } + + public AbstractPatchTest(String name) { + super(name); + } + + 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)); + } + } + + protected abstract String getWorkingFolder(); + + protected BufferedReader getReader(String name) { + InputStream resourceAsStream = asInputStream(name); + InputStreamReader reader2 = new InputStreamReader(resourceAsStream); + return new BufferedReader(reader2); + } + + protected InputStream asInputStream(String name) { + IPath path = new Path(getWorkingFolder()).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 + } + } + + protected void patch(final String old, String patch, String expt) + throws CoreException, IOException { + patcherPatch(old, patch, expt); + filePatch(old, patch, expt); + } + + void filePatch(final String old, String patch, String expt) + throws CoreException, IOException { + LineReader lr = new LineReader(getReader(expt)); + List inLines = lr.readLines(); + String expected = LineReader.createString(false, inLines); + + IStorage oldStorage = new StringStorage(old); + IStorage patchStorage = new StringStorage(patch); + IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); + assertTrue(patches.length == 1); + IFilePatchResult result = patches[0].apply(oldStorage, + new PatchConfiguration(), null); + assertTrue(result.hasMatches()); + assertFalse(result.hasRejects()); + InputStream actualStream = result.getPatchedContents(); + String actual = asString(actualStream); + assertEquals(expected, actual); + } + + protected String asString(InputStream exptStream) throws IOException { + return Utilities.readString(exptStream, ResourcesPlugin.getEncoding()); + } + + void patcherPatch(String old, String patch, String expt) { + LineReader lr = new LineReader(getReader(old)); + List inLines = lr.readLines(); + + WorkspacePatcher patcher = new WorkspacePatcher(); + try { + patcher.parse(getReader(patch)); + } catch (IOException e) { + e.printStackTrace(); + } + + FileDiff[] diffs = patcher.getDiffs(); + Assert.assertEquals(diffs.length, 1); + + FileDiffResult diffResult = patcher.getDiffResult(diffs[0]); + diffResult.patch(inLines, null); + + LineReader expectedContents = new LineReader(getReader(expt)); + List expectedLines = expectedContents.readLines(); + + Object[] expected = expectedLines.toArray(); + Object[] result = inLines.toArray(); + + Assert.assertEquals(expected.length, result.length); + + for (int i = 0; i < expected.length; i++) + Assert.assertEquals(expected[i], result[i]); + } + + protected Bundle getBundle() { + return CompareTestPlugin.getDefault().getBundle(); + } + +} \ No newline at end of file Index: patchdata/unifiedDiffFormatter/patch_additionD1.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionD1.txt diff -N patchdata/unifiedDiffFormatter/patch_additionD1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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: patchdata/unifiedDiffFormatter/patchConfiguration.properties =================================================================== RCS file: patchdata/unifiedDiffFormatter/patchConfiguration.properties diff -N patchdata/unifiedDiffFormatter/patchConfiguration.properties --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/patchConfiguration.properties 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +#This directory contains data files for purpose of testing org.eclipse.compare.internal.UnifiedDiffFormatter class. +#The content should be skipped while running org.eclipse.compare.tests.PatchTest.testPatchdataSubfolders(). +skipTest=true Index: patchdata/unifiedDiffFormatter/patch_empty.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_empty.txt diff -N patchdata/unifiedDiffFormatter/patch_empty.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/no_newline.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/no_newline.txt diff -N patchdata/unifiedDiffFormatter/no_newline.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/exp_context.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/exp_context.txt diff -N patchdata/unifiedDiffFormatter/exp_context.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/exp_context.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,25 @@ +[a] +[b] +[c] +[c1] +[c2] +[d] +[e] +[f] +[g] +[h] +[i1] +[j] +[k] +[l] +[m] +[n] +[p] +[q] +[r] +[s] +[s1] +[t] +[u] +[v] +[w] Index: patchdata/unifiedDiffFormatter/patch_additionB2.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionB2.txt diff -N patchdata/unifiedDiffFormatter/patch_additionB2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/exp_addition.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/exp_addition.txt diff -N patchdata/unifiedDiffFormatter/exp_addition.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/exp_addition.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,9 @@ +[1] +[2] +[3] +[4] +[5] +[6] +[7] +[8] +[9] Index: patchdata/unifiedDiffFormatter/patch_no_newline.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_no_newline.txt diff -N patchdata/unifiedDiffFormatter/patch_no_newline.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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/unifiedDiffFormatter/context.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/context.txt diff -N patchdata/unifiedDiffFormatter/context.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/context.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,23 @@ +[a] +[b] +[c] +[d] +[e] +[f] +[g] +[h] +[i] +[j] +[k] +[l] +[m] +[n] +[o] +[p] +[q] +[r] +[s] +[t] +[u] +[v] +[w] Index: patchdata/unifiedDiffFormatter/exp_addition2.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/exp_addition2.txt diff -N patchdata/unifiedDiffFormatter/exp_addition2.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/exp_addition2.txt 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,9 @@ +[1] +[2] +[3] +[4] +[5] +[6] +[7] +[8] +[9] \ No newline at end of file Index: patchdata/unifiedDiffFormatter/patch_additionA1.txt =================================================================== RCS file: patchdata/unifiedDiffFormatter/patch_additionA1.txt diff -N patchdata/unifiedDiffFormatter/patch_additionA1.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patchdata/unifiedDiffFormatter/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: 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,211 @@ +/******************************************************************************* + * 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: + * 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 + * IBM Corporation - implementation + *******************************************************************************/ +package org.eclipse.compare.tests; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +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; + +public class UnifiedDiffFormatterTest extends AbstractPatchTest { + + public UnifiedDiffFormatterTest(String name) { + super(name); + } + + private static final String TESTPATCHFILE = "testPatch.txt"; + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + getTestPatchFile().delete(); + } + + protected String getWorkingFolder() { + return "patchdata/unifiedDiffFormatter"; + } + + 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 false; + } + public boolean isPatchHunkOk() { + return false; + } + } + + public void testLeftEmptyPatch() throws CoreException, IOException { + createPatch("addition.txt", "exp_addition.txt", "patch_additionA2.txt"); + patch("addition.txt", "exp_addition.txt"); + } + + public void testRightEmptyPatch() throws CoreException, IOException { + createPatch("exp_addition.txt", "addition.txt", "patch_additionB2.txt"); + patch("exp_addition.txt", "addition.txt"); + } + + public void testEmptyFilesPatch() throws CoreException, IOException { + createPatch("empty1.txt", "empty2.txt", "patch_empty.txt"); + patch("empty1.txt", "empty2.txt"); + } + + public void testUnterminatedCreatePatch() throws CoreException, IOException { + createPatch("addition.txt", "exp_addition2.txt", "patch_additionC2.txt"); + patch("addition.txt", "exp_addition2.txt"); + } + + public void testCreateExamplePatch()throws CoreException, IOException { + createPatch("context.txt", "exp_context.txt", "patch_additionD2.txt"); + patch("context.txt", "exp_context.txt"); + } + + public void testBothFilesWithoutEndingNewlinePatch()throws CoreException, IOException { + createPatch("no_newline.txt", "exp_no_newline.txt", "patch_no_newline.txt"); + patch("no_newline.txt", "exp_no_newline.txt"); + } + + private void patch(final String old, String expt) throws CoreException, IOException { + patch(old, TESTPATCHFILE, expt); + } + + private void createPatch(String fromFilePath, String toFilePath, String expectedPatch) throws CoreException, IOException{ + + final Document fromDoc = new Document(readFileToString(getTestFile(fromFilePath).getName())); + final Document toDoc = new Document(readFileToString(getTestFile(toFilePath).getName())); + + DocumentMerger merger = new DocumentMerger(new FormatterTestDocMerger(fromDoc, toDoc)); + // Compare Editor calculates diffs while building the UI + merger.doDiff(); + + UnifiedDiffFormatter formatter = new UnifiedDiffFormatter(merger, + fromDoc, toDoc, fromFilePath, toFilePath, false); + formatter.generateDiff(getTestPatchFile()); + + String patchContent = readFileToString(TESTPATCHFILE); + String expectedContent = readFileToString(expectedPatch); + + String[] patchContents = patchContent.split("\n"); + String[] expectedContents = expectedContent.split("\n"); + + patchContent = getContentFromLines(patchContents); + expectedContent = getContentFromLines(expectedContents); + assertEquals(patchContent, expectedContent); + } + + private File getTestPatchFile() throws IOException { + return getTestFile(TESTPATCHFILE); + } + + private File getTestFile(String _name) { + IPath path= new Path(getWorkingFolder()).append(_name); + File file = path.toFile(); + if(!file.exists()) + try { + file.createNewFile(); + return file; + } catch (IOException e) { + fail("Failed while creating: " + _name); + return null; + } + return file; + } + + private String readFileToString(String name) throws IOException { + InputStream resourceAsStream = asInputStream(name); + return asString(resourceAsStream); + } + + private String getContentFromLines(String[] patchContents) { + // TODO: test only part of the patch content + StringBuffer patchContent = new StringBuffer(); + 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.append(line_split[0]); + continue; + } else if (line.startsWith(UnifiedDiffFormatter.INDEX_MARKER) + || line.startsWith(UnifiedDiffFormatter.DELIMITER)) { + continue; + } + patchContent.append(line); + } + return patchContent.toString(); + } +}