KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hansel > ProbeTable


1 package org.hansel;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import junit.framework.AssertionFailedError;
9 import junit.framework.TestResult;
10 import junit.framework.TestSuite;
11
12 import org.hansel.probes.ACmpBranchProbe;
13 import org.hansel.probes.BinaryBranchProbe;
14 import org.hansel.probes.MethodProbe;
15 import org.hansel.probes.NullCmpBranchProbe;
16 import org.hansel.probes.ProbeFilter;
17 import org.hansel.probes.SelectProbe;
18 import org.hansel.probes.UnaryBranchProbe;
19 import org.junit.runner.Description;
20 import org.junit.runner.notification.Failure;
21 import org.junit.runner.notification.RunNotifier;
22
23 /**
24  * This class contains a table of currently active Probes.
25  * It implements the singleton pattern.
26  *
27  * @author Niklas Mehner
28  */

29 public class ProbeTable extends TestSuite {
30     /** Singleton instance of the ProbeTable. */
31     private static ProbeTable table;
32     
33     /** List containing currently active probes. */
34     private List JavaDoc<Probe> probes;
35     
36     private Map JavaDoc<ProbeData, Probe> probeMap;
37
38     private boolean displayStatistics;
39
40     private ProbeFilter probeFilter;
41
42     public ProbeTable(ProbeFilter probeFilter) {
43         super("Coverage Test");
44         this.probes = new Vector JavaDoc<Probe>();
45         this.probeMap = new HashMap JavaDoc<ProbeData, Probe>();
46         this.displayStatistics = false;
47         this.probeFilter = probeFilter;
48     }
49
50     public void setDisplayStatistics(boolean display) {
51         this.displayStatistics = display;
52     }
53
54     public int addProbe(Probe probe) {
55         // getCached should be called, before adding
56
// a new probe.
57
if (probeMap.containsKey(probe.getProbeData())) {
58             throw new IllegalStateException JavaDoc("Duplicate probe.");
59         }
60
61         probes.add(probe);
62         probeMap.put(probe.getProbeData(), probe);
63
64         if ((probeFilter == null) || !probeFilter.filter(probe.getProbeData())) {
65             addTest(probe);
66         }
67         
68         return probes.size() - 1;
69     }
70
71
72     public Probe getCached(ProbeData pd) {
73         return (Probe) probeMap.get(pd);
74     }
75
76     public void hit(int index) {
77         ((MethodProbe) getProbe(index)).hit();
78     }
79
80     /**
81      * This is the method inserted by the instrumentation, to
82      * record execution of a given probe.
83      * @param index Index of the probe.
84      */

85     public static void hitMethod(int index) {
86         getProbeTable().hit(index);
87     }
88     
89     public void hitB(int condition, int index) {
90         ((UnaryBranchProbe) getProbe(index)).hit(condition);
91     }
92     
93     public static void hitBranch(int condition, int index) {
94         getProbeTable().hitB(condition, index);
95     }
96     
97     public void hit(int n, int m, int index) {
98         ((BinaryBranchProbe) getProbe(index)).hit(n, m);
99     }
100     
101     public static void hitBranch(int n, int m, int index) {
102         getProbeTable().hit(n, m, index);
103     }
104     
105     public void hit(Object JavaDoc obj1, Object JavaDoc obj2, int index) {
106         ((ACmpBranchProbe) getProbe(index)).hit(obj1, obj2);
107     }
108     
109     public static void hitBranch(Object JavaDoc obj1, Object JavaDoc obj2, int index) {
110         getProbeTable().hit(obj1, obj2, index);
111     }
112
113     public void hit(Object JavaDoc obj, int index) {
114         ((NullCmpBranchProbe) getProbe(index)).hit(obj);
115     }
116     
117     public static void hitBranch(Object JavaDoc obj, int index) {
118         getProbeTable().hit(obj, index);
119     }
120
121     public void hit(int value, int index) {
122         ((SelectProbe) getProbe(index)).hit(value);
123     }
124
125     public static void hitSelect(int value, int index) {
126        getProbeTable().hit(value, index);
127     }
128
129     /**
130      * Returns the index for the next probe. The index of a probe has to be
131      * unique, because it is used to identify the probe, when a call to
132      * ProbeTable.hit(index) is made by the instrumented code.
133      * @return Index for the next probe.
134      */

135     public int getProbeIndex() {
136         return probes.size();
137     }
138
139     /**
140      * Return the probe for a given index.
141      * @param index Index of the probe.
142      * @return Probe.
143      */

144     private Probe getProbe(int index) {
145         return (Probe) probes.get(index);
146     }
147
148     public void run(TestResult result) {
149         // Only add coverage problems, if the test did not cause
150
// any other errors/failures.
151
if ((result.errorCount() == 0) &&
152             (result.failureCount() == 0)) {
153             super.run(result);
154
155             int covered = getCovered();
156             int count = getSize();
157             if (displayStatistics && (covered != count)) {
158                 String JavaDoc msg = "Coverage Test failed: Only " + covered + " of "
159                              + count + " probes covered.";
160                 result.addFailure(this, new AssertionFailedError(msg));
161             }
162         } else {
163             result.startTest(this);
164  
165             String JavaDoc msg = "Coverage Failure: No coverage test performed, "
166             + "because test failed.";
167             result.addFailure(this, new AssertionFailedError(msg));
168             result.endTest(this);
169         }
170
171         // Clear the probe table
172
clear();
173     }
174
175     public void addProbeDescriptions(Description coverageDesc) throws ClassNotFoundException JavaDoc {
176         for (Probe probe : probes) {
177             coverageDesc.addChild(probe.getDescription());
178         }
179     }
180
181     public void run(RunNotifier result, Description coverageDescription, boolean hasErrors)
182     throws ClassNotFoundException JavaDoc {
183         // Only add coverage problems, if the test did not cause
184
// any other errors/failures.
185
if (!hasErrors) {
186             int covered = getCovered();
187             int count = getSize();
188             for (Probe probe : probes) {
189                probe.run(result, coverageDescription);
190             }
191             if (displayStatistics && (covered != count)) {
192                 String JavaDoc msg = "Coverage Test failed: Only " + covered + " of "
193                              + count + " probes covered.";
194                 result.fireTestFailure(new Failure(coverageDescription,
195                         new AssertionFailedError(msg)));
196             }
197         } else {
198             String JavaDoc msg = "Coverage Failure: No coverage test performed, "
199             + "because test failed.";
200             result.fireTestFailure(new Failure(coverageDescription,
201                     new AssertionFailedError(msg)));
202         }
203
204         // Clear the probe table
205
clear();
206     }
207     
208     
209     public static void setProbeTable(ProbeTable pt) {
210         table = pt;
211     }
212
213     /**
214      * Returns the singleton instance of the ProbeTable.
215      * @return ProbeTable.
216      */

217     public static ProbeTable getProbeTable() {
218         return table;
219     }
220
221     public void clear() {
222         probes.clear();
223     }
224
225     public int getSize() {
226         return probes.size();
227     }
228
229     public int getCovered() {
230         int covered = 0;
231         for (int i=0; i<probes.size(); i++) {
232             Probe probe = (Probe) probes.get(i);
233             if (!probe.coverageFailure()) {
234                 covered++;
235             }
236         }
237
238         return covered;
239     }
240
241     
242 }
243
Popular Tags