001: package com.ibm.emb.junit;
002:
003: import java.io.BufferedWriter;
004: import java.util.Enumeration;
005:
006: import junit.framework.AssertionFailedError;
007: import junit.framework.Test;
008: import junit.framework.TestFailure;
009: import junit.framework.TestResult;
010: import junit.framework.TestSuite;
011: import junit.textui.ResultPrinter;
012: import junit.textui.TestRunner;
013:
014: /**
015: * An extension of junit.textui.TestRunner which overrides several functions to
016: * provide logging of failed tests to configurable files.
017: */
018: public class EMBTestRunner extends TestRunner {
019:
020: protected EMBTestConfiguration embTestConfig = null;
021:
022: ResultPrinter rprinter = new ResultPrinter(System.out);
023:
024: int fColumn = 0;
025:
026: /**
027: * overwritten method
028: */
029: public synchronized void addError(Test test, Throwable t) {
030: super .addError(test, t);
031: }
032:
033: /**
034: * overwritten method
035: */
036: public synchronized void addFailure(Test test,
037: AssertionFailedError t) {
038: super .addFailure(test, t);
039: }
040:
041: /**
042: * Constructs a TestRunner.
043: */
044: public EMBTestRunner() {
045: embTestConfig = EMBTestConfiguration.instance();
046: }
047:
048: /**
049: * Creates the TestResult to be used for the test run.
050: */
051: protected TestResult createTestResult() {
052: return new TestResult();
053: }
054:
055: /**
056: * overwritten doRun method which also logs failed tests to a file
057: */
058: public TestResult doRun(Test suite, boolean wait) {
059: TestResult result = createTestResult();
060: result.addListener(this );
061: long startTime = System.currentTimeMillis();
062: suite.run(result);
063: long endTime = System.currentTimeMillis();
064: long runTime = endTime - startTime;
065: System.out.println("Time: " + elapsedTimeAsString(runTime));
066: try {
067: fileWriter().newLine();
068: fileWriter().write("Time: " + elapsedTimeAsString(runTime));
069: fileWriter().newLine();
070: print(result);
071: System.out.println();
072: fileWriter().newLine();
073: } catch (java.io.IOException e) {
074: System.out.println("Error writing result in "
075: + this .getClass().getName());
076: }
077: pause(wait);
078: return result;
079: }
080:
081: public synchronized void startTest(Test test) {
082: super .startTest(test);
083: try {
084: if (fColumn++ >= 80) {
085: fileWriter().newLine();
086: fColumn = 0;
087: }
088: } catch (java.io.IOException e) {
089: System.out.println("Error writing result in "
090: + this .getClass().getName());
091: }
092: }
093:
094: /**
095: * overwritten method which logs summary to a file and prints summary to the
096: * standard output
097: */
098: public synchronized void print(TestResult result) {
099: try {
100: fileHeader(result);
101: fileErrors(result);
102: fileFailures(result);
103: fileWriter().flush();
104: } catch (java.io.IOException e) {
105: System.out.println("Error writng result in "
106: + this .getClass().getName());
107: }
108: }
109:
110: /**
111: * overwritten method which logs errors to a file and prints the errors to
112: * the standard output
113: */
114: public void fileErrors(TestResult result)
115: throws java.io.IOException {
116: if (result.errorCount() != 0) {
117: if (result.errorCount() == 1) {
118: fileWriter().write(
119: "There was " + result.errorCount() + " error:");
120: fileWriter().newLine();
121: } else {
122: fileWriter().write(
123: "There were " + result.errorCount()
124: + " errors:");
125: fileWriter().newLine();
126: }
127: int i = 1;
128: for (Enumeration e = result.errors(); e.hasMoreElements(); i++) {
129: TestFailure failure = (TestFailure) e.nextElement();
130: fileWriter().write(i + ") " + failure.failedTest());
131: fileWriter().newLine();
132: fileWriter().write(
133: getFilteredTrace(failure.thrownException()));
134: fileWriter().newLine();
135: }
136: }
137: }
138:
139: public void endTest(Test test) {
140: }
141:
142: /**
143: * overwritten method which logs failures to a file and prints failures to
144: * the standard output
145: */
146: public void fileFailures(TestResult result)
147: throws java.io.IOException {
148: if (result.failureCount() != 0) {
149: if (result.failureCount() == 1) {
150: fileWriter().write(
151: "There was " + result.failureCount()
152: + " failure:");
153: fileWriter().newLine();
154: } else {
155: fileWriter().write(
156: "There were " + result.failureCount()
157: + " failures:");
158: fileWriter().newLine();
159: }
160: int i = 1;
161: for (Enumeration e = result.failures(); e.hasMoreElements(); i++) {
162: TestFailure failure = (TestFailure) e.nextElement();
163: fileWriter().write(i + ") " + failure.failedTest());
164: fileWriter().newLine();
165: Throwable t = failure.thrownException();
166: fileWriter().write(
167: getFilteredTrace(failure.thrownException()));
168: fileWriter().newLine();
169: }
170: }
171: }
172:
173: /**
174: * overwritten method which prints the header to a file and prints the
175: * header of the report
176: */
177: public void fileHeader(TestResult result)
178: throws java.io.IOException {
179: if (result.wasSuccessful()) {
180: fileWriter().newLine();
181: fileWriter().write("OK");
182: fileWriter().write(" (" + result.runCount() + " tests)");
183: fileWriter().newLine();
184:
185: // added to print summary to console
186: System.out.println();
187: System.out.println("OK (" + result.runCount() + " tests)");
188: System.out.println();
189:
190: } else {
191: fileWriter().newLine();
192: fileWriter().write("FAILURES!!!");
193: fileWriter().newLine();
194: fileWriter().write(
195: "Tests run: " + result.runCount() + ", Failures: "
196: + result.failureCount() + ", Errors: "
197: + result.errorCount());
198: fileWriter().newLine();
199:
200: // print to console
201: System.out.println("FAILURES!!!");
202: System.out.println("Tests run: " + result.runCount()
203: + ", Failures: " + result.failureCount()
204: + ", Errors: " + result.errorCount());
205: System.out.println();
206: }
207: }
208:
209: /**
210: * overwritten run method which also logs errors to a file Runs a single
211: * test and collects its results. This method can be used to start a test
212: * run from your program.
213: *
214: * <pre>
215: *
216: * public static void main(String[] args) {
217: * com.ibm.emb.meb.junit.EMBTestRunner.run(suite());
218: * }
219: * </pre>
220: */
221: static public TestResult run(Test suite) {
222: EMBTestRunner aTestRunner = new EMBTestRunner();
223: return aTestRunner.doRun(suite, false);
224: }
225:
226: /**
227: * Runs a suite extracted from a TestCase subclass.
228: */
229: static public void run(Class testClass) {
230: run(new TestSuite(testClass));
231: }
232:
233: /**
234: * overwritten runAndWait method Runs a single test and waits until the user
235: * types RETURN.
236: */
237: static public void runAndWait(Test suite) {
238: EMBTestRunner aTestRunner = new EMBTestRunner();
239: aTestRunner.doRun(suite, true);
240: }
241:
242: /**
243: * fileWriter used to log the results obtained from the EMBTestConfiguration
244: * singleton
245: */
246: protected BufferedWriter fileWriter() {
247: return embTestConfig.getJunitWriter();
248: }
249: }
|