KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestAssertions


1 package org.hanseltest;
2
3 import junit.framework.TestCase;
4 import junit.framework.TestResult;
5
6 import org.hansel.AssertionCoverageDecorator;
7 import org.hansel.Util;
8
9 /**
10  * Test coverage of code containing assertions.
11  * WARNING: This test will fail, if CoverAssertion is compiled without
12  * assertions.
13  *
14  * @author Niklas Mehner.
15  */

16 public class TestAssertions extends TestCase {
17     /** Class to cover. */
18     private static final Class JavaDoc[] COVERED = {CoverAssertion.class};
19
20     /**
21      * Create a new test.
22      * @param name Name of the test.
23      */

24     public TestAssertions(String JavaDoc name) {
25         super(name);
26     }
27
28     /**
29      * Find out, if assertions are enabled.
30      * @return true, if assertions are enabled.
31      */

32     private static boolean assertionsEnabled() {
33         boolean enabled = false;
34         assert enabled = true;
35         return enabled;
36     }
37
38     /**
39      * No code is covered.
40      */

41     public void testNoCoverage() {
42         AssertionCoverageDecorator cd =
43                 new AssertionCoverageDecorator(NoCoverage.class,
44                                                COVERED);
45
46         TestResult result = new TestResult();
47         cd.run(result);
48         
49         //org.hansel.Util.dumpResult(result);
50
// Three failures: methods "init" and "coverAssertion"
51
assertEquals(2, result.failureCount());
52
53         // No errors
54
assertEquals(0, result.errorCount());
55     }
56
57     /**
58      * All of the code is covered.
59      */

60     public void testFullCoverage() {
61         AssertionCoverageDecorator cd =
62                 new AssertionCoverageDecorator(FullCoverage.class,
63                                                COVERED);
64
65         TestResult result = new TestResult();
66         cd.run(result);
67
68
69         Util.dumpResult(result);
70         
71         // No failures
72
assertEquals(0, result.failureCount());
73
74         // No errors
75
assertEquals(0, result.errorCount());
76     }
77
78     /**
79      * Testcase covering no code.
80      */

81     public static class NoCoverage extends TestCase {
82         /**
83          * Create a new test.
84          * @param name Name of the test.
85          */

86         public NoCoverage(String JavaDoc name) {
87             super(name);
88         }
89         
90         /** Does nothing. */
91         public void testNothing() {
92         }
93     }
94
95     /**
96      * Testcase covering all of the code.
97      */

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

103         public FullCoverage(String JavaDoc name) {
104             super(name);
105         }
106         
107         /** Covers all of the code. */
108         public void testAll() {
109            CoverAssertion ca = new CoverAssertion();
110            assertEquals(2, ca.coverAssertion(2));
111
112            if (assertionsEnabled()) {
113                try {
114                    ca.coverAssertion(-1);
115                } catch (AssertionError JavaDoc e) {
116                    // we've been expecting this
117
}
118            } else {
119                assertEquals(-1, ca.coverAssertion(-1));
120            }
121         }
122     }
123 }
124
Popular Tags