KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestSwitch


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 /**
10  * Test the coverage test of switch statements.
11  *
12  * @author Niklas Mehner
13  */

14 public class TestSwitch extends TestCase {
15     /** Class the tests cover. */
16     private static final Class JavaDoc[] COVERED = {CoverSwitch.class};
17
18     /**
19      * Create a new test.
20      * @param name Name of the test.
21      */

22     public TestSwitch(String JavaDoc name) {
23         super(name);
24     }
25
26     /**
27      * Assert that a failure message contains a given string.
28      * @param message Message that has to be contained in the failure.
29      * @param failure Failure, that should contain message.
30      */

31     private void assertContains(String JavaDoc message,
32                                 TestFailure failure) {
33         String JavaDoc 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     /**
43      * Returns the test result for running a coverage test on
44      * COVERED for a given test class.
45      *
46      * @param clazz The class of the test to be run.
47      * @return Result of the test run.
48      */

49     private TestResult getTestResult(Class JavaDoc 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     /**
61      * No code is covered.
62      */

63     public void testNoCoverage() {
64         TestResult result = getTestResult(NoCoverage.class);
65         // Four methods not covered, the switch probes do not report
66
// an failure, because, they are not hit at all.
67
assertEquals(4, result.failureCount());
68         assertEquals(0, result.errorCount());
69     }
70
71     /**
72      * Test full coverage.
73      */

74     public void testFullCoverage() {
75         TestResult result = getTestResult(FullCoverage.class);
76     
77         // Four methods not covered, the switch probes do not report
78
// an failure, because, they are not hit at all.
79
assertEquals(0, result.failureCount());
80         assertEquals(0, result.errorCount());
81     }
82
83     /**
84      * Test part coverage (1).
85      */

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     /**
99      * Test part coverage (2).
100      */

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     /**
113      * Test part coverage (3).
114      */

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     /**
127      * TestCase does covers no code at all.
128      */

129     public static class NoCoverage extends TestCase {
130         /**
131          * Creates a new test.
132          * @param name Name of the test.
133          */

134         public NoCoverage(String JavaDoc name) {
135             super(name);
136         }
137
138         /** Empty method. */
139         public void testNothing() {
140         }
141     }
142
143     /**
144      * Covers the whole CoverSwitch class.
145      */

146     public static class FullCoverage extends TestCase {
147         /**
148          * Creates a new test.
149          * @param name Name of the test.
150          */

151         public FullCoverage(String JavaDoc name) {
152             super(name);
153         }
154
155         /**
156          * Single Test of this class.
157          */

158         public void testAll() {
159             CoverSwitch cs = new CoverSwitch();
160             // Cover simpleSwitch
161
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             // Cover NoDefault
168
assertEquals(3, cs.coverNoDefault(0));
169             assertEquals(2, cs.coverNoDefault(5));
170
171             // Cover simpleSwitch
172
assertEquals(1, cs.coverSimpleSwitch2(1));
173             assertEquals(2, cs.coverSimpleSwitch2(5));
174             assertEquals(3, cs.coverSimpleSwitch2(7));
175         }
176     }
177
178     /**
179      * Covers only a part of the CoverSwitch class.
180      */

181     public static class PartCoverage extends TestCase {
182         /**
183          * Creates a new test.
184          * @param name Name of the test.
185          */

186         public PartCoverage(String JavaDoc name) {
187             super(name);
188         }
189
190         /**
191          * Single Test of this class.
192          */

193          public void testAll() {
194             CoverSwitch cs = new CoverSwitch();
195             // Cover simpleSwitch
196
// Covers only default case
197
assertEquals(4, cs.coverSimpleSwitch(-1));
198             assertEquals(4, cs.coverSimpleSwitch(2));
199
200             // Cover NoDefault
201
assertEquals(3, cs.coverNoDefault(0));
202             assertEquals(2, cs.coverNoDefault(5));
203
204             // Cover simpleSwitch
205
assertEquals(1, cs.coverSimpleSwitch2(1));
206             assertEquals(2, cs.coverSimpleSwitch2(5));
207             assertEquals(3, cs.coverSimpleSwitch2(7));
208         }
209     }
210
211     /**
212      * Covers only a part of the CoverSwitch class.
213      */

214     public static class PartCoverage2 extends TestCase {
215         /**
216          * Creates a new test.
217          * @param name Name of the test.
218          */

219         public PartCoverage2(String JavaDoc name) {
220             super(name);
221         }
222
223         /**
224          * Single Test of this class.
225          */

226         public void testAll() {
227             CoverSwitch cs = new CoverSwitch();
228             // Cover simpleSwitch
229
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             // Cover NoDefault
236
// Does not cover default case
237
assertEquals(3, cs.coverNoDefault(0));
238
239             // Cover simpleSwitch
240
assertEquals(1, cs.coverSimpleSwitch2(1));
241             assertEquals(2, cs.coverSimpleSwitch2(5));
242             assertEquals(3, cs.coverSimpleSwitch2(7));
243         }
244     }
245
246     /**
247      * Covers only a part of the CoverSwitch class.
248      */

249     public static class PartCoverage3 extends TestCase {
250         /**
251          * Creates a new test.
252          * @param name Name of the test.
253          */

254         public PartCoverage3(String JavaDoc name) {
255             super(name);
256         }
257
258         /**
259          * Single Test of this class.
260          */

261         public void testAll() {
262             CoverSwitch cs = new CoverSwitch();
263             // Cover simpleSwitch
264
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             // Cover NoDefault
271
// Does not cover default case
272
assertEquals(3, cs.coverNoDefault(0));
273             assertEquals(2, cs.coverNoDefault(5));
274
275             // Cover simpleSwitch
276
assertEquals(1, cs.coverSimpleSwitch2(1));
277             assertEquals(3, cs.coverSimpleSwitch2(7));
278         }
279     }
280 }
281
Popular Tags