1 package org.hanseltest; 2 3 import junit.framework.TestCase; 4 import junit.framework.TestFailure; 5 import junit.framework.TestResult; 6 7 import org.hansel.CoverageDecorator; 8 9 14 public class TestSwitch extends TestCase { 15 16 private static final Class [] COVERED = {CoverSwitch.class}; 17 18 22 public TestSwitch(String name) { 23 super(name); 24 } 25 26 31 private void assertContains(String message, 32 TestFailure failure) { 33 String failureMessage = failure.thrownException().getMessage(); 34 35 int index = failureMessage.indexOf(message); 36 37 assertTrue("Failure message '" + failureMessage 38 + "' does not contain '" + message + "'.", 39 index > -1); 40 } 41 42 49 private TestResult getTestResult(Class clazz) { 50 CoverageDecorator cd = new CoverageDecorator(clazz, 51 COVERED); 52 53 TestResult result = new TestResult(); 54 55 cd.run(result); 56 57 return result; 58 } 59 60 63 public void testNoCoverage() { 64 TestResult result = getTestResult(NoCoverage.class); 65 assertEquals(4, result.failureCount()); 68 assertEquals(0, result.errorCount()); 69 } 70 71 74 public void testFullCoverage() { 75 TestResult result = getTestResult(FullCoverage.class); 76 77 assertEquals(0, result.failureCount()); 80 assertEquals(0, result.errorCount()); 81 } 82 83 86 public void testPartCoverage() { 87 TestResult result = getTestResult(PartCoverage.class); 88 89 assertEquals(1, result.failureCount()); 90 assertEquals(0, result.errorCount()); 91 92 TestFailure failure = (TestFailure) result.failures().nextElement(); 93 94 assertContains("Switch-cases '0', '1' and '5' have not been covered.", 95 failure); 96 } 97 98 101 public void testPartCoverage2() { 102 TestResult result = getTestResult(PartCoverage2.class); 103 104 assertEquals(1, result.failureCount()); 105 assertEquals(0, result.errorCount()); 106 107 TestFailure failure = (TestFailure) result.failures().nextElement(); 108 109 assertContains("Switch-case 'default' has not been covered.", failure); 110 } 111 112 115 public void testPartCoverage3() { 116 TestResult result = getTestResult(PartCoverage3.class); 117 118 assertEquals(1, result.failureCount()); 119 assertEquals(0, result.errorCount()); 120 121 TestFailure failure = (TestFailure) result.failures().nextElement(); 122 assertContains("Switch-cases '4', '5' and '6' have not been covered.", 123 failure); 124 } 125 126 129 public static class NoCoverage extends TestCase { 130 134 public NoCoverage(String name) { 135 super(name); 136 } 137 138 139 public void testNothing() { 140 } 141 } 142 143 146 public static class FullCoverage extends TestCase { 147 151 public FullCoverage(String name) { 152 super(name); 153 } 154 155 158 public void testAll() { 159 CoverSwitch cs = new CoverSwitch(); 160 assertEquals(1, cs.coverSimpleSwitch(0)); 162 assertEquals(2, cs.coverSimpleSwitch(1)); 163 assertEquals(3, cs.coverSimpleSwitch(5)); 164 assertEquals(4, cs.coverSimpleSwitch(-1)); 165 assertEquals(4, cs.coverSimpleSwitch(2)); 166 167 assertEquals(3, cs.coverNoDefault(0)); 169 assertEquals(2, cs.coverNoDefault(5)); 170 171 assertEquals(1, cs.coverSimpleSwitch2(1)); 173 assertEquals(2, cs.coverSimpleSwitch2(5)); 174 assertEquals(3, cs.coverSimpleSwitch2(7)); 175 } 176 } 177 178 181 public static class PartCoverage extends TestCase { 182 186 public PartCoverage(String name) { 187 super(name); 188 } 189 190 193 public void testAll() { 194 CoverSwitch cs = new CoverSwitch(); 195 assertEquals(4, cs.coverSimpleSwitch(-1)); 198 assertEquals(4, cs.coverSimpleSwitch(2)); 199 200 assertEquals(3, cs.coverNoDefault(0)); 202 assertEquals(2, cs.coverNoDefault(5)); 203 204 assertEquals(1, cs.coverSimpleSwitch2(1)); 206 assertEquals(2, cs.coverSimpleSwitch2(5)); 207 assertEquals(3, cs.coverSimpleSwitch2(7)); 208 } 209 } 210 211 214 public static class PartCoverage2 extends TestCase { 215 219 public PartCoverage2(String name) { 220 super(name); 221 } 222 223 226 public void testAll() { 227 CoverSwitch cs = new CoverSwitch(); 228 assertEquals(1, cs.coverSimpleSwitch(0)); 230 assertEquals(2, cs.coverSimpleSwitch(1)); 231 assertEquals(3, cs.coverSimpleSwitch(5)); 232 assertEquals(4, cs.coverSimpleSwitch(-1)); 233 assertEquals(4, cs.coverSimpleSwitch(2)); 234 235 assertEquals(3, cs.coverNoDefault(0)); 238 239 assertEquals(1, cs.coverSimpleSwitch2(1)); 241 assertEquals(2, cs.coverSimpleSwitch2(5)); 242 assertEquals(3, cs.coverSimpleSwitch2(7)); 243 } 244 } 245 246 249 public static class PartCoverage3 extends TestCase { 250 254 public PartCoverage3(String name) { 255 super(name); 256 } 257 258 261 public void testAll() { 262 CoverSwitch cs = new CoverSwitch(); 263 assertEquals(1, cs.coverSimpleSwitch(0)); 265 assertEquals(2, cs.coverSimpleSwitch(1)); 266 assertEquals(3, cs.coverSimpleSwitch(5)); 267 assertEquals(4, cs.coverSimpleSwitch(-1)); 268 assertEquals(4, cs.coverSimpleSwitch(2)); 269 270 assertEquals(3, cs.coverNoDefault(0)); 273 assertEquals(2, cs.coverNoDefault(5)); 274 275 assertEquals(1, cs.coverSimpleSwitch2(1)); 277 assertEquals(3, cs.coverSimpleSwitch2(7)); 278 } 279 } 280 } 281 | Popular Tags |