Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 6 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
23 src/main/java/org/junit/runner/Description.java
@@ -5,9 +5,14 @@
5 5 import java.util.ArrayList;
6 6 import java.util.Arrays;
7 7 import java.util.Collection;
  8 +import java.util.List;
8 9 import java.util.regex.Matcher;
9 10 import java.util.regex.Pattern;
10 11
  12 +import org.junit.Test;
  13 +import org.junit.runners.model.FrameworkMethod;
  14 +import org.junit.runners.model.TestClass;
  15 +
11 16 /**
12 17 * <p>A <code>Description</code> describes a test which is to be run or has been run. <code>Descriptions</code>
13 18 * can be atomic (a single test) or compound (containing children tests). <code>Descriptions</code> are used
@@ -89,6 +94,24 @@ public static Description createSuiteDescription(Class<?> testClass) {
89 94 }
90 95
91 96 /**
  97 + * Create a <code>Description</code> with methods matching <code>methodPattern</code>
  98 + * @param clazz to search for methods matching <code>methodPattern</code>
  99 + * @param methodPattern regexp method name pattern
  100 + * @return
  101 + */
  102 + public static Description createSuiteDescription(Class<?> clazz, String methodPattern) {
  103 + TestClass testClass = new TestClass(clazz);
  104 + Description suite = createSuiteDescription(clazz);
  105 + for (FrameworkMethod m : testClass.getAnnotatedMethods(Test.class)) {
  106 + final String methodName= m.getName();
  107 + if (methodName.matches(methodPattern)) {
  108 + suite.addChild(createTestDescription(clazz, methodName));
  109 + }
  110 + }
  111 + return suite;
  112 + }
  113 +
  114 + /**
92 115 * Describes a Runner which runs no tests
93 116 */
94 117 public static final Description EMPTY= new Description("No Tests");
85 src/main/java/org/junit/runner/JUnitCore.java
... ... @@ -1,10 +1,10 @@
1 1 package org.junit.runner;
2 2
3   -import java.lang.reflect.Method;
4 3 import java.util.ArrayList;
5 4 import java.util.List;
6 5
7 6 import junit.runner.Version;
  7 +import org.junit.Test;
8 8 import org.junit.internal.JUnitSystem;
9 9 import org.junit.internal.RealSystem;
10 10 import org.junit.internal.TextListener;
@@ -14,6 +14,8 @@
14 14 import org.junit.runner.notification.Failure;
15 15 import org.junit.runner.notification.RunListener;
16 16 import org.junit.runner.notification.RunNotifier;
  17 +import org.junit.runners.model.FrameworkMethod;
  18 +import org.junit.runners.model.TestClass;
