Bug 520148: [UnitTest] Unittest framework v2.0

  added context menu to re-run tests

Change-Id: I3111ade2e1288fb7812a52bd0a1e5330f80bf811
diff --git a/plugins/org.eclipse.ease.lang.unittest.ui/plugin.xml b/plugins/org.eclipse.ease.lang.unittest.ui/plugin.xml
index c7350b3..446d8a8 100644
--- a/plugins/org.eclipse.ease.lang.unittest.ui/plugin.xml
+++ b/plugins/org.eclipse.ease.lang.unittest.ui/plugin.xml
@@ -150,6 +150,16 @@
                tooltip="Create a test report">
          </command>
       </menuContribution>
+      <menuContribution
+            allPopups="false"
+            locationURI="popup:org.eclipse.ease.views.unittest">
+         <command
+               commandId="org.eclipse.ease.lang.unittest.ui.commands.runSelectedTests"
+               icon="icons/elcl16/run_all_tests.png"
+               label="Run"
+               style="push">
+         </command>
+      </menuContribution>
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
@@ -203,6 +213,28 @@
          </enabledWhen>
       </handler>
       <handler
+            class="org.eclipse.ease.lang.unittest.ui.handlers.RunSelectedTests"
+            commandId="org.eclipse.ease.lang.unittest.ui.commands.runSelectedTests">
+         <enabledWhen>
+            <with
+                  variable="org.eclipse.ease.lang.unittest.ui.testsuite">
+               <and>
+                  <test
+                        forcePluginActivation="true"
+                        property="org.eclipse.ease.lang.unittest.ui.testsuite.exists">
+                  </test>
+                  <not>
+                     <test
+                           forcePluginActivation="true"
+                           property="org.eclipse.ease.lang.unittest.ui.testsuite.status"
+                           value="RUNNING">
+                     </test>
+                  </not>
+               </and>
+            </with>
+         </enabledWhen>
+      </handler>
+      <handler
             class="org.eclipse.ease.lang.unittest.ui.handlers.TerminateTestSuite"
             commandId="org.eclipse.ease.lang.unittest.ui.commands.terminateTestSuite">
          <enabledWhen>
@@ -260,12 +292,6 @@
       </command>
       <command
             categoryId="org.eclipse.ease.lang.unittest.ui.category.unittest"
-            description="Run all tests"
-            id="org.eclipse.ease.lang.unittest.ui.commands.runAllTests"
-            name="Run All Tests">
-      </command>
-      <command
-            categoryId="org.eclipse.ease.lang.unittest.ui.category.unittest"
             defaultHandler="org.eclipse.ease.lang.unittest.ui.handlers.DebugTestSuiteFromEditor"
             description="Debug this Test Suite"
             id="org.eclipse.ease.lang.unittest.ui.commands.debugTestSuite"
@@ -273,6 +299,12 @@
       </command>
       <command
             categoryId="org.eclipse.ease.lang.unittest.ui.category.unittest"
+            description="Run all tests"
+            id="org.eclipse.ease.lang.unittest.ui.commands.runAllTests"
+            name="Run All Tests">
+      </command>
+      <command
+            categoryId="org.eclipse.ease.lang.unittest.ui.category.unittest"
             defaultHandler="org.eclipse.ease.lang.unittest.ui.handlers.AddFolderForVariablesHandler"
             description="Add a new folder"
             id="org.eclipse.ease.lang.unittest.ui.commands.addFolderForVariables"
@@ -320,6 +352,12 @@
             id="org.eclipse.ease.lang.unittest.ui.commands.createReport"
             name="Create Report">
       </command>
+      <command
+            categoryId="org.eclipse.ease.lang.unittest.ui.category.unittest"
+            description="Run selected tests"
+            id="org.eclipse.ease.lang.unittest.ui.commands.runSelectedTests"
+            name="Run Selected Tests">
+      </command>
    </extension>
    <extension
          point="org.eclipse.ui.views">
diff --git a/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunAllTests.java b/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunAllTests.java
index 17fcb8d..613774a 100644
--- a/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunAllTests.java
+++ b/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunAllTests.java
@@ -39,7 +39,7 @@
 				engine.setErrorStream(console.getErrorStream());
 				engine.setInputStream(console.getInputStream());
 
-				engine.executeAsync(getTestRoot(testRoot));
+				engine.executeAsync(getTestRoot(testRoot, event));
 				engine.schedule();
 			}
 		}
@@ -52,9 +52,11 @@
 	 *
 	 * @param testSuite
 	 *            test suite
+	 * @param event
+	 *            event that triggered this handler
 	 * @return object to be executed
 	 */
-	protected Object getTestRoot(ITestSuite testSuite) {
+	protected Object getTestRoot(ITestSuite testSuite, ExecutionEvent event) {
 		return testSuite;
 	}
 }
