tmf: Add SwtBot tests for importing as experiment
authorBernd Hufmann <Bernd.Hufmann@ericsson.com>
Wed, 9 Mar 2016 19:18:25 +0000 (14:18 -0500)
committerBernd Hufmann <bernd.hufmann@ericsson.com>
Fri, 11 Mar 2016 20:49:50 +0000 (15:49 -0500)
The following test cases are added:
- create experiment when importing from directory
- create experiment when importing from archive
- create experiment during import with experiment name validation

Utility methods are added to SwtBotUtils to create an experiment and
to clear the experiment folder.

Change-Id: I3fc5f83efc811f267cfc31a48c603915c9d74646
Signed-off-by: Bernd Hufmann <Bernd.Hufmann@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/68083
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
ctf/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/META-INF/MANIFEST.MF
ctf/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ctf/ui/swtbot/tests/AbstractImportAndReadSmokeTest.java
ctf/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ctf/ui/swtbot/tests/StandardImportAndReadSmokeTest.java
tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java

index 27f6e4acc8c239e55f9db4f572201dc795dab124..f4a16e3c39f6aab19d834af0c7a966d7d8ffd7ff 100644 (file)
@@ -14,11 +14,12 @@ Require-Bundle: org.junit;bundle-version="4.0.0",
  org.eclipse.ui,
  org.eclipse.ui.ide,
  org.eclipse.ui.views,
- org.eclipse.tracecompass.tmf.core,
- org.eclipse.tracecompass.tmf.ui,
+ org.eclipse.tracecompass.tmf.core;bundle-version="2.0.0",
+ org.eclipse.tracecompass.tmf.ui;bundle-version="2.0.0",
  org.eclipse.tracecompass.tmf.ui.swtbot.tests,
- org.eclipse.tracecompass.tmf.ctf.core,
- org.eclipse.tracecompass.tmf.ctf.core.tests
+ org.eclipse.tracecompass.tmf.ctf.core;bundle-version="2.0.0",
+ org.eclipse.tracecompass.tmf.ctf.core.tests,
+ org.eclipse.tracecompass.common.core;bundle-version="2.0.0"
 Import-Package: org.apache.log4j,
  org.apache.log4j.varia,
  org.eclipse.swtbot.eclipse.finder,
index 411ef668be0e32c1d0c0cc243a25a32a517ed9ac..4a6a63e3901154ff5c990deff812d2ad91545a17 100644 (file)
@@ -14,6 +14,7 @@
 package org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests;
 
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.util.List;
 
@@ -155,6 +156,18 @@ public abstract class AbstractImportAndReadSmokeTest {
         SWTBotUtils.waitForJobs();
     }
 
+    /**
+     * Checks finish button enablement
+     *
+     * @param isEnabled
+     *            state to check against
+     *
+     */
+    protected void checkFinishButton(boolean isEnabled) {
+        final SWTBotButton finishButton = fBot.button("Finish");
+        assertTrue(finishButton.isEnabled() == isEnabled);
+    }
+
     // ---------------------------------------------
     // Helpers for testing views
     // ---------------------------------------------
index 7e6275e912315c80bef2450f6db66ea12d8b40ee..7be0215c63749cc92bf7c24164e37385e7f596d9 100644 (file)
@@ -14,6 +14,7 @@
 package org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests;
 
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -38,6 +39,7 @@ import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.URIUtil;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.widgets.Shell;
@@ -60,6 +62,8 @@ import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Impo
 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages;
 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
@@ -133,6 +137,28 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
         testImport(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE, false, false);
     }
 
