001: package org.hanseltest;
002:
003: import junit.framework.TestCase;
004: import junit.framework.TestFailure;
005: import junit.framework.TestResult;
006:
007: import org.hansel.CoverageDecorator;
008:
009: /**
010: * Test the coverage test of switch statements.
011: *
012: * @author Niklas Mehner
013: */
014: public class TestSwitch extends TestCase {
015: /** Class the tests cover. */
016: private static final Class[] COVERED = { CoverSwitch.class };
017:
018: /**
019: * Create a new test.
020: * @param name Name of the test.
021: */
022: public TestSwitch(String name) {
023: super (name);
024: }
025:
026: /**
027: * Assert that a failure message contains a given string.
028: * @param message Message that has to be contained in the failure.
029: * @param failure Failure, that should contain message.
030: */
031: private void assertContains(String message, TestFailure failure) {
032: String failureMessage = failure.thrownException().getMessage();
033:
034: int index = failureMessage.indexOf(message);
035:
036: assertTrue("Failure message '" + failureMessage
037: + "' does not contain '" + message + "'.", index > -1);
038: }
039:
040: /**
041: * Returns the test result for running a coverage test on
042: * COVERED for a given test class.
043: *
044: * @param clazz The class of the test to be run.
045: * @return Result of the test run.
046: */
047: private TestResult getTestResult(Class clazz) {
048: CoverageDecorator cd = new CoverageDecorator(clazz, COVERED);
049:
050: TestResult result = new TestResult();
051:
052: cd.run(result);
053:
054: return result;
055: }
056:
057: /**
058: * No code is covered.
059: */
060: public void testNoCoverage() {
061: TestResult result = getTestResult(NoCoverage.class);
062: // Four methods not covered, the switch probes do not report
063: // an failure, because, they are not hit at all.
064: assertEquals(4, result.failureCount());
065: assertEquals(0, result.errorCount());
066: }
067:
068: /**
069: * Test full coverage.
070: */
071: public void testFullCoverage() {
072: TestResult result = getTestResult(FullCoverage.class);
073:
074: // Four methods not covered, the switch probes do not report
075: // an failure, because, they are not hit at all.
076: assertEquals(0, result.failureCount());
077: assertEquals(0, result.errorCount());
078: }
079:
080: /**
081: * Test part coverage (1).
082: */
083: public void testPartCoverage() {
084: TestResult result = getTestResult(PartCoverage.class);
085:
086: assertEquals(1, result.failureCount());
087: assertEquals(0, result.errorCount());
088:
089: TestFailure failure = (TestFailure) result.failures()
090: .nextElement();
091:
092: assertContains(
093: "Switch-cases '0', '1' and '5' have not been covered.",
094: failure);
095: }
096:
097: /**
098: * Test part coverage (2).
099: */
100: public void testPartCoverage2() {
101: TestResult result = getTestResult(PartCoverage2.class);
102:
103: assertEquals(1, result.failureCount());
104: assertEquals(0, result.errorCount());
105:
106: TestFailure failure = (TestFailure) result.failures()
107: .nextElement();
108:
109: assertContains("Switch-case 'default' has not been covered.",
110: failure);
111: }
112:
113: /**
114: * Test part coverage (3).
115: */
116: public void testPartCoverage3() {
117: TestResult result = getTestResult(PartCoverage3.class);
118:
119: assertEquals(1, result.failureCount());
120: assertEquals(0, result.errorCount());
121:
122: TestFailure failure = (TestFailure) result.failures()
123: .nextElement();
124: assertContains(
125: "Switch-cases '4', '5' and '6' have not been covered.",
126: failure);
127: }
128:
129: /**
130: * TestCase does covers no code at all.
131: */
132: public static class NoCoverage extends TestCase {
133: /**
134: * Creates a new test.
135: * @param name Name of the test.
136: */
137: public NoCoverage(String name) {
138: super (name);
139: }
140:
141: /** Empty method. */
142: public void testNothing() {
143: }
144: }
145:
146: /**
147: * Covers the whole CoverSwitch class.
148: */
149: public static class FullCoverage extends TestCase {
150: /**
151: * Creates a new test.
152: * @param name Name of the test.
153: */
154: public FullCoverage(String name) {
155: super (name);
156: }
157:
158: /**
159: * Single Test of this class.
160: */
161: public void testAll() {
162: CoverSwitch cs = new CoverSwitch();
163: // Cover simpleSwitch
164: assertEquals(1, cs.coverSimpleSwitch(0));
165: assertEquals(2, cs.coverSimpleSwitch(1));
166: assertEquals(3, cs.coverSimpleSwitch(5));
167: assertEquals(4, cs.coverSimpleSwitch(-1));
168: assertEquals(4, cs.coverSimpleSwitch(2));
169:
170: // Cover NoDefault
171: assertEquals(3, cs.coverNoDefault(0));
172: assertEquals(2, cs.coverNoDefault(5));
173:
174: // Cover simpleSwitch
175: assertEquals(1, cs.coverSimpleSwitch2(1));
176: assertEquals(2, cs.coverSimpleSwitch2(5));
177: assertEquals(3, cs.coverSimpleSwitch2(7));
178: }
179: }
180:
181: /**
182: * Covers only a part of the CoverSwitch class.
183: */
184: public static class PartCoverage extends TestCase {
185: /**
186: * Creates a new test.
187: * @param name Name of the test.
188: */
189: public PartCoverage(String name) {
190: super (name);
191: }
192:
193: /**
194: * Single Test of this class.
195: */
196: public void testAll() {
197: CoverSwitch cs = new CoverSwitch();
198: // Cover simpleSwitch
199: // Covers only default case
200: assertEquals(4, cs.coverSimpleSwitch(-1));
201: assertEquals(4, cs.coverSimpleSwitch(2));
202:
203: // Cover NoDefault
204: assertEquals(3, cs.coverNoDefault(0));
205: assertEquals(2, cs.coverNoDefault(5));
206:
207: // Cover simpleSwitch
208: assertEquals(1, cs.coverSimpleSwitch2(1));
209: assertEquals(2, cs.coverSimpleSwitch2(5));
210: assertEquals(3, cs.coverSimpleSwitch2(7));
211: }
212: }
213:
214: /**
215: * Covers only a part of the CoverSwitch class.
216: */
217: public static class PartCoverage2 extends TestCase {
218: /**
219: * Creates a new test.
220: * @param name Name of the test.
221: */
222: public PartCoverage2(String name) {
223: super (name);
224: }
225:
226: /**
227: * Single Test of this class.
228: */
229: public void testAll() {
230: CoverSwitch cs = new CoverSwitch();
231: // Cover simpleSwitch
232: assertEquals(1, cs.coverSimpleSwitch(0));
233: assertEquals(2, cs.coverSimpleSwitch(1));
234: assertEquals(3, cs.coverSimpleSwitch(5));
235: assertEquals(4, cs.coverSimpleSwitch(-1));
236: assertEquals(4, cs.coverSimpleSwitch(2));
237:
238: // Cover NoDefault
239: // Does not cover default case
240: assertEquals(3, cs.coverNoDefault(0));
241:
242: // Cover simpleSwitch
243: assertEquals(1, cs.coverSimpleSwitch2(1));
244: assertEquals(2, cs.coverSimpleSwitch2(5));
245: assertEquals(3, cs.coverSimpleSwitch2(7));
246: }
247: }
248:
249: /**
250: * Covers only a part of the CoverSwitch class.
251: */
252: public static class PartCoverage3 extends TestCase {
253: /**
254: * Creates a new test.
255: * @param name Name of the test.
256: */
257: public PartCoverage3(String name) {
258: super (name);
259: }
260:
261: /**
262: * Single Test of this class.
263: */
264: public void testAll() {
265: CoverSwitch cs = new CoverSwitch();
266: // Cover simpleSwitch
267: assertEquals(1, cs.coverSimpleSwitch(0));
268: assertEquals(2, cs.coverSimpleSwitch(1));
269: assertEquals(3, cs.coverSimpleSwitch(5));
270: assertEquals(4, cs.coverSimpleSwitch(-1));
271: assertEquals(4, cs.coverSimpleSwitch(2));
272:
273: // Cover NoDefault
274: // Does not cover default case
275: assertEquals(3, cs.coverNoDefault(0));
276: assertEquals(2, cs.coverNoDefault(5));
277:
278: // Cover simpleSwitch
279: assertEquals(1, cs.coverSimpleSwitch2(1));
280: assertEquals(3, cs.coverSimpleSwitch2(7));
281: }
282: }
283: }
|