diff --git a/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunFailedTests.java b/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunFailedTests.java
index 09dd687..cf0fffd 100644
--- a/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunFailedTests.java
+++ b/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunFailedTests.java
@@ -14,6 +14,7 @@
 import java.util.Collection;
 import java.util.HashSet;
 
+import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.ease.lang.unittest.FilteredTestCommand;
 import org.eclipse.ease.lang.unittest.runtime.ITestContainer;
 import org.eclipse.ease.lang.unittest.runtime.ITestEntity;
@@ -33,7 +34,7 @@
 	}
 
 	@Override
-	protected Object getTestRoot(ITestSuite testSuite) {
+	protected Object getTestRoot(ITestSuite testSuite, ExecutionEvent event) {
 		return new FilteredTestCommand(testSuite, addTestsWithErrors(testSuite, new HashSet<>()));
 	}
 }
diff --git a/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunSelectedTests.java b/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunSelectedTests.java
new file mode 100644
index 0000000..616dcdf
--- /dev/null
+++ b/plugins/org.eclipse.ease.lang.unittest.ui/src/org/eclipse/ease/lang/unittest/ui/handlers/RunSelectedTests.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Christian Pontesegger and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Christian Pontesegger - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.ease.lang.unittest.ui.handlers;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.ease.lang.unittest.FilteredTestCommand;
+import org.eclipse.ease.lang.unittest.runtime.ITestContainer;
+import org.eclipse.ease.lang.unittest.runtime.ITestEntity;
+import org.eclipse.ease.lang.unittest.runtime.ITestSuite;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+public class RunSelectedTests extends RunAllTests {
+
+	@Override
+	protected Object getTestRoot(ITestSuite testSuite, ExecutionEvent event) {
+		final ISelection menuSelection = HandlerUtil.getActiveMenuSelection(event);
+		if (menuSelection instanceof IStructuredSelection) {
+			final List<ITestEntity> selectedTests = new ArrayList<>();
+			for (final Object element : ((IStructuredSelection) menuSelection).toList()) {
+				if (element instanceof ITestContainer) {
+					selectedTests.add((ITestEntity) element);
+					ITestContainer parent = ((ITestContainer) element).getParent();
+					while (parent != null) {
+						selectedTests.add(parent);
+						parent = parent.getParent();
+					}
+				}
+			}
+
+			return new FilteredTestCommand(testSuite, selectedTests);
+
+		} else
+			return new FilteredTestCommand(testSuite, Collections.emptySet());
+	}
+}
diff --git a/plugins/org.eclipse.ease.lang.unittest/src-gen/org/eclipse/ease/lang/unittest/runtime/impl/TestSuite.java b/plugins/org.eclipse.ease.lang.unittest/src-gen/org/eclipse/ease/lang/unittest/runtime/impl/TestSuite.java
index 1a27275..b8bbccb 100644
--- a/plugins/org.eclipse.ease.lang.unittest/src-gen/org/eclipse/ease/lang/unittest/runtime/impl/TestSuite.java
+++ b/plugins/org.eclipse.ease.lang.unittest/src-gen/org/eclipse/ease/lang/unittest/runtime/impl/TestSuite.java
@@ -13,6 +13,7 @@
 import org.eclipse.ease.lang.unittest.definition.ITestSuiteDefinition;
 import org.eclipse.ease.lang.unittest.execution.ITestExecutionStrategy;
 import org.eclipse.ease.lang.unittest.runtime.IRuntimePackage;
+import org.eclipse.ease.lang.unittest.runtime.ITest;
 import org.eclipse.ease.lang.unittest.runtime.ITestContainer;
 import org.eclipse.ease.lang.unittest.runtime.ITestEntity;
 import org.eclipse.ease.lang.unittest.runtime.ITestSuite;
@@ -289,7 +290,7 @@
 			// testsuite setup
 			runSetupTeardownCode(ITestSuiteDefinition.CODE_LOCATION_TESTSUITE_SETUP, strategy);
 
-			if (!hasError()) {
+			if (!hasSetupError()) {
 				// not calling super.run() here as we do want to have full control on the entityStatus
 				for (final Object child : getChildren().toArray()) {
 					if (!isTerminated()) {
@@ -318,6 +319,18 @@
 	/**
 	 * @generated NOT
 	 */
+	private boolean hasSetupError() {
+		for (final ITestEntity child : getChildren().toArray(new ITestEntity[0])) {
+			if ((child instanceof ITest) && (child.hasError()))
+				return true;
+		}
+
+		return false;
+	}
+
+	/**
+	 * @generated NOT
+	 */
 	private void runSetupTeardownCode(String codeLocation, ITestExecutionStrategy strategy) {
 		final ITestSuiteDefinition definition = getDefinition();
 		if (definition != null) {