+    /**
+     * Test import from directory, create experiment
+     *
+     * @throws Exception
+     *             on error
+     */
+    @Test
+    public void testImportWithExperiment() throws Exception {
+        testImport(ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, false);
+    }
+
+    /**
+     * Test import from directory, create experiment (validate experiment name)
+     *
+     * @throws Exception
+     *             on error
+     */
+    @Test
+    public void testImportWithExperimentValidation() throws Exception {
+        testImport(ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, false, false);
+    }
+
     /**
      * Test import from directory, preserve folder structure
      *
@@ -179,6 +205,17 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
         testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true, true);
     }
 
+    /**
+     * Test import from archive, create Experiment
+     *
+     * @throws Exception
+     *             on error
+     */
+    @Test
+    public void testImportFromArchiveWithExperiment() throws Exception {
+        testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE | ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, true);
+    }
+
     /**
      * Test import from directory, preserve folder structure
      * @throws Exception on error
@@ -389,7 +426,23 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
     }
 
     private void testImport(int options, boolean testViews, boolean fromArchive) throws Exception {
+        testImport(options, testViews, fromArchive, true);
+    }
+
+    private void testImport(int options, boolean testViews, boolean fromArchive, boolean defaultExperiment) throws Exception {
         String expectedSourceLocation = null;
+
+        @NonNull String experimentName;
+        if (fromArchive) {
+            experimentName = checkNotNull(new Path(ARCHIVE_FILE_NAME).lastSegment());
+        } else {
+            experimentName = checkNotNull(new Path(TRACE_FOLDER_PARENT_PATH).lastSegment());
+        }
+
+        if (!defaultExperiment) {
+            SWTBotUtils.createExperiment(fBot, TRACE_PROJECT_NAME, experimentName);
+        }
+
         openImportWizard();
         if (fromArchive) {
             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;
@@ -405,6 +458,12 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
         }
 
         setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
+
+        if (!defaultExperiment) {
+            experimentName = verifyExperimentNameHandling(experimentName);
+        }
+        checkFinishButton(true);
+
         importFinish();
 
         IPath expectedElementPath = new Path(TRACE_NAME);
@@ -412,7 +471,7 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
             expectedElementPath = new Path(TRACE_FOLDER).append(expectedElementPath);
         }
 
-        checkOptions(options, expectedSourceLocation, expectedElementPath);
+        checkOptions(options, expectedSourceLocation, expectedElementPath, experimentName);
         TmfEventsEditor tmfEd = SWTBotUtils.openEditor(fBot, TRACE_PROJECT_NAME, expectedElementPath);
         if (testViews) {
             testViews(tmfEd);
@@ -420,6 +479,7 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
 
         fBot.closeAllEditors();
 
+        SWTBotUtils.clearExperimentFolder(fBot, TRACE_PROJECT_NAME);
         SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
     }
 
@@ -459,6 +519,8 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
             testViews(editor);
         }
 
+        SWTBotUtils.clearExperimentFolder(fBot, TRACE_PROJECT_NAME);
+
         SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
         if (testArchivePath != null) {
             Files.delete(Paths.get(testArchivePath));
@@ -653,6 +715,13 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
             checkBox.deselect();
         }
 
+        checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateExperiment);
+        if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT) != 0) {
+            checkBox.select();
+        } else {
+            checkBox.deselect();
+        }
+
         SWTBotCombo comboBox = fBot.comboBoxWithLabel(Messages.ImportTraceWizard_TraceType);
         if (traceTypeName != null && !traceTypeName.isEmpty()) {
             comboBox.setSelection(traceTypeName);
@@ -662,6 +731,10 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
     }
 
     private static void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath) throws CoreException {
+        checkOptions(optionFlags, expectedSourceLocation, expectedElementPath, null);
+    }
+
+    private static void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath, String experimentName) throws CoreException {
         IProject project = getProjectResource();
         assertTrue(project.exists());
         TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true);
@@ -689,9 +762,44 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe
         String sourceLocation = traceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION);
         assertNotNull(sourceLocation);
         assertEquals(expectedSourceLocation, sourceLocation);
+
+        TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
+        assertNotNull(expFolder);
+        if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT) != 0) {
+            if (experimentName != null) {
+                TmfExperimentElement expElement = expFolder.getExperiment(experimentName);
+                assertNotNull(expElement);
+                assertEquals(2, expElement.getTraces().size());
+            }
+        } else {
+            assertTrue(expFolder.getExperiments().size() == 0);
+        }
     }
 
     private static IProject getProjectResource() {
         return ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME);
     }
+
+    private @NonNull String verifyExperimentNameHandling(String aExperimentName) {
+        String experimentName = aExperimentName;
+
+        // experiment already exists
+        checkFinishButton(false);
+
+        SWTBotText expText = fBot.textInGroup("Options");
+
+        // Invalid experiment name (only whitespaces)
+        expText.setText(String.valueOf(' '));
+        checkFinishButton(false);
+
+        // Invalid experiment name
+        expText.setText(String.valueOf('/'));
+        checkFinishButton(false);
+
+        // Set valid experiment name
+        experimentName += '_';
+        expText.setText(experimentName);
+        return experimentName;
+    }
+
 }
index 652c4594034364cb3d209fce66e3c56310e7d235..29842d9393a38604a0446ba4c8d92d664bec3f16 100644 (file)
@@ -28,6 +28,7 @@ import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jface.bindings.keys.IKeyLookup;
 import org.eclipse.jface.bindings.keys.KeyStroke;
 import org.eclipse.jface.bindings.keys.ParseException;
@@ -56,7 +57,10 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
 import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;
+import org.eclipse.tracecompass.internal.tmf.ui.project.operations.NewExperimentOperation;
 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
@@ -78,6 +82,7 @@ import org.hamcrest.Matcher;
  *
  * @author Matthew Khouzam
  */
