View Javadoc
1   package org.codehaus.mojo.gwt.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Mostly a copy/paste of surefire TestListenerInvocationHandler
44   *
45   * @author ndeloof
46   * @version $Id$
47   */
48  public class MavenTestRunner
49      extends TestRunner
50  {
51      /** reporter to gather errors */
52      private ReporterManager reportManager;
53  
54      /** flag for test failures */
55      private boolean testHadFailed;
56  
57      /** entry point for runner in a dedicated JVM */
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       * {@inheritDoc}
80       *
81       * @see junit.textui.TestRunner#createTestResult()
82       */
83      @Override
84      protected TestResult createTestResult()
85      {
86          TestResult result = super.createTestResult();
87          result.addListener( this );
88          return result;
89      }
90  
91      /**
92       * {@inheritDoc}
93       *
94       * @see junit.textui.TestRunner#doRun(junit.framework.Test, boolean)
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      * A test started.
136      * @param test the test
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      * A test ended.
147      * @param test the test
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      * An error occurred.
161      * @param test the test
162      * @param t the error
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      * A failure occurred.
176      * @param test the test
177      * @param t the failure
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      * @param test the test
191      * @param t a throwable
192      * @return a StackTraceWriter to trace the error
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 }