001: package org.hanseltest;
002:
003: import junit.framework.TestCase;
004: import junit.framework.TestResult;
005:
006: import org.hansel.AssertionCoverageDecorator;
007: import org.hansel.Util;
008:
009: /**
010: * Test coverage of code containing assertions.
011: * WARNING: This test will fail, if CoverAssertion is compiled without
012: * assertions.
013: *
014: * @author Niklas Mehner.
015: */
016: public class TestAssertions extends TestCase {
017: /** Class to cover. */
018: private static final Class[] COVERED = { CoverAssertion.class };
019:
020: /**
021: * Create a new test.
022: * @param name Name of the test.
023: */
024: public TestAssertions(String name) {
025: super (name);
026: }
027:
028: /**
029: * Find out, if assertions are enabled.
030: * @return true, if assertions are enabled.
031: */
032: private static boolean assertionsEnabled() {
033: boolean enabled = false;
034: assert enabled = true;
035: return enabled;
036: }
037:
038: /**
039: * No code is covered.
040: */
041: public void testNoCoverage() {
042: AssertionCoverageDecorator cd = new AssertionCoverageDecorator(
043: NoCoverage.class, COVERED);
044:
045: TestResult result = new TestResult();
046: cd.run(result);
047:
048: //org.hansel.Util.dumpResult(result);
049: // Three failures: methods "init" and "coverAssertion"
050: assertEquals(2, result.failureCount());
051:
052: // No errors
053: assertEquals(0, result.errorCount());
054: }
055:
056: /**
057: * All of the code is covered.
058: */
059: public void testFullCoverage() {
060: AssertionCoverageDecorator cd = new AssertionCoverageDecorator(
061: FullCoverage.class, COVERED);
062:
063: TestResult result = new TestResult();
064: cd.run(result);
065:
066: Util.dumpResult(result);
067:
068: // No failures
069: assertEquals(0, result.failureCount());
070:
071: // No errors
072: assertEquals(0, result.errorCount());
073: }
074:
075: /**
076: * Testcase covering no code.
077: */
078: public static class NoCoverage extends TestCase {
079: /**
080: * Create a new test.
081: * @param name Name of the test.
082: */
083: public NoCoverage(String name) {
084: super (name);
085: }
086:
087: /** Does nothing. */
088: public void testNothing() {
089: }
090: }
091:
092: /**
093: * Testcase covering all of the code.
094: */
095: public static class FullCoverage extends TestCase {
096: /**
097: * Create a new test.
098: * @param name Name of the test.
099: */
100: public FullCoverage(String name) {
101: super (name);
102: }
103:
104: /** Covers all of the code. */
105: public void testAll() {
106: CoverAssertion ca = new CoverAssertion();
107: assertEquals(2, ca.coverAssertion(2));
108:
109: if (assertionsEnabled()) {
110: try {
111: ca.coverAssertion(-1);
112: } catch (AssertionError e) {
113: // we've been expecting this
114: }
115: } else {
116: assertEquals(-1, ca.coverAssertion(-1));
117: }
118: }
119: }
120: }
|