Permalink
Browse files

remove cmd line switch "--method=", use ClassName#MethodName instead

  • Loading branch information...
1 parent d1f506e commit aaf23e86fedc08a3e2c25df2775b8f3829a1db02 @schaarsc committed Aug 1, 2012
@@ -1,5 +1,6 @@
package org.junit.runner;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@@ -28,9 +29,14 @@
*/
public class JUnitCore {
/**
- * command line paramter used to run single methods instead of complete test classes.
+ * Separates class name from method name, e.g. ClassName#MethodName
*/
- public final static String METHOD = "--method=";
+ private static final String SEPARATOR= "#";
+
+ /**
+ * Method name wildcard, e.g. ClassName#M*Name
+ */
+ private static final String WILDCARD= "*";
private final RunNotifier fNotifier= new RunNotifier();
@@ -83,17 +89,25 @@ public static Result runClasses(Class<?>... classes) {
*/
private Result runMain(JUnitSystem system, String... args) {
system.out().println("JUnit version " + Version.id());
- List<String> methods = new ArrayList<String>();
+ List<Description> methods = new ArrayList<Description>();
List<Class<?>> classes= new ArrayList<Class<?>>();
List<Failure> missingClasses= new ArrayList<Failure>();
for (String each : args)
try {
- if(each.startsWith(METHOD)) {
- String[] m = each.split("=");
- if(m.length==2 && !m[1].isEmpty()) {
- methods.add(m[1]);
+ String[] class_method = each.split(SEPARATOR);
+ if (class_method.length==2) { //found: ClassName#MethodName
+ Class<?> clazz = Class.forName(class_method[0]);
+ String methodName = class_method[1];
+ if (methodName.contains(WILDCARD)) {
+ methods.addAll(findMatchingMethods(clazz, methodName));
+ } else {
+ methods.add(Description.createTestDescription(clazz, methodName));
}
- } else {
+ classes.add(clazz);
+ } else if (class_method.length==1 && each.endsWith(SEPARATOR)) { //found: ClassName#
+ Class<?> clazz = Class.forName(class_method[0]);
+ classes.add(clazz);
+ } else { //assume ClassName otherwise, wrong format may cause ClassNotFoundException
classes.add(Class.forName(each));
}
} catch (ClassNotFoundException e) {
@@ -121,23 +135,39 @@ private Result runMain(JUnitSystem system, String... args) {
public String getVersion() {
return Version.id();
}
+
+ /**
+ * Convert method name with wildcard into a list of {@link Description}
+ * @param clazz to search for methods matching <code>wildcardName</code>
+ * @param wildcardName method name pattern
+ * @return list of matching descriptions
+ */
+ private List<Description> findMatchingMethods(Class<?> clazz, String wildcardName) {
+ List<Description> result = new ArrayList<Description>();
+ String pattern = wildcardName.replace(WILDCARD, ".*");
+ for (Method m : clazz.getMethods()) {
+ final String methodName= m.getName();
+ if (methodName.matches(pattern)) {
+ result.add(Description.createTestDescription(clazz, methodName));
+ }
+ }
+ return result;
+ }
/**
* Construct new {@link Filter} based on method list.
* @param methods
* @return filter to run only methods listed in <code>methods</code>
*/
- private Filter createMethodFilter(final List<String> methods) {
+ private Filter createMethodFilter(final List<Description> methods) {
return new Filter() {
@Override
public boolean shouldRun(Description description) {
String methodName = description.getMethodName();
if (methodName == null) {
return true;
- } else if (methods.contains(methodName)) {
- return true;
}
- return false;
+ return methods.contains(description);
}
@Override
public String describe() {
@@ -51,7 +51,7 @@ public void run() {
testWasRun = false;
new MainRunner().runWithCheckForSystemExit(new Runnable() {
public void run() {
- JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example", JUnitCore.METHOD+"test");
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example#test");
}
});
assertTrue(testWasRun);
@@ -62,19 +62,56 @@ public void run() {
test2WasRun = false;
new MainRunner().runWithCheckForSystemExit(new Runnable() {
public void run() {
- JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example", JUnitCore.METHOD+"test", JUnitCore.METHOD+"test2");
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example#test"
+ , "org.junit.tests.running.core.CommandLineTest$Example#test2");
}
});
assertTrue(testWasRun);
assertTrue(test2WasRun);
}
+ @Test public void runTwoMethodsWithWildcard() {
+ testWasRun = false;
+ test2WasRun = false;
+ new MainRunner().runWithCheckForSystemExit(new Runnable() {
+ public void run() {
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example#t*st*");
+ }
+ });
+ assertTrue(testWasRun);
+ assertTrue(test2WasRun);
+ }
+
+ @Test public void missingMethod() {
+ testWasRun = false;
+ test2WasRun = false;
+ new MainRunner().runWithCheckForSystemExit(new Runnable() {
+ public void run() {
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example#");
+ }
+ });
+ assertTrue(testWasRun);
+ assertTrue(test2WasRun);
+ }
+
+ @Test public void missingClass() {
+ testWasRun = false;
+ test2WasRun = false;
+ new MainRunner().runWithCheckForSystemExit(new Runnable() {
+ public void run() {
+ JUnitCore.main("#test");
+ }
+ });
+ assertFalse(testWasRun);
+ assertFalse(test2WasRun);
+ }
+
@Test public void donotRunSingleMethod() {
testWasRun = false;
test2WasRun = false;
new MainRunner().runWithCheckForSystemExit(new Runnable() {
public void run() {
- JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example", JUnitCore.METHOD+"tst");
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example#tst");
}
});
assertFalse(testWasRun);

0 comments on commit aaf23e8

Please sign in to comment.