tmf: Bug 494689: fix failing name conflict handling in trace import
[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.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
17 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
32
33 import org.eclipse.core.resources.IFile;
34 import org.eclipse.core.resources.IFolder;
35 import org.eclipse.core.resources.IProject;
36 import org.eclipse.core.resources.IResource;
37 import org.eclipse.core.resources.ResourcesPlugin;
38 import org.eclipse.core.runtime.CoreException;
39 import org.eclipse.core.runtime.IPath;
40 import org.eclipse.core.runtime.Path;
41 import org.eclipse.core.runtime.URIUtil;
42 import org.eclipse.jdt.annotation.NonNull;
43 import org.eclipse.jface.viewers.StructuredSelection;
44 import org.eclipse.jface.wizard.WizardDialog;
45 import org.eclipse.swt.widgets.Shell;
46 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
47 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
48 import org.eclipse.swtbot.swt.finder.results.VoidResult;
49 import org.eclipse.swtbot.swt.finder.waits.Conditions;
50 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
51 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo;
52 import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio;
53 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
54 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
55 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
56 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
57 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
58 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
59 import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;
60 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportConfirmation;
61 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
62 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizardPage;
63 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages;
64 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
65 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
66 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
67 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
68 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
69 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
70 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
71 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
72 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
73 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
74 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
75 import org.eclipse.ui.IWorkbench;
76 import org.eclipse.ui.IWorkbenchWindow;
77 import org.eclipse.ui.PlatformUI;
78 import org.junit.AfterClass;
79 import org.junit.BeforeClass;
80 import org.junit.Test;
81 import org.junit.runner.RunWith;
82
83 /**
84 * SWTBot Smoke test using ImportTraceWizard.
85 *
86 * @author Bernd Hufmann
87 */
88 @RunWith(SWTBotJunit4ClassRunner.class)
89 public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTest {
90
91 private static final String TRACE_FOLDER_PARENT_PATH = "../../ctf/org.eclipse.tracecompass.ctf.core.tests/traces/";
92 private static final String ARCHIVE_FILE_NAME = "synctraces.tar.gz";
93 private static final String EMPTY_ARCHIVE_FOLDER = "emptyArchiveFolder";
94 private static final String EMPTY_FILE_NAME = "emptyFile";
95 private static final String TRACE_ARCHIVE_PATH = TRACE_FOLDER_PARENT_PATH + ARCHIVE_FILE_NAME;
96 private static final String TRACE_FOLDER_PARENT_NAME = "traces";
97 private static final String TRACE_PROJECT_NAME = "Tracing";
98
99 private static final String ARCHIVE_ROOT_ELEMENT_NAME = "/";
100 private static final String GENERATED_ARCHIVE_NAME = "testtraces.zip";
101 private static final String URI_SEPARATOR = "/";
102 private static final String URI_FILE_SCHEME = "file:";
103 private static final String URI_JAR_FILE_SCHEME = "jar:file:";
104 private static final boolean IS_WIN32 = System.getProperty("os.name").startsWith("Windows"); //$NON-NLS-1$//$NON-NLS-2$
105 private static final String URI_DEVICE_SEPARATOR = IS_WIN32 ? URI_SEPARATOR : "";
106
107 /** Test Class setup */
108 @BeforeClass
109 public static void beforeClass() {
110 createProject(TRACE_PROJECT_NAME);
111 }
112
113 /** Test Class tear down */
114 @AfterClass
115 public static void afterClass() {
116 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
117 }
118
119 /**
120 * Test import from directory
121 *
122 * @throws Exception
123 * on error
124 */
125 @Test
126 public void testImportFromDirectory() throws Exception {
127 testImport(0, false, false);
128 }
129
130 /**
131 * Test import from directory, create links
132 *
133 * @throws Exception
134 * on error
135 */
136 @Test
137 public void testImportFromDirectoryLinks() throws Exception {
138 testImport(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE, false, false);
139 }
140
141 /**
142 * Test import from directory, create experiment
143 *
144 * @throws Exception
145 * on error
146 */
147 @Test
148 public void testImportWithExperiment() throws Exception {
149 testImport(ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, false);
150 }
151
152 /**
153 * Test import from directory, create experiment (validate experiment name)
154 *
155 * @throws Exception
156 * on error
157 */
158 @Test
159 public void testImportWithExperimentValidation() throws Exception {
160 testImport(ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, false, false, true, ImportConfirmation.CONTINUE);
161 }
162
163 /**
164 * Test import from directory, preserve folder structure
165 *
166 * @throws Exception
167 * on error
168 */
169 @Test
170 public void testImportFromDirectoryPreserveFolder() throws Exception {
171 testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, false);
172 }
173
174 /**
175 * Test import from directory, create links, preserve folder structure
176 *
177 * @throws Exception
178 * on error
179 */
180 @Test
181 public void testImportFromDirectoryLinksPreserveFolder() throws Exception {
182 int options = ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE;
183 testImport(options, false, false);
184 }
185
186 /**
187 * Test import from directory, overwrite all
188 *
189 * @throws Exception
190 * on error
191 */
192 @Test
193 public void testImportFromDirectoryOverwrite() throws Exception {
194 testImport(0, false, false, true, false, ImportConfirmation.CONTINUE);
195 testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, false);
196 }
197
198 /**
199 * Test import from directory, overwrite all
200 *
201 * @throws Exception
202 * on error
203 */
204 @Test
205 public void testImportFromDirectoryOverwriteRenameAll() throws Exception {
206 testImport(0, false, false, true, false, ImportConfirmation.CONTINUE);
207 testImport(0, false, false, true, true, ImportConfirmation.RENAME_ALL);
208 }
209
210 /**
211 * Test import from directory, overwrite all
212 *
213 * @throws Exception
214 * on error
215 */
216 @Test
217 public void testImportFromDirectoryOverwriteOverwriteAll() throws Exception {
218 testImport(0, false, false, true, false, ImportConfirmation.CONTINUE);
219 testImport(0, false, false, true, true, ImportConfirmation.OVERWRITE_ALL);
220 }
221
222 /**
223 * Test import from archive
224 *
225 * @throws Exception
226 * on error
227 */
228 @Test
229 public void testImportFromArchive() throws Exception {
230 testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true, true);
231 }
232
233 /**
234 * Test import from archive, create Experiment
235 *
236 * @throws Exception
237 * on error
238 */
239 @Test
240 public void testImportFromArchiveWithExperiment() throws Exception {
241 testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE | ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, true);
242 }
243
244 /**
245 * Test import from directory, preserve folder structure
246 * @throws Exception on error
247 */
248 @Test
249 public void testImportFromArchivePreserveFolder() throws Exception {
250 testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, true);
251 }
252
253 /**
254 * Test import from directory, overwrite all
255 *
256 * @throws Exception
257 * on error
258 */
259 @Test
260 public void testImportFromArchiveOverwrite() throws Exception {
261 testImport(0, false, true, true, false, ImportConfirmation.CONTINUE);
262 testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, true);
263 }
264
265 /**
266 * Test import from directory containing archives
267 *
268 * @throws Exception
269 * on error
270 */
271 @Test
272 public void testExtractArchivesFromDirectory() throws Exception {
273 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, false);
274 }
275
276 /**
277 * Test import from directory containing archives, create links
278 * @throws Exception on error
279 */
280 @Test
281 public void testExtractArchivesFromDirectoryLinks() throws Exception {
282 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, false);
283 }
284
285 /**
286 * Test import from directory containing archives, create links, preserve folder structure
287 * @throws Exception on error
288 */
289 @Test
290 public void testExtractArchivesFromDirectoryLinksPreserveStruture() throws Exception {
291 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true, false);
292 }
293
294 /**
295 * Test import from archive containing archives
296 *
297 * @throws Exception
298 * on error
299 */
300 @Test
301 public void testExtractArchivesFromArchive() throws Exception {
302 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, true);
303 }
304
305 /**
306 * Test import from archive containing archives, preserve folder structure
307 *
308 * @throws Exception
309 * on error
310 */
311 @Test
312 public void testExtractArchivesFromArchivePreserveFolder() throws Exception {
313 testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, true);
314 }
315
316 /**
317 * Test import from an empty archive. This should not import anything.
318 *
319 * @throws Exception
320 * on error
321 */
322 @Test
323 public void testEmptyArchive() throws Exception {
324 String testArchivePath = createEmptyArchive();
325
326 openImportWizard();
327 selectImportFromArchive(testArchivePath);
328 selectFolder(ARCHIVE_ROOT_ELEMENT_NAME);
329 setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
330 importFinish();
331
332 assertNoTraces();
333
334 SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
335 Files.delete(Paths.get(testArchivePath));
336 }
337
338 /**
339 * Test import from an empty directory. This should not import anything.
340 *
341 * @throws Exception
342 * on error
343 */
344 @Test
345 public void testEmptyDirectory() throws Exception {
346 IFolder emptyDirectory = createEmptyDirectory();
347 String testDirectoryPath = emptyDirectory.getLocation().toOSString();
348
349 openImportWizard();
350 selectImportFromDirectory(testDirectoryPath);
351 selectFolder(EMPTY_ARCHIVE_FOLDER);
352 setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
353 importFinish();
354
355 assertNoTraces();
356
357 Files.delete(Paths.get(testDirectoryPath));
358 emptyDirectory.delete(true, null);
359 SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
360 }
361
362 /**
363 * Test import from an directory with an empty file. This should not import anything.
364 *
365 * @throws Exception
366 * on error
367 */
368 @Test
369 public void testEmptyFile() throws Exception {
370 IFolder folder = createEmptyDirectory();
371 createEmptyFile(folder);
372 String testDirectoryPath = folder.getLocation().toOSString();
373 openImportWizard();
374 selectImportFromDirectory(testDirectoryPath);
375 selectFile(EMPTY_FILE_NAME, EMPTY_ARCHIVE_FOLDER);
376 setOptions(ImportTraceWizardPage.OPTION_IMPORT_UNRECOGNIZED_TRACES, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
377 importFinish();
378
379 assertNoTraces();
380
381 SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
382 folder.delete(true, null);
383 }
384
385 /**
386 * Test import from a directory containing an empty archive. This should not import anything.
387 *
388 * @throws Exception
389 * on error
390 */
391 @Test
392 public void testDirectoryWithEmptyArchive() throws Exception {
393 String testArchivePath = createEmptyArchive();
394
395 openImportWizard();
396 selectImportFromDirectory(getProjectResource().getLocation().toOSString());
397 selectFile(GENERATED_ARCHIVE_NAME, TRACE_PROJECT_NAME);
398 setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
399 importFinish();
400
401 assertNoTraces();
402
403 SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
404 Files.delete(Paths.get(testArchivePath));
405 }
406
407 /**
408 * Test import from a nested empty archive. This should not import anything.
409 *
410 * @throws Exception
411 * on error
412 */
413 @Test
414 public void testNestedEmptyArchive() throws Exception {
415 IProject project = getProjectResource();
416
417 // Create the empty archive from an empty folder
418 String testArchivePath = createEmptyArchive();
419
420 // Rename archive so that we can create a new one with the same name
421 project.refreshLocal(IResource.DEPTH_ONE, null);
422 IFile[] files = project.getWorkspace().getRoot().findFilesForLocationURI(new File(testArchivePath).toURI());
423 IFile archiveFile = files[0];
424 String newEmptyArchiveName = "nested" + archiveFile.getName();
425 IPath dest = archiveFile.getFullPath().removeLastSegments(1).append(newEmptyArchiveName);
426 archiveFile.move(dest, true, null);
427 IFile renamedArchiveFile = archiveFile.getWorkspace().getRoot().getFile(dest);
428
429 createArchive(renamedArchiveFile);
430 renamedArchiveFile.delete(true, null);
431
432 openImportWizard();
433 selectImportFromArchive(testArchivePath);
434 selectFolder(ARCHIVE_ROOT_ELEMENT_NAME);
435 setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
436 importFinish();
437
438 assertNoTraces();
439
440 SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
441 Files.delete(Paths.get(testArchivePath));
442 }
443
444 private static void assertNoTraces() {
445 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(getProjectResource(), true);
446 assertNotNull(tmfProject);
447 TmfTraceFolder tracesFolder = tmfProject.getTracesFolder();
448 assertNotNull(tracesFolder);
449 List<TmfTraceElement> traces = tracesFolder.getTraces();
450 assertTrue(traces.isEmpty());
451 }
452
453 private void testImport(int options, boolean testViews, boolean fromArchive) throws Exception {
454 testImport(options, testViews, fromArchive, true, true, ImportConfirmation.CONTINUE);
455 }
456
457 private void testImport(int options, boolean testViews, boolean fromArchive, boolean defaultExperiment, boolean clearTraces, ImportConfirmation confirmationMode) throws Exception {
458 String expectedSourceLocation = null;
459
460 @NonNull String experimentName;
461 if (fromArchive) {
462 experimentName = checkNotNull(new Path(ARCHIVE_FILE_NAME).lastSegment());
463 } else {
464 experimentName = checkNotNull(new Path(TRACE_FOLDER_PARENT_PATH).lastSegment());
465 }
466
467 if (!defaultExperiment) {
468 SWTBotUtils.createExperiment(fBot, TRACE_PROJECT_NAME, experimentName);
469 }
470
471 openImportWizard();
472 if (fromArchive) {
473 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;
474 selectImportFromArchive(TRACE_ARCHIVE_PATH);
475 selectFolder(ARCHIVE_ROOT_ELEMENT_NAME);
476 SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace);
477 assertFalse(checkBox.isEnabled());
478 } else {
479 String sourcePath = TRACE_FOLDER_PARENT_PATH + File.separator + TRACE_FOLDER + File.separator + TRACE_NAME;
480 expectedSourceLocation = URI_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(sourcePath).getCanonicalPath()) + URI_SEPARATOR;
481 selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH);
482 selectFolder(new String [] {TRACE_FOLDER_PARENT_NAME, TRACE_FOLDER });
483 }
484
485 setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
486
487 if (!defaultExperiment) {
488 experimentName = verifyExperimentNameHandling(experimentName);
489 }
490 checkFinishButton(true);
491
492 importFinish(confirmationMode);
493
494 IPath expectedElementPath = new Path(TRACE_NAME);
495 if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) {
496 expectedElementPath = new Path(TRACE_FOLDER).append(expectedElementPath);
497 }
498
499 if (confirmationMode == ImportConfirmation.RENAME_ALL) {
500 IPath expectedElementPathRenamed = new Path(TRACE_NAME + "(2)");
501 checkOptions(options, expectedSourceLocation, expectedElementPath, experimentName, expectedElementPathRenamed);
502 } else {
503 checkOptions(options, expectedSourceLocation, expectedElementPath, experimentName, null);
504 }
505
506 TmfEventsEditor tmfEd = SWTBotUtils.openEditor(fBot, TRACE_PROJECT_NAME, expectedElementPath);
507 if (testViews) {
508 testViews(tmfEd);
509 }
510
511 fBot.closeAllEditors();
512
513 SWTBotUtils.clearExperimentFolder(fBot, TRACE_PROJECT_NAME);
514 if (clearTraces) {
515 SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
516 }
517 }
518
519 private void testImportAndExtractArchives(int options, boolean testViews, boolean fromArchive) throws Exception {
520 String expectedSourceLocation;
521 IPath expectedElementPath;
522 String testArchivePath = null;
523 if (fromArchive) {
524 testArchivePath = createNestedArchive();
525 openImportWizard();
526 selectImportFromArchive(testArchivePath);
527 selectFile(ARCHIVE_FILE_NAME, ARCHIVE_ROOT_ELEMENT_NAME, TRACE_PROJECT_NAME, TRACE_FOLDER_PARENT_NAME);
528
529 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
530 + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR;
531 expectedElementPath = new Path(TRACE_PROJECT_NAME).append(TRACE_FOLDER_PARENT_NAME).append(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME);
532 } else {
533 openImportWizard();
534 selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH);
535 selectFile(ARCHIVE_FILE_NAME, TRACE_FOLDER_PARENT_NAME);
536 expectedElementPath = new Path(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME);
537 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;
538 }
539
540 if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) == 0) {
541 expectedElementPath = new Path(TRACE_NAME);
542 }
543
544 setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
545 importFinish();
546 // Archives should never be imported as links
547 int expectedOptions = options & ~ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE;
548 checkOptions(expectedOptions, expectedSourceLocation, expectedElementPath);
549
550 TmfEventsEditor editor = SWTBotUtils.openEditor(fBot, TRACE_PROJECT_NAME, expectedElementPath);
551 if (testViews) {
552 testViews(editor);
553 }
554
555 SWTBotUtils.clearExperimentFolder(fBot, TRACE_PROJECT_NAME);
556
557 SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
558 if (testArchivePath != null) {
559 Files.delete(Paths.get(testArchivePath));
560 }
561 }
562
563 /**
564 * Create a temporary archive containing a nested archive. For example,
565 * testtraces.zip/synctraces.tar.gz can be used to test a nested archive.
566 */
567 private static String createNestedArchive() throws IOException, CoreException, URISyntaxException {
568 // Link to the test traces folder. We use a link so that we can safely
569 // delete the entire project when we are done.
570 IProject project = getProjectResource();
571 String canonicalPath = new File(TRACE_FOLDER_PARENT_PATH).getCanonicalPath();
572 IFolder folder = project.getFolder(TRACE_FOLDER_PARENT_NAME);
573 folder.createLink(new Path(canonicalPath), IResource.REPLACE, null);
574 IFile file = folder.getFile(ARCHIVE_FILE_NAME);
575 String archivePath = createArchive(file);
576 folder.delete(true, null);
577 return archivePath;
578 }
579
580 /**
581 * Create the empty archive from an empty folder
582 */
583 private static String createEmptyArchive() throws CoreException, URISyntaxException {
584 IFolder tempEmptyDirectory = createEmptyDirectory();
585 String archivePath = createArchive(tempEmptyDirectory);
586 tempEmptyDirectory.delete(true, null);
587 return archivePath;
588 }
589
590 private static IFolder createEmptyDirectory() throws CoreException {
591 IProject project = getProjectResource();
592 IFolder folder = project.getFolder(EMPTY_ARCHIVE_FOLDER);
593 folder.create(true, true, null);
594 return folder;
595 }
596
597 private static void createEmptyFile(IFolder folder) throws CoreException {
598 // Create empty file
599 IFile file = folder.getFile(EMPTY_FILE_NAME);
600 file.create(new ByteArrayInputStream(new byte[0]), true, null);
601 }
602
603 /**
604 * Create a temporary archive from the specified resource.
605 */
606 private static String createArchive(IResource sourceResource) throws URISyntaxException {
607 IPath exportedPath = sourceResource.getFullPath();
608
609 SWTBotTreeItem traceFilesProject = SWTBotUtils.selectProject(fBot, TRACE_PROJECT_NAME);
610 traceFilesProject.contextMenu("Export...").click();
611
612 fBot.waitUntil(Conditions.shellIsActive("Export"));
613 SWTBotShell activeShell = fBot.activeShell();
614 SWTBotTree exportWizardsTree = fBot.tree();
615 SWTBotTreeItem treeItem = SWTBotUtils.getTreeItem(fBot, exportWizardsTree, "General", "Archive File");
616 treeItem.select();
617 fBot.button("Next >").click();
618 fBot.button("&Deselect All").click();
619 try {
620 String resolveLinkedResLabel = "Resolve and export linked resources";
621 fBot.waitUntil(Conditions.waitForWidget(withMnemonic(resolveLinkedResLabel)), 100);
622 fBot.checkBox(resolveLinkedResLabel).select();
623 } catch (TimeoutException e) {
624 // Ignore, doesn't exist pre-4.6M5
625 }
626
627 if (sourceResource instanceof IFile) {
628 String[] folderPath = exportedPath.removeLastSegments(1).segments();
629 String fileName = exportedPath.lastSegment();
630 selectFile(fileName, folderPath);
631 } else {
632 selectFolder(exportedPath.segments());
633 }
634
635 String workspacePath = URIUtil.toFile(URIUtil.fromString(System.getProperty("osgi.instance.area"))).getAbsolutePath();
636 final String archiveDestinationPath = workspacePath + File.separator + TRACE_PROJECT_NAME + File.separator + GENERATED_ARCHIVE_NAME;
637 fBot.comboBox().setText(archiveDestinationPath);
638 fBot.button("&Finish").click();
639 fBot.waitUntil(Conditions.shellCloses(activeShell));
640 return archiveDestinationPath;
641 }
642
643 private void testViews(TmfEventsEditor editor) {
644 testHistogramView(getViewPart("Histogram"), editor);
645 testPropertyView(getViewPart("Properties"));
646 testStatisticsView(getViewPart("Statistics"));
647 }
648
649 private static void openImportWizard() {
650 fWizard = new ImportTraceWizard();
651
652 UIThreadRunnable.asyncExec(new VoidResult() {
653 @Override
654 public void run() {
655 final IWorkbench workbench = PlatformUI.getWorkbench();
656 // Fire the Import Trace Wizard
657 if (workbench != null) {
658 final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
659 Shell shell = activeWorkbenchWindow.getShell();
660 assertNotNull(shell);
661 ((ImportTraceWizard) fWizard).init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY);
662 WizardDialog dialog = new WizardDialog(shell, fWizard);
663 dialog.open();
664 }
665 }
666 });
667
668 fBot.waitUntil(ConditionHelpers.isWizardReady(fWizard));
669 }
670
671 private static void selectImportFromDirectory(String directoryPath) {
672 SWTBotRadio button = fBot.radio("Select roo&t directory:");
673 button.click();
674
675 SWTBotCombo sourceCombo = fBot.comboBox();
676 File traceFolderParent = new File(directoryPath);
677 sourceCombo.setText(traceFolderParent.getAbsolutePath());
678
679 SWTBotText text = fBot.text();
680 text.setFocus();
681 }
682
683 private static void selectImportFromArchive(String archivePath) {
684 SWTBotRadio button = fBot.radio("Select &archive file:");
685 button.click();
686
687 SWTBotCombo sourceCombo = fBot.comboBox(1);
688
689 sourceCombo.setText(new File(archivePath).getAbsolutePath());
690
691 SWTBotText text = fBot.text();
692 text.setFocus();
693 }
694
695 private static void selectFolder(String... treePath) {
696 selectFolder(true, treePath);
697 }
698
699 private static void selectFolder(boolean check, String... treePath) {
700 SWTBotTree tree = fBot.tree();
701 fBot.waitUntil(Conditions.widgetIsEnabled(tree));
702 SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(fBot, tree, treePath);
703 if (check) {
704 folderNode.check();
705 } else {
706 folderNode.select();
707 }
708 }
709
710 private static void selectFile(String fileName, String... folderTreePath) {
711 selectFolder(false, folderTreePath);
712
713 SWTBotTable fileTable = fBot.table();
714 fBot.waitUntil(Conditions.widgetIsEnabled(fileTable));
715 fBot.waitUntil(ConditionHelpers.isTableItemAvailable(fileName, fileTable));
716 SWTBotTableItem tableItem = fileTable.getTableItem(fileName);
717 tableItem.check();
718 }
719
720 private static void setOptions(int optionFlags, String traceTypeName) {
721 SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace);
722 if (checkBox.isEnabled()) {
723 if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0) {
724 checkBox.select();
725 } else {
726 checkBox.deselect();
727 }
728 }
729
730 checkBox = fBot.checkBox(Messages.ImportTraceWizard_PreserveFolderStructure);
731 if ((optionFlags & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) {
732 checkBox.select();
733 } else {
734 checkBox.deselect();
735 }
736
737 checkBox = fBot.checkBox(Messages.ImportTraceWizard_ImportUnrecognized);
738 if ((optionFlags & ImportTraceWizardPage.OPTION_IMPORT_UNRECOGNIZED_TRACES) != 0) {
739 checkBox.select();
740 } else {
741 checkBox.deselect();
742 }
743
744 checkBox = fBot.checkBox(Messages.ImportTraceWizard_OverwriteExistingTrace);
745 if ((optionFlags & ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES) != 0) {
746 checkBox.select();
747 } else {
748 checkBox.deselect();
749 }
750
751 checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateExperiment);
752 if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT) != 0) {
753 checkBox.select();
754 } else {
755 checkBox.deselect();
756 }
757
758 SWTBotCombo comboBox = fBot.comboBoxWithLabel(Messages.ImportTraceWizard_TraceType);
759 if (traceTypeName != null && !traceTypeName.isEmpty()) {
760 comboBox.setSelection(traceTypeName);
761 } else {
762 comboBox.setSelection(ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
763 }
764 }
765
766 private static void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath) throws CoreException {
767 checkOptions(optionFlags, expectedSourceLocation, expectedElementPath, null, null);
768 }
769
770 private static void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath, String experimentName, IPath expectedElementPathRenamed) throws CoreException {
771 IProject project = getProjectResource();
772 assertTrue(project.exists());
773 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true);
774 assertNotNull(tmfProject);
775 TmfTraceFolder tracesFolder = tmfProject.getTracesFolder();
776 assertNotNull(tracesFolder);
777 List<TmfTraceElement> traces = tracesFolder.getTraces();
778 assertFalse(traces.isEmpty());
779 Collections.sort(traces, new Comparator<TmfTraceElement>() {
780 @Override
781 public int compare(TmfTraceElement arg0, TmfTraceElement arg1) {
782 return arg0.getElementPath().compareTo(arg1.getElementPath());
783 }
784 });
785
786 TmfTraceElement tmfTraceElement = traces.get(0);
787 IResource traceResource = tmfTraceElement.getResource();
788
789 assertEquals((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0, traceResource.isLinked());
790
791 // i.e. /Tracing/Traces
792 IPath expectedPath = Path.ROOT.append(new Path(TRACE_PROJECT_NAME)).append(TmfTracesFolder.TRACES_FOLDER_NAME).append(expectedElementPath);
793 assertEquals(expectedPath, traceResource.getFullPath());
794
795 if (expectedElementPathRenamed != null) {
796 IPath expectedPathRenamed = Path.ROOT.append(new Path(TRACE_PROJECT_NAME)).append(TmfTracesFolder.TRACES_FOLDER_NAME).append(expectedElementPathRenamed);
797 IResource traceResourceRenamed = traces.get(1).getResource();
798 assertEquals(expectedPathRenamed, traceResourceRenamed.getFullPath());
799 }
800
801 String sourceLocation = traceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION);
802 assertNotNull(sourceLocation);
803 assertEquals(expectedSourceLocation, sourceLocation);
804
805 TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
806 assertNotNull(expFolder);
807 if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT) != 0) {
808 if (experimentName != null) {
809 TmfExperimentElement expElement = expFolder.getExperiment(experimentName);
810 assertNotNull(expElement);
811 assertEquals(2, expElement.getTraces().size());
812 }
813 } else {
814 assertTrue(expFolder.getExperiments().size() == 0);
815 }
816 }
817
818 private static IProject getProjectResource() {
819 return ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME);
820 }
821
822 private @NonNull String verifyExperimentNameHandling(String aExperimentName) {
823 String experimentName = aExperimentName;
824
825 // experiment already exists
826 checkFinishButton(false);
827
828 SWTBotText expText = fBot.textInGroup("Options");
829
830 // Invalid experiment name (only whitespaces)
831 expText.setText(String.valueOf(' '));
832 checkFinishButton(false);
833
834 // Invalid experiment name
835 expText.setText(String.valueOf('/'));
836 checkFinishButton(false);
837
838 // Set valid experiment name
839 experimentName += '_';
840 expText.setText(experimentName);
841 return experimentName;
842 }
843
844 }
This page took 0.051335 seconds and 5 git commands to generate.