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