Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSzymon Brandys2009-04-09 09:27:55 +0000
committerSzymon Brandys2009-04-09 09:27:55 +0000
commitd0f97eb7fb9027fc6c73e6ef08b6039fa38a4299 (patch)
tree62740f84cdf5f4071b32a64f9b05b2d7dac26ce2 /tests/org.eclipse.compare.tests
parent3dc7bd6c8230cfdc15ce115ab7132869ac383ab5 (diff)
downloadeclipse.platform.team-d0f97eb7fb9027fc6c73e6ef08b6039fa38a4299.tar.gz
eclipse.platform.team-d0f97eb7fb9027fc6c73e6ef08b6039fa38a4299.tar.xz
eclipse.platform.team-d0f97eb7fb9027fc6c73e6ef08b6039fa38a4299.zip
Bug 266812 - [Tests] Add tests for creating/applying patches from UII20090414-0755
Diffstat (limited to 'tests/org.eclipse.compare.tests')
-rw-r--r--tests/org.eclipse.compare.tests/patchdata/patch_addition.txt10
-rw-r--r--tests/org.eclipse.compare.tests/patchdata/patch_context0.txt10
-rw-r--r--tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java1
-rw-r--r--tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchUITest.java238
-rw-r--r--tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/ReflectionUtils.java56
-rw-r--r--tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UITest.java24
6 files changed, 311 insertions, 28 deletions
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_addition.txt b/tests/org.eclipse.compare.tests/patchdata/patch_addition.txt
index 4ec0df92e..1d9ea8675 100644
--- a/tests/org.eclipse.compare.tests/patchdata/patch_addition.txt
+++ b/tests/org.eclipse.compare.tests/patchdata/patch_addition.txt
@@ -1,5 +1,11 @@
---- addition.txt 2005-05-09 12:14:04.000000000 +0200
-+++ exp_addition.txt 2005-05-09 13:15:34.000000000 +0200
+### Eclipse Workspace Patch 1.0
+#P ApplyPatchTest
+Index: exp_addition.txt
+===================================================================
+RCS file: exp_addition.txt
+diff -N exp_addition.txt
+--- /dev/null 1 Jan 1970 00:00:00 -0000
++++ exp_addition.txt 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,9 @@
+[1]
+[2]
diff --git a/tests/org.eclipse.compare.tests/patchdata/patch_context0.txt b/tests/org.eclipse.compare.tests/patchdata/patch_context0.txt
index 4fbbf3e33..1adb77b11 100644
--- a/tests/org.eclipse.compare.tests/patchdata/patch_context0.txt
+++ b/tests/org.eclipse.compare.tests/patchdata/patch_context0.txt
@@ -1,5 +1,11 @@
---- old.txt 2005-05-07 00:16:20.000000000 +0200
-+++ new.txt 2005-05-07 00:16:32.000000000 +0200
+### Eclipse Workspace Patch 1.0
+#P ApplyPatchTest
+Index: context.txt
+===================================================================
+RCS file: context.txt
+diff -N context.txt
+--- context.txt 2005-05-07 00:16:20.000000000 +0200
++++ context.txt 2005-05-07 00:16:32.000000000 +0200
@@ -3,0 +4,2 @@
+[c1]
+[c2]
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 0c43c9e7a..27799dbf5 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
@@ -33,6 +33,7 @@ public class AllTests {
suite.addTestSuite(FileDiffResultTest.class);
suite.addTestSuite(ContentMergeViewerTest.class);
suite.addTestSuite(PatchLinesTest.class);
+ suite.addTestSuite(PatchUITest.class);
// $JUnit-END$
return suite;
}
diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchUITest.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchUITest.java
new file mode 100644
index 000000000..18690aad0
--- /dev/null
+++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchUITest.java
@@ -0,0 +1,238 @@
+/*******************************************************************************
+ * 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
+ * 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.lang.reflect.InvocationTargetException;
+
+import junit.framework.TestCase;
+
+import org.eclipse.compare.CompareConfiguration;
+import org.eclipse.compare.internal.patch.PatchWizard;
+import org.eclipse.compare.internal.patch.PatchWizardDialog;
+import org.eclipse.core.internal.resources.WorkspaceRoot;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IStorage;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.TreePath;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.jface.wizard.IWizardPage;
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.internal.WorkbenchPlugin;
+
+public class PatchUITest extends TestCase {
+
+ private static final String TEST_PROJECT = "ApplyPatchTest";
+
+ private IWorkspaceRoot workspaceRoot = null;
+ private IProject testProject = null;
+
+ private PatchWizardDialog wizardDialog = null;
+ private PatchWizard wizard = null;
+
+ public PatchUITest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
+ testProject = workspaceRoot.getProject(TEST_PROJECT);
+ testProject.create(null);
+ testProject.open(null);
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ testProject.delete(true, null);
+ }
+
+ public void testApplyClipboardPatch() throws CoreException {
+ copyIntoClipboard("patch_context0.txt");
+ copyIntoWorkspace("context.txt");
+
+ openPatchWizard();
+ assertTrue(wizard.getPageCount() == 3);
+ IWizardPage patchWizardPage = wizard.getPages()[0];
+
+ assertTrue(patchWizardPage.canFlipToNextPage());
+
+ callMethod(wizardDialog, "nextPressed", new Object[] {});
+
+ processQueuedEvents();
+ assertTrue(wizard.canFinish());
+ wizard.performFinish();
+ wizardDialog.close();
+
+ InputStream expected = PatchUtils.asInputStream("exp_context.txt");
+ InputStream actual = testProject.getFile("context.txt").getContents();
+ compareStreams(expected, actual);
+ }
+
+ public void testApplyWorkspacePatch() throws CoreException {
+ copyIntoWorkspace("patch_addition.txt");
+
+ openPatchWizard();
+ assertTrue(wizard.getPageCount() == 3);
+ IWizardPage patchWizardPage = wizard.getPages()[0];
+
+ getButton(patchWizardPage, "fUseClipboardButton").setSelection(false);
+ getButton(patchWizardPage, "fUsePatchFileButton").setSelection(false);
+ getButton(patchWizardPage, "fUseWorkspaceButton").setSelection(true);
+
+ TreeViewer tree = getTreeViewer(patchWizardPage, "fTreeViewer");
+ treeSelect(tree, TEST_PROJECT + "/patch_addition.txt");
+
+ processQueuedEvents();
+ assertTrue(patchWizardPage.canFlipToNextPage());
+ callMethod(wizardDialog, "nextPressed", new Object[] {});
+
+ assertTrue(wizard.canFinish());
+ wizard.performFinish();
+ wizardDialog.close();
+
+ InputStream expected = PatchUtils.asInputStream("exp_addition.txt");
+ InputStream actual = testProject.getFile("exp_addition.txt")
+ .getContents();
+ compareStreams(expected, actual);
+ }
+
+ private void openPatchWizard() {
+ ImageDescriptor patchWizardImage = null;
+ String patchWizardTitle = null;
+
+ IStorage patch = null;
+ IResource target = null;
+ CompareConfiguration configuration = new CompareConfiguration();
+
+ wizard = new PatchWizard(patch, target, configuration);
+ if (patchWizardImage != null)
+ wizard.setDefaultPageImageDescriptor(patchWizardImage);
+ if (patchWizardTitle != null)
+ wizard.setWindowTitle(patchWizardTitle);
+ wizard.setNeedsProgressMonitor(true);
+
+ wizardDialog = new PatchWizardDialog(getShell(), wizard);
+ wizard.setDialog(wizardDialog);
+ wizardDialog.setBlockOnOpen(false);
+ wizardDialog.open();
+ }
+
+ private void copyIntoClipboard(String name) {
+ Clipboard clipboard = new Clipboard(getShell().getDisplay());
+ InputStream patchIS = PatchUtils.asInputStream(name);
+ String patch = null;
+ try {
+ patch = PatchUtils.asString(patchIS);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ TextTransfer textTransfer = TextTransfer.getInstance();
+ Transfer[] transfers = new Transfer[] { textTransfer };
+ Object[] data = new Object[] { patch };
+ clipboard.setContents(data, transfers);
+ clipboard.dispose();
+ }
+
+ private void copyIntoWorkspace(String name) {
+ IFile file = testProject.getFile(name);
+ InputStream is = PatchUtils.asInputStream(name);
+ try {
+ file.create(is, true, null);
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ private void compareStreams(InputStream expectedIS, InputStream actualIS) {
+ String expected = null;
+ String actual = null;
+ try {
+ expected = PatchUtils.asString(expectedIS);
+ actual = PatchUtils.asString(actualIS);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ assertEquals(expected, actual);
+ }
+
+ private void treeSelect(TreeViewer tree, String path) {
+ WorkspaceRoot root = (WorkspaceRoot) tree.getInput();
+ IFile file = root.getFile(path);
+ TreePath treePath = new TreePath(new Object[] { file });
+ TreeSelection sel = new TreeSelection(treePath);
+ tree.setSelection(sel);
+ }
+
+ private Button getButton(Object object, String name) {
+ return (Button) getField(object, name);
+ }
+
+ private TreeViewer getTreeViewer(Object object, String name) {
+ return (TreeViewer) getField(object, name);
+ }
+
+ private Object getField(Object object, String name) {
+ Object ret = null;
+ try {
+ ret = ReflectionUtils.getField(object, name);
+ } catch (IllegalArgumentException e) {
+ fail(e.getMessage());
+ } catch (SecurityException e) {
+ fail(e.getMessage());
+ } catch (IllegalAccessException e) {
+ fail(e.getMessage());
+ } catch (NoSuchFieldException e) {
+ fail(e.getMessage());
+ }
+ return ret;
+ }
+
+ private Object callMethod(Object object, String name, Object args[]) {
+ Object ret = null;
+ try {
+ ret = ReflectionUtils.callMethod(object, name, args);
+ } catch (IllegalArgumentException e) {
+ fail(e.getMessage());
+ } catch (IllegalAccessException e) {
+ fail(e.getMessage());
+ } catch (InvocationTargetException e) {
+ fail(e.getMessage());
+ } catch (NoSuchMethodException e) {
+ fail(e.getMessage());
+ }
+ return ret;
+ }
+
+ private Shell getShell() {
+ return WorkbenchPlugin.getDefault().getWorkbench()
+ .getActiveWorkbenchWindow().getShell();
+ }
+
+ private void processQueuedEvents() {
+ while (Display.getCurrent().readAndDispatch()) {
+ // Process all the events in the queue
+ }
+ }
+
+}
diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/ReflectionUtils.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/ReflectionUtils.java
new file mode 100644
index 000000000..538738a4f
--- /dev/null
+++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/ReflectionUtils.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * 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.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class ReflectionUtils {
+
+ public static Object callMethod(Object object, String name, Object args[])
+ throws IllegalArgumentException, IllegalAccessException,
+ InvocationTargetException, NoSuchMethodException {
+ Class types[] = new Class[args.length];
+ for (int i = 0; i < args.length; i++) {
+ types[i] = args[i].getClass();
+ }
+ Method method = null;
+ Class clazz = object.getClass();
+ NoSuchMethodException ex = null;
+ while (method == null && clazz != null) {
+ try {
+ method = clazz.getDeclaredMethod(name, types);
+ } catch (NoSuchMethodException e) {
+ if (ex == null) {
+ ex = e;
+ }
+ clazz = clazz.getSuperclass();
+ }
+ }
+ if (method == null) {
+ throw ex;
+ }
+ method.setAccessible(true);
+ Object ret = method.invoke(object, args);
+ return ret;
+ }
+
+ public static Object getField(Object object, String name)
+ throws IllegalArgumentException, IllegalAccessException,
+ SecurityException, NoSuchFieldException {
+ Field field = object.getClass().getDeclaredField(name);
+ field.setAccessible(true);
+ Object ret = field.get(object);
+ return ret;
+ }
+
+} \ No newline at end of file
diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UITest.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UITest.java
deleted file mode 100644
index c557f228b..000000000
--- a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UITest.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2006 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 junit.framework.TestCase;
-
-public class UITest extends TestCase {
-
- public UITest(String name) {
- super(name);
- }
-
- public void testUI() {
- // intentionally left empty
- }
-}

Back to the top