17 19
18 20 /**
19 21 * <code>JUnitCore</code> is a facade for running tests. It supports running JUnit 4 tests,
@@ -34,11 +36,6 @@
34 36 */
35 37 private static final String SEPARATOR= "#";
36 38
37   - /**
38   - * Method name wildcard, e.g. ClassName#M*Name
39   - */
40   - private static final String WILDCARD= "*";
41   -
42 39 private final RunNotifier fNotifier= new RunNotifier();
43 40
44 41 /**
@@ -96,24 +93,19 @@ private Result runMain(JUnitSystem system, String... args) {
96 93 for (String each : args)
97 94 try {
98 95 String[] class_method= each.split(SEPARATOR);
99   - if (class_method.length==2) { //found: ClassName#MethodName
100   - Class<?> clazz= Class.forName(class_method[0]);
101   - String methodName= class_method[1];
102   - final List<Description> foundMatchingMethods= findMatchingMethods(clazz, methodName);
103   - if (foundMatchingMethods.size()==0) {
104   - system.out().println("No matching method found for: " + methodName);
105   - Description description= Description.createSuiteDescription(each);
106   - Failure failure= new Failure(description, new NoTestsRemainException());
107   - missingClasses.add(failure);
108   - } else {
109   - methods.addAll(foundMatchingMethods);
110   - classes.add(clazz);
111   - }
112   - } else if (class_method.length==1 && each.endsWith(SEPARATOR)) { //found: ClassName#
113   - Class<?> clazz = Class.forName(class_method[0]);
  96 + Class<?> clazz= Class.forName(class_method[0]);
  97 + String methodName= ".*";
  98 + if (class_method.length == 2)
  99 + methodName= class_method[1];
  100 + final Description foundMatchingMethods= Description.createSuiteDescription(clazz, methodName);
  101 + if (foundMatchingMethods.getChildren().isEmpty()) {
  102 + system.out().println("No matching method found for: " + each);
  103 + Description description= Description.createSuiteDescription(each);
  104 + Failure failure= new Failure(description, new NoTestsRemainException());
  105 + missingClasses.add(failure);
  106 + } else {
  107 + methods.addAll(foundMatchingMethods.getChildren());
114 108 classes.add(clazz);
115   - } else { //assume ClassName otherwise, wrong format may cause ClassNotFoundException
116   - classes.add(Class.forName(each));
117 109 }
118 110 } catch (ClassNotFoundException e) {
119 111 system.out().println("Could not find class: " + each);
@@ -123,7 +115,7 @@ private Result runMain(JUnitSystem system, String... args) {
123 115 }
124 116 RunListener listener= new TextListener(system);
125 117 addListener(listener);
126   - Result result= run(createMethodFilter(methods), classes.toArray(new Class[0]));
  118 + Result result= run(Filter.matchMethodDescriptions(methods), classes.toArray(new Class[0]));
127 119 for (Failure each : missingClasses)
128 120 result.getFailures().add(each);
129 121 return result;
@@ -135,51 +127,6 @@ private Result runMain(JUnitSystem system, String... args) {
135 127 public String getVersion() {
136 128 return Version.id();
137 129 }
138   -
139   - /**
140   - * Convert method name with wildcard into a list of {@link Description}
141   - * @param clazz to search for methods matching <code>wildcardName</code>
142   - * @param wildcardName method name pattern
143   - * @return list of matching descriptions
144   - */
145   - private List<Description> findMatchingMethods(Class<?> clazz, String wildcardName) {
146   - List<Description> result = new ArrayList<Description>();
147   - String pattern = wildcardName.replace(WILDCARD, ".*");
148   - for (Method m : clazz.getMethods()) {
149   - final String methodName= m.getName();
150   - if (methodName.matches(pattern)) {
151   - result.add(Description.createTestDescription(clazz, methodName));
152   - }
153   - }
154   - return result;
155   - }
156   -
157   - /**
158   - * Construct new {@link Filter} based on method list.
159   - * @param methods contains methods allowed by this filter
160   - * @return new {@link Filter} to run only methods listed in <code>methods</code>
161   - */
162   - private Filter createMethodFilter(final List<Description> methods) {
163   - final List<String> classNames= new ArrayList<String>();
164   - for (Description d : methods) {
165   - classNames.add(d.getClassName());
166   - }
167   - return new Filter() {
168   - @Override
169   - public boolean shouldRun(Description description) {
170   - String methodName = description.getMethodName();
171   - String className = description.getClassName();
172   - if (methodName == null) {
173   - return true;
174   - }
175   - return classNames.contains(className)?methods.contains(description):true;
176   - }
177   - @Override
178   - public String describe() {
179   - return "command line method filter";
180   - }
181   - };
182   - }
183 130
184 131 /**
185 132 * Run only test matching filter.
52 src/main/java/org/junit/runner/manipulation/Filter.java
... ... @@ -1,5 +1,8 @@
1 1 package org.junit.runner.manipulation;
2 2
  3 +import java.util.ArrayList;
  4 +import java.util.List;
  5 +
3 6 import org.junit.runner.Description;
4 7 import org.junit.runner.Request;
5 8
@@ -64,6 +67,55 @@ public String describe() {
64 67 };
65 68 }
66 69
  70 + /**
  71 + * If a filter is to be created from a list, there are two modes to choose from:
  72 + * <li> RELAXED: filter runs all methods in the list <b>and</b> all methods from classes not contained in the list.
  73 + * <li> STRICT: filter runs only methods described in the list.
  74 + */
  75 + public enum FilterMode { RELAXED, STRICT }
  76 +
  77 + /**
  78 + * Returns a {@link Filter} that runs methods described by {@code desiredDescriptions}.
  79 + * @param desiredDescriptions contains methods allowed by this filter
  80 + * @param mode if set to strict runs only methods in {@code desiredDescriptions},
  81 + * if set to relaxed runs methods in {@code desiredDescriptions} and methods from classes not in {@code desiredDescriptions}
  82 + * @return new {@link Filter} based on {@code desiredDescriptions}
  83 + */
  84 + public static Filter matchMethodDescriptions(final List<Description> desiredDescriptions, final FilterMode mode) {
  85 + final List<String> classNames= new ArrayList<String>();
  86 + for (Description d : desiredDescriptions) {
  87 + classNames.add(d.getClassName());
  88 + }
  89 + return new Filter() {
  90 + @Override
  91 + public boolean shouldRun(Description description) {
  92 + String methodName = description.getMethodName();
  93 + String className = description.getClassName();
  94 + if (methodName == null) {
  95 + return true;
  96 + }
  97 + if (!classNames.contains(className) && mode.equals(FilterMode.RELAXED))
  98 + return true;
  99 + else if (!classNames.contains(className) && mode.equals(FilterMode.STRICT))
  100 + return false;
  101 + else
  102 + return desiredDescriptions.contains(description);
  103 + }
  104 + @Override
  105 + public String describe() {
  106 + return "Description list method filter";
  107 + }
  108 + };
  109 + }
  110 +
  111 + /**
  112 + *
  113 + * @param desiredDescriptions is a list of methods
  114 + * @return <code>Filter</code> to run only methods listed in <code>desiredDescripitons</code>
  115 + */
  116 + public static Filter matchMethodDescriptions(final List<Description> desiredDescriptions) {
  117 + return matchMethodDescriptions(desiredDescriptions, FilterMode.STRICT);
  118 + }
67 119
68 120 /**
69 121 * @param description the description of the test to be run
2  src/test/java/org/junit/tests/running/core/CommandLineTest.java
@@ -75,7 +75,7 @@ public void run() {
75 75 test2WasRun = false;
76 76 new MainRunner().runWithCheckForSystemExit(new Runnable() {
77 77 public void run() {
78   - JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example#t*st*");
  78 + JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example#t.*st.*");
79 79 }
80 80 });
81 81 assertTrue(testWasRun);

No commit comments for this range

Something went wrong with that request. Please try again.