Permalink
Browse files

refactoring JUnitCore#findMatchingMethods

  • Loading branch information...
schaarsc committed Sep 1, 2012
1 parent 962c9c9 commit a5665318a39b56af5ede5b5d7e2857b78c118209
@@ -5,9 +5,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.junit.Test;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.TestClass;
+
/**
* <p>A <code>Description</code> describes a test which is to be run or has been run. <code>Descriptions</code>
* can be atomic (a single test) or compound (containing children tests). <code>Descriptions</code> are used
@@ -88,6 +93,24 @@ public static Description createSuiteDescription(Class<?> testClass) {
return new Description(testClass.getName(), testClass.getAnnotations());
}
+ /**
+ * Create a <code>Description</code> with methods matching <code>methodPattern</code>
+ * @param clazz to search for methods matching <code>methodPattern</code>
+ * @param methodPattern regexp method name pattern
+ * @return
+ */
+ public static Description createSuiteDescription(Class<?> clazz, String methodPattern) {
+ TestClass testClass = new TestClass(clazz);
+ Description suite = createSuiteDescription(clazz);
+ for (FrameworkMethod m : testClass.getAnnotatedMethods(Test.class)) {
+ final String methodName= m.getName();
+ if (methodName.matches(methodPattern)) {
+ suite.addChild(createTestDescription(clazz, methodName));
+ }
+ }
+ return suite;
+ }
+
/**
* Describes a Runner which runs no tests
*/
@@ -93,24 +93,19 @@ private Result runMain(JUnitSystem system, String... args) {
for (String each : args)
try {
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];
- final List<Description> foundMatchingMethods= findMatchingMethods(clazz, methodName);
- if (foundMatchingMethods.size()==0) {
- system.out().println("No matching method found for: " + methodName);
- Description description= Description.createSuiteDescription(each);
- Failure failure= new Failure(description, new NoTestsRemainException());
- missingClasses.add(failure);
- } else {
- methods.addAll(foundMatchingMethods);
- classes.add(clazz);
- }
- } else if (class_method.length==1 && each.endsWith(SEPARATOR)) { //found: ClassName#
- Class<?> clazz = Class.forName(class_method[0]);
+ Class<?> clazz= Class.forName(class_method[0]);
+ String methodName= ".*";
+ if (class_method.length == 2)
+ methodName= class_method[1];
+ final Description foundMatchingMethods= Description.createSuiteDescription(clazz, methodName);
+ if (foundMatchingMethods.getChildren().isEmpty()) {
+ system.out().println("No matching method found for: " + each);
+ Description description= Description.createSuiteDescription(each);
+ Failure failure= new Failure(description, new NoTestsRemainException());
+ missingClasses.add(failure);
+ } else {
+ methods.addAll(foundMatchingMethods.getChildren());
classes.add(clazz);
- } else { //assume ClassName otherwise, wrong format may cause ClassNotFoundException
- classes.add(Class.forName(each));
}
} catch (ClassNotFoundException e) {
system.out().println("Could not find class: " + each);
@@ -120,7 +115,7 @@ private Result runMain(JUnitSystem system, String... args) {
}
RunListener listener= new TextListener(system);
addListener(listener);
- Result result= run(Filter.matchMethodDescriptions(methods, Filter.FilterMode.RELAXED), classes.toArray(new Class[0]));
+ Result result= run(Filter.matchMethodDescriptions(methods), classes.toArray(new Class[0]));
for (Failure each : missingClasses)
result.getFailures().add(each);
return result;
@@ -132,24 +127,6 @@ private Result runMain(JUnitSystem system, String... args) {
public String getVersion() {
return Version.id();
}
-
- /**
- * Convert method pattern into a list of {@link Description}.
- * @param clazz to search for methods matching <code>methodPattern</code>
- * @param methodPattern method name pattern
- * @return list of matching descriptions
- */
- private List<Description> findMatchingMethods(Class<?> clazz, String methodPattern) {
- TestClass testClass = new TestClass(clazz);
- List<Description> result = new ArrayList<Description>();
- for (FrameworkMethod m : testClass.getAnnotatedMethods(Test.class)) {
- final String methodName= m.getName();
- if (methodName.matches(methodPattern)) {
- result.add(Description.createTestDescription(clazz, methodName));
- }
- }
- return result;
- }
/**
* Run only test matching filter.
@@ -107,6 +107,15 @@ public String describe() {
}
};
}
+
+ /**
+ *
+ * @param desiredDescriptions is a list of methods
+ * @return <code>Filter</code> to run only methods listed in <code>desiredDescripitons</code>
+ */
+ public static Filter matchMethodDescriptions(final List<Description> desiredDescriptions) {
+ return matchMethodDescriptions(desiredDescriptions, FilterMode.STRICT);
+ }
/**
* @param description the description of the test to be run

0 comments on commit a566531

Please sign in to comment.