+@SuppressWarnings("restriction")
 public final class SWTBotUtils {
 
     private static final String WINDOW_MENU = "Window";
@@ -188,6 +193,39 @@ public final class SWTBotUtils {
         deleteProject(projectName, true, bot);
     }
 
+    /**
+     * Creates an experiment
+     *
+     * @param bot
+     *            a given workbench bot
+     * @param projectName
+     *            the name of the project, creates the project if needed
+     * @param expName
+     *            the experiment name
+     */
+    public static void createExperiment(SWTWorkbenchBot bot, String projectName, final @NonNull String expName) {
+        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+        TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true);
+        TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
+        assertNotNull(expFolder);
+        NewExperimentOperation operation = new NewExperimentOperation(expFolder, expName);
+        operation.run(new NullProgressMonitor());
+
+        bot.waitUntil(new DefaultCondition() {
+            @Override
+            public boolean test() throws Exception {
+                TmfExperimentElement experiment = expFolder.getExperiment(expName);
+                return experiment != null;
+            }
+
+            @Override
+            public String getFailureMessage() {
+                return "Experiment (" + expName + ") couldn't be created";
+            }
+        });
+    }
+
+
     /**
      * Focus on the main window
      *
@@ -544,7 +582,53 @@ public final class SWTBotUtils {
                 return "Traces Folder not empty (" + fTraceNb + ")";
             }
         });
+    }
+
+    /**
+     * Clear the experiment folder
+     *
+     * @param bot
+     *            a given workbench bot
+     * @param projectName
+     *            the name of the project (needs to exist)
+     */
+    public static void clearExperimentFolder(SWTWorkbenchBot bot, String projectName) {
+        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+        TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, false);
+        TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
+        expFolder.getExperiments().forEach(experiment -> {
+            IResource resource = experiment.getResource();
+            try {
+                // Close the experiment if open
+                experiment.closeEditors();
+
+                IPath path = resource.getLocation();
+                if (path != null) {
+                    // Delete supplementary files
+                    experiment.deleteSupplementaryFolder();
+                }
+                // Finally, delete the experiment
+                resource.delete(true, null);
+            } catch (CoreException e) {
+                fail(e.getMessage());
+            }
+        });
+
+        bot.waitUntil(new DefaultCondition() {
+            private int fExperimentNb = 0;
 
+            @Override
+            public boolean test() throws Exception {
+                List<TmfExperimentElement> experiments = expFolder.getExperiments();
+                fExperimentNb = experiments.size();
+                return fExperimentNb == 0;
+            }
+
+            @Override
+            public String getFailureMessage() {
+                return "Experiment Folder not empty (" + fExperimentNb + ")";
+            }
+        });
     }
 
     /**
This page took 0.033104 seconds and 5 git commands to generate.