1 package org.hansel; 2 3 import java.util.HashMap ; 4 import java.util.List ; 5 import java.util.Map ; 6 import java.util.Vector ; 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 29 public class ProbeTable extends TestSuite { 30 31 private static ProbeTable table; 32 33 34 private List <Probe> probes; 35 36 private Map <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 <Probe>(); 45 this.probeMap = new HashMap <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 if (probeMap.containsKey(probe.getProbeData())) { 58 throw new IllegalStateException ("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 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 obj1, Object obj2, int index) { 106 ((ACmpBranchProbe) getProbe(index)).hit(obj1, obj2); 107 } 108 109 public static void hitBranch(Object obj1, Object obj2, int index) { 110 getProbeTable().hit(obj1, obj2, index); 111 } 112 113 public void hit(Object obj, int index) { 114 ((NullCmpBranchProbe) getProbe(index)).hit(obj); 115 } 116 117 public static void hitBranch(Object 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 135 public int getProbeIndex() { 136 return probes.size(); 137 } 138 139 144 private Probe getProbe(int index) { 145 return (Probe) probes.get(index); 146 } 147 148 public void run(TestResult result) { 149 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 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 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(); 173 } 174 175 public void addProbeDescriptions(Description coverageDesc) throws ClassNotFoundException { 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 { 183 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 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 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(); 206 } 207 208 209 public static void setProbeTable(ProbeTable pt) { 210 table = pt; 211 } 212 213 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 |