001: package org.hansel;
002:
003: import java.io.PrintStream;
004: import java.io.PrintWriter;
005:
006: import junit.framework.AssertionFailedError;
007: import junit.framework.Test;
008: import junit.framework.TestResult;
009:
010: import org.junit.runner.Description;
011: import org.junit.runner.notification.Failure;
012: import org.junit.runner.notification.RunNotifier;
013: import org.objectweb.asm.MethodVisitor;
014:
015: /**
016: * Abstract super class of all probes.
017: * @author Niklas Mehner.
018: */
019: public abstract class Probe implements Test {
020: public static final String HIT_CLASS = ProbeTable.class.getName()
021: .replace('.', '/');
022:
023: private ProbeData pd;
024: private int id;
025: private Description description;
026:
027: /**
028: * Create a new Probe.
029: * @param pd Data for this probe.
030: */
031: public Probe(ProbeData pd) {
032: this .pd = pd;
033: this .id = ProbeTable.getProbeTable().addProbe(this );
034: }
035:
036: public abstract String getFailureMessage();
037:
038: public abstract boolean displayFailure();
039:
040: public abstract boolean coverageFailure();
041:
042: /**
043: * Returns the code of the probe.
044: * @param cp ConstantPool all names of methods etc. have to be
045: * inserted.
046: * @return List of instructions to call this probe.
047: */
048: public abstract void insertProbeCode(MethodVisitor cv);
049:
050: public ProbeData getProbeData() {
051: return pd;
052: }
053:
054: public int getID() {
055: return id;
056: }
057:
058: public void addResult(Test test, TestResult result) {
059: }
060:
061: public boolean equals(Object o) {
062: if (o.getClass() != getClass()) {
063: return false;
064: }
065:
066: Probe p = (Probe) o;
067:
068: return pd.equals(p.pd);
069: }
070:
071: public int hashCode() {
072: return pd.hashCode();
073: }
074:
075: public String toString() {
076: return "Probe in \"" + pd.getShortClassName() + ".java\" line "
077: + pd.getLineNumber();
078: }
079:
080: public int countTestCases() {
081: return 1;
082: }
083:
084: public void run(TestResult result) {
085: result.startTest(this );
086:
087: if (displayFailure()) {
088: result.addFailure(this , new CoverageError(pd));
089: }
090:
091: result.endTest(this );
092: }
093:
094: public void run(RunNotifier result, Description coverageDescription)
095: throws ClassNotFoundException {
096: result.fireTestStarted(getDescription());
097: if (displayFailure()) {
098: result.fireTestFailure(new Failure(getDescription(),
099: new CoverageError(pd)));
100: }
101: result.fireTestFinished(getDescription());
102: }
103:
104: public String getName() {
105: return pd.getShortClassName() + ":" + pd.getLineNumber();
106: }
107:
108: private class CoverageError extends AssertionFailedError {
109: private static final long serialVersionUID = 1L;
110: private ProbeData pd;
111:
112: public CoverageError(ProbeData pd) {
113: super (" Coverage failure: " + getFailureMessage());
114: this .pd = pd;
115: }
116:
117: public void printStackTrace() {
118: printStackTrace(System.err);
119: }
120:
121: public void printStackTrace(PrintStream s) {
122: printStackTrace(new PrintWriter(s, true));
123: }
124:
125: public void printStackTrace(PrintWriter s) {
126: s.println(getMessage());
127: s.println(" at " + pd.getClassName() + "."
128: + pd.getMethodName() + "(" + pd.getShortClassName()
129: + ".java:" + pd.getLineNumber() + ")");
130: }
131:
132: }
133:
134: public Description getDescription() throws ClassNotFoundException {
135: if (description == null) {
136: description = Description.createTestDescription(pd
137: .getTargetClass(), getName());
138: }
139: return description;
140: }
141: }
|