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.team.tests.ccvs.ui; |
12 |
|
13 |
import java.io.BufferedReader; |
14 |
import java.io.IOException; |
15 |
import java.io.InputStream; |
16 |
import java.io.InputStreamReader; |
17 |
import java.lang.reflect.InvocationTargetException; |
18 |
import java.net.URL; |
19 |
|
20 |
import junit.framework.Test; |
21 |
|
22 |
import org.eclipse.core.resources.IFile; |
23 |
import org.eclipse.core.resources.IProject; |
24 |
import org.eclipse.core.resources.IResource; |
25 |
import org.eclipse.core.resources.IWorkspaceRunnable; |
26 |
import org.eclipse.core.resources.ResourcesPlugin; |
27 |
import org.eclipse.core.runtime.CoreException; |
28 |
import org.eclipse.core.runtime.IPath; |
29 |
import org.eclipse.core.runtime.IProgressMonitor; |
30 |
import org.eclipse.core.runtime.Path; |
31 |
import org.eclipse.jface.wizard.IWizardPage; |
32 |
import org.eclipse.jface.wizard.WizardDialog; |
33 |
import org.eclipse.swt.SWT; |
34 |
import org.eclipse.swt.widgets.Button; |
35 |
import org.eclipse.swt.widgets.Display; |
36 |
import org.eclipse.swt.widgets.Event; |
37 |
import org.eclipse.swt.widgets.Shell; |
38 |
import org.eclipse.swt.widgets.Text; |
39 |
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages; |
40 |
import org.eclipse.team.internal.ccvs.ui.wizards.GenerateDiffFileWizard; |
41 |
import org.eclipse.team.tests.ccvs.core.EclipseTest; |
42 |
import org.eclipse.team.tests.ccvs.core.TeamCVSTestPlugin; |
43 |
import org.eclipse.ui.IWorkbenchPage; |
44 |
import org.eclipse.ui.IWorkbenchPart; |
45 |
import org.eclipse.ui.IWorkbenchWindow; |
46 |
import org.eclipse.ui.PlatformUI; |
47 |
|
48 |
public class CreatePatchTest extends EclipseTest { |
49 |
|
50 |
public static final String PATCHDATA = "patchdata"; |
51 |
|
52 |
private final static int INITIAL_WIDTH = 300; |
53 |
private final static int INITIAL_HEIGHT = 350; |
54 |
|
55 |
private GenerateDiffFileWizard wizard = null; |
56 |
private WizardDialog wizardDialog = null; |
57 |
|
58 |
private IProject testProject = null; |
59 |
|
60 |
public CreatePatchTest() { |
61 |
super(); |
62 |
} |
63 |
|
64 |
public CreatePatchTest(String name) { |
65 |
super(name); |
66 |
} |
67 |
|
68 |
public static Test suite() { |
69 |
return suite(CreatePatchTest.class); |
70 |
} |
71 |
|
72 |
protected void setUp() throws Exception { |
73 |
super.setUp(); |
74 |
testProject = createProject("ApplyPatchTest", new String[] {}); |
75 |
} |
76 |
|
77 |
protected void tearDown() throws Exception { |
78 |
super.tearDown(); |
79 |
testProject.delete(true, null); |
80 |
} |
81 |
|
82 |
public void testCreateWorkspacePatch() throws Exception { |
83 |
copyIntoWorkspace("exp_addition.txt", "addition.txt"); |
84 |
|
85 |
openGenerateDiffFileWizard(new IResource[] { testProject }); |
86 |
assertTrue(wizard.getPageCount() == 2); |
87 |
|
88 |
IWizardPage locationPage = wizard.getPages()[0]; |
89 |
|
90 |
getButton(locationPage, "cpRadio").setSelection(false); |
91 |
getButton(locationPage, "fsRadio").setSelection(false); |
92 |
getButton(locationPage, "wsRadio").setSelection(true); |
93 |
getButton(locationPage, "wsRadio").notifyListeners(SWT.Selection, |
94 |
createSelectionEvent()); |
95 |
getText(locationPage, "wsPathText").setText( |
96 |
"/" + testProject.getName() + "/patch_addition.txt"); |
97 |
|
98 |
waitForSelection(); |
99 |
|
100 |
processQueuedEvents(); |
101 |
assertTrue(locationPage.canFlipToNextPage()); |
102 |
callMethod(wizardDialog, "nextPressed"); |
103 |
|
104 |
IWizardPage optionsPage = wizard.getPages()[1]; |
105 |
|
106 |
getButton(optionsPage, "unifiedDiffOption").setSelection(true); |
107 |
getButton(optionsPage, "contextDiffOption").setSelection(false); |
108 |
getButton(optionsPage, "regularDiffOption").setSelection(false); |
109 |
getButton(optionsPage, "unified_workspaceRelativeOption").setSelection( |
110 |
true); |
111 |
getButton(optionsPage, "unified_projectRelativeOption").setSelection( |
112 |
false); |
113 |
getButton(optionsPage, "unified_selectionRelativeOption").setSelection( |
114 |
false); |
115 |
|
116 |
processQueuedEvents(); |
117 |
assertTrue(wizard.canFinish()); |
118 |
wizard.performFinish(); |
119 |
wizardDialog.close(); |
120 |
|
121 |
// Ignore lines prefixed with following values to avoid timestamps |
122 |
// mismatches |
123 |
String prefixesToIgnore[] = new String[] { "#P ApplyPatchTest-", |
124 |
"--- /dev/null", "+++ addition.txt" }; |
125 |
|
126 |
InputStream expectedIS = asInputStream("patch_addition.txt"); |
127 |
String expected = filterStream(expectedIS, prefixesToIgnore); |
128 |
|
129 |
IFile patchFile = testProject.getFile("patch_addition.txt"); |
130 |
String actual = readProjectFile(patchFile, prefixesToIgnore); |
131 |
|
132 |
assertEquals(expected, actual); |
133 |
} |
134 |
|
135 |
private void openGenerateDiffFileWizard(IResource resources[]) { |
136 |
String title = CVSUIMessages.GenerateCVSDiff_title; |
137 |
wizard = new GenerateDiffFileWizard(getActivePart(), resources, true); |
138 |
wizard.setWindowTitle(title); |
139 |
wizardDialog = new WizardDialog(getShell(), wizard); |
140 |
wizardDialog.setMinimumPageSize(INITIAL_WIDTH, INITIAL_HEIGHT); |
141 |
wizardDialog.setBlockOnOpen(false); |
142 |
wizardDialog.open(); |
143 |
} |
144 |
|
145 |
private void copyIntoWorkspace(String source, String target) { |
146 |
IFile file = testProject.getFile(target); |
147 |
InputStream is = asInputStream(source); |
148 |
try { |
149 |
if (file.exists()) { |
150 |
file.setContents(is, true, true, null); |
151 |
} else { |
152 |
file.create(is, true, null); |
153 |
} |
154 |
} catch (CoreException e) { |
155 |
fail(e.getMessage()); |
156 |
} |
157 |
} |
158 |
|
159 |
private void waitForSelection() { |
160 |
IWizardPage locationPage = wizard.getPages()[0]; |
161 |
int toSleep = 100; |
162 |
int totalWaited = 0; |
163 |
IResource[] sel = null; |
164 |
while (sel == null || sel.length == 0) { |
165 |
sel = (IResource[]) callMethod(locationPage, "getSelectedResources"); |
166 |
processQueuedEvents(); |
167 |
try { |
168 |
Thread.sleep(toSleep); |
169 |
} catch (InterruptedException e) { |
170 |
fail(e.getMessage()); |
171 |
} |
172 |
totalWaited += toSleep; |
173 |
assertTrue(totalWaited < 2500); |
174 |
} |
175 |
} |
176 |
|
177 |
private String readProjectFile(final IFile file, |
178 |
final String[] prefixesToIgnore) { |
179 |
final String[] ret = new String[1]; |
180 |
int toSleep = 100; |
181 |
int totalWaited = 0; |
182 |
while (!file.exists()) { |
183 |
processQueuedEvents(); |
184 |
try { |
185 |
Thread.sleep(toSleep); |
186 |
} catch (InterruptedException e) { |
187 |
fail(e.getMessage()); |
188 |
} |
189 |
totalWaited += toSleep; |
190 |
assertTrue(totalWaited < 2500); |
191 |
} |
192 |
try { |
193 |
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { |
194 |
public void run(IProgressMonitor monitor) throws CoreException { |
195 |
ret[0] = filterStream(file.getContents(), prefixesToIgnore); |
196 |
} |
197 |
}, file, IResource.NONE, null); |
198 |
} catch (CoreException e) { |
199 |
fail(e.getMessage()); |
200 |
} |
201 |
return ret[0]; |
202 |
} |
203 |
|
204 |
private String filterStream(InputStream stream, String prefixesToIgnore[]) { |
205 |
BufferedReader reader = new BufferedReader( |
206 |
new InputStreamReader(stream)); |
207 |
String line = null; |
208 |
StringBuffer buffer = new StringBuffer(); |
209 |
try { |
210 |
while ((line = reader.readLine()) != null) { |
211 |
boolean ignore = false; |
212 |
for (int i = 0; i < prefixesToIgnore.length && !ignore; i++) { |
213 |
ignore = line.startsWith(prefixesToIgnore[i]); |
214 |
} |
215 |
if (!ignore) { |
216 |
buffer.append(line + "\n"); |
217 |
} |
218 |
} |
219 |
} catch (IOException e) { |
220 |
fail(e.getMessage()); |
221 |
} |
222 |
String ret = buffer.toString(); |
223 |
try { |
224 |
reader.close(); |
225 |
} catch (IOException e) { |
226 |
fail(e.getMessage()); |
227 |
} |
228 |
return ret; |
229 |
} |
230 |
|
231 |
private InputStream asInputStream(String name) { |
232 |
IPath path = new Path(PATCHDATA).append(name); |
233 |
try { |
234 |
URL base = TeamCVSTestPlugin.getDefault().getBundle().getEntry("/"); |
235 |
URL url = new URL(base, path.toString()); |
236 |
return url.openStream(); |
237 |
} catch (IOException e) { |
238 |
fail("Failed while reading " + name); |
239 |
return null; |
240 |
} |
241 |
} |
242 |
|
243 |
private Text getText(Object object, String name) { |
244 |
return (Text) getField(object, name); |
245 |
} |
246 |
|
247 |
private Button getButton(Object object, String name) { |
248 |
return (Button) getField(object, name); |
249 |
} |
250 |
|
251 |
private Object getField(Object object, String name) { |
252 |
Object ret = null; |
253 |
try { |
254 |
ret = ReflectionUtils.getField(object, name); |
255 |
} catch (IllegalArgumentException e) { |
256 |
fail(e.getMessage()); |
257 |
} catch (SecurityException e) { |
258 |
fail(e.getMessage()); |
259 |
} catch (IllegalAccessException e) { |
260 |
fail(e.getMessage()); |
261 |
} catch (NoSuchFieldException e) { |
262 |
fail(e.getMessage()); |
263 |
} |
264 |
return ret; |
265 |
} |
266 |
|
267 |
private Object callMethod(Object object, String name) { |
268 |
return callMethod(object, name, new Object[] {}); |
269 |
} |
270 |
|
271 |
private Object callMethod(Object object, String name, Object args[]) { |
272 |
Object ret = null; |
273 |
try { |
274 |
ret = ReflectionUtils.callMethod(object, name, args); |
275 |
} catch (IllegalArgumentException e) { |
276 |
fail(e.getMessage()); |
277 |
} catch (IllegalAccessException e) { |
278 |
fail(e.getMessage()); |
279 |
} catch (InvocationTargetException e) { |
280 |
fail(e.getMessage()); |
281 |
} catch (NoSuchMethodException e) { |
282 |
fail(e.getMessage()); |
283 |
} |
284 |
return ret; |
285 |
} |
286 |
|
287 |
private Shell getShell() { |
288 |
return getActivePart().getSite().getShell(); |
289 |
} |
290 |
|
291 |
private IWorkbenchPart getActivePart() { |
292 |
IWorkbenchWindow window = PlatformUI.getWorkbench() |
293 |
.getActiveWorkbenchWindow(); |
294 |
if (window == null) { |
295 |
return null; |
296 |
} |
297 |
IWorkbenchPage page = window.getActivePage(); |
298 |
if (page == null) { |
299 |
return null; |
300 |
} |
301 |
return page.getActivePart(); |
302 |
} |
303 |
|
304 |
private void processQueuedEvents() { |
305 |
while (Display.getCurrent().readAndDispatch()) { |
306 |
} |
307 |
} |
308 |
|
309 |
private Event createSelectionEvent() { |
310 |
Event event = new Event(); |
311 |
event.type = SWT.Selection; |
312 |
return event; |
313 |
} |
314 |
|
315 |
} |