ctf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ctf / ui / swtbot / tests / StandardImportAndReadSmokeTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 * Marc-Andre Laperle - Added tests for extracting archives during import
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertTrue;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URISyntaxException;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27
28 import org.eclipse.core.resources.IFolder;
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.ResourcesPlugin;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.core.runtime.IPath;
34 import org.eclipse.core.runtime.Path;
35 import org.eclipse.core.runtime.URIUtil;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.jface.wizard.WizardDialog;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
40 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
41 import org.eclipse.swtbot.swt.finder.results.VoidResult;
42 import org.eclipse.swtbot.swt.finder.waits.Conditions;
43 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
44 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo;
45 import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio;
46 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
48 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
49 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
50 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
51 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
52 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
53 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizardPage;
54 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages;
55 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
56 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
57 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
58 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
59 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
60 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
61 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
62 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
63 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
64 import org.eclipse.ui.IWorkbench;
65 import org.eclipse.ui.IWorkbenchWindow;
66 import org.eclipse.ui.PlatformUI;
67 import org.junit.Test;
68 import org.junit.runner.RunWith;
69
70 /**
71 * SWTBot Smoke test using ImportTraceWizard.
72 *
73 * @author Bernd Hufmann
74 */
75 @RunWith(SWTBotJunit4ClassRunner.class)
76 public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTest {
77
78 private static final String TRACE_FOLDER_PARENT_PATH = fTrace.getPath() + File.separator + ".." + File.separator + ".." + File.separator;
79 private static final String ARCHIVE_FILE_NAME = "synctraces.tar.gz";
80 private static final String TRACE_ARCHIVE_PATH = TRACE_FOLDER_PARENT_PATH + ARCHIVE_FILE_NAME;
81 private static final String TRACE_FOLDER_PARENT_NAME = "traces";
82 private static final String TRACE_PROJECT_NAME = "Tracing";
83
84 private static final String ARCHIVE_ROOT_ELEMENT_NAME = "/";
85 private static final String GENERATED_ARCHIVE_NAME = "testtraces.zip";
86 private static final String URI_SEPARATOR = "/";
87 private static final String URI_FILE_SCHEME = "file:";
88 private static final String URI_JAR_FILE_SCHEME = "jar:file:";
89 private static final boolean IS_WIN32 = System.getProperty("os.name").startsWith("Windows"); //$NON-NLS-1$//$NON-NLS-2$
90 private static final String URI_DEVICE_SEPARATOR = IS_WIN32 ? URI_SEPARATOR : "";
91
92 /**
93 * Test import from directory
94 *
95 * @throws Exception
96 * on error
97 */
98 @Test
99 public void testImportFromDirectory() throws Exception {
100 testImport(0, false, false);
101 }
102
103 /**
104 * Test import from directory, create links
105 *
106 * @throws Exception
107 * on error
108 */
109 @Test
110 public void testImportFromDirectoryLinks() throws Exception {
111 testImport(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE, false, false);
112 }
113
114 /**
115 * Test import from directory, preserve folder structure
116 *
117 * @throws Exception
118 * on error
119 */
120 @Test
121 public void testImportFromDirectoryPreserveFolder() throws Exception {
122 testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, false);
123 }
124
125 /**
126 * Test import from directory, create links, preserve folder structure
127 *
128 * @throws Exception
129 * on error
130 */
131 @Test
132 public void testImportFromDirectoryLinksPreserveFolder() throws Exception {
133 int options = ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE;
134 testImport(options, false, false);
135 }
136
137 /**
138 * Test import from directory, overwrite all
139 *
140 * @throws Exception
141 * on error
142 */
143 @Test
144 public void testImportFromDirectoryOverwrite() throws Exception {
145 testImport(0, false, false);
146 testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, false);
147 }
148
149 /**
150 * Test import from archive
151 *
152 * @throws Exception
153 * on error
154 */
155 @Test
156 public void testImportFromArchive() throws Exception {
157 testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true, true);
158 }
159
160 /**
161 * Test import from directory, preserve folder structure
162 * @throws Exception on error
163 */
164 @Test
165 public void testImportFromArchivePreserveFolder() throws Exception {
166 testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, true);
167 }
168
169 /**
170 * Test import from directory, overwrite all
171 *
172 * @throws Exception
173 * on error
174 */
175 @Test
176 public void testImportFromArchiveOverwrite() throws Exception {
177 testImport(0, false, true);
178 testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, true);
179 }
180
181 /**
182 * Test import from directory containing archives
183 *
184 * @throws Exception
185 * on error
186 */
187 @Test
188 public void testExtractArchivesFromDirectory() throws Exception {
189 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false);
190 }
191
192 /**
193 * Test import from directory containing archives, create links
194 * @throws Exception on error
195 */
196 @Test
197 public void testExtractArchivesFromDirectoryLinks() throws Exception {
198 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false);
199 }
200
201 /**
202 * Test import from directory containing archives, create links, preserve folder structure
203 * @throws Exception on error
204 */
205 @Test
206 public void testExtractArchivesFromDirectoryLinksPreserveStruture() throws Exception {
207 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false);
208 }
209
210 /**
211 * Test import from archive containing archives
212 *
213 * @throws Exception
214 * on error
215 */
216 @Test
217 public void testExtractArchivesFromArchive() throws Exception {
218 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, true);
219 }
220
221 /**
222 * Test import from archive containing archives, preserve folder structure
223 *
224 * @throws Exception
225 * on error
226 */
227 @Test
228 public void testExtractArchivesFromArchivePreserveFolder() throws Exception {
229 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true);
230 }
231
232 private void testImport(int options, boolean testViews, boolean fromArchive) throws Exception {
233 createProject();
234 String expectedSourceLocation = null;
235 openImportWizard();
236 if (fromArchive) {
237 expectedSourceLocation = URI_JAR_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(TRACE_ARCHIVE_PATH).getCanonicalPath()) + "!" + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR;
238 selectImportFromArchive(TRACE_ARCHIVE_PATH);
239 selectFolder(ARCHIVE_ROOT_ELEMENT_NAME);
240 SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace);
241 assertFalse(checkBox.isEnabled());
242 } else {
243 String sourcePath = TRACE_FOLDER_PARENT_PATH + File.separator + TRACE_FOLDER + File.separator + TRACE_NAME;
244 expectedSourceLocation = URI_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(sourcePath).getCanonicalPath()) + URI_SEPARATOR;
245 selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH);
246 selectFolder(new String [] {TRACE_FOLDER_PARENT_NAME, TRACE_FOLDER });
247 }
248
249 setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
250 importFinish();
251
252 IPath expectedElementPath = new Path(TRACE_NAME);
253 if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) {
254 expectedElementPath = new Path(TRACE_FOLDER).append(expectedElementPath);
255 }
256
257 checkOptions(options, expectedSourceLocation, expectedElementPath);
258 TmfEventsEditor tmfEd = SWTBotUtils.openEditor(fBot, getProjectName(), expectedElementPath);
259 if (testViews) {
260 testViews(tmfEd);
261 }
262
263 fBot.closeAllEditors();
264
265 SWTBotUtils.deleteProject(getProjectName(), fBot);
266 }
267
268 private void testImportAndExtractArchives(int options, boolean fromArchive) throws Exception {
269 createProject();
270
271 String expectedSourceLocation;
272 IPath expectedElementPath;
273 if (fromArchive) {
274 String testArchivePath = createArchive();
275 openImportWizard();
276 selectImportFromArchive(testArchivePath);
277 selectFile(ARCHIVE_FILE_NAME, ARCHIVE_ROOT_ELEMENT_NAME, TRACE_PROJECT_NAME, TRACE_FOLDER_PARENT_NAME);
278
279 expectedSourceLocation = URI_JAR_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(testArchivePath).getCanonicalPath()) + "!" + URI_SEPARATOR + TRACE_PROJECT_NAME + URI_SEPARATOR + TRACE_FOLDER_PARENT_NAME + URI_SEPARATOR + ARCHIVE_FILE_NAME
280 + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR;
281 expectedElementPath = new Path(TRACE_PROJECT_NAME).append(TRACE_FOLDER_PARENT_NAME).append(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME);
282 } else {
283 openImportWizard();
284 selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH);
285 selectFile(ARCHIVE_FILE_NAME, TRACE_FOLDER_PARENT_NAME);
286 expectedElementPath = new Path(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME);
287 expectedSourceLocation = URI_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(TRACE_FOLDER_PARENT_PATH).getCanonicalPath()) + URI_SEPARATOR + ARCHIVE_FILE_NAME + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR;
288 }
289
290 if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) == 0) {
291 expectedElementPath = new Path(TRACE_NAME);
292 }
293
294 setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
295 importFinish();
296 // Archives should never be imported as links
297 int expectedOptions = options & ~ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE;
298 checkOptions(expectedOptions, expectedSourceLocation, expectedElementPath);
299
300 TmfEventsEditor editor = SWTBotUtils.openEditor(fBot, TRACE_PROJECT_NAME, expectedElementPath);
301 testViews(editor);
302
303 SWTBotUtils.deleteProject(getProjectName(), fBot);
304 }
305
306 /**
307 * Create a temporary archive containing a nested archive. For example,
308 * testtraces.zip/synctraces.tar.gz can be used to test a nested archive.
309 */
310 private String createArchive() throws URISyntaxException, CoreException, IOException {
311
312 // Link to the test traces folder. We use a link so that we can safely
313 // delete the entire project when we are done.
314 IProject project = getProjectResource();
315 String canonicalPath = new File(TRACE_FOLDER_PARENT_PATH).getCanonicalPath();
316 IFolder folder = project.getFolder(TRACE_FOLDER_PARENT_NAME);
317 folder.createLink(new Path(canonicalPath), IResource.REPLACE, null);
318
319 SWTBotTreeItem traceFilesProject = SWTBotUtils.selectProject(fBot, TRACE_PROJECT_NAME);
320 traceFilesProject.contextMenu("Export...").click();
321
322 fBot.waitUntil(Conditions.shellIsActive("Export"));
323 SWTBotShell activeShell = fBot.activeShell();
324 SWTBotTree exportWizardsTree = fBot.tree();
325 SWTBotTreeItem treeItem = SWTBotUtils.getTreeItem(fBot, exportWizardsTree, "General", "Archive File");
326 treeItem.select();
327 fBot.button("Next >").click();
328 fBot.button("&Deselect All").click();
329 selectFile(ARCHIVE_FILE_NAME, TRACE_PROJECT_NAME, TRACE_FOLDER_PARENT_NAME);
330
331 String workspacePath = URIUtil.toFile(URIUtil.fromString(System.getProperty("osgi.instance.area"))).getAbsolutePath();
332 final String archiveDestinationPath = workspacePath + File.separator + TRACE_PROJECT_NAME + File.separator + GENERATED_ARCHIVE_NAME;
333 fBot.comboBox().setText(archiveDestinationPath);
334 fBot.button("&Finish").click();
335 fBot.waitUntil(Conditions.shellCloses(activeShell));
336 return archiveDestinationPath;
337 }
338
339 private void testViews(TmfEventsEditor editor) {
340 testHistogramView(getViewPart("Histogram"), editor);
341 testPropertyView(getViewPart("Properties"));
342 testStatisticsView(getViewPart("Statistics"));
343 }
344
345 private static void openImportWizard() {
346 fWizard = new ImportTraceWizard();
347
348 UIThreadRunnable.asyncExec(new VoidResult() {
349 @Override
350 public void run() {
351 final IWorkbench workbench = PlatformUI.getWorkbench();
352 // Fire the Import Trace Wizard
353 if (workbench != null) {
354 final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
355 Shell shell = activeWorkbenchWindow.getShell();
356 assertNotNull(shell);
357 ((ImportTraceWizard) fWizard).init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY);
358 WizardDialog dialog = new WizardDialog(shell, fWizard);
359 dialog.open();
360 }
361 }
362 });
363
364 fBot.waitUntil(ConditionHelpers.isWizardReady(fWizard));
365 }
366
367 private static void selectImportFromDirectory(String directoryPath) {
368 SWTBotRadio button = fBot.radio("Select roo&t directory:");
369 button.click();
370
371 SWTBotCombo sourceCombo = fBot.comboBox();
372 File traceFolderParent = new File(directoryPath);
373 sourceCombo.setText(traceFolderParent.getAbsolutePath());
374
375 SWTBotText text = fBot.text();
376 text.setFocus();
377 }
378
379 private static void selectImportFromArchive(String archivePath) {
380 SWTBotRadio button = fBot.radio("Select &archive file:");
381 button.click();
382
383 SWTBotCombo sourceCombo = fBot.comboBox(1);
384
385 sourceCombo.setText(new File(archivePath).getAbsolutePath());
386
387 SWTBotText text = fBot.text();
388 text.setFocus();
389 }
390
391 private static void selectFolder(String... treePath) {
392 SWTBotTree tree = fBot.tree();
393 fBot.waitUntil(Conditions.widgetIsEnabled(tree));
394 SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(fBot, tree, treePath);
395 folderNode.check();
396 }
397
398 private static void selectFile(String fileName, String... folderTreePath) {
399 SWTBotTree folderTree = fBot.tree();
400 fBot.waitUntil(Conditions.widgetIsEnabled(folderTree));
401 SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(fBot, folderTree, folderTreePath);
402 folderNode.select();
403
404 SWTBotTable fileTable = fBot.table();
405 fBot.waitUntil(Conditions.widgetIsEnabled(fileTable));
406 fBot.waitUntil(ConditionHelpers.isTableItemAvailable(fileName, fileTable));
407 SWTBotTableItem tableItem = fileTable.getTableItem(fileName);
408 tableItem.check();
409 }
410
411 private static void setOptions(int optionFlags, String traceTypeName) {
412 SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace);
413 if (checkBox.isEnabled()) {
414 if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0) {
415 checkBox.select();
416 } else {
417 checkBox.deselect();
418 }
419 }
420
421 checkBox = fBot.checkBox(Messages.ImportTraceWizard_PreserveFolderStructure);
422 if ((optionFlags & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) {
423 checkBox.select();
424 } else {
425 checkBox.deselect();
426 }
427
428 checkBox = fBot.checkBox(Messages.ImportTraceWizard_ImportUnrecognized);
429 if ((optionFlags & ImportTraceWizardPage.OPTION_IMPORT_UNRECOGNIZED_TRACES) != 0) {
430 checkBox.select();
431 } else {
432 checkBox.deselect();
433 }
434
435 checkBox = fBot.checkBox(Messages.ImportTraceWizard_OverwriteExistingTrace);
436 if ((optionFlags & ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES) != 0) {
437 checkBox.select();
438 } else {
439 checkBox.deselect();
440 }
441
442 SWTBotCombo comboBox = fBot.comboBoxWithLabel(Messages.ImportTraceWizard_TraceType);
443 if (traceTypeName != null && !traceTypeName.isEmpty()) {
444 comboBox.setSelection(traceTypeName);
445 } else {
446 comboBox.setSelection(ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
447 }
448 }
449
450 private void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath) throws CoreException {
451 IProject project = getProjectResource();
452 assertTrue(project.exists());
453 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true);
454 assertNotNull(tmfProject);
455 TmfTraceFolder tracesFolder = tmfProject.getTracesFolder();
456 assertNotNull(tracesFolder);
457 List<TmfTraceElement> traces = tracesFolder.getTraces();
458 assertFalse(traces.isEmpty());
459 Collections.sort(traces, new Comparator<TmfTraceElement>() {
460 @Override
461 public int compare(TmfTraceElement arg0, TmfTraceElement arg1) {
462 return arg0.getElementPath().compareTo(arg1.getElementPath());
463 }
464 });
465
466 TmfTraceElement tmfTraceElement = traces.get(0);
467 IResource traceResource = tmfTraceElement.getResource();
468
469 assertEquals((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0, traceResource.isLinked());
470
471 // i.e. /Tracing/Traces
472 IPath expectedPath = Path.ROOT.append(new Path(TRACE_PROJECT_NAME)).append(TmfTracesFolder.TRACES_FOLDER_NAME).append(expectedElementPath);
473 assertEquals(expectedPath, traceResource.getFullPath());
474
475 String sourceLocation = traceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION);
476 assertNotNull(sourceLocation);
477 assertEquals(expectedSourceLocation, sourceLocation);
478 }
479
480 @Override
481 protected String getProjectName() {
482 return TRACE_PROJECT_NAME;
483 }
484
485 private IProject getProjectResource() {
486 return ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectName());
487 }
488 }
This page took 0.046925 seconds and 5 git commands to generate.