001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2007 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution.testing;
010:
011: import javolution.util.FastTable;
012: import j2me.lang.CharSequence;
013: import j2me.util.List;
014:
015: /**
016: * <p> This class represents a collection of {@link TestCase test cases} and
017: * detailed information about the test being performed. [code]
018: * class TextBuilderSuite extends TestSuite {
019: * public void run() {
020: * TestContext.info("Test Suite for TextBuilder");
021: * TestContext.test(appendInt);
022: * ...
023: * }
024: * TestCase appendInt = new TestCase() {
025: * TextBuilder tmp = new TextBuilder();
026: * int i;
027: * public void prepare() {
028: * tmp.reset();
029: * i = MathLib.randomInt(Integer.MIN_VALUE, Integer.MAX_VALUE);
030: * }
031: * public void execute() {
032: * tmp.append(i);
033: * }
034: * public void validate() {
035: * TextContext.assertEquals(String.valueOf(i), tmp.toString());
036: * ... // We may also validate min, max, zero boundary cases here.
037: * }
038: * public CharSequence getDescription() {
039: * return "TextBuilder.append(int)";
040: * }
041: * };
042: * ...
043: * }[/code]</p>
044: * <p> Test suites can be run in the current logging context or within
045: * specialized {@link TestContext test contexts}:[code]
046: *
047: * // Runs test suite directly (validation with results being logged).
048: * new TextBuilderSuite().run();
049: *
050: * // Performs regression (no logging but exception if test fails).
051: * TestContext.enter(TestContext.REGRESSION);
052: * try {
053: * new TextBuilderSuite().run();
054: * } finally {
055: * TestContext.exit();
056: * }
057: *
058: * // Performance measurements.
059: * TimeContext.enter();
060: * try {
061: * new TextBuilderSuite().run();
062: * } finally {
063: * TimeContext.exit();
064: * }[/code]<p>
065: *
066: * <p> Nothing prevent a test suite to run other test suites. It is also
067: * possible {@link #getTestCases() to retrieve} all the test cases which
068: * are to be executed by a test suite (for integration with an IDE for
069: * example).</p>
070: *
071: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
072: * @version 5.2, August 19, 2007
073: * @see <a href="http://en.wikipedia.org/wiki/Test_suite">
074: * Wikipedia: Test Suite</a>
075: */
076: public abstract class TestSuite implements Runnable {
077:
078: /**
079: * Default constructor.
080: */
081: protected TestSuite() {
082: }
083:
084: /**
085: * Runs this test suite.
086: */
087: public abstract void run();
088:
089: /**
090: * Retrieves the list of test cases to be executed by this test suite.
091: *
092: * @return the ordered list of test cases.
093: */
094: public List getTestCases() {
095: GetTestCases getTestCases = new GetTestCases();
096: TestContext.enter(getTestCases);
097: try {
098: this .run();
099: } finally {
100: TestContext.exit();
101: }
102: return getTestCases._testCases;
103: }
104:
105: private static final class GetTestCases extends TestContext {
106: FastTable _testCases = new FastTable();
107:
108: public boolean doAssertEquals(String message, Object expected,
109: Object actual) {
110: return true;
111: }
112:
113: public void doTest(TestCase testCase) {
114: _testCases.add(testCase);
115: }
116:
117: public boolean isErrorLogged() {
118: return false;
119: }
120:
121: public boolean isInfoLogged() {
122: return false;
123: }
124:
125: public boolean isWarningLogged() {
126: return false;
127: }
128:
129: public void logError(Throwable error, CharSequence message) {
130: }
131:
132: public void logInfo(CharSequence message) {
133: }
134:
135: public void logWarning(CharSequence message) {
136: }
137: }
138:
139: /**
140: * Returns the description of this test suite or <code>null</code> if none.
141: *
142: * @return the description or <code>null</code>
143: */
144: public CharSequence getDescription() {
145: return null;
146: }
147:
148: /**
149: * Returns the <code>String</code> representation of this test suite
150: * (the description or the class name by default).
151: *
152: * @return the string representation of this test suite.
153: */
154: public String toString() {
155: CharSequence description = getDescription();
156: return description == null ? this.getClass().getName()
157: : description.toString();
158: }
159: }
|