001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test;
017:
018: import java.io.PrintStream;
019: import java.util.Enumeration;
020:
021: import junit.framework.TestFailure;
022: import junit.framework.TestResult;
023:
024: /**
025: *
026: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
027: */
028: public class ResultPrinter extends junit.textui.ResultPrinter {
029:
030: public ResultPrinter(PrintStream writer) {
031: super (writer);
032: }
033:
034: public PrintStream writer() {
035: return getWriter();
036: }
037:
038: public void printFailures(TestResult result) {
039: if (result.failureCount() != 0) {
040: writer().println("\n~~ Failure Results ~~~~~~~~~\n");
041: if (result.failureCount() == 1)
042: writer().println(
043: "There was " + result.failureCount()
044: + " failure:");
045: else
046: writer().println(
047: "There were " + result.failureCount()
048: + " failures:");
049:
050: int i = 1;
051: writer().println("\nFailure Summary:");
052: for (Enumeration e = result.failures(); e.hasMoreElements(); i++) {
053: TestFailure failure = (TestFailure) e.nextElement();
054: writer().println(i + ") " + failure.failedTest());
055: }
056: i = 1;
057: writer().println("\nFailure Details:");
058: for (Enumeration e = result.failures(); e.hasMoreElements(); i++) {
059: TestFailure failure = (TestFailure) e.nextElement();
060: writer()
061: .println("\n" + i + ") " + failure.failedTest());
062: Throwable t = failure.thrownException();
063: if (t.getMessage() != null)
064: writer().println("\t\"" + t.getMessage() + "\"");
065: else {
066: writer().println();
067: failure.thrownException().printStackTrace();
068: }
069: }
070: }
071: }
072:
073: /**
074: * Prints the header of the report
075: */
076: public void printHeader(TestResult result) {
077: if (result.wasSuccessful()) {
078: writer().println();
079: writer().print("OK");
080: writer().println(" (" + result.runCount() + " tests)");
081:
082: } else {
083: writer().println();
084: writer().println("FAILURES!!!");
085: writer().println("~~ Test Results ~~~~~~~~~~~~");
086: writer().println(" Run: " + result.runCount());
087: writer().println(" Failures: " + result.failureCount());
088: writer().println(" Errors: " + result.errorCount());
089: }
090: }
091:
092: public void printErrors(TestResult result) {
093: if (result.errorCount() != 0) {
094: writer().println("\n~~ Error Results ~~~~~~~~~~~\n");
095: if (result.errorCount() == 1)
096: writer().println(
097: "There was " + result.errorCount() + " error:");
098: else
099: writer().println(
100: "There were " + result.errorCount()
101: + " errors:");
102:
103: writer().println("\nError Summary:");
104: int i = 1;
105: for (Enumeration e = result.errors(); e.hasMoreElements(); i++) {
106: TestFailure failure = (TestFailure) e.nextElement();
107: writer().println(i + ") " + failure.failedTest());
108: }
109: writer().println("\nError Details:");
110: i = 1;
111: for (Enumeration e = result.errors(); e.hasMoreElements(); i++) {
112: TestFailure failure = (TestFailure) e.nextElement();
113: writer().println(i + ") " + failure.failedTest());
114: String trace = getRelevantStackTrace(failure
115: .thrownException());
116: writer().println(trace);
117: }
118: }
119: }
120:
121: public String getRelevantStackTrace(Throwable t) {
122: StringBuffer trace = new StringBuffer();
123:
124: try {
125: // Cut the stack trace after "at junit.framework" is found
126: // Return just the first part.
127: java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
128: java.io.PrintWriter pw = new java.io.PrintWriter(bos);
129: t.printStackTrace(pw);
130: pw.close();
131:
132: java.io.BufferedReader reader = new java.io.BufferedReader(
133: new java.io.StringReader(bos.toString()));
134: String line = reader.readLine();
135: while (line != null) {
136: if (line.indexOf("at junit.framework") != -1)
137: break;
138: if (line
139: .indexOf("at org.apache.openejb.test.NumberedTestCase") != -1)
140: break;
141: if (line
142: .indexOf("at org.apache.openejb.test.TestSuite") != -1)
143: break;
144:
145: trace.append(line).append('\n');
146: line = reader.readLine();
147: }
148: } catch (Exception e) {
149: }
150:
151: return trace.toString();
152: }
153:
154: }
|