Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 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.io.IOException; |
14 |
import java.io.InputStream; |
15 |
import java.lang.reflect.InvocationTargetException; |
16 |
|
17 |
import junit.framework.TestCase; |
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.CoreException; |
30 |
import org.eclipse.jface.resource.ImageDescriptor; |
31 |
import org.eclipse.jface.viewers.TreePath; |
32 |
import org.eclipse.jface.viewers.TreeSelection; |
33 |
import org.eclipse.jface.viewers.TreeViewer; |
34 |
import org.eclipse.jface.wizard.IWizardPage; |
35 |
import org.eclipse.swt.dnd.Clipboard; |
36 |
import org.eclipse.swt.dnd.TextTransfer; |
37 |
import org.eclipse.swt.dnd.Transfer; |
38 |
import org.eclipse.swt.widgets.Button; |
39 |
import org.eclipse.swt.widgets.Display; |
40 |
import org.eclipse.swt.widgets.Shell; |
41 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
42 |
|
43 |
public class PatchUITest extends TestCase { |
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 |
|
53 |
public PatchUITest(String name) { |
54 |
super(name); |
55 |
} |
56 |
|
57 |
protected void setUp() throws Exception { |
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 |
assertTrue(wizard.getPageCount() == 3); |
76 |
IWizardPage patchWizardPage = wizard.getPages()[0]; |
77 |
|
78 |
assertTrue(patchWizardPage.canFlipToNextPage()); |
79 |
|
80 |
callMethod(wizardDialog, "nextPressed", new Object[] {}); |
81 |
|
82 |
processQueuedEvents(); |
83 |
assertTrue(wizard.canFinish()); |
84 |
wizard.performFinish(); |
85 |
wizardDialog.close(); |
86 |
|
87 |
InputStream expected = PatchUtils.asInputStream("exp_context.txt"); |
88 |
InputStream actual = testProject.getFile("context.txt").getContents(); |
89 |
compareStreams(expected, actual); |
90 |
} |
91 |
|
92 |
public void testApplyWorkspacePatch() throws CoreException { |
93 |
copyIntoWorkspace("patch_addition.txt"); |
94 |
|
95 |
openPatchWizard(); |
96 |
assertTrue(wizard.getPageCount() == 3); |
97 |
IWizardPage patchWizardPage = wizard.getPages()[0]; |
98 |
|
99 |
getButton(patchWizardPage, "fUseClipboardButton").setSelection(false); |
100 |
getButton(patchWizardPage, "fUsePatchFileButton").setSelection(false); |
101 |
getButton(patchWizardPage, "fUseWorkspaceButton").setSelection(true); |
102 |
|
103 |
TreeViewer tree = getTreeViewer(patchWizardPage, "fTreeViewer"); |
104 |
treeSelect(tree, TEST_PROJECT + "/patch_addition.txt"); |
105 |
|
106 |
processQueuedEvents(); |
107 |
assertTrue(patchWizardPage.canFlipToNextPage()); |
108 |
callMethod(wizardDialog, "nextPressed", new Object[] {}); |
109 |
|
110 |
assertTrue(wizard.canFinish()); |
111 |
wizard.performFinish(); |
112 |
wizardDialog.close(); |
113 |
|
114 |
InputStream expected = PatchUtils.asInputStream("exp_addition.txt"); |
115 |
InputStream actual = testProject.getFile("exp_addition.txt") |
116 |
.getContents(); |
117 |
compareStreams(expected, actual); |
118 |
} |
119 |
|
120 |
private void openPatchWizard() { |
121 |
ImageDescriptor patchWizardImage = null; |
122 |
String patchWizardTitle = null; |
123 |
|
124 |
IStorage patch = null; |
125 |
IResource target = null; |
126 |
CompareConfiguration configuration = new CompareConfiguration(); |
127 |
|
128 |
wizard = new PatchWizard(patch, target, configuration); |
129 |
if (patchWizardImage != null) |
130 |
wizard.setDefaultPageImageDescriptor(patchWizardImage); |
131 |
if (patchWizardTitle != null) |
132 |
wizard.setWindowTitle(patchWizardTitle); |
133 |
wizard.setNeedsProgressMonitor(true); |
134 |
|
135 |
wizardDialog = new PatchWizardDialog(getShell(), wizard); |
136 |
wizard.setDialog(wizardDialog); |
137 |
wizardDialog.setBlockOnOpen(false); |
138 |
wizardDialog.open(); |
139 |
} |
140 |
|
141 |
private void copyIntoClipboard(String name) { |
142 |
Clipboard clipboard = new Clipboard(getShell().getDisplay()); |
143 |
InputStream patchIS = PatchUtils.asInputStream(name); |
144 |
String patch = null; |
145 |
try { |
146 |
patch = PatchUtils.asString(patchIS); |
147 |
} catch (IOException e) { |
148 |
fail(e.getMessage()); |
149 |
} |
150 |
TextTransfer textTransfer = TextTransfer.getInstance(); |
151 |
Transfer[] transfers = new Transfer[] { textTransfer }; |
152 |
Object[] data = new Object[] { patch }; |
153 |
clipboard.setContents(data, transfers); |
154 |
clipboard.dispose(); |
155 |
} |
156 |
|
157 |
private void copyIntoWorkspace(String name) { |
158 |
IFile file = testProject.getFile(name); |
159 |
InputStream is = PatchUtils.asInputStream(name); |
160 |
try { |
161 |
file.create(is, true, null); |
162 |
} catch (CoreException e) { |
163 |
fail(e.getMessage()); |
164 |
} |
165 |
} |
166 |
|
167 |
private void compareStreams(InputStream expectedIS, InputStream actualIS) { |
168 |
String expected = null; |
169 |
String actual = null; |
170 |
try { |
171 |
expected = PatchUtils.asString(expectedIS); |
172 |
actual = PatchUtils.asString(actualIS); |
173 |
} catch (IOException e) { |
174 |
fail(e.getMessage()); |
175 |
} |
176 |
assertEquals(expected, actual); |
177 |
} |
178 |
|
179 |
private void treeSelect(TreeViewer tree, String path) { |
180 |
WorkspaceRoot root = (WorkspaceRoot) tree.getInput(); |
181 |
IFile file = root.getFile(path); |
182 |
TreePath treePath = new TreePath(new Object[] { file }); |
183 |
TreeSelection sel = new TreeSelection(treePath); |
184 |
tree.setSelection(sel); |
185 |
} |
186 |
|
187 |
private Button getButton(Object object, String name) { |
188 |
return (Button) getField(object, name); |
189 |
} |
190 |
|
191 |
private TreeViewer getTreeViewer(Object object, String name) { |
192 |
return (TreeViewer) getField(object, name); |
193 |
} |
194 |
|
195 |
private Object getField(Object object, String name) { |
196 |
Object ret = null; |
197 |
try { |
198 |
ret = ReflectionUtils.getField(object, name); |
199 |
} catch (IllegalArgumentException e) { |
200 |
fail(e.getMessage()); |
201 |
} catch (SecurityException e) { |
202 |
fail(e.getMessage()); |
203 |
} catch (IllegalAccessException e) { |
204 |
fail(e.getMessage()); |
205 |
} catch (NoSuchFieldException e) { |
206 |
fail(e.getMessage()); |
207 |
} |
208 |
return ret; |
209 |
} |
210 |
|
211 |
private Object callMethod(Object object, String name, Object args[]) { |
212 |
Object ret = null; |
213 |
try { |
214 |
ret = ReflectionUtils.callMethod(object, name, args); |
215 |
} catch (IllegalArgumentException e) { |
216 |
fail(e.getMessage()); |
217 |
} catch (IllegalAccessException e) { |
218 |
fail(e.getMessage()); |
219 |
} catch (InvocationTargetException e) { |
220 |
fail(e.getMessage()); |
221 |
} catch (NoSuchMethodException e) { |
222 |
fail(e.getMessage()); |
223 |
} |
224 |
return ret; |
225 |
} |
226 |
|
227 |
private Shell getShell() { |
228 |
return WorkbenchPlugin.getDefault().getWorkbench() |
229 |
.getActiveWorkbenchWindow().getShell(); |
230 |
} |
231 |
|
232 |
private void processQueuedEvents() { |
233 |
while (Display.getCurrent().readAndDispatch()) { |
234 |
// Process all the events in the queue |
235 |
} |
236 |
} |
237 |
|
238 |
} |