KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.junit.internal.runners;
2
3 import junit.extensions.TestDecorator;
4 import junit.framework.AssertionFailedError;
5 import junit.framework.JUnit4TestAdapter;
6 import junit.framework.JUnit4TestCaseFacade;
7 import junit.framework.Test;
8 import junit.framework.TestCase;
9 import junit.framework.TestListener;
10 import junit.framework.TestResult;
11 import junit.framework.TestSuite;
12 import org.junit.runner.Description;
13 import org.junit.runner.Runner;
14 import org.junit.runner.notification.Failure;
15 import org.junit.runner.notification.RunNotifier;
16
17 public class OldTestClassRunner extends Runner {
18     private static final class OldTestClassAdaptingListener implements
19             TestListener {
20         private final RunNotifier fNotifier;
21
22         private OldTestClassAdaptingListener(RunNotifier notifier) {
23             fNotifier= notifier;
24         }
25
26         public void endTest(Test test) {
27             fNotifier.fireTestFinished(asDescription(test));
28         }
29
30         public void startTest(Test test) {
31             fNotifier.fireTestStarted(asDescription(test));
32         }
33
34         // Implement junit.framework.TestListener
35
public void addError(Test test, Throwable JavaDoc t) {
36             Failure failure= new Failure(asDescription(test), t);
37             fNotifier.fireTestFailure(failure);
38         }
39
40         private Description asDescription(Test test) {
41             if (test instanceof JUnit4TestCaseFacade) {
42                 JUnit4TestCaseFacade facade= (JUnit4TestCaseFacade) test;
43                 return facade.getDescription();
44             }
45             return Description.createTestDescription(test.getClass(), getName(test));
46         }
47
48         private String JavaDoc getName(Test test) {
49             if (test instanceof TestCase)
50                 return ((TestCase) test).getName();
51             else
52                 return test.toString();
53         }
54
55         public void addFailure(Test test, AssertionFailedError t) {
56             addError(test, t);
57         }
58     }
59
60     private Test fTest;
61     
62     @SuppressWarnings JavaDoc("unchecked")
63     public OldTestClassRunner(Class JavaDoc<?> klass) {
64         this(new TestSuite(klass.asSubclass(TestCase.class)));
65     }
66
67     public OldTestClassRunner(Test test) {
68         super();
69         fTest= test;
70     }
71
72     @Override JavaDoc
73     public void run(RunNotifier notifier) {
74         TestResult result= new TestResult();
75         result.addListener(createAdaptingListener(notifier));
76         fTest.run(result);
77     }
78
79     public static TestListener createAdaptingListener(final RunNotifier notifier) {
80         return new OldTestClassAdaptingListener(notifier);
81     }
82     
83     @Override JavaDoc
84     public Description getDescription() {
85         return makeDescription(fTest);
86     }
87
88     private Description makeDescription(Test test) {
89         if (test instanceof TestCase) {
90             TestCase tc= (TestCase) test;
91             return Description.createTestDescription(tc.getClass(), tc.getName());
92         } else if (test instanceof TestSuite) {
93             TestSuite ts= (TestSuite) test;
94             String JavaDoc name= ts.getName() == null ? "" : ts.getName();
95             Description description= Description.createSuiteDescription(name);
96             int n= ts.testCount();
97             for (int i= 0; i < n; i++)
98                 description.addChild(makeDescription(ts.testAt(i)));
99             return description;
100         } else if (test instanceof JUnit4TestAdapter) {
101             JUnit4TestAdapter adapter= (JUnit4TestAdapter) test;
102             return adapter.getDescription();
103         } else if (test instanceof TestDecorator) {
104             TestDecorator decorator= (TestDecorator) test;
105             return makeDescription(decorator.getTest());
106         } else {
107             // This is the best we can do in this case
108
return Description.createSuiteDescription(test.getClass());
109         }
110     }
111 }
112
Popular Tags