From: David S. <ds...@us...> - 2006-10-17 19:06:12
|
Update of /cvsroot/junit/junit/org/junit/internal/runners In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv18074/org/junit/internal/runners Modified Files: Tag: saff_r41_runner_refactoring JavaMethod.java JavaClass.java Added Files: Tag: saff_r41_runner_refactoring JavaModelElement.java Log Message: Introduce JavaModelElement (this may eventually get rid of BeforeAndAfterRunner, but I'm not sure yet) --- NEW FILE: JavaModelElement.java --- package org.junit.internal.runners; public abstract class JavaModelElement { public abstract String getName(); } Index: JavaMethod.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaMethod.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- JavaMethod.java 26 Sep 2006 16:51:39 -0000 1.1.2.1 +++ JavaMethod.java 17 Oct 2006 19:06:09 -0000 1.1.2.2 @@ -4,18 +4,25 @@ package org.junit.internal.runners; import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.List; -class JavaMethod { +import org.junit.Ignore; +import org.junit.Test; +import org.junit.Test.None; + +public class JavaMethod extends JavaModelElement { // TODO: push out private final Method fMethod; - JavaMethod(Method current) { + public JavaMethod(Method current) { fMethod= current; } - public boolean isShadowedBy(Method previous) { + private boolean isShadowedBy(JavaMethod previousJavaMethod) { + Method previous= previousJavaMethod.fMethod; if (!previous.getName().equals(fMethod.getName())) return false; if (previous.getParameterTypes().length != fMethod @@ -29,8 +36,8 @@ return true; } - boolean isShadowedBy(List<Method> results) { - for (Method each : results) { + boolean isShadowedBy(List<JavaMethod> results) { + for (JavaMethod each : results) { if (isShadowedBy(each)) return true; } @@ -41,7 +48,84 @@ return fMethod.getAnnotation(methodAnnotation.getAnnotationClass()); } - public Method getMethod() { - return fMethod; + boolean isIgnored() { + return fMethod.getAnnotation(Ignore.class) != null; + } + + long getTimeout() { + return getTestAnnotation().timeout(); + } + + private Test getTestAnnotation() { + return fMethod.getAnnotation(Test.class); + } + + Class<? extends Throwable> expectedException() { + Test annotation= getTestAnnotation(); + if (annotation.expected() == None.class) + return null; + else + return annotation.expected(); + } + + boolean isUnexpected(Throwable exception) { + return ! expectedException().isAssignableFrom(exception.getClass()); + } + + boolean expectsException() { + return expectedException() != null; + } + + void invoke(Object object) throws IllegalAccessException, + InvocationTargetException { + fMethod.invoke(object); + } + + @Override + public String getName() { + return fMethod.getName(); + } + + void validateAsTestMethod(boolean isStatic, List<Throwable> errors) { + Method each= fMethod; + if (Modifier.isStatic(each.getModifiers()) != isStatic) { + String state= isStatic ? "should" : "should not"; + errors.add(new Exception("Method " + each.getName() + "() " + + state + " be static")); + } + if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) + errors.add(new Exception("Class " + each.getDeclaringClass().getName() + + " should be public")); + if (!Modifier.isPublic(each.getModifiers())) + errors.add(new Exception("Method " + each.getName() + + " should be public")); + if (each.getReturnType() != Void.TYPE) + errors.add(new Exception("Method " + each.getName() + + " should be void")); + if (each.getParameterTypes().length != 0) + errors.add(new Exception("Method " + each.getName() + + " should have no parameters")); + } + + void runUnprotected(JavaTestInterpreter javaTestInterpreter, Object test, PerTestNotifier perTestNotifier) { + try { + javaTestInterpreter.executeMethodBody(test, this); + if (expectsException()) + perTestNotifier.addFailure(new AssertionError( + "Expected exception: " + + expectedException().getName())); + } catch (InvocationTargetException e) { + Throwable actual= e.getTargetException(); + if (!expectsException()) + perTestNotifier.addFailure(actual); + else if (isUnexpected(actual)) { + String message= "Unexpected exception, expected<" + + expectedException().getName() + + "> but was<" + actual.getClass().getName() + ">"; + perTestNotifier.addFailure(new Exception(message, actual)); + } + } catch (Throwable e) { + perTestNotifier.addFailure(e); + } } } \ No newline at end of file Index: JavaClass.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaClass.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- JavaClass.java 26 Sep 2006 16:51:39 -0000 1.1.2.1 +++ JavaClass.java 17 Oct 2006 19:06:09 -0000 1.1.2.2 @@ -4,16 +4,17 @@ package org.junit.internal.runners; import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; -class JavaClass { +public class JavaClass extends JavaModelElement { // TODO: push out private final Class<?> fClass; - JavaClass(Class<?> type) { + public JavaClass(Class<?> type) { fClass= type; } @@ -27,14 +28,13 @@ return results; } - List<Method> getTestMethods(MethodAnnotation methodAnnotation) { - List<Method> results= new ArrayList<Method>(); + List<JavaMethod> getTestMethods(MethodAnnotation methodAnnotation) { + List<JavaMethod> results= new ArrayList<JavaMethod>(); for (JavaClass eachClass : getSuperClasses()) { - List<JavaMethod> methods= eachClass.getDeclaredMethods(); - for (JavaMethod eachMethod : methods) { + for (JavaMethod eachMethod : eachClass.getDeclaredMethods()) { Annotation annotation= eachMethod.getAnnotation(methodAnnotation); if (annotation != null && ! eachMethod.isShadowedBy(results)) - results.add(eachMethod.getMethod()); + results.add(eachMethod); } } if (methodAnnotation.runsTopToBottom()) @@ -51,7 +51,30 @@ return javaMethods; } - List<Method> getTestMethods(Class<? extends Annotation> type) { + List<JavaMethod> getTestMethods(Class<? extends Annotation> type) { return getTestMethods(new MethodAnnotation(type)); } + + public Class getTestClass() { + return fClass; + } + + void validateNoArgConstructor(List<Throwable> errors) { + try { + getTestClass().getConstructor(); + } catch (Exception e) { + errors.add(new Exception("Test class should have public zero-argument constructor", e)); + } + } + + @Override + public String getName() { + return fClass.getName(); + } + + Object newTestObject() + throws InstantiationException, IllegalAccessException, + InvocationTargetException, NoSuchMethodException { + return getTestClass().getConstructor().newInstance(); + } } \ No newline at end of file |