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