Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoritz Eysholdt2014-09-10 13:53:59 +0000
committerMarkus Keller2014-11-14 19:47:21 +0000
commit3b64020e89d0cefe9dca260edf6f9f633a03f4e9 (patch)
tree6a3765cdfb6f9e21d19ae78cc600c906deed77ef
parent9ff202fc64cd812e095e02606144c01e7281dd36 (diff)
downloadeclipse.jdt.ui-3b64020e89d0cefe9dca260edf6f9f633a03f4e9.tar.gz
eclipse.jdt.ui-3b64020e89d0cefe9dca260edf6f9f633a03f4e9.tar.xz
eclipse.jdt.ui-3b64020e89d0cefe9dca260edf6f9f633a03f4e9.zip
Bug 443498 run subtrees of junit tests or a Java method when using custom runners
For example, for the following tree of tests that you might get from JUnit’s ‘Parameterized’ runner —————————————- MyTestClass [0] testMethod[0] testMethod2[0] [1] testMethod[1] testMethod2[1] —————————————- it is now possible to run only —————————————- [1] testMethod[1] testMethod2[1] —————————————- by clicking on the “run” action on “[1]” in the JUnit View. Furthermore, it is possible to execute —————————————- MyTestClass [0] testMethod2[0] [1] testMethod2[1] —————————————- by choosing “run as” -> “JUnit Test” on the Java Method “testMethod2”. To make this possible, this contribution contains the following changes: - if a test suite (e.g. “[1]”) has no class, the containers test class is executed and the suite’s name is passed to the runner as the testName. The test runner will then filter JUnit’s tree of test descriptions accordingly. - The runner now combines three strategies to filter test descriptions: - If the test name is specified as “foo (bar)” (this is JUnit’s formant), the test description that is an exact match will be executed. - If the test name is “foo”, the runner will execute suites with the name “foo” and methods that have the name “foo” and are in the root tests’s class. - in addition to the two cases above, and if foo is a valid Java identifier, all test cases will be matched if the leading part of their description’s name that is also a Java identifier, equals foo. These changes are not specific to the 'Parameterized' runner and are intend to work with all custom runners. For the method-based filtering, however, a runner needs to prefix test descriptions the method name. Bugzilla reference: https://bugs.eclipse.org/bugs/show_bug.cgi?id=443498 Change-Id: I8c9ade21374b922c4d440f142d9592d72e062862 Signed-off-by: Moritz Eysholdt <moritz.eysholdt@itemis.de>
-rw-r--r--org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnitLaunchConfigurationConstants.java9
-rw-r--r--org.eclipse.jdt.junit.core/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationDelegate.java2
-rw-r--r--org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestRunnerViewPart.java2
-rw-r--r--org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java53
-rw-r--r--org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationTab.java6
-rw-r--r--org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchShortcut.java109
-rw-r--r--org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/DescriptionMatcher.java165
-rw-r--r--org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestClassReference.java69
-rw-r--r--org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestLoader.java55
-rw-r--r--org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestMethodReference.java68
-rw-r--r--org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestReference.java72
-rw-r--r--org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/SubForestFilter.java76
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/AbstractTestRunListenerTest.java16
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/JUnitJUnitTests.java3
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredParameterizedRunnerTest4.java161
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredStandardRunnerTest4.java92
16 files changed, 763 insertions, 195 deletions
diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnitLaunchConfigurationConstants.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnitLaunchConfigurationConstants.java
index af5f10da0f..c96848ca9f 100644
--- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnitLaunchConfigurationConstants.java
+++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnitLaunchConfigurationConstants.java
@@ -40,9 +40,14 @@ public class JUnitLaunchConfigurationConstants {
public static final String ATTR_PORT= JUnitCorePlugin.PLUGIN_ID+".PORT"; //$NON-NLS-1$
/**
- * The test method, or "" iff running the whole test type.
+ * The test name, or "" iff running the whole test type.
*/
- public static final String ATTR_TEST_METHOD_NAME= JUnitCorePlugin.PLUGIN_ID+".TESTNAME"; //$NON-NLS-1$
+ public static final String ATTR_TEST_NAME= JUnitCorePlugin.PLUGIN_ID + ".TESTNAME"; //$NON-NLS-1$
+
+ /**
+ * @Deprecated use {@link #ATTR_TEST_NAME}
+ **/
+ public static final String ATTR_TEST_METHOD_NAME= ATTR_TEST_NAME;
public static final String ATTR_KEEPRUNNING = JUnitCorePlugin.PLUGIN_ID+ ".KEEPRUNNING_ATTR"; //$NON-NLS-1$
/**
diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationDelegate.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationDelegate.java
index aa6e2ba102..2effbe5f25 100644
--- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationDelegate.java
+++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationDelegate.java
@@ -249,7 +249,7 @@ public class JUnitLaunchConfigurationDelegate extends AbstractJavaLaunchConfigur
IJavaProject javaProject= getJavaProject(configuration);
IJavaElement testTarget= getTestTarget(configuration, javaProject);
- String testMethodName= configuration.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); //$NON-NLS-1$
+ String testMethodName= configuration.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_NAME, ""); //$NON-NLS-1$
if (testMethodName.length() > 0) {
if (testTarget instanceof IType) {
return new IMember[] { ((IType) testTarget).getMethod(testMethodName, new String[0]) };
diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestRunnerViewPart.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestRunnerViewPart.java
index b2d691f017..41fe5ccbbd 100644
--- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestRunnerViewPart.java
+++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestRunnerViewPart.java
@@ -2094,7 +2094,7 @@ action enablement
// reset the container
tmp.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
if (testName != null) {
- tmp.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, testName);
+ tmp.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_NAME, testName);
}
relaunch(tmp, launchMode);
return;
diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java
index 3ab858ca27..8ff5d290a2 100644
--- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java
+++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java
@@ -34,6 +34,8 @@ import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TableItem;
+import org.eclipse.core.runtime.CoreException;
+
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
@@ -53,12 +55,14 @@ import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.part.PageBook;
+import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.internal.junit.launcher.ITestFinder;
import org.eclipse.jdt.internal.junit.model.TestCaseElement;
import org.eclipse.jdt.internal.junit.model.TestElement;
import org.eclipse.jdt.internal.junit.model.TestElement.Status;
@@ -66,6 +70,8 @@ import org.eclipse.jdt.internal.junit.model.TestRoot;
import org.eclipse.jdt.internal.junit.model.TestRunSession;
import org.eclipse.jdt.internal.junit.model.TestSuiteElement;
+import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
+
import org.eclipse.jdt.internal.ui.viewsupport.ColoringLabelProvider;
import org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator;
@@ -221,9 +227,14 @@ public class TestViewer {
if (testElement instanceof TestSuiteElement) {
manager.add(new OpenTestAction(fTestRunnerPart, testLabel));
manager.add(new Separator());
- if (testClassExists(className) && !fTestRunnerPart.lastLaunchIsKeptAlive()) {
- manager.add(new RerunAction(JUnitMessages.RerunAction_label_run, fTestRunnerPart, testElement.getId(), className, null, ILaunchManager.RUN_MODE));
- manager.add(new RerunAction(JUnitMessages.RerunAction_label_debug, fTestRunnerPart, testElement.getId(), className, null, ILaunchManager.DEBUG_MODE));
+ if (!fTestRunnerPart.lastLaunchIsKeptAlive()) {
+ IType testType= findTestClass(testElement);
+ if (testType != null) {
+ String qualifiedName= testType.getFullyQualifiedName();
+ String testName= qualifiedName.equals(className) ? null : testElement.getTestName();
+ manager.add(new RerunAction(JUnitMessages.RerunAction_label_run, fTestRunnerPart, testElement.getId(), qualifiedName, testName, ILaunchManager.RUN_MODE));
+ manager.add(new RerunAction(JUnitMessages.RerunAction_label_debug, fTestRunnerPart, testElement.getId(), qualifiedName, testName, ILaunchManager.DEBUG_MODE));
+ }
}
} else {
TestCaseElement testCaseElement= (TestCaseElement) testElement;
@@ -253,17 +264,37 @@ public class TestViewer {
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
}
- private boolean testClassExists(String className) {
+ /*
+ * Returns the element's test class or the next container's test class, which exists, and for which ITestFinder.isTest() is true.
+ */
+ private IType findTestClass(ITestElement element) {
+ ITestFinder finder= ((TestRunSession)element.getTestRunSession()).getTestRunnerKind().getFinder();
IJavaProject project= fTestRunnerPart.getLaunchedProject();
if (project == null)
- return false;
- try {
- IType type= project.findType(className);
- return type != null;
- } catch (JavaModelException e) {
- // fall through
+ return null;
+ ITestElement current= element;
+ while (current != null) {
+ try {
+ String className= null;
+ if (current instanceof TestRoot) {
+ ILaunchConfiguration configuration= ((TestRunSession)element.getTestRunSession()).getLaunch().getLaunchConfiguration();
+ className= configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String)null);
+ } else if (current instanceof TestElement) {
+ className= ((TestElement)current).getClassName();
+ }
+ if (className != null) {
+ IType type= project.findType(className);
+ if (type != null && finder.isTest(type))
+ return type;
+ }
+ } catch (JavaModelException e) {
+ // fall through
+ } catch (CoreException e) {
+ // fall through
+ }
+ current= current instanceof TestElement ? ((TestElement)current).getParent() : null;
}
- return false;
+ return null;
}
public Control getTestViewerControl() {
diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationTab.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationTab.java
index c1c5c53424..44a59393c8 100644
--- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationTab.java
+++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationTab.java
@@ -465,7 +465,7 @@ public class JUnitLaunchConfigurationTab extends AbstractLaunchConfigurationTab
fOriginalTestMethodName= ""; //$NON-NLS-1$
try {
testTypeName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); //$NON-NLS-1$
- fOriginalTestMethodName = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); //$NON-NLS-1$
+ fOriginalTestMethodName = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_NAME, ""); //$NON-NLS-1$
} catch (CoreException ce) {
}
fTestRadioButton.setSelection(true);
@@ -508,12 +508,12 @@ public class JUnitLaunchConfigurationTab extends AbstractLaunchConfigurationTab
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, fContainerElement.getHandleIdentifier());
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); //$NON-NLS-1$
//workaround for bug 65399
- config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); //$NON-NLS-1$
+ config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_NAME, ""); //$NON-NLS-1$
} else {
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText());
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fTestText.getText());
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
- config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, fTestMethodText.getText());
+ config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_NAME, fTestMethodText.getText());
}
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, fKeepRunning.getSelection());
try {
diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchShortcut.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchShortcut.java
index 1b76ae33a4..330a0430fe 100644
--- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchShortcut.java
+++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchShortcut.java
@@ -14,6 +14,8 @@ package org.eclipse.jdt.junit.launcher;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
@@ -21,6 +23,8 @@ import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
import org.eclipse.core.resources.IResource;
@@ -89,6 +93,9 @@ public class JUnitLaunchShortcut implements ILaunchShortcut2 {
private static final String EMPTY_STRING= ""; //$NON-NLS-1$
+ // see org.junit.runner.Description.METHOD_AND_CLASS_NAME_PATTERN
+ private static final Pattern METHOD_AND_CLASS_NAME_PATTERN= Pattern.compile("(.*)\\((.*)\\)"); //$NON-NLS-1$
+
/**
* Default constructor.
*/
@@ -286,17 +293,40 @@ public class JUnitLaunchShortcut implements ILaunchShortcut2 {
}
/**
- * Creates a launch configuration working copy for the given element. The launch configuration type created will be of the type returned by {@link #getLaunchConfigurationTypeId}.
- * The element type can only be of type {@link IJavaProject}, {@link IPackageFragmentRoot}, {@link IPackageFragment}, {@link IType} or {@link IMethod}.
+ * Creates a launch configuration working copy for the given element. The launch configuration
+ * type created will be of the type returned by {@link #getLaunchConfigurationTypeId}. The
+ * element type can only be of type {@link IJavaProject}, {@link IPackageFragmentRoot},
+ * {@link IPackageFragment}, {@link IType} or {@link IMethod}.
*
- * Clients can extend this method (should call super) to configure additional attributes on the launch configuration working copy.
+ * Clients can extend this method (should call super) to configure additional attributes on the
+ * launch configuration working copy.
+ *
* @param element element to launch
*
* @return a launch configuration working copy for the given element
* @throws CoreException if creation failed
*/
protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement element) throws CoreException {
- final String testName;
+ return createLaunchConfiguration(element, null);
+ }
+
+ /**
+ * Creates a launch configuration working copy for the given element. The launch configuration
+ * type created will be of the type returned by {@link #getLaunchConfigurationTypeId}. The
+ * element type can only be of type {@link IJavaProject}, {@link IPackageFragmentRoot},
+ * {@link IPackageFragment}, {@link IType} or {@link IMethod}.
+ *
+ * Clients can extend this method (should call super) to configure additional attributes on the
+ * launch configuration working copy.
+ *
+ * @param element element to launch
+ * @param testName name of the test to launch, e.g. the method name or an artificial name
+ * created by a JUnit runner
+ *
+ * @return a launch configuration working copy for the given element
+ * @throws CoreException if creation failed
+ */
+ protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement element, String testName) throws CoreException {
final String mainTypeQualifiedName;
final String containerHandleId;
@@ -304,25 +334,26 @@ public class JUnitLaunchShortcut implements ILaunchShortcut2 {
case IJavaElement.JAVA_PROJECT:
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
case IJavaElement.PACKAGE_FRAGMENT: {
- String name= JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_FULLY_QUALIFIED);
containerHandleId= element.getHandleIdentifier();
mainTypeQualifiedName= EMPTY_STRING;
- testName= name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
+ break;
}
- break;
case IJavaElement.TYPE: {
containerHandleId= EMPTY_STRING;
- mainTypeQualifiedName= ((IType) element).getFullyQualifiedName('.'); // don't replace, fix for binary inner types
- testName= element.getElementName();
+ mainTypeQualifiedName= ((IType)element).getFullyQualifiedName('.'); // don't replace, fix for binary inner types
+ break;
}
- break;
case IJavaElement.METHOD: {
- IMethod method= (IMethod) element;
+ if (testName != null) {
+ String message= "Test-names can not be specified when launching a Java method.";
+ throw new CoreException(new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, message));
+ }
+ testName= element.getElementName();
+ IMethod method= (IMethod)element;
containerHandleId= EMPTY_STRING;
mainTypeQualifiedName= method.getDeclaringType().getFullyQualifiedName('.');
- testName= method.getDeclaringType().getElementName() + '.' + method.getElementName();
+ break;
}
- break;
default:
throw new IllegalArgumentException("Invalid element type to create a launch configuration: " + element.getClass().getName()); //$NON-NLS-1$
}
@@ -330,7 +361,8 @@ public class JUnitLaunchShortcut implements ILaunchShortcut2 {
String testKindId= TestKindRegistry.getContainerTestKindId(element);
ILaunchConfigurationType configType= getLaunchManager().getLaunchConfigurationType(getLaunchConfigurationTypeId());
- ILaunchConfigurationWorkingCopy wc= configType.newInstance(null, getLaunchManager().generateLaunchConfigurationName(testName));
+ String configName= getLaunchManager().generateLaunchConfigurationName(suggestLaunchConfigurationName(element, testName));
+ ILaunchConfigurationWorkingCopy wc= configType.newInstance(null, configName);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainTypeQualifiedName);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, element.getJavaProject().getElementName());
@@ -339,22 +371,59 @@ public class JUnitLaunchShortcut implements ILaunchShortcut2 {
wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKindId);
JUnitMigrationDelegate.mapResources(wc);
AssertionVMArg.setArgDefault(wc);
- if (element instanceof IMethod) {
- wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, element.getElementName()); // only set for methods
+ if (testName != null) {
+ wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_NAME, testName);
}
return wc;
}
/**
- * Returns the attribute names of the attributes that are compared when looking for an existing similar launch configuration.
- * Clients can override and replace to customize.
+ * Computes a human-readable name for a launch configuration. The name serve's as suggestion and
+ * it's the callers responsibility to make it valid and unique.
+ *
+ * @param element The Java Element that will be executed.
+ * @param fullTestName The test name. See
+ * org.eclipse.jdt.internal.junit4.runner.DescriptionMatcher for supported formats.
+ * @return The suggested name for the launch configuration.
+ */
+ protected String suggestLaunchConfigurationName(IJavaElement element, String fullTestName) {
+ switch (element.getElementType()) {
+ case IJavaElement.JAVA_PROJECT:
+ case IJavaElement.PACKAGE_FRAGMENT_ROOT:
+ case IJavaElement.PACKAGE_FRAGMENT:
+ String name= JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_FULLY_QUALIFIED);
+ return name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
+ case IJavaElement.TYPE:
+ if (fullTestName != null) {
+ Matcher matcher= METHOD_AND_CLASS_NAME_PATTERN.matcher(fullTestName);
+ if (matcher.matches()) {
+ String typeFQN= matcher.group(2);
+ String testName= matcher.group(1);
+ int typeFQNDot= typeFQN.lastIndexOf('.');
+ String typeName= typeFQNDot >= 0 ? typeFQN.substring(typeFQNDot + 1) : typeFQN;
+ return typeName + " " + testName; //$NON-NLS-1$
+ }
+ return element.getElementName() + " " + fullTestName;//$NON-NLS-1$
+ }
+ return element.getElementName();
+ case IJavaElement.METHOD:
+ IMethod method= (IMethod)element;
+ return method.getDeclaringType().getElementName() + '.' + method.getElementName();
+ default:
+ throw new IllegalArgumentException("Invalid element type to create a launch configuration: " + element.getClass().getName()); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Returns the attribute names of the attributes that are compared when looking for an existing
+ * similar launch configuration. Clients can override and replace to customize.
*
* @return the attribute names of the attributes that are compared
*/
protected String[] getAttributeNamesToCompare() {
return new String[] {
- IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER,
- IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME
+ IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER,
+ IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, JUnitLaunchConfigurationConstants.ATTR_TEST_NAME
};
}
diff --git a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/DescriptionMatcher.java b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/DescriptionMatcher.java
new file mode 100644
index 0000000000..659a210b65
--- /dev/null
+++ b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/DescriptionMatcher.java
@@ -0,0 +1,165 @@
+package org.eclipse.jdt.internal.junit4.runner;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.junit.runner.Description;
+import org.junit.runners.Parameterized;
+
+/**
+ * This class matches JUnit's {@link Description} against a string.
+ *
+ * See {@link #create(Class, String)} for details.
+ *
+ * @author Moritz Eysholdt
+ */
+public abstract class DescriptionMatcher {
+
+ private static class CompositeMatcher extends DescriptionMatcher {
+ private final List<DescriptionMatcher> fMatchers;
+
+ public CompositeMatcher(List<DescriptionMatcher> matchers) {
+ fMatchers= matchers;
+ }
+
+ @Override
+ public boolean matches(Description description) {
+ for (DescriptionMatcher matcher : fMatchers)
+ if (matcher.matches(description))
+ return true;
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return fMatchers.toString();
+ }
+ }
+
+ private static class ExactMatcher extends DescriptionMatcher {
+ private final String fDisplayName;
+
+ public ExactMatcher(String className) {
+ fDisplayName= className;
+ }
+
+ public ExactMatcher(String className, String testName) {
+ // see org.junit.runner.Description.formatDisplayName(String, String)
+ fDisplayName= String.format("%s(%s)", testName, className); //$NON-NLS-1$
+ }
+
+ @Override
+ public boolean matches(Description description) {
+ return fDisplayName.equals(description.getDisplayName());
+ }
+
+ @Override
+ public String toString() {
+ return String.format("{%s:fDisplayName=%s]", getClass().getSimpleName(), fDisplayName); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * This class extracts the leading chars from {@link Description#getMethodName()} which are a
+ * valid Java identifier. If this identifier equals this class' identifier, the Description is
+ * matched.
+ *
+ * Please be aware that {@link Description#getMethodName()} can be any value a JUnit runner has
+ * computed. It is not necessarily a valid method name. For example, {@link Parameterized} uses
+ * the format 'methodname[i]', with 'i' being the row index in the table of test data.
+ *
+ * @author Moritz Eysholdt
+ */
+ private static class LeadingIdentifierMatcher extends DescriptionMatcher {
+
+ private final String fClassName;
+
+ private final String fLeadingIdentifier;
+
+ public LeadingIdentifierMatcher(String className, String leadingIdentifier) {
+ super();
+ fClassName= className;
+ fLeadingIdentifier= leadingIdentifier;
+ }
+
+ @Override
+ public boolean matches(Description description) {
+ String className= description.getClassName();
+ if (fClassName.equals(className)) {
+ String methodName= description.getMethodName();
+ if (methodName != null) {
+ return fLeadingIdentifier.equals(extractLeadingIdentifier(methodName));
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("{%s:fClassName=%s,fLeadingIdentifier=%s]", getClass().getSimpleName(), fClassName, fLeadingIdentifier); //$NON-NLS-1$
+ }
+
+ }
+
+ /**
+ * Creates a matcher object that can decide for {@link Description}s whether they match the
+ * supplied 'matchString' or not.
+ *
+ * Several strategies for matching are applied:
+ * <ul>
+ * <li>if 'matchString' equals {@link Description#getDisplayName()}, it's always a match</li>
+ * <li>if 'matchString' has the format foo(bar), which is JUnit's format, it tries to match
+ * methods base on leading identifiers. See {@link LeadingIdentifierMatcher}.</li>
+ * <li>if 'matchString' does not have the format foo(bar), it also tries to match Descriptions
+ * that equal clazz(matchString), with 'clazz' being this method's parameter. Furthermore, if
+ * 'matchString' is a Java identifier, it matches Descriptions via leading identifiers. See
+ * {@link LeadingIdentifierMatcher}</li>
+ * </ul>
+ *
+ * @param clazz A class that is used when 'matchString' does not have the format
+ * methodName(className).
+ *
+ * @param matchString A string to match JUnit's {@link Description}s against.
+ *
+ * @return A matcher object.
+ */
+ public static DescriptionMatcher create(Class<?> clazz, String matchString) {
+ List<DescriptionMatcher> matchers= new ArrayList<DescriptionMatcher>();
+ matchers.add(new ExactMatcher(matchString));
+ Matcher parsed= METHOD_AND_CLASS_NAME_PATTERN.matcher(matchString);
+ if (parsed.matches()) {
+ String className= parsed.group(2);
+ String testName= parsed.group(1);
+ if (testName.equals(extractLeadingIdentifier(testName)))
+ matchers.add(new LeadingIdentifierMatcher(className, testName));
+ } else {
+ String className= clazz.getName();
+ if (!className.equals(matchString)) {
+ matchers.add(new ExactMatcher(className, matchString));
+ if (matchString.equals(extractLeadingIdentifier(matchString)))
+ matchers.add(new LeadingIdentifierMatcher(className, matchString));
+ }
+ }
+ return new CompositeMatcher(matchers);
+ }
+
+ private static String extractLeadingIdentifier(String string) {
+ if (string.length() == 0)
+ return null;
+ if (!Character.isJavaIdentifierStart(string.charAt(0)))
+ return null;
+ for (int i= 1; i < string.length(); i++) {
+ if (!Character.isJavaIdentifierPart(string.charAt(i))) {
+ return string.substring(0, i);
+ }
+ }
+ return string;
+ }
+
+ // see org.junit.runner.Description.METHOD_AND_CLASS_NAME_PATTERN
+ private static final Pattern METHOD_AND_CLASS_NAME_PATTERN= Pattern.compile("(.*)\\((.*)\\)"); //$NON-NLS-1$
+
+ public abstract boolean matches(Description description);
+}
diff --git a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestClassReference.java b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestClassReference.java
deleted file mode 100644
index 8eee1ca701..0000000000
--- a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestClassReference.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2006, 2009 IBM Corporation 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:
- * David Saff (saff@mit.edu) - initial API and implementation
- * (bug 102632: [JUnit] Support for JUnit 4.)
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.junit4.runner;
-
-import org.junit.runner.Description;
-import org.junit.runner.Request;
-
-import org.eclipse.jdt.internal.junit.runner.ITestIdentifier;
-import org.eclipse.jdt.internal.junit.runner.IVisitsTestTrees;
-
-public class JUnit4TestClassReference extends JUnit4TestReference {
- protected final Class<?> fClass;
-
- public JUnit4TestClassReference(Class<?> clazz, String[] failureNames) {
- super(Request.aClass(clazz), failureNames);
- fClass= clazz;
- }
-
- public int countTestCases() {
- return fRunner.testCount();
- }
-
- public String getName() {
- return fClass.getName();
- }
-
- public void sendTree(final IVisitsTestTrees notified) {
- sendDescriptionTree(notified, fRunner.getDescription());
- }
-
- private void sendDescriptionTree(final IVisitsTestTrees notified, org.junit.runner.Description description) {
- if (description.isTest()) {
- notified.visitTreeEntry(new JUnit4Identifier(description), false, 1);
- } else {
- notified.visitTreeEntry(new JUnit4Identifier(description), true, description.getChildren().size());
- for (Description child : description.getChildren()) {
- sendDescriptionTree(notified, child);
- }
- }
- }
-
- @Override
- public boolean equals(Object obj) {
- if (! (obj instanceof JUnit4TestReference))
- return false;
-
- JUnit4TestReference ref= (JUnit4TestReference) obj;
- return (ref.getIdentifier().equals(getIdentifier()));
- }
-
- @Override
- public int hashCode() {
- return fClass.hashCode();
- }
-
- public ITestIdentifier getIdentifier() {
- return new JUnit4Identifier(fRunner.getDescription());
- }
-}
diff --git a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestLoader.java b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestLoader.java
index 0e3bc5e85d..d9524069b1 100644
--- a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestLoader.java
+++ b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestLoader.java
@@ -14,9 +14,14 @@ package org.eclipse.jdt.internal.junit4.runner;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.List;
import junit.framework.Test;
+import org.junit.runner.Description;
+import org.junit.runner.Request;
+import org.junit.runner.Runner;
+
import org.eclipse.jdt.internal.junit.runner.ITestLoader;
import org.eclipse.jdt.internal.junit.runner.ITestReference;
import org.eclipse.jdt.internal.junit.runner.RemoteTestRunner;
@@ -41,33 +46,67 @@ public class JUnit4TestLoader implements ITestLoader {
return refs;
}
+ private Description getRootDescription(Runner runner, DescriptionMatcher matcher) {
+ Description current= runner.getDescription();
+ while (true) {
+ List<Description> children= current.getChildren();
+ if (children.size() != 1 || matcher.matches(current))
+ return current;
+ current= children.get(0);
+ }
+ }
+
private ITestReference createTest(Class<?> clazz, String testName, String[] failureNames, RemoteTestRunner listener) {
if (clazz == null)
return null;
- if (testName == null)
- return new JUnit4TestClassReference(clazz, failureNames);
- else if (isJUnit3SetUpTest(clazz, testName)) {
+ if (testName != null && isJUnit3SetUpTest(clazz, testName)) {
JUnit3TestLoader jUnit3TestLoader= new JUnit3TestLoader();
Test test= jUnit3TestLoader.getTest(clazz, testName, listener);
return new JUnit3TestReference(test);
}
- return new JUnit4TestMethodReference(clazz, testName, failureNames);
+ if (testName != null) {
+ return createFilteredTest(clazz, testName, failureNames);
+ }
+ return createUnfilteredTest(clazz, failureNames);
+ }
+
+ private ITestReference createFilteredTest(Class<?> clazz, String testName, String[] failureNames) {
+ DescriptionMatcher matcher= DescriptionMatcher.create(clazz, testName);
+ SubForestFilter filter= new SubForestFilter(matcher);
+ Request request= sortByFailures(Request.classWithoutSuiteMethod(clazz).filterWith(filter), failureNames);
+ Runner runner= request.getRunner();
+ Description description= getRootDescription(runner, matcher);
+ return new JUnit4TestReference(runner, description);
+ }
+
+ private ITestReference createUnfilteredTest(Class<?> clazz, String[] failureNames) {
+ Request request= sortByFailures(Request.aClass(clazz), failureNames);
+ Runner runner= request.getRunner();
+ Description description= runner.getDescription();
+ return new JUnit4TestReference(runner, description);
+ }
+
+ private Request sortByFailures(Request request, String[] failureNames) {
+ if (failureNames != null) {
+ return request.sortWith(new FailuresFirstSorter(failureNames));
+ }
+ return request;
}
private boolean isJUnit3SetUpTest(Class<?> clazz, String testName) {
- if (! Test.class.isAssignableFrom(clazz))
+ if (!Test.class.isAssignableFrom(clazz))
return false;
try {
Method testMethod= clazz.getMethod(testName);
if (testMethod.getAnnotation(org.junit.Test.class) != null)
return false;
-
+
Method setup= clazz.getMethod(JUnit3TestLoader.SET_UP_TEST_METHOD_NAME, new Class[] { Test.class });
int modifiers= setup.getModifiers();
if (setup.getReturnType() == Test.class && Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers))
return true;
- } catch (SecurityException e1) {
- } catch (NoSuchMethodException e) {
+ } catch (@SuppressWarnings("unused") SecurityException e1) {
+ } catch (@SuppressWarnings("unused") NoSuchMethodException e) {
}
return false;
}
diff --git a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestMethodReference.java b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestMethodReference.java
deleted file mode 100644
index de6ff0dfb4..0000000000
--- a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestMethodReference.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2006, 2010 IBM Corporation 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:
- * David Saff (saff@mit.edu) - initial API and implementation
- * (bug 102632: [JUnit] Support for JUnit 4.)
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.junit4.runner;
-
-import org.junit.runner.Description;
-import org.junit.runner.Request;
-
-import org.eclipse.jdt.internal.junit.runner.ITestIdentifier;
-import org.eclipse.jdt.internal.junit.runner.IVisitsTestTrees;
-
-public class JUnit4TestMethodReference extends JUnit4TestReference {
- private final Description fDescription;
-
- public JUnit4TestMethodReference(Class<?> clazz, String methodName, String[] failureNames) {
- super(createRequest(clazz, methodName), failureNames);
- fDescription = Description.createTestDescription(clazz, methodName);
- }
-
- private static Request createRequest(final Class<?> clazz, String methodName) {
- Description method= Description.createTestDescription(clazz, methodName);
- return Request.classWithoutSuiteMethod(clazz).filterWith(method);
- }
-
- public int countTestCases() {
- return 1;
- }
-
- public void sendTree(IVisitsTestTrees notified) {
- notified.visitTreeEntry(getIdentifier(), false, 1);
- }
-
- public String getName() {
- return fDescription.toString();
- }
-
- @Override
- public boolean equals(Object obj) {
- if (! (obj instanceof JUnit4TestMethodReference))
- return false;
-
- JUnit4TestMethodReference ref = (JUnit4TestMethodReference) obj;
- return (ref.fDescription.equals(fDescription));
- }
-
- @Override
- public int hashCode() {
- return fDescription.hashCode();
- }
-
- @Override
- public String toString() {
- return fDescription.toString();
- }
-
- public ITestIdentifier getIdentifier() {
- return new JUnit4Identifier(fDescription);
- }
-}
diff --git a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestReference.java b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestReference.java
index 46339eedb4..93bdc3f478 100644
--- a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestReference.java
+++ b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestReference.java
@@ -12,7 +12,7 @@
package org.eclipse.jdt.internal.junit4.runner;
-import org.junit.runner.Request;
+import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunListener;
@@ -20,17 +20,53 @@ import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;
import org.eclipse.jdt.internal.junit.runner.IStopListener;
+import org.eclipse.jdt.internal.junit.runner.ITestIdentifier;
import org.eclipse.jdt.internal.junit.runner.ITestReference;
+import org.eclipse.jdt.internal.junit.runner.IVisitsTestTrees;
import org.eclipse.jdt.internal.junit.runner.TestExecution;
-public abstract class JUnit4TestReference implements ITestReference {
- protected Runner fRunner;
+public class JUnit4TestReference implements ITestReference {
+ protected final Runner fRunner;
- public JUnit4TestReference(Request request, String[] failureNames) {
- if (failureNames != null) {
- request= request.sortWith(new FailuresFirstSorter(failureNames));
+ protected final Description fRoot;
+
+ public JUnit4TestReference(Runner runner, Description root) {
+ fRunner= runner;
+ fRoot= root;
+ }
+
+ public int countTestCases() {
+ return countTestCases(fRoot);
+ }
+
+ private int countTestCases(Description description) {
+ if (description.isTest()) {
+ return 1;
+ } else {
+ int result= 0;
+ for (Description child : description.getChildren()) {
+ result+= countTestCases(child);
+ }
+ return result;
}
- fRunner= request.getRunner();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof JUnit4TestReference))
+ return false;
+
+ JUnit4TestReference ref= (JUnit4TestReference)obj;
+ return (ref.fRoot.equals(fRoot));
+ }
+
+ public ITestIdentifier getIdentifier() {
+ return new JUnit4Identifier(fRoot);
+ }
+
+ @Override
+ public int hashCode() {
+ return fRoot.hashCode();
}
public void run(TestExecution execution) {
@@ -49,10 +85,30 @@ public abstract class JUnit4TestReference implements ITestReference {
notifier.fireTestRunStarted(fRunner.getDescription());
fRunner.run(notifier);
notifier.fireTestRunFinished(result);
- } catch (StoppedByUserException e) {
+ } catch (@SuppressWarnings("unused") StoppedByUserException e) {
// not interesting, see https://bugs.eclipse.org/329498
} finally {
notifier.removeListener(listener);
}
}
+
+ public void sendTree(IVisitsTestTrees notified) {
+ sendTree(notified, fRoot);
+ }
+
+ private void sendTree(final IVisitsTestTrees notified, Description description) {
+ if (description.isTest()) {
+ notified.visitTreeEntry(new JUnit4Identifier(description), false, 1);
+ } else {
+ notified.visitTreeEntry(new JUnit4Identifier(description), true, description.getChildren().size());
+ for (Description child : description.getChildren()) {
+ sendTree(notified, child);
+ }
+ }
+ }
+
+ @Override
+ public String toString() {
+ return fRoot.toString();
+ }
}
diff --git a/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/SubForestFilter.java b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/SubForestFilter.java
new file mode 100644
index 0000000000..f9ed04a0d6
--- /dev/null
+++ b/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/SubForestFilter.java
@@ -0,0 +1,76 @@
+package org.eclipse.jdt.internal.junit4.runner;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.runner.Description;
+import org.junit.runner.Runner;
+import org.junit.runner.manipulation.Filter;
+import org.junit.runner.manipulation.NoTestsRemainException;
+
+/**
+ * This filter keeps all matched {@link Description}s in a tree, including the children and
+ * container of the matched Description.
+ *
+ * It is allowed to match more then one Description.
+ *
+ * @author Moritz Eysholdt
+ */
+public class SubForestFilter extends Filter {
+
+ private Set<Description> fIncluded= null;
+
+ private final DescriptionMatcher fMatcher;
+
+ public SubForestFilter(DescriptionMatcher matcher) {
+ fMatcher= matcher;
+ }
+
+ @Override
+ public void apply(Object child) throws NoTestsRemainException {
+ if (child instanceof Runner && fIncluded == null) {
+ fIncluded= new HashSet<Description>();
+ collectIncludedDescriptions(((Runner)child).getDescription());
+ if (fIncluded.isEmpty())
+ throw new NoTestsRemainException();
+ }
+ super.apply(child);
+ }
+
+ private boolean collectIncludedDescriptions(Description description) {
+ if (fMatcher.matches(description)) {
+ includeWithChildren(description);
+ return true;
+ }
+ boolean hasIncludedChild= false;
+ for (Description child : description.getChildren()) {
+ hasIncludedChild|= collectIncludedDescriptions(child);
+ }
+ if (hasIncludedChild) {
+ this.fIncluded.add(description);
+ }
+ return hasIncludedChild;
+ }
+
+ @Override
+ public String describe() {
+ return fMatcher.toString();
+ }
+
+ public Set<Description> getIncludedDescriptions() {
+ return fIncluded;
+ }
+
+ private void includeWithChildren(Description description) {
+ fIncluded.add(description);
+ for (Description child : description.getChildren()) {
+ includeWithChildren(child);
+ }
+ }
+
+ @Override
+ public boolean shouldRun(Description description) {
+ return fIncluded.contains(description);
+ }
+
+}
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/AbstractTestRunListenerTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/AbstractTestRunListenerTest.java
index 85ca97b836..794a723948 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/AbstractTestRunListenerTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/AbstractTestRunListenerTest.java
@@ -92,8 +92,8 @@ public class AbstractTestRunListenerTest extends TestCase {
}
private static class TestJUnitLaunchShortcut extends JUnitLaunchShortcut {
- public static ILaunchConfigurationWorkingCopy createConfiguration(IJavaElement element) throws CoreException {
- ILaunchConfigurationWorkingCopy copy= new TestJUnitLaunchShortcut().createLaunchConfiguration(element);
+ public static ILaunchConfigurationWorkingCopy createConfiguration(IJavaElement element, String testName) throws CoreException {
+ ILaunchConfigurationWorkingCopy copy= new TestJUnitLaunchShortcut().createLaunchConfiguration(element, testName);
return copy;
}
}
@@ -107,6 +107,10 @@ public class AbstractTestRunListenerTest extends TestCase {
}
protected void launchJUnit(IJavaElement aTest, String testKindID) throws CoreException {
+ launchJUnit(aTest, testKindID, (String)null);
+ }
+
+ protected void launchJUnit(IJavaElement aTest, String testKindID, String testName) throws CoreException {
ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, null);
ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
@@ -151,7 +155,7 @@ public class AbstractTestRunListenerTest extends TestCase {
};
lm.addLaunchListener(launchesListener);
- ILaunchConfigurationWorkingCopy configuration= TestJUnitLaunchShortcut.createConfiguration(aTest);
+ ILaunchConfigurationWorkingCopy configuration= TestJUnitLaunchShortcut.createConfiguration(aTest, testName);
if (testKindID != null) {
configuration.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKindID);
}
@@ -176,7 +180,11 @@ public class AbstractTestRunListenerTest extends TestCase {
}
protected String[] launchJUnit(IJavaElement aTest, String testKindID, final TestRunLog log) throws CoreException {
- launchJUnit(aTest, testKindID);
+ return launchJUnit(aTest, testKindID, null, log);
+ }
+
+ protected String[] launchJUnit(IJavaElement aTest, String testKindID, String testName, final TestRunLog log) throws CoreException {
+ launchJUnit(aTest, testKindID, testName);
boolean success= new DisplayHelper(){
protected boolean condition() {
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/JUnitJUnitTests.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/JUnitJUnitTests.java
index 51bc8a8dd6..c10a352b9c 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/JUnitJUnitTests.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/JUnitJUnitTests.java
@@ -32,6 +32,9 @@ public class JUnitJUnitTests {
suite.addTestSuite(TestRunListenerTest3.class);
suite.addTestSuite(TestRunListenerTest4.class);
+
+ suite.addTestSuite(TestRunFilteredStandardRunnerTest4.class);
+ suite.addTestSuite(TestRunFilteredParameterizedRunnerTest4.class);
suite.addTest(TestRunSessionSerializationTests3.suite());
suite.addTest(TestRunSessionSerializationTests4.suite());
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredParameterizedRunnerTest4.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredParameterizedRunnerTest4.java
new file mode 100644
index 0000000000..cc44c0ba12
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredParameterizedRunnerTest4.java
@@ -0,0 +1,161 @@
+/*******************************************************************************
+ * Copyright (c) 2006, 2013 IBM Corporation 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.jdt.junit.tests;
+
+import org.eclipse.jdt.junit.JUnitCore;
+import org.eclipse.jdt.junit.TestRunListener;
+import org.eclipse.jdt.junit.model.ITestElement.ProgressState;
+import org.eclipse.jdt.junit.model.ITestElement.Result;
+import org.eclipse.jdt.testplugin.JavaProjectHelper;
+
+import org.eclipse.core.runtime.Path;
+
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
+
+import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;
+
+public class TestRunFilteredParameterizedRunnerTest4 extends AbstractTestRunListenerTest {
+
+ private IType fATestCase;
+
+ private String[] runTreeTest(IType typeToLaunch, String testName, int step) throws Exception {
+ TestRunLog log= new TestRunLog();
+ final TestRunListener testRunListener= new TestRunListeners.TreeTest(log, step);
+ JUnitCore.addTestRunListener(testRunListener);
+ try {
+ return launchJUnit(typeToLaunch, TestKindRegistry.JUNIT4_TEST_KIND_ID, testName, log);
+ } finally {
+ JUnitCore.removeTestRunListener(testRunListener);
+ }
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ fProject= JavaProjectHelper.createJavaProject("TestRunListenerTest", "bin");
+ JavaProjectHelper.addVariableEntry(fProject, new Path("JUNIT_HOME/junit.jar"), null, null);
+ JavaProjectHelper.addToClasspath(fProject, JavaCore.newContainerEntry(JUnitCore.JUNIT4_CONTAINER_PATH));
+ JavaProjectHelper.addRTJar15(fProject);
+ String source=
+ "package pack;\n"+
+ "\n"+
+ "import java.util.Arrays;\n"+
+ "\n"+
+ "import org.junit.Assert;\n"+
+ "import org.junit.Test;\n"+
+ "import org.junit.runner.RunWith;\n"+
+ "import org.junit.runners.Parameterized;\n"+
+ "import org.junit.runners.Parameterized.Parameter;\n"+
+ "import org.junit.runners.Parameterized.Parameters;\n"+
+ "\n" +
+ "@RunWith(Parameterized.class)\n"+
+ "public class ATestCase {\n"+
+ "\n"+
+ " @Parameters\n"+
+ " public static Iterable<Object[]> data() {\n"+
+ " return Arrays.asList(new Object[][] { { 6 }, { 12 } });\n"+
+ " }\n"+
+ "\n"+
+ " @Parameter\n"+
+ " public int param;\n"+
+ "\n"+
+ " @Test\n"+
+ " public void testDiv() {\n"+
+ " Assert.assertEquals(0, param % 2);\n"+
+ " }\n"+
+ "\n"+
+ " @Test\n"+
+ " public void testDiv2() {\n"+
+ " Assert.assertEquals(0, param % 3);\n"+
+ " }\n"+
+ "}\n;";
+ fATestCase= createType(source, "pack", "ATestCase.java");
+ }
+
+ public void testMatchRoot() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase pack.ATestCase", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.suiteAsString("pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 1),
+ TestRunListeners.suiteAsString("[0]", ProgressState.COMPLETED, Result.OK, null, 2),
+ TestRunListeners.testCaseAsString("testDiv[0]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 3),
+ TestRunListeners.testCaseAsString("testDiv2[0]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 3),
+ TestRunListeners.suiteAsString("[1]", ProgressState.COMPLETED, Result.OK, null, 2),
+ TestRunListeners.testCaseAsString("testDiv[1]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 3),
+ TestRunListeners.testCaseAsString("testDiv2[1]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 3),
+ };
+ String[] actual= runTreeTest(fATestCase, "pack.ATestCase", 10);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+ public void testMatchSubtree1ByName() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase [0]", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.suiteAsString("[0]", ProgressState.COMPLETED, Result.OK, null, 1),
+ TestRunListeners.testCaseAsString("testDiv[0]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 2),
+ TestRunListeners.testCaseAsString("testDiv2[0]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 2),
+ };
+ String[] actual= runTreeTest(fATestCase, "[0]", 6);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+ public void testMatchSubtree1Leaf1ByName() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase testDiv[0]", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.testCaseAsString("testDiv[0]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 1),
+ };
+ String[] actual= runTreeTest(fATestCase, "testDiv[0]", 4);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+ public void testMatchSubtree1Leaf1ByNameAndClass() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase testDiv[0]", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.testCaseAsString("testDiv[0]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 1),
+ };
+ String[] actual2= runTreeTest(fATestCase, "testDiv[0](pack.ATestCase)", 4);
+ assertEqualLog(expectedSequence, actual2);
+ }
+
+ public void testMatchSubtree1Leaf2ByName() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase testDiv[1]", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.testCaseAsString("testDiv[1]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 1),
+ };
+ String[] actual= runTreeTest(fATestCase, "testDiv[1]", 4);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+ public void testMatchSubtree2ByName() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase [1]", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.suiteAsString("[1]", ProgressState.COMPLETED, Result.OK, null, 1),
+ TestRunListeners.testCaseAsString("testDiv[1]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 2),
+ TestRunListeners.testCaseAsString("testDiv2[1]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 2),
+ };
+ String[] actual= runTreeTest(fATestCase, "[1]", 6);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+ public void testMatchAllFirstLeafs() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase testDiv", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.suiteAsString("pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 1),
+ TestRunListeners.suiteAsString("[0]", ProgressState.COMPLETED, Result.OK, null, 2),
+ TestRunListeners.testCaseAsString("testDiv[0]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 3),
+ TestRunListeners.suiteAsString("[1]", ProgressState.COMPLETED, Result.OK, null, 2),
+ TestRunListeners.testCaseAsString("testDiv[1]", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 3),
+ };
+ String[] actual= runTreeTest(fATestCase, "testDiv", 6);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+}
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredStandardRunnerTest4.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredStandardRunnerTest4.java
new file mode 100644
index 0000000000..42c5f0f2b0
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestRunFilteredStandardRunnerTest4.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * Copyright (c) 2006, 2013 IBM Corporation 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.jdt.junit.tests;
+
+import org.eclipse.jdt.junit.JUnitCore;
+import org.eclipse.jdt.junit.TestRunListener;
+import org.eclipse.jdt.junit.model.ITestElement.FailureTrace;
+import org.eclipse.jdt.junit.model.ITestElement.ProgressState;
+import org.eclipse.jdt.junit.model.ITestElement.Result;
+import org.eclipse.jdt.testplugin.JavaProjectHelper;
+
+import org.eclipse.core.runtime.Path;
+
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
+
+import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;
+
+public class TestRunFilteredStandardRunnerTest4 extends AbstractTestRunListenerTest {
+
+ private IType fATestCase;
+
+ private String[] runTreeTest(IType typeToLaunch, String testName, int step) throws Exception {
+ TestRunLog log= new TestRunLog();
+ final TestRunListener testRunListener= new TestRunListeners.TreeTest(log, step);
+ JUnitCore.addTestRunListener(testRunListener);
+ try {
+ return launchJUnit(typeToLaunch, TestKindRegistry.JUNIT4_TEST_KIND_ID, testName, log);
+ } finally {
+ JUnitCore.removeTestRunListener(testRunListener);
+ }
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ fProject= JavaProjectHelper.createJavaProject("TestRunListenerTest", "bin");
+ JavaProjectHelper.addVariableEntry(fProject, new Path("JUNIT_HOME/junit.jar"), null, null);
+ JavaProjectHelper.addToClasspath(fProject, JavaCore.newContainerEntry(JUnitCore.JUNIT4_CONTAINER_PATH));
+ JavaProjectHelper.addRTJar15(fProject);
+ String source=
+ "package pack;\n" +
+ "import org.junit.Test;\n"+
+ "import org.junit.FixMethodOrder;\n"+
+ "import org.junit.runners.MethodSorters;\n"+
+ "import static org.junit.Assert.*;\n"+
+ "\n" +
+ "@FixMethodOrder(MethodSorters.NAME_ASCENDING)\n" + // workaround for http://randomallsorts.blogspot.de/2012/12/junit-411-whats-new-test-execution-order.html
+ "public class ATestCase {\n" +
+ " @Test public void test1Succeed() { }\n" +
+ " @Test public void test2Fail() { fail(); }\n" +
+ "}";
+ fATestCase= createType(source, "pack", "ATestCase.java");
+ }
+
+ public void testFilterToTest1Succeed() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase test1Succeed", ProgressState.COMPLETED, Result.OK, 0),
+ TestRunListeners.testCaseAsString("test1Succeed", "pack.ATestCase", ProgressState.COMPLETED, Result.OK, null, 1),
+ };
+ String[] actual= runTreeTest(fATestCase, "test1Succeed", 4);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+ public void testFilterToTest2Fail() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase test2Fail", ProgressState.COMPLETED, Result.FAILURE, 0),
+ TestRunListeners.testCaseAsString("test2Fail", "pack.ATestCase", ProgressState.COMPLETED, Result.FAILURE, new FailureTrace("java.lang.AssertionError", null, null), 1),
+ };
+ String[] actual= runTreeTest(fATestCase, "test2Fail", 4);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+ public void testFilterToNoTestsRemain() throws Exception {
+ String[] expectedSequence= new String[] {
+ TestRunListeners.sessionAsString("ATestCase thisdoesnotexist", ProgressState.COMPLETED, Result.ERROR, 0),
+ TestRunListeners.testCaseAsString("initializationError", "org.junit.runner.manipulation.Filter", ProgressState.COMPLETED, Result.ERROR, new FailureTrace("java.lang.Exception", null, null), 1),
+ };
+ String[] actual= runTreeTest(fATestCase, "thisdoesnotexist", 4);
+ assertEqualLog(expectedSequence, actual);
+ }
+
+
+}

Back to the top