CVS: junit/src/org/junit/internal/runners TestClassMethodsRunner.java, 1.2, 1.3 TestIntrospector.java, 1.3, 1.4 MethodValidator.java, 1.2, 1.3 BeforeAndAfterRunner.java, 1.2, 1.3 MixIn.java, 1.1, NONE TestMethod.java, 1.1, NONE
David Saff <[email protected]>
| Newsgroups | gmane.comp.java.junit.devel |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/junit/junit/src/org/junit/internal/runners
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv11688/src/org/junit/internal/runners
Modified Files:
TestClassMethodsRunner.java TestIntrospector.java
MethodValidator.java BeforeAndAfterRunner.java
Removed Files:
MixIn.java TestMethod.java
Log Message:
Can @Ignore classes
assertArrayEquals added
Index: TestClassMethodsRunner.java
===================================================================
RCS file: /cvsroot/junit/junit/src/org/junit/internal/runners/TestClassMethodsRunner.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- TestClassMethodsRunner.java 27 Dec 2006 22:27:22 -0000 1.2
+++ TestClassMethodsRunner.java 24 Jan 2007 16:32:59 -0000 1.3
@@ -17,33 +17,28 @@
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
-import org.junit.runners.MethodRunner;
-public class TestClassMethodsRunner extends Runner implements Filterable,
- Sortable {
+public class TestClassMethodsRunner extends Runner implements Filterable, Sortable {
private final List<Method> fTestMethods;
-
private final Class<?> fTestClass;
- // This assumes that some containing runner will perform validation of the
- // test methods
+ // This assumes that some containing runner will perform validation of the test methods
public TestClassMethodsRunner(Class<?> klass) {
fTestClass= klass;
- fTestMethods= new TestIntrospector(getTestClass())
- .getTestMethods(Test.class);
+ fTestMethods= new TestIntrospector(getTestClass()).getTestMethods(Test.class);
}
-
+
@Override
public void run(RunNotifier notifier) {
if (fTestMethods.isEmpty())
- testAborted(notifier, getDescription(), new Exception(
- "No runnable methods"));
+ testAborted(notifier, getDescription(), new Exception("No runnable methods"));
for (Method method : fTestMethods)
invokeTestMethod(method, notifier);
}
- private void testAborted(RunNotifier notifier, Description description,
- Throwable cause) {
+ private void testAborted(RunNotifier notifier, Description description, Throwable cause) {
+ // TODO: duped!
+ // TODO: envious
notifier.fireTestStarted(description);
notifier.fireTestFailure(new Failure(description, cause));
notifier.fireTestFinished(description);
@@ -54,14 +49,14 @@
Description spec= Description.createSuiteDescription(getName());
List<Method> testMethods= fTestMethods;
for (Method method : testMethods)
- spec.addChild(methodDescription(method));
+ spec.addChild(methodDescription(method));
return spec;
}
protected String getName() {
return getTestClass().getName();
}
-
+
protected Object createTest() throws Exception {
return getTestClass().getConstructor().newInstance();
}
@@ -71,38 +66,17 @@
try {
test= createTest();
} catch (InvocationTargetException e) {
- testAborted(notifier, method, e.getCause());
- return;
- } catch (Throwable e) {
- testAborted(notifier, method, e);
- return;
- }
-
- TestMethod testMethod= new TestMethod(test, method,
- methodDescription(method));
- try {
- MethodRunner runner= testMethod.findCustomRunner(this);
- if (runner == null)
- createMethodRunner(test, method, notifier).run();
- else {
- runner.run(testMethod, notifier);
- }
+ testAborted(notifier, methodDescription(method), e.getCause());
+ return;
} catch (Exception e) {
- Throwable exception= new RuntimeException(
- "Exception creating custom method runner", e);
- testAborted(notifier, method, exception);
+ testAborted(notifier, methodDescription(method), e);
+ return;
}
+ createMethodRunner(test, method, notifier).run();
}
- private void testAborted(RunNotifier notifier, Method method,
- Throwable cause) {
- testAborted(notifier, methodDescription(method), cause);
- }
-
- protected TestMethodRunner createMethodRunner(Object test, Method method,
- RunNotifier notifier) {
- return new TestMethodRunner(test, method, notifier,
- methodDescription(method));
+ protected TestMethodRunner createMethodRunner(Object test, Method method, RunNotifier notifier) {
+ return new TestMethodRunner(test, method, notifier, methodDescription(method));
}
protected String testName(Method method) {
@@ -110,8 +84,7 @@
}
protected Description methodDescription(Method method) {
- return Description.createTestDescription(getTestClass(),
- testName(method));
+ return Description.createTestDescription(getTestClass(), testName(method));
}
public void filter(Filter filter) throws NoTestsRemainException {
@@ -127,8 +100,7 @@
public void sort(final Sorter sorter) {
Collections.sort(fTestMethods, new Comparator<Method>() {
public int compare(Method o1, Method o2) {
- return sorter.compare(methodDescription(o1),
- methodDescription(o2));
+ return sorter.compare(methodDescription(o1), methodDescription(o2));
}
});
}
Index: TestIntrospector.java
===================================================================
RCS file: /cvsroot/junit/junit/src/org/junit/internal/runners/TestIntrospector.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- TestIntrospector.java 27 Dec 2006 22:27:22 -0000 1.3
+++ TestIntrospector.java 24 Jan 2007 16:32:59 -0000 1.4
@@ -11,121 +11,76 @@
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Test.None;
-import org.junit.runners.Replaces;
-
-public class TestIntrospector {
- private static class MethodCollector {
- private List<Method> methods= new ArrayList<Method>();
-
- private Class<? extends Annotation> annotationClass;
-
- public MethodCollector(Class<? extends Annotation> annotationClass) {
- this.annotationClass= annotationClass;
- }
- public MethodCollector addMethods(Class<?> testClass) {
- for (Method eachMethod : testClass.getDeclaredMethods()) {
- if (hasAnnotation(eachMethod) && !isShadowed(eachMethod))
- methods.add(eachMethod);
- }
- addSuperclasses(testClass);
- addMixins(testClass);
- return this;
- }
-
- private boolean hasAnnotation(Method method) {
- return method.getAnnotation(annotationClass) != null
- || hasReplacementAnnotation(method);
- }
+public class TestIntrospector {
+ private final Class< ?> fTestClass;
+
+ public TestIntrospector(Class<?> testClass) {
+ fTestClass= testClass;
+ }
- private boolean hasReplacementAnnotation(Method method) {
- Annotation[] annotations= method.getAnnotations();
- for (Annotation annotation : annotations) {
- Replaces replaces= annotation.annotationType().getAnnotation(
- Replaces.class);
- if (replaces != null
- && replaces.value().equals(annotationClass))
- return true;
+ public List<Method> getTestMethods(Class<? extends Annotation> annotationClass) {
+ List<Method> results= new ArrayList<Method>();
+ for (Class<?> eachClass : getSuperClasses(fTestClass)) {
+ Method[] methods= eachClass.getDeclaredMethods();
+ for (Method eachMethod : methods) {
+ Annotation annotation= eachMethod.getAnnotation(annotationClass);
+ if (annotation != null && ! isShadowed(eachMethod, results))
+ results.add(eachMethod);
}
- return false;
}
+ if (runsTopToBottom(annotationClass))
+ Collections.reverse(results);
+ return results;
+ }
- private void addMixins(Class<?> testClass) {
- MixIn mixins= testClass.getAnnotation(MixIn.class);
- if (mixins != null)
- for (Class<?> type : mixins.value())
- addMethods(type);
- }
+ public boolean isIgnored(Method eachMethod) {
+ return eachMethod.getAnnotation(Ignore.class) != null;
+ }
- private void addSuperclasses(Class<?> testClass) {
- Class<?> superclass= testClass.getSuperclass();
- if (superclass != null)
- addMethods(testClass.getSuperclass());
+ private boolean runsTopToBottom(Class< ? extends Annotation> annotation) {
+ return annotation.equals(Before.class) || annotation.equals(BeforeClass.class);
+ }
+
+ private boolean isShadowed(Method method, List<Method> results) {
+ for (Method each : results) {
+ if (isShadowed(method, each))
+ return true;
}
+ return false;
+ }
- private boolean isShadowed(Method method) {
- for (Method each : methods) {
- if (isShadowed(method, each))
- return true;
- }
+ private boolean isShadowed(Method current, Method previous) {
+ if (! previous.getName().equals(current.getName()))
return false;
- }
-
- private boolean isShadowed(Method current, Method previous) {
- if (!previous.getName().equals(current.getName()))
- return false;
- if (previous.getParameterTypes().length != current
- .getParameterTypes().length)
+ if (previous.getParameterTypes().length != current.getParameterTypes().length)
+ return false;
+ for (int i= 0; i < previous.getParameterTypes().length; i++) {
+ if (! previous.getParameterTypes()[i].equals(current.getParameterTypes()[i]))
return false;
- for (int i= 0; i < previous.getParameterTypes().length; i++) {
- if (!previous.getParameterTypes()[i].equals(current
- .getParameterTypes()[i]))
- return false;
- }
- return true;
- }
-
- public List<Method> getMethods() {
- return methods;
}
+ return true;
}
- private final Class<?> fTestClass;
-
- public TestIntrospector(Class<?> testClass) {
- fTestClass= testClass;
- }
-
- public List<Method> getTestMethods(
- Class<? extends Annotation> annotationClass) {
- List<Method> results= new MethodCollector(annotationClass).addMethods(
- fTestClass).getMethods();
- if (runsTopToBottom(annotationClass))
- Collections.reverse(results);
+ private List<Class<?>> getSuperClasses(Class< ?> testClass) {
+ ArrayList<Class<?>> results= new ArrayList<Class<?>>();
+ Class<?> current= testClass;
+ while (current != null) {
+ results.add(current);
+ current= current.getSuperclass();
+ }
return results;
}
- public boolean isIgnored(Method method) {
- return method.getAnnotation(Ignore.class) != null;
- }
-
- private boolean runsTopToBottom(Class<? extends Annotation> annotation) {
- return annotation.equals(Before.class)
- || annotation.equals(BeforeClass.class);
- }
-
long getTimeout(Method method) {
Test annotation= method.getAnnotation(Test.class);
- if (annotation == null)
- return 0;
- return annotation.timeout();
+ long timeout= annotation.timeout();
+ return timeout;
}
Class<? extends Throwable> expectedException(Method method) {
Test annotation= method.getAnnotation(Test.class);
- if (annotation == null)
- return null;
if (annotation.expected() == None.class)
return null;
else
@@ -133,3 +88,4 @@
}
}
+
Index: MethodValidator.java
===================================================================
RCS file: /cvsroot/junit/junit/src/org/junit/internal/runners/MethodValidator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- MethodValidator.java 27 Dec 2006 17:45:54 -0000 1.2
+++ MethodValidator.java 24 Jan 2007 16:32:59 -0000 1.3
@@ -57,15 +57,14 @@
}
private void validateTestMethods(Class<? extends Annotation> annotation,
- boolean shouldBeStatic) {
+ boolean isStatic) {
List<Method> methods= fIntrospector.getTestMethods(annotation);
for (Method each : methods) {
- if (shouldBeStatic && !isStatic(each))
- fErrors.add(new Exception("Method " + each.getName() + "() "
- + "should be static"));
- if (!shouldBeStatic && runsAsStatic(each))
+ if (Modifier.isStatic(each.getModifiers()) != isStatic) {
+ String state= isStatic ? "should" : "should not";
fErrors.add(new Exception("Method " + each.getName() + "() "
- + "should not be static"));
+ + state + " be static"));
+ }
if (!Modifier.isPublic(each.getDeclaringClass().getModifiers()))
fErrors.add(new Exception("Class " + each.getDeclaringClass().getName()
+ " should be public"));
@@ -75,24 +74,9 @@
if (each.getReturnType() != Void.TYPE)
fErrors.add(new Exception("Method " + each.getName()
+ " should be void"));
- if (effectiveParameterCount(each, shouldBeStatic) != 0)
+ if (each.getParameterTypes().length != 0)
fErrors.add(new Exception("Method " + each.getName()
+ " should have no parameters"));
}
}
-
- private int effectiveParameterCount(Method method, boolean shouldBeStatic) {
- int rawLength= method.getParameterTypes().length;
- if (!shouldBeStatic && isStatic(method) && rawLength > 0)
- return rawLength - 1;
- return rawLength;
- }
-
- private boolean runsAsStatic(Method method) {
- return isStatic(method) && !(method.getParameterTypes().length == 1);
- }
-
- private boolean isStatic(Method method) {
- return Modifier.isStatic(method.getModifiers());
- }
}
Index: BeforeAndAfterRunner.java
===================================================================
RCS file: /cvsroot/junit/junit/src/org/junit/internal/runners/BeforeAndAfterRunner.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- BeforeAndAfterRunner.java 27 Dec 2006 17:45:54 -0000 1.2
+++ BeforeAndAfterRunner.java 24 Jan 2007 16:32:59 -0000 1.3
@@ -3,7 +3,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
import java.util.List;
public abstract class BeforeAndAfterRunner {
@@ -72,12 +71,6 @@
}
private void invokeMethod(Method method) throws Exception {
- if (Modifier.isStatic(method.getModifiers()))
- if (method.getParameterTypes().length > 0)
- method.invoke(null, fTest);
- else
- method.invoke(null);
- else
- method.invoke(fTest);
+ method.invoke(fTest);
}
}
--- MixIn.java DELETED ---
--- TestMethod.java DELETED ---
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV