001: package org.hanseltest;
002:
003: import junit.framework.TestCase;
004: import junit.framework.TestResult;
005:
006: import org.hansel.CoverageDecorator;
007:
008: /**
009: * Test the coverage test of for statements.
010: *
011: * @author Niklas Mehner
012: */
013: public class TestFor extends TestCase {
014: /** Class the tests cover. */
015: private static final Class[] COVERED = { CoverFor.class };
016:
017: /**
018: * Create a new test.
019: * @param name Name of the test.
020: */
021: public TestFor(String name) {
022: super (name);
023: }
024:
025: /**
026: * No code is covered.
027: */
028: public void testNoCoverage() {
029: CoverageDecorator cd = new CoverageDecorator(NoCoverage.class,
030: COVERED);
031:
032: TestResult result = new TestResult();
033:
034: cd.run(result);
035:
036: // 3 Failures: 2 Methods and 1 Constructor
037: assertEquals(3, result.failureCount());
038:
039: // No errors
040: assertEquals(0, result.errorCount());
041: }
042:
043: /**
044: * Only a part of the class is covered.
045: * See inner class PartCoverage1 for more detail.
046: */
047: public void testPartCoverage1() {
048: CoverageDecorator cd = new CoverageDecorator(
049: PartCoverage1.class, COVERED);
050:
051: TestResult result = new TestResult();
052:
053: cd.run(result);
054:
055: // One failures: The second method is not covered.
056: assertEquals(1, result.failureCount());
057:
058: // No errors
059: assertEquals(0, result.errorCount());
060: }
061:
062: /**
063: * Only a part of the class is covered.
064: * See inner class PartCoverage2 for more detail.
065: */
066: public void testPartCoverage2() {
067: CoverageDecorator cd = new CoverageDecorator(
068: PartCoverage2.class, COVERED);
069:
070: TestResult result = new TestResult();
071:
072: cd.run(result);
073:
074: // Only the expression in expressionFor() is not fully covered.
075: //org.hansel.Util.dumpResult(result);
076: assertEquals(1, result.failureCount());
077:
078: // No errors
079: assertEquals(0, result.errorCount());
080: }
081:
082: /**
083: * Test full coverage of the class.
084: */
085: public void testFullCoverage() {
086: CoverageDecorator cd = new CoverageDecorator(
087: FullCoverage.class, COVERED);
088:
089: TestResult result = new TestResult();
090:
091: cd.run(result);
092:
093: // No failures
094: assertEquals(0, result.failureCount());
095:
096: // No errors
097: assertEquals(0, result.errorCount());
098: }
099:
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 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 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 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 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: }
|