From dc5f4661240f98469f498e6ab69a1512c8b39d0c Mon Sep 17 00:00:00 2001 From: "Craig P. Motlin" Date: Wed, 13 Nov 2013 22:08:16 -0500 Subject: [PATCH] Allow custom test runners to create their own TestClasses and customize the scanning of annotations. --- src/main/java/org/junit/runners/ParentRunner.java | 10 +++++++--- src/main/java/org/junit/runners/model/TestClass.java | 12 ++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) mode change 100644 => 100755 src/main/java/org/junit/runners/ParentRunner.java mode change 100644 => 100755 src/main/java/org/junit/runners/model/TestClass.java diff --git a/src/main/java/org/junit/runners/ParentRunner.java b/src/main/java/org/junit/runners/ParentRunner.java old mode 100644 new mode 100755 index b321c6a201d5..ff75ddd58232 --- a/src/main/java/org/junit/runners/ParentRunner.java +++ b/src/main/java/org/junit/runners/ParentRunner.java @@ -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 // @@ -218,7 +222,7 @@ private void validateClassRules(List errors) { * * * - * + * * @return {@code Statement} */ protected Statement classBlock(final RunNotifier notifier) { diff --git a/src/main/java/org/junit/runners/model/TestClass.java b/src/main/java/org/junit/runners/model/TestClass.java old mode 100644 new mode 100755 index b7a0f7510d1d..863b89dde1c2 --- a/src/main/java/org/junit/runners/model/TestClass.java +++ b/src/main/java/org/junit/runners/model/TestClass.java @@ -47,6 +47,13 @@ public TestClass(Class klass) { Map, List> fieldsForAnnotations = new LinkedHashMap, List>(); + scanAnnotatedMembers(methodsForAnnotations, fieldsForAnnotations); + + fMethodsForAnnotations = makeDeeplyUnmodifiable(methodsForAnnotations); + fFieldsForAnnotations = makeDeeplyUnmodifiable(fieldsForAnnotations); + } + + protected void scanAnnotatedMembers(Map, List> methodsForAnnotations, Map, List> fieldsForAnnotations) { for (Class eachClass : getSuperClasses(fClass)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); @@ -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) { @@ -72,7 +76,7 @@ public int compare(Field field1, Field field2) { return declaredFields; } - private static > void addToAnnotationLists(T member, + protected static > void addToAnnotationLists(T member, Map, List> map) { for (Annotation each : member.getAnnotations()) { Class type = each.annotationType();