private void addTestsFromTestCase(final Class<?> theClass) { fName = theClass.getName(); try { getTestConstructor(theClass); // Avoid generating multiple error messages } catch (NoSuchMethodException e) { addTest(warning("Class " + theClass.getName() + " has no public constructor TestCase(String name) or TestCase()")); return; } if (!Modifier.isPublic(theClass.getModifiers())) { addTest(warning("Class " + theClass.getName() + " is not public")); return; } Class<?> superClass = theClass; List<String> names = new ArrayList<String>(); while (Test.class.isAssignableFrom(superClass)) { for (Method each : MethodSorter.getDeclaredMethods(superClass)) { addTestMethod(each, names, theClass); } superClass = superClass.getSuperclass(); } if (fTests.size() == 0) { addTest(warning("No tests found in " + theClass.getName())); } }
public List<Method> getAnnotatedMethods(Class<? extends Annotation> annotationClass) { List<Method> results = new ArrayList<Method>(); for (Class<?> eachClass : getSuperClasses(fClass)) { Method[] methods = MethodSorter.getDeclaredMethods(eachClass); for (Method eachMethod : methods) { Annotation annotation = eachMethod.getAnnotation(annotationClass); if (annotation != null && !isShadowed(eachMethod, results)) { results.add(eachMethod); } } } if (runsTopToBottom(annotationClass)) { Collections.reverse(results); } return results; }
/** * Creates a {@code TestClass} wrapping {@code klass}. Each time this * constructor executes, the class is scanned for annotations, which can be * an expensive process (we hope in future JDK's it will not be.) Therefore, * try to share instances of {@code TestClass} where possible. */ public TestClass(Class<?> klass) { fClass = klass; if (klass != null && klass.getConstructors().length > 1) { throw new IllegalArgumentException( "Test class can only have one constructor"); } for (Class<?> eachClass : getSuperClasses(fClass)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), fMethodsForAnnotations); } for (Field eachField : eachClass.getDeclaredFields()) { addToAnnotationLists(new FrameworkField(eachField), fFieldsForAnnotations); } } }
@Override public void setUp() throws Exception { baseSetUp(); if (!beforeClassDone) { beforeClass(); beforeClassDone = true; } if (lastTest == null) { // for class-level afterClass, list the test methods and do the // afterClass in the tearDown of last method Class<?> scanClass = getClass(); while (Test.class.isAssignableFrom(scanClass)) { for (Method m : MethodSorter.getDeclaredMethods(scanClass)) { String methodName = m.getName(); if (methodName.startsWith("test") && m.getParameterTypes().length == 0 && m.getReturnType().equals(Void.TYPE)) { lastTest = methodName; } } scanClass = scanClass.getSuperclass(); } if (lastTest == null) { fail("Could not find any last test in " + getClass().getName()); } else { getLogWriter() .info("Last test for " + getClass().getName() + ": " + lastTest); } } }
private static Class<?> findExpectedType(Class<?> fromClass) { for (Class<?> c = fromClass; c != Object.class; c = c.getSuperclass()) { for (Method method : MethodSorter.getDeclaredMethods(c)) { if (isMatchesSafelyMethod(method)) { return method.getParameterTypes()[0]; } } } throw new Error("Cannot determine correct type for matchesSafely() method."); }
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) { for (Class<?> eachClass : getSuperClasses(fClass)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); } // ensuring fields are sorted to make sure that entries are inserted // and read from fieldForAnnotations in a deterministic order for (Field eachField : getSortedDeclaredFields(eachClass)) { addToAnnotationLists(new FrameworkField(eachField), fieldsForAnnotations); } } }