View Javadoc
1   package org.apache.maven.surefire.junitcore;
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 org.apache.maven.plugin.surefire.StartupReportConfiguration;
23  import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger;
24  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
25  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
26  import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
27  import org.apache.maven.surefire.report.ReporterFactory;
28  import org.apache.maven.surefire.report.RunListener;
29  import org.apache.maven.surefire.testset.TestSetFailedException;
30  import org.junit.runner.Computer;
31  import org.junit.runner.JUnitCore;
32  import org.junit.runner.Result;
33  
34  import java.io.File;
35  import java.util.HashMap;
36  import java.util.concurrent.ExecutionException;
37  
38  import static org.apache.maven.surefire.junitcore.ConcurrentRunListener.createInstance;
39  import static org.apache.maven.surefire.report.ConsoleOutputCapture.startCapture;
40  
41  /**
42   * @author Kristian Rosenvold
43   */
44  public class JUnitCoreTester
45  {
46      private final Computer computer;
47  
48      public JUnitCoreTester()
49      {
50          this( new Computer() );
51      }
52  
53      public JUnitCoreTester( Computer computer )
54      {
55          this.computer = computer;
56      }
57  
58      public Result run( boolean parallelClasses, Class<?>... classes )
59          throws TestSetFailedException, ExecutionException
60      {
61          ReporterFactory reporterManagerFactory = defaultNoXml();
62  
63          try
64          {
65              final HashMap<String, TestSet> classMethodCounts = new HashMap<>();
66              RunListener reporter = createInstance( classMethodCounts, reporterManagerFactory, parallelClasses, false,
67                                                           new DefaultDirectConsoleReporter( System.out ) );
68              startCapture( (ConsoleOutputReceiver) reporter );
69  
70              JUnitCoreRunListener runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
71              JUnitCore junitCore = new JUnitCore();
72  
73              junitCore.addListener( runListener );
74              final Result run = junitCore.run( computer, classes );
75              junitCore.removeListener( runListener );
76              return run;
77          }
78          finally
79          {
80              reporterManagerFactory.close();
81              if ( computer instanceof ConfigurableParallelComputer )
82              {
83                  ( (ConfigurableParallelComputer) computer ).close();
84              }
85          }
86      }
87  
88      /**
89       * For testing purposes only.
90       *
91       * @return DefaultReporterFactory for testing purposes
92       */
93      public static DefaultReporterFactory defaultNoXml()
94      {
95          return new DefaultReporterFactory( defaultStartupReportConfiguration(), new NullConsoleLogger() );
96      }
97  
98      /**
99       * For testing purposes only.
100      *
101      * @return StartupReportConfiguration fo testing purposes
102      */
103     private static StartupReportConfiguration defaultStartupReportConfiguration()
104     {
105         File target = new File( "./target" );
106         File statisticsFile = new File( target, "TESTHASHxXML" );
107         return new StartupReportConfiguration( true, true, "PLAIN", false, true, target, false, null, statisticsFile,
108                 false, 0, null, null, false );
109     }
110 }