1 package org.codehaus.mojo.gwt.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import junit.framework.AssertionFailedError;
27 import junit.framework.Test;
28 import junit.framework.TestResult;
29 import junit.framework.TestSuite;
30 import junit.textui.TestRunner;
31
32 import org.apache.maven.surefire.report.BriefConsoleReporter;
33 import org.apache.maven.surefire.report.FileReporter;
34 import org.apache.maven.surefire.report.PojoStackTraceWriter;
35 import org.apache.maven.surefire.report.ReportEntry;
36 import org.apache.maven.surefire.report.Reporter;
37 import org.apache.maven.surefire.report.ReporterException;
38 import org.apache.maven.surefire.report.ReporterManager;
39 import org.apache.maven.surefire.report.StackTraceWriter;
40 import org.apache.maven.surefire.report.XMLReporter;
41
42
43
44
45
46
47
48 public class MavenTestRunner
49 extends TestRunner
50 {
51
52 private ReporterManager reportManager;
53
54
55 private boolean testHadFailed;
56
57
58 public static void main( String args[] )
59 {
60 try
61 {
62 MavenTestRunner runner = new MavenTestRunner();
63 TestResult r = runner.start( args );
64 if ( !r.wasSuccessful() )
65 {
66 System.exit( FAILURE_EXIT );
67 }
68 System.exit( SUCCESS_EXIT );
69 }
70 catch ( Throwable t )
71 {
72 t.printStackTrace();
73 System.err.println( t.getMessage() );
74 System.exit( EXCEPTION_EXIT );
75 }
76 }
77
78
79
80
81
82
83 @Override
84 protected TestResult createTestResult()
85 {
86 TestResult result = super.createTestResult();
87 result.addListener( this );
88 return result;
89 }
90
91
92
93
94
95
96 @Override
97 public TestResult doRun( Test suite, boolean wait )
98 {
99 try
100 {
101 reportManager.runStarting( suite.countTestCases() );
102 ReportEntry report = new ReportEntry( this.getClass().getName(), suite.toString(), "starting" );
103 reportManager.testSetStarting( report );
104 TestResult result = createTestResult();
105 suite.run( result );
106 return result;
107 }
108 catch ( ReporterException e )
109 {
110 System.err.println( "Failed to log in test report " + e );
111 return null;
112 }
113 finally
114 {
115 ReportEntry report = new ReportEntry( this.getClass().getName(), suite.toString(), "ended" );
116 reportManager.testSetCompleted( report );
117 reportManager.runCompleted();
118 }
119 }
120
121
122
123
124 public MavenTestRunner()
125 {
126 String dir = System.getProperty( "surefire.reports" );
127 List<Reporter> reports = new ArrayList<Reporter>();
128 reports.add( new XMLReporter( new File( dir ), false ) );
129 reports.add( new FileReporter( new File( dir ), false ) );
130 reports.add( new BriefConsoleReporter( true ) );
131 reportManager = new ReporterManager( reports );
132 }
133
134
135
136
137
138 public void startTest( Test test )
139 {
140 testHadFailed = false;
141 ReportEntry report = new ReportEntry( test.getClass().getName(), test.toString(), test.getClass().getName() );
142 reportManager.testStarting( report );
143 }
144
145
146
147
148
149 public void endTest( Test test )
150 {
151 if ( !testHadFailed )
152 {
153 ReportEntry report =
154 new ReportEntry( test.getClass().getName(), test.toString(), test.getClass().getName() );
155 reportManager.testSucceeded( report );
156 }
157 }
158
159
160
161
162
163
164 public void addError( Test test, Throwable t )
165 {
166 String desc = test.toString();
167 ReportEntry report =
168 new ReportEntry( test.getClass().getName(), desc, desc, getStackTraceWriter( test, t ) );
169
170 reportManager.testError( report );
171 testHadFailed = true;
172 }
173
174
175
176
177
178
179 public void addFailure( Test test, AssertionFailedError t )
180 {
181 String desc = test.toString();
182 ReportEntry report =
183 new ReportEntry( test.getClass().getName(), desc, desc, getStackTraceWriter( test, t ) );
184
185 reportManager.testFailed( report );
186 testHadFailed = true;
187 }
188
189
190
191
192
193
194 private StackTraceWriter getStackTraceWriter( Test test, Throwable t )
195 {
196 String name = test.getClass().getName();
197 String testName = "UNKNOWN";
198 if ( test instanceof TestSuite )
199 {
200 testName = ( (TestSuite) test ).getName();
201 }
202 return new PojoStackTraceWriter( name, testName, t );
203 }
204 }