001: package org.hanseltest;
002:
003: import junit.framework.TestCase;
004: import junit.framework.TestResult;
005:
006: import org.hansel.CoverageDecorator;
007: import org.hansel.Util;
008:
009: /**
010: * Test the coverage test of if statements.
011: *
012: * @author Niklas Mehner
013: */
014: public class TestIf extends TestCase {
015: /** Class the tests cover. */
016: private static final Class[] COVERED = { CoverIf.class };
017:
018: /**
019: * Create a new test.
020: * @param name Name of the test.
021: */
022: public TestIf(String name) {
023: super (name);
024: }
025:
026: /**
027: * No code is covered.
028: */
029: public void testNoCoverage() {
030: CoverageDecorator cd = new CoverageDecorator(NoCoverage.class,
031: COVERED);
032:
033: TestResult result = new TestResult();
034:
035: cd.run(result);
036:
037: // 5 Failures: 4 Methods and 1 Constructor
038: assertEquals(5, result.failureCount());
039:
040: // No errors
041: assertEquals(0, result.errorCount());
042: }
043:
044: /**
045: * Only a part of the class is covered.
046: * See inner class PartCoverage1 for more detail.
047: */
048: public void testPartCoverage1() {
049: CoverageDecorator cd = new CoverageDecorator(
050: PartCoverage1.class, COVERED);
051:
052: TestResult result = new TestResult();
053:
054: cd.run(result);
055:
056: // Three failures: The first three methods are not fully covered.
057: assertEquals(3, result.failureCount());
058:
059: // No errors
060: assertEquals(0, result.errorCount());
061: }
062:
063: /**
064: * Only a part of the class is covered.
065: * See inner class PartCoverage2 for more detail.
066: */
067: public void testPartCoverage2() {
068: CoverageDecorator cd = new CoverageDecorator(
069: PartCoverage2.class, COVERED);
070:
071: TestResult result = new TestResult();
072:
073: cd.run(result);
074:
075: org.hansel.Util.dumpResult(result);
076: // Three failures: The first "if" is not fully covered.
077: // The second if is not fully covered. The third if is not covered at
078: // all but that failure message is suppressed, because it is caused by
079: // the first failure.
080: // Also the ifPointer method is not covered at all.
081: assertEquals(3, result.failureCount());
082:
083: // No errors
084: assertEquals(0, result.errorCount());
085: }
086:
087: /**
088: * Test full coverage of the class.
089: */
090: public void testFullCoverage() {
091: CoverageDecorator cd = new CoverageDecorator(
092: FullCoverage.class, COVERED);
093:
094: TestResult result = new TestResult();
095:
096: cd.run(result);
097:
098: Util.dumpResult(result);
099: // No failures
100: assertEquals(0, result.failureCount());
101:
102: // No errors
103: assertEquals(0, result.errorCount());
104: }
105:
106: /**
107: * TestCase does covers no code at all.
108: */
109: public static class NoCoverage extends TestCase {
110: /**
111: * Creates a new test.
112: * @param name Name of the test.
113: */
114: public NoCoverage(String name) {
115: super (name);
116: }
117:
118: /** Empty method. */
119: public void testNothing() {
120: }
121: }
122:
123: /**
124: * This testcase only calls the methods with condition=false..
125: */
126: public static class PartCoverage1 extends TestCase {
127: /**
128: * Creates a new test.
129: * @param name Name of the test.
130: */
131: public PartCoverage1(String name) {
132: super (name);
133: }
134:
135: /** Covers parts of the code. */
136: public void testAll() {
137: System.out.println("Probetable class: "
138: + org.hansel.ProbeTable.class);
139: CoverIf ci = new CoverIf();
140:
141: assertEquals(0, ci.simpleIf(false));
142: assertEquals(4, ci.ifElse(false));
143:
144: assertEquals(1, ci.nestedIf(true, true));
145: assertEquals(2, ci.nestedIf(true, false));
146: assertEquals(3, ci.nestedIf(false, true));
147: assertEquals(4, ci.nestedIf(false, false));
148: }
149:
150: }
151:
152: /**
153: * This testcase only calls the nestedIf methods with parameters
154: * (true, true). The other methods are fully covered.
155: */
156: public static class PartCoverage2 extends TestCase {
157: /**
158: * Creates a new test.
159: * @param name Name of the test.
160: */
161: public PartCoverage2(String name) {
162: super (name);
163: }
164:
165: /** Covers parts of the code. */
166: public void testAll() {
167: CoverIf ci = new CoverIf();
168:
169: // Cover the first two messages
170: assertEquals(0, ci.simpleIf(false));
171: assertEquals(1, ci.simpleIf(true));
172: assertEquals(4, ci.ifElse(false));
173: assertEquals(3, ci.ifElse(true));
174:
175: // Only make one call to the third method.
176: assertEquals(1, ci.nestedIf(true, true));
177: }
178:
179: }
180:
181: /**
182: * Test for full coverage.
183: */
184: public static class FullCoverage extends TestCase {
185: /**
186: * Creates a new test.
187: * @param name Name of the test.
188: */
189: public FullCoverage(String name) {
190: super (name);
191: }
192:
193: /** Covers all the code. */
194: public void testAll() {
195: CoverIf ci = new CoverIf();
196:
197: assertEquals(0, ci.simpleIf(false));
198: assertEquals(1, ci.simpleIf(true));
199:
200: assertEquals(4, ci.ifElse(false));
201: assertEquals(3, ci.ifElse(true));
202:
203: assertEquals(1, ci.nestedIf(true, true));
204: assertEquals(2, ci.nestedIf(true, false));
205: assertEquals(3, ci.nestedIf(false, true));
206: assertEquals(4, ci.nestedIf(false, false));
207:
208: assertEquals(0, ci.ifPointer(null, null));
209: assertEquals(1, ci.ifPointer(new Object(), null));
210: }
211: }
212:
213: }
|