KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > internal > runners > TestClassMethodsRunner


1 package org.junit.internal.runners;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.Comparator JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.junit.Test;
11 import org.junit.runner.Description;
12 import org.junit.runner.Runner;
13 import org.junit.runner.manipulation.Filter;
14 import org.junit.runner.manipulation.Filterable;
15 import org.junit.runner.manipulation.NoTestsRemainException;
16 import org.junit.runner.manipulation.Sortable;
17 import org.junit.runner.manipulation.Sorter;
18 import org.junit.runner.notification.RunNotifier;
19
20 public class TestClassMethodsRunner extends Runner implements Filterable, Sortable {
21     private final List JavaDoc<Method JavaDoc> fTestMethods;
22     private final Class JavaDoc<?> fTestClass;
23
24     // This assumes that some containing runner will perform validation of the test methods
25
public TestClassMethodsRunner(Class JavaDoc<?> klass) {
26         fTestClass= klass;
27         fTestMethods= new TestIntrospector(getTestClass()).getTestMethods(Test.class);
28     }
29     
30     @Override JavaDoc
31     public void run(RunNotifier notifier) {
32         if (fTestMethods.isEmpty())
33             notifier.testAborted(getDescription(), new Exception JavaDoc("No runnable methods"));
34         for (Method JavaDoc method : fTestMethods)
35             invokeTestMethod(method, notifier);
36     }
37
38     @Override JavaDoc
39     public Description getDescription() {
40         Description spec= Description.createSuiteDescription(getName());
41         List JavaDoc<Method JavaDoc> testMethods= fTestMethods;
42         for (Method JavaDoc method : testMethods)
43                 spec.addChild(methodDescription(method));
44         return spec;
45     }
46
47     protected String JavaDoc getName() {
48         return getTestClass().getName();
49     }
50     
51     protected Object JavaDoc createTest() throws Exception JavaDoc {
52         return getTestClass().getConstructor().newInstance();
53     }
54
55     protected void invokeTestMethod(Method JavaDoc method, RunNotifier notifier) {
56         Object JavaDoc test;
57         try {
58             test= createTest();
59         } catch (InvocationTargetException JavaDoc e) {
60             notifier.testAborted(methodDescription(method), e.getCause());
61             return;
62         } catch (Exception JavaDoc e) {
63             notifier.testAborted(methodDescription(method), e);
64             return;
65         }
66         createMethodRunner(test, method, notifier).run();
67     }
68
69     protected TestMethodRunner createMethodRunner(Object JavaDoc test, Method JavaDoc method, RunNotifier notifier) {
70         return new TestMethodRunner(test, method, notifier, methodDescription(method));
71     }
72
73     protected String JavaDoc testName(Method JavaDoc method) {
74         return method.getName();
75     }
76
77     protected Description methodDescription(Method JavaDoc method) {
78         return Description.createTestDescription(getTestClass(), testName(method));
79     }
80
81     public void filter(Filter filter) throws NoTestsRemainException {
82         for (Iterator JavaDoc<Method JavaDoc> iter= fTestMethods.iterator(); iter.hasNext();) {
83             Method JavaDoc method= iter.next();
84             if (!filter.shouldRun(methodDescription(method)))
85                 iter.remove();
86         }
87         if (fTestMethods.isEmpty())
88             throw new NoTestsRemainException();
89     }
90
91     public void sort(final Sorter sorter) {
92         Collections.sort(fTestMethods, new Comparator JavaDoc<Method JavaDoc>() {
93             public int compare(Method JavaDoc o1, Method JavaDoc o2) {
94                 return sorter.compare(methodDescription(o1), methodDescription(o2));
95             }
96         });
97     }
98
99     protected Class JavaDoc<?> getTestClass() {
100         return fTestClass;
101     }
102 }
Popular Tags