1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 * Marc-Andre Laperle - Added tests for extracting archives during import
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ctf
.ui
.swtbot
.tests
;
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
;
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
;
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
;
71 * SWTBot Smoke test using ImportTraceWizard.
73 * @author Bernd Hufmann
75 @RunWith(SWTBotJunit4ClassRunner
.class)
76 public class StandardImportAndReadSmokeTest
extends AbstractImportAndReadSmokeTest
{
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";
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
: "";
93 * Test import from directory
99 public void testImportFromDirectory() throws Exception
{
100 testImport(0, false, false);
104 * Test import from directory, create links
110 public void testImportFromDirectoryLinks() throws Exception
{
111 testImport(ImportTraceWizardPage
.OPTION_CREATE_LINKS_IN_WORKSPACE
, false, false);
115 * Test import from directory, preserve folder structure
121 public void testImportFromDirectoryPreserveFolder() throws Exception
{
122 testImport(ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
, false, false);
126 * Test import from directory, create links, preserve folder structure
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);
138 * Test import from directory, overwrite all
144 public void testImportFromDirectoryOverwrite() throws Exception
{
145 testImport(0, false, false);
146 testImport(ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
, false, false);
150 * Test import from archive
156 public void testImportFromArchive() throws Exception
{
157 testImport(ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
, true, true);
161 * Test import from directory, preserve folder structure
162 * @throws Exception on error
165 public void testImportFromArchivePreserveFolder() throws Exception
{
166 testImport(ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
, false, true);
170 * Test import from directory, overwrite all
176 public void testImportFromArchiveOverwrite() throws Exception
{
177 testImport(0, false, true);
178 testImport(ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
, false, true);
182 * Test import from directory containing archives
188 public void testExtractArchivesFromDirectory() throws Exception
{
189 testImportAndExtractArchives(ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
, false);
193 * Test import from directory containing archives, create links
194 * @throws Exception on error
197 public void testExtractArchivesFromDirectoryLinks() throws Exception
{
198 testImportAndExtractArchives(ImportTraceWizardPage
.OPTION_CREATE_LINKS_IN_WORKSPACE
| ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
, false);
202 * Test import from directory containing archives, create links, preserve folder structure
203 * @throws Exception on error
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);
211 * Test import from archive containing archives
217 public void testExtractArchivesFromArchive() throws Exception
{
218 testImportAndExtractArchives(ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
, true);
222 * Test import from archive containing archives, preserve folder structure
228 public void testExtractArchivesFromArchivePreserveFolder() throws Exception
{
229 testImportAndExtractArchives(ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
| ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
, true);
232 private void testImport(int options
, boolean testViews
, boolean fromArchive
) throws Exception
{
234 String expectedSourceLocation
= null;
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());
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
});
249 setOptions(options
, ImportTraceWizardPage
.TRACE_TYPE_AUTO_DETECT
);
252 IPath expectedElementPath
= new Path(TRACE_NAME
);
253 if ((options
& ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
) != 0) {
254 expectedElementPath
= new Path(TRACE_FOLDER
).append(expectedElementPath
);
257 checkOptions(options
, expectedSourceLocation
, expectedElementPath
);
258 TmfEventsEditor tmfEd
= SWTBotUtils
.openEditor(fBot
, getProjectName(), expectedElementPath
);
263 fBot
.closeAllEditors();
265 SWTBotUtils
.deleteProject(getProjectName(), fBot
);
268 private void testImportAndExtractArchives(int options
, boolean fromArchive
) throws Exception
{
271 String expectedSourceLocation
;
272 IPath expectedElementPath
;
274 String testArchivePath
= createArchive();
276 selectImportFromArchive(testArchivePath
);
277 selectFile(ARCHIVE_FILE_NAME
, ARCHIVE_ROOT_ELEMENT_NAME
, TRACE_PROJECT_NAME
, TRACE_FOLDER_PARENT_NAME
);
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
);
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
;
290 if ((options
& ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
) == 0) {
291 expectedElementPath
= new Path(TRACE_NAME
);
294 setOptions(options
, ImportTraceWizardPage
.TRACE_TYPE_AUTO_DETECT
);
296 // Archives should never be imported as links
297 int expectedOptions
= options
& ~ImportTraceWizardPage
.OPTION_CREATE_LINKS_IN_WORKSPACE
;
298 checkOptions(expectedOptions
, expectedSourceLocation
, expectedElementPath
);
300 TmfEventsEditor editor
= SWTBotUtils
.openEditor(fBot
, TRACE_PROJECT_NAME
, expectedElementPath
);
303 SWTBotUtils
.deleteProject(getProjectName(), fBot
);
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.
310 private String
createArchive() throws URISyntaxException
, CoreException
, IOException
{
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);
319 SWTBotTreeItem traceFilesProject
= SWTBotUtils
.selectProject(fBot
, TRACE_PROJECT_NAME
);
320 traceFilesProject
.contextMenu("Export...").click();
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");
327 fBot
.button("Next >").click();
328 fBot
.button("&Deselect All").click();
329 selectFile(ARCHIVE_FILE_NAME
, TRACE_PROJECT_NAME
, TRACE_FOLDER_PARENT_NAME
);
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
;
339 private void testViews(TmfEventsEditor editor
) {
340 testHistogramView(getViewPart("Histogram"), editor
);
341 testPropertyView(getViewPart("Properties"));
342 testStatisticsView(getViewPart("Statistics"));
345 private static void openImportWizard() {
346 fWizard
= new ImportTraceWizard();
348 UIThreadRunnable
.asyncExec(new VoidResult() {
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
);
364 fBot
.waitUntil(ConditionHelpers
.isWizardReady(fWizard
));
367 private static void selectImportFromDirectory(String directoryPath
) {
368 SWTBotRadio button
= fBot
.radio("Select roo&t directory:");
371 SWTBotCombo sourceCombo
= fBot
.comboBox();
372 File traceFolderParent
= new File(directoryPath
);
373 sourceCombo
.setText(traceFolderParent
.getAbsolutePath());
375 SWTBotText text
= fBot
.text();
379 private static void selectImportFromArchive(String archivePath
) {
380 SWTBotRadio button
= fBot
.radio("Select &archive file:");
383 SWTBotCombo sourceCombo
= fBot
.comboBox(1);
385 sourceCombo
.setText(new File(archivePath
).getAbsolutePath());
387 SWTBotText text
= fBot
.text();
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
);
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
);
404 SWTBotTable fileTable
= fBot
.table();
405 fBot
.waitUntil(Conditions
.widgetIsEnabled(fileTable
));
406 fBot
.waitUntil(ConditionHelpers
.isTableItemAvailable(fileName
, fileTable
));
407 SWTBotTableItem tableItem
= fileTable
.getTableItem(fileName
);
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) {
421 checkBox
= fBot
.checkBox(Messages
.ImportTraceWizard_PreserveFolderStructure
);
422 if ((optionFlags
& ImportTraceWizardPage
.OPTION_PRESERVE_FOLDER_STRUCTURE
) != 0) {
428 checkBox
= fBot
.checkBox(Messages
.ImportTraceWizard_ImportUnrecognized
);
429 if ((optionFlags
& ImportTraceWizardPage
.OPTION_IMPORT_UNRECOGNIZED_TRACES
) != 0) {
435 checkBox
= fBot
.checkBox(Messages
.ImportTraceWizard_OverwriteExistingTrace
);
436 if ((optionFlags
& ImportTraceWizardPage
.OPTION_OVERWRITE_EXISTING_RESOURCES
) != 0) {
442 SWTBotCombo comboBox
= fBot
.comboBoxWithLabel(Messages
.ImportTraceWizard_TraceType
);
443 if (traceTypeName
!= null && !traceTypeName
.isEmpty()) {
444 comboBox
.setSelection(traceTypeName
);
446 comboBox
.setSelection(ImportTraceWizardPage
.TRACE_TYPE_AUTO_DETECT
);
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
>() {
461 public int compare(TmfTraceElement arg0
, TmfTraceElement arg1
) {
462 return arg0
.getElementPath().compareTo(arg1
.getElementPath());
466 TmfTraceElement tmfTraceElement
= traces
.get(0);
467 IResource traceResource
= tmfTraceElement
.getResource();
469 assertEquals((optionFlags
& ImportTraceWizardPage
.OPTION_CREATE_LINKS_IN_WORKSPACE
) != 0, traceResource
.isLinked());
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());
475 String sourceLocation
= traceResource
.getPersistentProperty(TmfCommonConstants
.SOURCE_LOCATION
);
476 assertNotNull(sourceLocation
);
477 assertEquals(expectedSourceLocation
, sourceLocation
);
481 protected String
getProjectName() {
482 return TRACE_PROJECT_NAME
;
485 private IProject
getProjectResource() {
486 return ResourcesPlugin
.getWorkspace().getRoot().getProject(getProjectName());