1 package org.andromda.maven.plugin.bootstrap;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.io.StringWriter;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import junit.framework.AssertionFailedError;
11 import junit.framework.Test;
12 import junit.framework.TestListener;
13
14
15
16
17
18
19 public class TranslationLibraryTestFormatter
20 implements TestListener
21 {
22
23
24
25 private File reportFile;
26
27
28
29
30 private StringWriter report;
31
32
33
34
35 private PrintWriter reportWriter;
36
37
38
39
40 public TranslationLibraryTestFormatter()
41 {
42 this.report = new StringWriter();
43 this.reportWriter = new PrintWriter(this.report);
44 }
45
46
47
48
49
50
51
52 public void setReportFile(final File reportFile)
53 {
54 this.reportFile = reportFile;
55 }
56
57
58
59
60 private int numberOfErrors = 0;
61
62
63
64
65 public void addError(
66 Test test,
67 Throwable throwable)
68 {
69 this.numberOfErrors++;
70 this.collectFailure(
71 test,
72 throwable);
73 }
74
75
76
77
78 private int numberOfFailures = 0;
79
80
81
82
83 public void addFailure(
84 Test test,
85 AssertionFailedError failure)
86 {
87 this.numberOfFailures++;
88 this.collectFailure(
89 test,
90 failure);
91 }
92
93
94
95
96 public void endTest(Test test)
97 {
98 }
99
100
101
102
103 private int numberOfTests = 0;
104
105
106
107
108 public void startTest(Test test)
109 {
110 this.numberOfTests++;
111 }
112
113
114
115
116 private long startTime = 0;
117
118
119
120
121 private static final String newLine = System.getProperty("line.separator");
122
123
124
125
126
127 public void startTestSuite(final String name)
128 {
129 startTime = System.currentTimeMillis();
130 this.reportWriter.println("-------------------------------------------------------------------------------");
131 this.reportWriter.println(name + " Test Suite");
132 this.reportWriter.println("-------------------------------------------------------------------------------");
133 }
134
135
136
137
138
139
140
141
142
143
144 private void collectFailure(
145 Test test,
146 Throwable throwable)
147 {
148 this.failures.add(new Failure(
149 test,
150 throwable));
151 }
152
153 private Collection<Failure> failures = new ArrayList<Failure>();
154
155
156
157
158
159
160
161 String endTestSuite()
162 {
163 final double elapsed = ((System.currentTimeMillis() - this.startTime) / 1000.0);
164 final StringBuilder summary = new StringBuilder("Tests: " + String.valueOf(this.numberOfTests) + ", ");
165 summary.append("Failures: ").append(this.numberOfFailures).append(", ");
166 summary.append("Errors: ").append(this.numberOfErrors).append(", ");
167 summary.append("Time elapsed: ").append(elapsed).append(" sec");
168 summary.append(newLine);
169 summary.append(newLine);
170 this.reportWriter.print(summary);
171
172 for (final Failure failure : this.failures)
173 {
174 final Throwable information = failure.information;
175 if (information instanceof AssertionFailedError)
176 {
177 this.reportWriter.println(failure.information.getMessage());
178 }
179 else
180 {
181 this.reportWriter.println("ERROR:");
182 }
183 information.printStackTrace(this.reportWriter);
184 this.reportWriter.println();
185 }
186
187 if (this.reportFile != null)
188 {
189 try
190 {
191 final File parent = this.reportFile.getParentFile();
192 if (parent != null && !parent.exists())
193 {
194 parent.mkdirs();
195 }
196 final FileWriter writer = new FileWriter(this.reportFile);
197 writer.write(report.toString());
198 writer.flush();
199 writer.close();
200 }
201 catch (IOException exception)
202 {
203 throw new RuntimeException(exception);
204 }
205 }
206 return summary.toString();
207 }
208
209
210
211
212 private static final class Failure
213 {
214 @SuppressWarnings({ "unused" })
215 Test test;
216 Throwable information;
217
218 Failure(
219 final Test test,
220 final Throwable information)
221 {
222 this.test = test;
223 this.information = information;
224 }
225 }
226 }