KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestExpression


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 coverage of expressions.
10  *
11  * @author Niklas Mehner
12  */

13 public class TestExpression extends TestCase {
14     /** Class that should be covered by the tests. */
15     private static final Class JavaDoc[] COVERED = {CoverExpression.class};
16
17     /**
18      * Creates a new TestCase.
19      * @param name Name of the test.
20      */

21     public TestExpression(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         // 5 Failures: 4 Methods and 1 Constructor
37
assertEquals(5, result.failureCount());
38
39         // No errors
40
assertEquals(0, result.errorCount());
41     }
42
43     /**
44      * Validate that a test covering all of the code, does not cause
45      * any coverage failures or errors.
46      */

47     public void testFullCoverage() {
48         CoverageDecorator cd = new CoverageDecorator(FullCoverage.class,
49                                                      COVERED);
50
51         TestResult result = new TestResult();
52
53         cd.run(result);
54
55         assertEquals(0, result.failureCount());
56
57         // No errors
58
assertEquals(0, result.errorCount());
59     }
60
61     /** Does not cover any code. */
62     public static class NoCoverage extends TestCase {
63
64         /**
65          * Creates a new test.
66          * @param name Name of the test.
67          */

68         public NoCoverage(String JavaDoc name) {
69             super(name);
70         }
71
72         /**
73          * Tests nothing.
74          */

75         public void testNothing() {
76         }
77     }
78
79     /**
80      * Test for full coverage.
81      */

82     public static class FullCoverage extends TestCase {
83
84         /**
85          * Creates a new test.
86          * @param name Name of the test.
87          */

88         public FullCoverage(String JavaDoc name) {
89             super(name);
90         }
91         
92         /**
93          * tests everything.
94          */

95         public void testAll() {
96             CoverExpression ce = new CoverExpression();
97             assertTrue(ce.coverNot(false));
98             assertTrue(!ce.coverNot(true));
99
100             assertTrue(ce.coverAnd(true, true));
101             assertTrue(!ce.coverAnd(false, true));
102             assertTrue(!ce.coverAnd(true, false));
103
104             assertTrue(ce.coverOr(true, false));
105             assertTrue(!ce.coverOr(false, false));
106             assertTrue(ce.coverOr(false, true));
107
108             assertTrue(ce.coverComplex(true, true, true, true));
109             assertTrue(!ce.coverComplex(false, false, false, false));
110             assertTrue(ce.coverComplex(true, false, true, true));
111             assertTrue(!ce.coverComplex(true, false, true, false));
112         }
113     }
114
115 }
116
Popular Tags