Permalink
Browse files

CategoriesMark2 reuse code properly from TestClass

  • Loading branch information...
1 parent 04189a8 commit 1bbab715de4c64943948ecc3739c8d5569378672 David Saff committed Feb 9, 2010
@@ -2,7 +2,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
-import java.util.ArrayList;
import java.util.List;
import org.junit.After;
@@ -182,7 +181,8 @@ protected void validateInstanceMethods(List<Throwable> errors) {
}
private void validateFields(List<Throwable> errors) {
- for (FrameworkField each : ruleFields())
+ for (FrameworkField each : getTestClass()
+ .getAnnotatedFields(Rule.class))
validateRuleField(each.getField(), errors);
}
@@ -352,35 +352,12 @@ protected Statement withAfters(FrameworkMethod method, Object target,
private Statement withRules(FrameworkMethod method, Object target,
Statement statement) {
Statement result= statement;
- for (MethodRule each : rules(target))
+ for (MethodRule each : getTestClass().getAnnotatedFieldValues(target,
+ Rule.class, MethodRule.class))
result= each.apply(result, method, target);
return result;
}
- /**
- * @return the MethodRules that can transform the block that runs each
- * method in the tested class.
- */
- protected List<MethodRule> rules(Object test) {
- List<MethodRule> results= new ArrayList<MethodRule>();
- for (FrameworkField each : ruleFields())
- results.add(createRule(test, each));
- return results;
- }
-
- private List<FrameworkField> ruleFields() {
- return getTestClass().getAnnotatedFields(Rule.class);
- }
-
- private MethodRule createRule(Object test, FrameworkField each) {
- try {
- return (MethodRule) each.get(test);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(
- "How did getFields return a field we couldn't access?");
- }
- }
-
private EachTestNotifier makeNotifier(FrameworkMethod method,
RunNotifier notifier) {
Description description= describeChild(method);
@@ -20,6 +20,7 @@
private final Class<?> fClass;
private Map<Class<?>, List<FrameworkMethod>> fMethodsForAnnotations= new HashMap<Class<?>, List<FrameworkMethod>>();
+
private Map<Class<?>, List<FrameworkField>> fFieldsForAnnotations= new HashMap<Class<?>, List<FrameworkField>>();
/**
@@ -36,13 +37,16 @@ public TestClass(Class<?> klass) {
for (Class<?> eachClass : getSuperClasses(fClass)) {
for (Method eachMethod : eachClass.getDeclaredMethods())
- addToAnnotationLists(new FrameworkMethod(eachMethod), fMethodsForAnnotations);
+ addToAnnotationLists(new FrameworkMethod(eachMethod),
+ fMethodsForAnnotations);
for (Field eachField : eachClass.getDeclaredFields())
- addToAnnotationLists(new FrameworkField(eachField), fFieldsForAnnotations);
+ addToAnnotationLists(new FrameworkField(eachField),
+ fFieldsForAnnotations);
}
}
- private <T extends FrameworkMember<T>> void addToAnnotationLists(T member, Map<Class<?>, List<T>> map) {
+ private <T extends FrameworkMember<T>> void addToAnnotationLists(T member,
+ Map<Class<?>, List<T>> map) {
for (Annotation each : member.getAnnotations()) {
Class<? extends Annotation> type= each.annotationType();
List<T> members= getAnnotatedMembers(map, type);
@@ -65,10 +69,11 @@ public TestClass(Class<?> klass) {
}
/**
- * Returns, efficiently, all the non-overridden fields in this class and
- * its superclasses that are annotated with {@code annotationClass}.
+ * Returns, efficiently, all the non-overridden fields in this class and its
+ * superclasses that are annotated with {@code annotationClass}.
*/
- public List<FrameworkField> getAnnotatedFields(Class<? extends Annotation> annotationClass) {
+ public List<FrameworkField> getAnnotatedFields(
+ Class<? extends Annotation> annotationClass) {
return getAnnotatedMembers(fFieldsForAnnotations, annotationClass);
}
@@ -129,4 +134,18 @@ public String getName() {
return new Annotation[0];
return fClass.getAnnotations();
}
+
+ public <T> List<T> getAnnotatedFieldValues(Object test,
+ Class<? extends Annotation> annotationClass, Class<T> valueClass) {
+ List<T> results= new ArrayList<T>();
+ for (FrameworkField each : getAnnotatedFields(annotationClass)) {
+ try {
+ results.add(valueClass.cast(each.get(test)));
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(
+ "How did getFields return a field we couldn't access?");
+ }
+ }
+ return results;
+ }
}
@@ -8,7 +8,6 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
-import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -23,32 +22,33 @@
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
-import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;
public class CategoriesMark2Test {
- public static class CategoryFilter2 extends Filter2 {
+ public static class CategoryFilter extends Filter2 {
private final Class<?> fIncluded;
- public CategoryFilter2(Class<?> included) {
+ public CategoryFilter(Class<?> included) {
fIncluded= included;
}
- public static CategoryFilter2 include(Class<?> included) {
- return new CategoryFilter2(included);
+ public static CategoryFilter include(Class<?> included) {
+ return new CategoryFilter(included);
}
+ // TODO: this type correct?
@Override
public List<Runner> matchingRunners(
List<? extends Runner> allPossibleRunners) {
ArrayList<Runner> result= new ArrayList<Runner>();
for (Runner eachRunner : allPossibleRunners) {
- Collection<Annotation> annotations= eachRunner.getDescription().getAnnotations();
+ Collection<Annotation> annotations= eachRunner.getDescription()
+ .getAnnotations();
// TODO: extract method
for (Annotation eachAnnotation : annotations) {
if (eachAnnotation.annotationType().equals(Category.class)) {
- Category category = (Category) eachAnnotation;
+ Category category= (Category) eachAnnotation;
Class<?>[] categories= category.value();
if (Arrays.asList(categories).contains(fIncluded))
result.add(eachRunner);
@@ -84,6 +84,8 @@ public Listed(Class<?>... classes) {
}
}
+ // Classes -> RunnerBuilder
+
public static abstract class Classes {
public abstract Collection<? extends Class<?>> get();
@@ -96,10 +98,17 @@ public Listed(Class<?>... classes) {
}
public static class Suite2 extends Runner {
- private final Class<?> fTestClass;
+ private final TestClass fTestClass;
+
+ private final Object fInstance;
- public Suite2(Class<?> testClass) {
- fTestClass= testClass;
+ public Suite2(Class<?> testClass) throws InitializationError {
+ fTestClass= new TestClass(testClass);
+ try {
+ fInstance= fTestClass.getOnlyConstructor().newInstance();
+ } catch (Exception e) {
+ throw new InitializationError(e);
+ }
}
@Override
@@ -108,6 +117,7 @@ public Description getDescription() {
return null;
}
+ // TODO: require an instance?
@Override
public void run(RunNotifier notifier) {
List<Class<?>> allPossibleClasses= gatherClasses();
@@ -126,17 +136,8 @@ public void run(RunNotifier notifier) {
}
private List<Filter2> getFilters() {
- // TODO: shouldn't do this twice
- ArrayList<Filter2> result= new ArrayList<Filter2>();
- TestClass testClass= new TestClass(fTestClass);
- Object target= createInstance(testClass);
- List<FrameworkField> fields= testClass
- .getAnnotatedFields(FilterWith.class);
- for (FrameworkField each : fields)
- result.add((Filter2) getValue(each, target));
-
- // TODO Auto-generated method stub
- return result;
+ return fTestClass.getAnnotatedFieldValues(fInstance,
+ FilterWith.class, Filter2.class);
}
private List<Runner> runnersForClasses(List<Class<?>> allPossibleClasses) {
@@ -155,54 +156,12 @@ public void run(RunNotifier notifier) {
private List<Class<?>> gatherClasses() {
ArrayList<Class<?>> result= new ArrayList<Class<?>>();
- // TODO: Something's wrong with the naming here.
- TestClass testClass= new TestClass(fTestClass);
- // TODO: where _should_ we be instantiating this?
- Object target= createInstance(testClass);
- List<FrameworkField> fields= testClass
- .getAnnotatedFields(SuiteClasses2.class);
- for (FrameworkField each : fields) {
- Classes classes= (Classes) getValue(each, target);
- // TODO: naming?
- result.addAll(classes.get());
- }
+ List<Classes> classeses= fTestClass.getAnnotatedFieldValues(
+ fInstance, SuiteClasses2.class, Classes.class);
+ for (Classes each : classeses)
+ result.addAll(each.get());
return result;
}
-
- private Object getValue(FrameworkField each, Object target) {
- // TODO: we must do this for rules, right?
- try {
- return each.get(target);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // TODO Auto-generated method stub
- return null;
- }
-
- private Object createInstance(TestClass testClass) {
- try {
- return testClass.getOnlyConstructor().newInstance();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // TODO Auto-generated method stub
- return null;
- }
}
static class Yes {
@@ -238,7 +197,7 @@ public void no1() {
public Classes classes= new Listed(Yes1.class, No1.class);
@FilterWith
- public Filter2 filter= CategoryFilter2.include(Yes.class);
+ public Filter2 filter= CategoryFilter.include(Yes.class);
}
@RunWith(Suite2.class)
@@ -247,7 +206,7 @@ public void no1() {
public Classes classes= new Listed(Yes1.class, Yes2.class, No1.class);
@FilterWith
- public Filter2 filter= CategoryFilter2.include(Yes.class);
+ public Filter2 filter= CategoryFilter.include(Yes.class);
}
@RunWith(Suite2.class)
@@ -262,11 +221,11 @@ public void no1() {
public Classes classes= new Listed(Yes1.class, Yes2.class, No1.class);
@FilterWith
- public Filter2 filter= CategoryFilter2.include(No.class);
+ public Filter2 filter= CategoryFilter.include(No.class);
}
@Test
- public void gatherClasses() {
+ public void gatherClasses() throws InitializationError {
assertEquals(2, new Suite2(OnlyYesJustOne.class).gatherClasses().size());
}
@@ -290,16 +249,17 @@ public void runAllThree() {
assertEquals(3, result.getRunCount());
assertThat(testResult(Everything.class), isSuccessful());
}
-
- @Test public void runOneNo() {
+
+ @Test
+ public void runOneNo() {
Result result= new JUnitCore().run(Nos.class);
assertEquals(1, result.getRunCount());
- assertThat(testResult(Nos.class), isSuccessful());
+ assertThat(testResult(Nos.class), isSuccessful());
}
@Test
public void matchingRunnersOnCategories() throws InitializationError {
- assertEquals(1, CategoryFilter2.include(Yes.class).matchingRunners(
+ assertEquals(1, CategoryFilter.include(Yes.class).matchingRunners(
Arrays.asList(new BlockJUnit4ClassRunner(Yes1.class))).size());
}
}

0 comments on commit 1bbab71

Please sign in to comment.