KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hansel > Probe


1 package org.hansel;
2
3 import java.io.PrintStream JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5
6 import junit.framework.AssertionFailedError;
7 import junit.framework.Test;
8 import junit.framework.TestResult;
9
10 import org.junit.runner.Description;
11 import org.junit.runner.notification.Failure;
12 import org.junit.runner.notification.RunNotifier;
13 import org.objectweb.asm.MethodVisitor;
14
15 /**
16  * Abstract super class of all probes.
17  * @author Niklas Mehner.
18  */

19 public abstract class Probe implements Test {
20     public static final String JavaDoc HIT_CLASS = ProbeTable.class.getName().replace('.', '/');
21
22     private ProbeData pd;
23     private int id;
24     private Description description;
25
26     /**
27      * Create a new Probe.
28      * @param pd Data for this probe.
29      */

30     public Probe(ProbeData pd) {
31         this.pd = pd;
32         this.id = ProbeTable.getProbeTable().addProbe(this);
33     }
34     
35     public abstract String JavaDoc getFailureMessage();
36
37     public abstract boolean displayFailure();
38     public abstract boolean coverageFailure();
39
40     /**
41      * Returns the code of the probe.
42      * @param cp ConstantPool all names of methods etc. have to be
43      * inserted.
44      * @return List of instructions to call this probe.
45      */

46     public abstract void insertProbeCode(MethodVisitor cv);
47  
48     public ProbeData getProbeData() {
49         return pd;
50     }
51
52     public int getID() {
53         return id;
54     }
55
56     public void addResult(Test test, TestResult result) {
57     }
58
59     public boolean equals(Object JavaDoc o) {
60         if (o.getClass() != getClass()) {
61             return false;
62         }
63
64         Probe p = (Probe) o;
65
66         return pd.equals(p.pd);
67     }
68
69     public int hashCode() {
70         return pd.hashCode();
71     }
72
73     public String JavaDoc toString() {
74         return "Probe in \"" + pd.getShortClassName()
75                + ".java\" line " + pd.getLineNumber();
76     }
77
78     public int countTestCases() {
79         return 1;
80     }
81
82     public void run(TestResult result) {
83         result.startTest(this);
84
85         if (displayFailure()) {
86             result.addFailure(this, new CoverageError(pd));
87         }
88
89         result.endTest(this);
90     }
91
92     public void run(RunNotifier result, Description coverageDescription )
93     throws ClassNotFoundException JavaDoc {
94         result.fireTestStarted(getDescription());
95         if (displayFailure()) {
96             result.fireTestFailure(new Failure(getDescription(), new CoverageError(pd)));
97         }
98         result.fireTestFinished(getDescription());
99     }
100
101     public String JavaDoc getName() {
102         return pd.getShortClassName() + ":" +
103                pd.getLineNumber();
104     }
105
106     private class CoverageError extends AssertionFailedError {
107         private static final long serialVersionUID = 1L;
108         private ProbeData pd;
109         
110         public CoverageError(ProbeData pd) {
111             super(" Coverage failure: " + getFailureMessage());
112             this.pd = pd;
113         }
114
115         public void printStackTrace() {
116             printStackTrace(System.err);
117         }
118
119         public void printStackTrace(PrintStream JavaDoc s) {
120             printStackTrace(new PrintWriter JavaDoc(s, true));
121         }
122
123         public void printStackTrace(PrintWriter JavaDoc s) {
124             s.println(getMessage());
125             s.println(" at "
126                       + pd.getClassName()
127                       + "."
128                       + pd.getMethodName()
129                       + "("
130                       + pd.getShortClassName()
131                       + ".java:"
132                       + pd.getLineNumber()
133                       + ")");
134         }
135
136     }
137
138     public Description getDescription() throws ClassNotFoundException JavaDoc {
139         if (description == null) {
140            description = Description.createTestDescription(
141                    pd.getTargetClass(), getName());
142         }
143         return description;
144     }
145 }
146
Popular Tags