Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom test runners to create their own TestClasses and customize the scanning of annotations. #763

Merged
merged 1 commit into from
Nov 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/org/junit/runners/ParentRunner.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,19 @@ public void finished() {
// do nothing
}
};

/**
* Constructs a new {@code ParentRunner} that will run {@code @TestClass}
*/
protected ParentRunner(Class<?> testClass) throws InitializationError {
fTestClass = new TestClass(testClass);
fTestClass = createTestClass(testClass);
validate();
}

protected TestClass createTestClass(Class<?> testClass) {
return new TestClass(testClass);
}

//
// Must be overridden
//
Expand Down Expand Up @@ -218,7 +222,7 @@ private void validateClassRules(List<Throwable> errors) {
* </ol>
* </li>
* </ol>
*
*
* @return {@code Statement}
*/
protected Statement classBlock(final RunNotifier notifier) {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/junit/runners/model/TestClass.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public TestClass(Class<?> klass) {
Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations =
new LinkedHashMap<Class<? extends Annotation>, List<FrameworkField>>();

scanAnnotatedMembers(methodsForAnnotations, fieldsForAnnotations);

fMethodsForAnnotations = makeDeeplyUnmodifiable(methodsForAnnotations);
fFieldsForAnnotations = makeDeeplyUnmodifiable(fieldsForAnnotations);
}

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);
Expand All @@ -57,9 +64,6 @@ public TestClass(Class<?> klass) {
addToAnnotationLists(new FrameworkField(eachField), fieldsForAnnotations);
}
}

fMethodsForAnnotations = makeDeeplyUnmodifiable(methodsForAnnotations);
fFieldsForAnnotations = makeDeeplyUnmodifiable(fieldsForAnnotations);
}

private static Field[] getSortedDeclaredFields(Class<?> clazz) {
Expand All @@ -72,7 +76,7 @@ public int compare(Field field1, Field field2) {
return declaredFields;
}

private static <T extends FrameworkMember<T>> void addToAnnotationLists(T member,
protected static <T extends FrameworkMember<T>> void addToAnnotationLists(T member,
Map<Class<? extends Annotation>, List<T>> map) {
for (Annotation each : member.getAnnotations()) {
Class<? extends Annotation> type = each.annotationType();
Expand Down