KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestFor


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

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

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

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

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

66     public void testPartCoverage2() {
67         CoverageDecorator cd = new CoverageDecorator(PartCoverage2.class,
68                                                      COVERED);
69
70         TestResult result = new TestResult();
71
72         cd.run(result);
73     
74         // Only the expression in expressionFor() is not fully covered.
75
//org.hansel.Util.dumpResult(result);
76
assertEquals(1, result.failureCount());
77
78         // No errors
79
assertEquals(0, result.errorCount());
80     }
81
82     /**
83      * Test full coverage of the class.
84      */

85     public void testFullCoverage() {
86         CoverageDecorator cd = new CoverageDecorator(FullCoverage.class,
87                                                      COVERED);
88
89         TestResult result = new TestResult();
90
91         cd.run(result);
92       
93         // No failures
94
assertEquals(0, result.failureCount());
95
96         // No errors
97
assertEquals(0, result.errorCount());
98     }
99
100     /**
101      * TestCase does covers no code at all.
102      */

103     public static class NoCoverage extends TestCase {
104         /**
105          * Creates a new test.
106          * @param name Name of the test.
107          */

108         public NoCoverage(String JavaDoc name) {
109             super(name);
110         }
111
112         /** Empty method. */
113         public void testNothing() {
114         }
115     }
116
117     /**
118      * This testcase only calls the methods with condition=false..
119      */

120     public static class PartCoverage1 extends TestCase {
121         /**
122          * Creates a new test.
123          * @param name Name of the test.
124          */

125         public PartCoverage1(String JavaDoc name) {
126             super(name);
127         }
128         
129         /** Covers parts of the code. */
130         public void testAll() {
131             CoverFor ci = new CoverFor();
132
133             assertEquals(9, ci.simpleFor(4));
134         }
135
136     }
137
138     /**
139      * This testcase only calls the nestedIf methods with parameters
140      * (true, true). The other methods are fully covered.
141      */

142     public static class PartCoverage2 extends TestCase {
143         /**
144          * Creates a new test.
145          * @param name Name of the test.
146          */

147         public PartCoverage2(String JavaDoc name) {
148             super(name);
149         }
150         
151         /** Covers parts of the code. */
152         public void testAll() {
153             CoverFor ci = new CoverFor();
154
155             assertEquals(5, ci.simpleFor(0));
156             assertEquals(9, ci.simpleFor(4));
157
158             assertEquals(5, ci.expressionFor(0, 0));
159             assertEquals(9, ci.expressionFor(4, 8));
160        }
161
162     }
163
164     /**
165      * Test for full coverage.
166      */

167     public static class FullCoverage extends TestCase {
168         /**
169          * Creates a new test.
170          * @param name Name of the test.
171          */

172         public FullCoverage(String JavaDoc name) {
173             super(name);
174         }
175        
176         /** Covers all the code. */
177         public void testAll() {
178             CoverFor ci = new CoverFor();
179
180             assertEquals(5, ci.simpleFor(0));
181             assertEquals(9, ci.simpleFor(4));
182
183             assertEquals(5, ci.expressionFor(0, 0));
184             assertEquals(9, ci.expressionFor(4, 8));
185             assertEquals(10, ci.expressionFor(8, 5));
186         }
187     }
188
189
190
191 }
192
Popular Tags