KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestIf


1 package org.hanseltest;
2
3 import junit.framework.TestCase;
4 import junit.framework.TestResult;
5
6 import org.hansel.CoverageDecorator;
7 import org.hansel.Util;
8
9 /**
10  * Test the coverage test of if statements.
11  *
12  * @author Niklas Mehner
13  */

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

22     public TestIf(String JavaDoc name) {
23         super(name);
24     }
25
26     /**
27      * No code is covered.
28      */

29     public void testNoCoverage() {
30         CoverageDecorator cd = new CoverageDecorator(NoCoverage.class,
31                                                      COVERED);
32
33         TestResult result = new TestResult();
34
35         cd.run(result);
36
37         // 5 Failures: 4 Methods and 1 Constructor
38
assertEquals(5, result.failureCount());
39
40         // No errors
41
assertEquals(0, result.errorCount());
42     }
43
44     /**
45      * Only a part of the class is covered.
46      * See inner class PartCoverage1 for more detail.
47      */

48     public void testPartCoverage1() {
49         CoverageDecorator cd = new CoverageDecorator(PartCoverage1.class,
50                                                      COVERED);
51
52         TestResult result = new TestResult();
53
54         cd.run(result);
55
56         // Three failures: The first three methods are not fully covered.
57
assertEquals(3, result.failureCount());
58
59         // No errors
60
assertEquals(0, result.errorCount());
61     }
62
63     /**
64      * Only a part of the class is covered.
65      * See inner class PartCoverage2 for more detail.
66      */

67     public void testPartCoverage2() {
68         CoverageDecorator cd = new CoverageDecorator(PartCoverage2.class,
69                                                      COVERED);
70
71         TestResult result = new TestResult();
72
73         cd.run(result);
74
75         org.hansel.Util.dumpResult(result);
76         // Three failures: The first "if" is not fully covered.
77
// The second if is not fully covered. The third if is not covered at
78
// all but that failure message is suppressed, because it is caused by
79
// the first failure.
80
// Also the ifPointer method is not covered at all.
81
assertEquals(3, result.failureCount());
82
83         // No errors
84
assertEquals(0, result.errorCount());
85     }
86
87     /**
88      * Test full coverage of the class.
89      */

90     public void testFullCoverage() {
91         CoverageDecorator cd = new CoverageDecorator(FullCoverage.class,
92                                                      COVERED);
93
94         TestResult result = new TestResult();
95
96         cd.run(result);
97
98         Util.dumpResult(result);
99         // No failures
100
assertEquals(0, result.failureCount());
101
102         // No errors
103
assertEquals(0, result.errorCount());
104     }
105
106     /**
107      * TestCase does covers no code at all.
108      */

109     public static class NoCoverage extends TestCase {
110         /**
111          * Creates a new test.
112          * @param name Name of the test.
113          */

114         public NoCoverage(String JavaDoc name) {
115             super(name);
116         }
117
118         /** Empty method. */
119         public void testNothing() {
120         }
121     }
122
123     /**
124      * This testcase only calls the methods with condition=false..
125      */

126     public static class PartCoverage1 extends TestCase {
127         /**
128          * Creates a new test.
129          * @param name Name of the test.
130          */

131         public PartCoverage1(String JavaDoc name) {
132             super(name);
133         }
134         
135         /** Covers parts of the code. */
136         public void testAll() {
137             System.out.println("Probetable class: " + org.hansel.ProbeTable.class);
138             CoverIf ci = new CoverIf();
139
140             assertEquals(0, ci.simpleIf(false));
141             assertEquals(4, ci.ifElse(false));
142
143             assertEquals(1, ci.nestedIf(true, true));
144             assertEquals(2, ci.nestedIf(true, false));
145             assertEquals(3, ci.nestedIf(false, true));
146             assertEquals(4, ci.nestedIf(false, false));
147         }
148
149     }
150
151     /**
152      * This testcase only calls the nestedIf methods with parameters
153      * (true, true). The other methods are fully covered.
154      */

155     public static class PartCoverage2 extends TestCase {
156         /**
157          * Creates a new test.
158          * @param name Name of the test.
159          */

160         public PartCoverage2(String JavaDoc name) {
161             super(name);
162         }
163         
164         /** Covers parts of the code. */
165         public void testAll() {
166             CoverIf ci = new CoverIf();
167
168             // Cover the first two messages
169
assertEquals(0, ci.simpleIf(false));
170             assertEquals(1, ci.simpleIf(true));
171             assertEquals(4, ci.ifElse(false));
172             assertEquals(3, ci.ifElse(true));
173
174             // Only make one call to the third method.
175
assertEquals(1, ci.nestedIf(true, true));
176         }
177
178     }
179
180     /**
181      * Test for full coverage.
182      */

183     public static class FullCoverage extends TestCase {
184         /**
185          * Creates a new test.
186          * @param name Name of the test.
187          */

188         public FullCoverage(String JavaDoc name) {
189             super(name);
190         }
191        
192         /** Covers all the code. */
193         public void testAll() {
194             CoverIf ci = new CoverIf();
195
196             assertEquals(0, ci.simpleIf(false));
197             assertEquals(1, ci.simpleIf(true));
198
199             assertEquals(4, ci.ifElse(false));
200             assertEquals(3, ci.ifElse(true));
201
202             assertEquals(1, ci.nestedIf(true, true));
203             assertEquals(2, ci.nestedIf(true, false));
204             assertEquals(3, ci.nestedIf(false, true));
205             assertEquals(4, ci.nestedIf(false, false));
206
207             assertEquals(0, ci.ifPointer(null, null));
208             assertEquals(1, ci.ifPointer(new Object JavaDoc(), null));
209          }
210     }
211
212
213
214 }
215
Popular Tags