001: package org.andromda.maven.plugin.translationlibrary;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.io.PrintWriter;
007: import java.io.StringWriter;
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.Iterator;
011:
012: import junit.framework.AssertionFailedError;
013: import junit.framework.Test;
014: import junit.framework.TestListener;
015:
016: /**
017: * Formats the translation-library test results into the correct format.
018: *
019: * @author Chad Brandon
020: */
021: public class TranslationLibraryTestFormatter implements TestListener {
022: /**
023: * Stores the report file location.
024: */
025: private File reportFile;
026:
027: /**
028: * Stores the contents of the report.
029: */
030: private StringWriter report;
031:
032: /**
033: * The print writer for the report.
034: */
035: private PrintWriter reportWriter;
036:
037: public TranslationLibraryTestFormatter() {
038: this .report = new StringWriter();
039: this .reportWriter = new PrintWriter(this .report);
040: }
041:
042: /**
043: * Sets the file that will contain the report results (i.e.
044: * the results of the cartridge test run).
045: *
046: * @param reportFile the file to which the report output will be written.
047: */
048: public void setReportFile(final File reportFile) {
049: this .reportFile = reportFile;
050: }
051:
052: /**
053: * Keeps track of the number of errors.
054: */
055: private int numberOfErrors = 0;
056:
057: /**
058: * @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable)
059: */
060: public void addError(Test test, Throwable throwable) {
061: this .numberOfErrors++;
062: this .collectFailure(test, throwable);
063: }
064:
065: /**
066: * Keeps track of the number of failures.
067: */
068: private int numberOfFailures = 0;
069:
070: /**
071: * @see junit.framework.TestListener#addFailure(junit.framework.Test, junit.framework.AssertionFailedError)
072: */
073: public void addFailure(Test test, AssertionFailedError failure) {
074: this .numberOfFailures++;
075: this .collectFailure(test, failure);
076: }
077:
078: public void endTest(Test test) {
079: }
080:
081: /**
082: * Keeps track of the number of tests run.
083: */
084: private int numberOfTests = 0;
085:
086: public void startTest(Test test) {
087: this .numberOfTests++;
088: }
089:
090: /**
091: * Stores the start time of the test suite
092: */
093: private long startTime = 0;
094:
095: /**
096: * Stores the new line separator.
097: */
098: private static final String newLine = System
099: .getProperty("line.separator");
100:
101: /**
102: * The testsuite started.
103: */
104: public void startTestSuite(final String name) {
105: startTime = System.currentTimeMillis();
106: this .reportWriter
107: .println("-------------------------------------------------------------------------------");
108: this .reportWriter.println(name + " Test Suite");
109: this .reportWriter
110: .println("-------------------------------------------------------------------------------");
111: }
112:
113: /**
114: * Adds a failure to the current <code>failures</code>
115: * collection (these are rendered at the end of test suite
116: * execution).
117: *
118: * @param type the failure type (error or failure).
119: * @param test the actual test.
120: * @param throwable the failure information.
121: */
122: private void collectFailure(Test test, Throwable throwable) {
123: this .failures.add(new Failure(test, throwable));
124: }
125:
126: private Collection failures = new ArrayList();
127:
128: /**
129: * Signifies the test suite ended and returns the summary of the
130: * test.
131: *
132: * @param test the test suite being run.
133: * @return the test summary.
134: */
135: String endTestSuite() {
136: final double elapsed = ((System.currentTimeMillis() - this .startTime) / 1000.0);
137: final StringBuffer summary = new StringBuffer("Tests: "
138: + String.valueOf(this .numberOfTests) + ", ");
139: summary.append("Failures: "
140: + String.valueOf(this .numberOfFailures) + ", ");
141: summary.append("Errors: " + String.valueOf(this .numberOfErrors)
142: + ", ");
143: summary.append("Time elapsed: " + elapsed).append(" sec");
144: summary.append(newLine);
145: summary.append(newLine);
146: this .reportWriter.print(summary);
147:
148: for (final Iterator iterator = this .failures.iterator(); iterator
149: .hasNext();) {
150: final Failure failure = (Failure) iterator.next();
151: final Throwable information = failure.information;
152: if (information instanceof AssertionFailedError) {
153: this .reportWriter.println(failure.information
154: .getMessage());
155: } else {
156: this .reportWriter.println("ERROR:");
157: }
158: information.printStackTrace(this .reportWriter);
159: this .reportWriter.println();
160: }
161:
162: if (this .reportFile != null) {
163: try {
164: final File parent = this .reportFile.getParentFile();
165: if (parent != null && !parent.exists()) {
166: parent.mkdirs();
167: }
168: final FileWriter writer = new FileWriter(
169: this .reportFile);
170: writer.write(report.toString());
171: writer.flush();
172: writer.close();
173: } catch (IOException exception) {
174: throw new RuntimeException(exception);
175: }
176: }
177: return summary.toString();
178: }
179:
180: /**
181: * Stores the information about a test failure.
182: */
183: private static final class Failure {
184: Test test;
185: Throwable information;
186:
187: Failure(final Test test, final Throwable information) {
188: this.test = test;
189: this.information = information;
190: }
191: }
192: }
|