KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestSimple


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 basic coverage problems.
11  * @author Niklas Mehner
12  */

13 public class TestSimple extends TestCase {
14     /** Class to be covered by this test. */
15     private static final Class JavaDoc[] COVERED = {CoverSimple.class};
16
17     /**
18      * Create a new Test.
19      * @param name Name of the test.
20      */

21     public TestSimple(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         cd.run(result);
34
35         // Two failures: Constructor and method
36
assertEquals(2, result.failureCount());
37
38         // No errors
39
assertEquals(0, result.errorCount());
40     }
41
42     /**
43      * No code is covered, but test fails. No coverage results
44      * are added to the result in this case.
45      */

46     public void testErrorNoCoverage() {
47         CoverageDecorator cd = new CoverageDecorator(ErrorNoCoverage.class,
48                                                      COVERED);
49
50         TestResult result = new TestResult();
51         cd.run(result);
52
53         // One failure: "No coverage test performed"
54
assertEquals(1, result.failureCount());
55
56         // No errors
57
assertEquals(1, result.errorCount());
58     }
59
60     /**
61      * Only the constructor is covered.
62      */

63     public void testConstructorCoverage() {
64         CoverageDecorator cd =
65                  new CoverageDecorator(ConstructorCoverage.class,
66                                        COVERED);
67
68         TestResult result = new TestResult();
69         cd.run(result);
70     
71         assertEquals(1, result.failureCount());
72
73         // No errors
74
assertEquals(0, result.errorCount());
75     }
76
77     /**
78      * Test full coverage.
79      */

80     public void testFullCoverage() throws Exception JavaDoc {
81         CoverageDecorator cd =
82                  new CoverageDecorator(FullCoverage.class,
83                                        COVERED);
84
85         TestResult result = new TestResult();
86         cd.run(result);
87         
88         Util.dumpResult(result);
89         assertEquals(0, result.failureCount());
90
91         // No errors
92
assertEquals(0, result.errorCount());
93     }
94
95     /**
96      * Covertest testing nothing.
97      */

98     public static class NoCoverage extends TestCase {
99         /**
100          * Create a new Test.
101          * @param name Name of the test.
102          */

103         public NoCoverage(String JavaDoc name) {
104             super(name);
105         }
106         
107         /** Does nothing. */
108         public void testNothing() {
109         }
110     }
111
112     /**
113      * This is a test that fails to cover any code and throws an exception
114      * when run.
115      */

116     public static class ErrorNoCoverage extends TestCase {
117         /**
118          * Create a new Test.
119          * @param name Name of the test.
120          */

121         public ErrorNoCoverage(String JavaDoc name) {
122             super(name);
123         }
124         
125         /**
126          * Throws an UnsupportedOperationException.
127          */

128         public void testNothing() {
129             throw new UnsupportedOperationException JavaDoc("Unsupported.");
130         }
131     }
132          
133     /**
134      * This test covers only the constructor.
135      */

136     public static class ConstructorCoverage extends TestCase {
137         /**
138          * Create a new Test.
139          * @param name Name of the test.
140          */

141          public ConstructorCoverage(String JavaDoc name) {
142             super(name);
143         }
144
145         /** Invokes the CoverSimple() constructor. */
146         public void testConstructor() {
147             new CoverSimple();
148         }
149     }
150
151     /** TestCase covering the whole CoverSimple class. */
152     public static class FullCoverage extends TestCase {
153         
154         /** Covers all of the class. */
155         public void testFullCoverage() {
156             CoverSimple cs = new CoverSimple();
157             assertEquals(5, cs.coverSimple());
158         }
159     }
160
161 }
162
Popular Tags