View | Details | Raw Unified | Return to bug 266812 | Differences between
and this patch

Collapse All | Expand All

(-)patchdata/patch_addition.txt
Lines 1-5 Link Here
(-)patchdata/patch_context0.txt
Lines 1-5 Link Here
(-)src/org/eclipse/compare/tests/AllTests.java (+1 lines)
Lines 33-38 Link Here
33
		suite.addTestSuite(FileDiffResultTest.class);
33
		suite.addTestSuite(FileDiffResultTest.class);
34
		suite.addTestSuite(ContentMergeViewerTest.class);
34
		suite.addTestSuite(ContentMergeViewerTest.class);
35
		suite.addTestSuite(PatchLinesTest.class);
35
		suite.addTestSuite(PatchLinesTest.class);
36
		suite.addTestSuite(UITest.class);
36
		// $JUnit-END$
37
		// $JUnit-END$
37
		return suite;
38
		return suite;
38
	}
39
	}
(-)src/org/eclipse/compare/tests/UITest.java (-4 / +208 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 10-24 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.compare.tests;
11
package org.eclipse.compare.tests;
12
12
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.lang.reflect.InvocationTargetException;
16
13
import junit.framework.TestCase;
17
import junit.framework.TestCase;
14
18
19
import org.eclipse.compare.CompareConfiguration;
20
import org.eclipse.compare.internal.patch.PatchWizard;
21
import org.eclipse.compare.internal.patch.PatchWizardDialog;
22
import org.eclipse.core.internal.resources.WorkspaceRoot;
23
import org.eclipse.core.resources.IFile;
24
import org.eclipse.core.resources.IProject;
25
import org.eclipse.core.resources.IResource;
26
import org.eclipse.core.resources.IStorage;
27
import org.eclipse.core.resources.IWorkspaceRoot;
28
import org.eclipse.core.resources.ResourcesPlugin;
29
import org.eclipse.core.runtime.Assert;
30
import org.eclipse.core.runtime.CoreException;
31
import org.eclipse.jface.resource.ImageDescriptor;
32
import org.eclipse.jface.viewers.TreePath;
33
import org.eclipse.jface.viewers.TreeSelection;
34
import org.eclipse.jface.viewers.TreeViewer;
35
import org.eclipse.jface.wizard.IWizardPage;
36
import org.eclipse.swt.dnd.Clipboard;
37
import org.eclipse.swt.dnd.TextTransfer;
38
import org.eclipse.swt.dnd.Transfer;
39
import org.eclipse.swt.widgets.Button;
40
import org.eclipse.swt.widgets.Shell;
41
import org.eclipse.ui.internal.WorkbenchPlugin;
42
15
public class UITest extends TestCase {
43
public class UITest extends TestCase {
16
44
45
	private static final String TEST_PROJECT = "ApplyPatchTest";
46
47
	private IWorkspaceRoot workspaceRoot = null;
48
	private IProject testProject = null;
49
50
	private PatchWizardDialog wizardDialog = null;
51
	private PatchWizard wizard = null;
52
17
	public UITest(String name) {
53
	public UITest(String name) {
18
		super(name);
54
		super(name);
19
	}
55
	}
20
		
56
21
	public void testUI() {
57
	protected void setUp() throws Exception {
22
		// intentionally left empty
58
		super.setUp();
59
		workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
60
		testProject = workspaceRoot.getProject(TEST_PROJECT);
61
		testProject.create(null);
62
		testProject.open(null);
63
	}
64
65
	protected void tearDown() throws Exception {
66
		super.tearDown();
67
		testProject.delete(true, null);
68
	}
69
70
	public void testApplyClipboardPatch() throws CoreException {
71
		copyIntoClipboard("patch_context0.txt");
72
		copyIntoWorkspace("context.txt");
73
74
		openPatchWizard();
75
		Assert.isTrue(wizard.getPageCount() == 3);
76
		IWizardPage patchWizardPage = wizard.getPages()[0];
77
78
		Assert.isTrue(patchWizardPage.canFlipToNextPage());
79
80
		callMethod(wizardDialog, "nextPressed", new Object[] {});
81
82
		Assert.isTrue(wizard.canFinish());
83
		wizard.performFinish();
84
85
		InputStream expected = PatchUtils.asInputStream("exp_context.txt");
86
		InputStream actual = testProject.getFile("context.txt").getContents();
87
		compareStreams(expected, actual);
88
	}
89
90
	public void testApplyWorkspacePatch() throws CoreException {
91
		copyIntoWorkspace("patch_addition.txt");
92
93
		openPatchWizard();
94
		Assert.isTrue(wizard.getPageCount() == 3);
95
		IWizardPage patchWizardPage = wizard.getPages()[0];
96
97
		getButton(patchWizardPage, "fUseClipboardButton").setSelection(false);
98
		getButton(patchWizardPage, "fUsePatchFileButton").setSelection(false);
99
		getButton(patchWizardPage, "fUseWorkspaceButton").setSelection(true);
100
101
		TreeViewer tree = getTreeViewer(patchWizardPage, "fTreeViewer");
102
		treeSelect(tree, TEST_PROJECT + "/patch_addition.txt");
103
		Assert.isTrue(patchWizardPage.canFlipToNextPage());
104
105
		callMethod(wizardDialog, "nextPressed", new Object[] {});
106
107
		Assert.isTrue(wizard.canFinish());
108
		wizard.performFinish();
109
110
		InputStream expected = PatchUtils.asInputStream("exp_addition.txt");
111
		InputStream actual = testProject.getFile("exp_addition.txt")
112
				.getContents();
113
		compareStreams(expected, actual);
23
	}
114
	}
115
116
	private void openPatchWizard() {
117
		ImageDescriptor patchWizardImage = null;
118
		String patchWizardTitle = null;
119
120
		IStorage patch = null;
121
		IResource target = null;
122
		CompareConfiguration configuration = new CompareConfiguration();
123
124
		wizard = new PatchWizard(patch, target, configuration);
125
		if (patchWizardImage != null)
126
			wizard.setDefaultPageImageDescriptor(patchWizardImage);
127
		if (patchWizardTitle != null)
128
			wizard.setWindowTitle(patchWizardTitle);
129
		wizard.setNeedsProgressMonitor(true);
130
131
		wizardDialog = new PatchWizardDialog(getShell(), wizard);
132
		wizard.setDialog(wizardDialog);
133
		wizardDialog.setBlockOnOpen(false);
134
		wizardDialog.open();
135
	}
136
137
	private void copyIntoClipboard(String name) {
138
		Clipboard clipboard = new Clipboard(getShell().getDisplay());
139
		InputStream patchIS = PatchUtils.asInputStream(name);
140
		String patch = null;
141
		try {
142
			patch = PatchUtils.asString(patchIS);
143
		} catch (IOException e) {
144
			fail(e.getMessage());
145
		}
146
		TextTransfer textTransfer = TextTransfer.getInstance();
147
		Transfer[] transfers = new Transfer[] { textTransfer };
148
		Object[] data = new Object[] { patch };
149
		clipboard.setContents(data, transfers);
150
		clipboard.dispose();
151
	}
152
153
	private void copyIntoWorkspace(String name) {
154
		IFile file = testProject.getFile(name);
155
		InputStream is = PatchUtils.asInputStream(name);
156
		try {
157
			file.create(is, true, null);
158
		} catch (CoreException e) {
159
			fail(e.getMessage());
160
		}
161
	}
162
163
	private void compareStreams(InputStream expectedIS, InputStream actualIS) {
164
		String expected = null;
165
		String actual = null;
166
		try {
167
			expected = PatchUtils.asString(expectedIS);
168
			actual = PatchUtils.asString(actualIS);
169
		} catch (IOException e) {
170
			fail(e.getMessage());
171
		}
172
		assertEquals(expected, actual);
173
	}
174
175
	private void treeSelect(TreeViewer tree, String path) {
176
		WorkspaceRoot root = (WorkspaceRoot) tree.getInput();
177
		IFile file = root.getFile(path);
178
		TreePath treePath = new TreePath(new Object[] { file });
179
		TreeSelection sel = new TreeSelection(treePath);
180
		tree.setSelection(sel);
181
	}
182
183
	private Button getButton(Object object, String name) {
184
		return (Button) getField(object, name);
185
	}
186
187
	private TreeViewer getTreeViewer(Object object, String name) {
188
		return (TreeViewer) getField(object, name);
189
	}
190
191
	private Object getField(Object object, String name) {
192
		Object ret = null;
193
		try {
194
			ret = ReflectionUtils.getField(object, name);
195
		} catch (IllegalArgumentException e) {
196
			fail(e.getMessage());
197
		} catch (SecurityException e) {
198
			fail(e.getMessage());
199
		} catch (IllegalAccessException e) {
200
			fail(e.getMessage());
201
		} catch (NoSuchFieldException e) {
202
			fail(e.getMessage());
203
		}
204
		return ret;
205
	}
206
207
	private Object callMethod(Object object, String name, Object args[]) {
208
		Object ret = null;
209
		try {
210
			ret = ReflectionUtils.callMethod(object, name, args);
211
		} catch (IllegalArgumentException e) {
212
			fail(e.getMessage());
213
		} catch (IllegalAccessException e) {
214
			fail(e.getMessage());
215
		} catch (InvocationTargetException e) {
216
			fail(e.getMessage());
217
		} catch (NoSuchMethodException e) {
218
			fail(e.getMessage());
219
		}
220
		return ret;
221
	}
222
223
	private Shell getShell() {
224
		return WorkbenchPlugin.getDefault().getWorkbench()
225
				.getActiveWorkbenchWindow().getShell();
226
	}
227
24
}
228
}
(-)src/org/eclipse/compare/tests/ReflectionUtils.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.compare.tests;
12
13
import java.lang.reflect.Field;
14
import java.lang.reflect.InvocationTargetException;
15
import java.lang.reflect.Method;
16
17
public class ReflectionUtils {
18
19
	public static Object callMethod(Object object, String name, Object args[])
20
			throws IllegalArgumentException, IllegalAccessException,
21
			InvocationTargetException, NoSuchMethodException {
22
		Class types[] = new Class[args.length];
23
		for (int i = 0; i < args.length; i++) {
24
			types[i] = args[i].getClass();
25
		}
26
		Method method = null;
27
		Class clazz = object.getClass();
28
		NoSuchMethodException ex = null;
29
		while (method == null && clazz != null) {
30
			try {
31
				method = clazz.getDeclaredMethod(name, types);
32
			} catch (NoSuchMethodException e) {
33
				if (ex == null) {
34
					ex = e;
35
				}
36
				clazz = clazz.getSuperclass();
37
			}
38
		}
39
		if (method == null) {
40
			throw ex;
41
		}
42
		method.setAccessible(true);
43
		Object ret = method.invoke(object, args);
44
		return ret;
45
	}
46
47
	public static Object getField(Object object, String name)
48
			throws IllegalArgumentException, IllegalAccessException,
49
			SecurityException, NoSuchFieldException {
50
		Field field = object.getClass().getDeclaredField(name);
51
		field.setAccessible(true);
52
		Object ret = field.get(object);
53
		return ret;
54
	}
55
56
}

Return to bug 266812