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 basic coverage problems.
011: * @author Niklas Mehner
012: */
013: public class TestSimple extends TestCase {
014: /** Class to be covered by this test. */
015: private static final Class[] COVERED = { CoverSimple.class };
016:
017: /**
018: * Create a new Test.
019: * @param name Name of the test.
020: */
021: public TestSimple(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: cd.run(result);
034:
035: // Two failures: Constructor and method
036: assertEquals(2, result.failureCount());
037:
038: // No errors
039: assertEquals(0, result.errorCount());
040: }
041:
042: /**
043: * No code is covered, but test fails. No coverage results
044: * are added to the result in this case.
045: */
046: public void testErrorNoCoverage() {
047: CoverageDecorator cd = new CoverageDecorator(
048: ErrorNoCoverage.class, COVERED);
049:
050: TestResult result = new TestResult();
051: cd.run(result);
052:
053: // One failure: "No coverage test performed"
054: assertEquals(1, result.failureCount());
055:
056: // No errors
057: assertEquals(1, result.errorCount());
058: }
059:
060: /**
061: * Only the constructor is covered.
062: */
063: public void testConstructorCoverage() {
064: CoverageDecorator cd = new CoverageDecorator(
065: ConstructorCoverage.class, COVERED);
066:
067: TestResult result = new TestResult();
068: cd.run(result);
069:
070: assertEquals(1, result.failureCount());
071:
072: // No errors
073: assertEquals(0, result.errorCount());
074: }
075:
076: /**
077: * Test full coverage.
078: */
079: public void testFullCoverage() throws Exception {
080: CoverageDecorator cd = new CoverageDecorator(
081: FullCoverage.class, COVERED);
082:
083: TestResult result = new TestResult();
084: cd.run(result);
085:
086: Util.dumpResult(result);
087: assertEquals(0, result.failureCount());
088:
089: // No errors
090: assertEquals(0, result.errorCount());
091: }
092:
093: /**
094: * Covertest testing nothing.
095: */
096: public static class NoCoverage extends TestCase {
097: /**
098: * Create a new Test.
099: * @param name Name of the test.
100: */
101: public NoCoverage(String name) {
102: super (name);
103: }
104:
105: /** Does nothing. */
106: public void testNothing() {
107: }
108: }
109:
110: /**
111: * This is a test that fails to cover any code and throws an exception
112: * when run.
113: */
114: public static class ErrorNoCoverage extends TestCase {
115: /**
116: * Create a new Test.
117: * @param name Name of the test.
118: */
119: public ErrorNoCoverage(String name) {
120: super (name);
121: }
122:
123: /**
124: * Throws an UnsupportedOperationException.
125: */
126: public void testNothing() {
127: throw new UnsupportedOperationException("Unsupported.");
128: }
129: }
130:
131: /**
132: * This test covers only the constructor.
133: */
134: public static class ConstructorCoverage extends TestCase {
135: /**
136: * Create a new Test.
137: * @param name Name of the test.
138: */
139: public ConstructorCoverage(String name) {
140: super (name);
141: }
142:
143: /** Invokes the CoverSimple() constructor. */
144: public void testConstructor() {
145: new CoverSimple();
146: }
147: }
148:
149: /** TestCase covering the whole CoverSimple class. */
150: public static class FullCoverage extends TestCase {
151:
152: /** Covers all of the class. */
153: public void testFullCoverage() {
154: CoverSimple cs = new CoverSimple();
155: assertEquals(5, cs.coverSimple());
156: }
157: }
158:
159: }
|