KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hansel > CoverageDecorator


1 package org.hansel;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.HashSet JavaDoc;
5
6 import junit.framework.TestResult;
7 import junit.framework.TestSuite;
8
9 import org.hansel.probes.ProbeFilter;
10
11 /**
12  * This class decorated tests with coverage testing.
13  * Test classes that are added to this decorator are reloaded with an
14  * instrumenting classloader, that adds coverage checks to the code.
15  * After all tests are run, and if non of them has failed, the coverage results
16  * are added to the result of the test.
17  *
18  * @author Niklas Mehner
19  */

20 public class CoverageDecorator extends TestSuite {
21     private String JavaDoc[] classNamesCovered;
22     private Class JavaDoc[] classesCovered;
23     
24     //private TestSuite testSuite;
25

26     private ProbeTable probeTable;
27     
28     /**
29      * Creates a new (empty) CoverageDecorator. This does not
30      * print out any coverage statistics
31      * @param classesCovered Classes that have to be covered.
32      */

33     public CoverageDecorator(Class JavaDoc[] classesCovered) {
34         this(classesCovered, null);
35     }
36     
37     /**
38      * Creates a new (empty) CoverageDecorator. This does not
39      * print out any coverage statistics
40      * @param classesCovered Classes that have to be covered.
41      */

42     public CoverageDecorator(Class JavaDoc[] classesCovered,
43                              ProbeFilter probeFilter) {
44         patchTestClassLoader();
45         this.classesCovered = classesCovered;
46         this.classNamesCovered = new String JavaDoc[classesCovered.length];
47         for (int i=0; i<classNamesCovered.length; i++) {
48             this.classNamesCovered[i] = classesCovered[i].getName();
49         }
50         this.probeTable = new ProbeTable(probeFilter);
51     }
52     
53     private void patchTestClassLoader() {
54         // Warning: Ugly code.
55
// TODO: Reimplement for junit4?
56
/* if (getClass().getClassLoader() instanceof TestCaseClassLoader) {
57             try {
58                 Class tccl = TestCaseClassLoader.class;
59                 java.lang.reflect.Field f = tccl.getDeclaredField("fExcluded");
60                 f.setAccessible(true);
61                 Vector v = (Vector) f.get(getClass().getClassLoader());
62                 if (!v.contains("org.hansel.")) {
63                     v.add("org.hansel.");
64                 }
65             } catch (Exception e) {
66                 e.printStackTrace();
67             }
68         } */

69     }
70     /**
71      * Creates a new CoverageDecorator containing the test instantiated
72      * from the given class
73      * @param testClass Class the test is instantiated from. The restrictions
74      * for the class are the same as for <code> junit.framework.addTestSuite()
75      * </code>
76      * @param classesCovered Classes that have to be covered.
77      */

78     public CoverageDecorator(Class JavaDoc testClass, Class JavaDoc[] classesCovered) {
79         this(classesCovered);
80         
81         addTestSuite(testClass);
82     }
83     
84     /**
85      * Creates a new CoverageDecorator containging the test instantiated
86      * from the given class
87      * @param testClass Class the test is instantiated from. The restrictions
88      * for the class are the same as for <code> junit.framework.addTestSuite()
89      * </code>
90      * @param classesCovered Classes that have to be covered.
91      */

92     public CoverageDecorator(Class JavaDoc testClass,
93                  Class JavaDoc[] classesCovered,
94                  ProbeFilter probeFilter) {
95         this(classesCovered, probeFilter);
96         
97         addTestSuite(testClass);
98     }
99     
100     public void setDisplayStatistics(boolean display) {
101         probeTable.setDisplayStatistics(display);
102     }
103     
104     public Class JavaDoc[] getClassesCovered() {
105         return classesCovered;
106     }
107     
108     protected ProbeTable getProbeTable() {
109         return probeTable;
110     }
111     
112     protected void superRun(TestResult result) {
113         super.run(result);
114     }
115     
116     protected boolean init(TestResult result) {
117         ProbeTable.setProbeTable(probeTable);
118
119         try {
120             Startup.init(new HashSet JavaDoc<String JavaDoc>(Arrays.asList(classNamesCovered)));
121         } catch (Exception JavaDoc e) {
122             result.addError(this, e);
123             return false;
124         }
125
126         return true;
127     }
128
129     protected void shutdown(TestResult result) {
130         try {
131             loadClasses();
132             Startup.tearDown();
133             
134             probeTable.run(result);
135         } catch (Exception JavaDoc e) {
136             result.addError(this, e);
137         }
138     }
139
140     public void run(TestResult result) {
141         //System.out.println("Running test.");
142
if (!init(result)) {
143             return;
144         }
145
146         super.run(result);
147
148         shutdown(result);
149     //System.out.println("Exiting test.");
150
}
151
152     private void loadClasses() {
153         // Make sure all classes are linked, so all probes get created.
154
for (int i=0; i<classesCovered.length; i++) {
155             classesCovered[i].getDeclaredFields();
156         }
157     }
158     
159     public String JavaDoc toString() {
160         return "Coverage Decorator";
161     }
162 }
163
Popular Tags