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.report.DefaultReporterFactory;
23  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
24  import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
25  import org.apache.maven.surefire.report.ReporterFactory;
26  import org.apache.maven.surefire.report.RunListener;
27  import org.apache.maven.surefire.testset.TestSetFailedException;
28  import org.junit.runner.Computer;
29  import org.junit.runner.JUnitCore;
30  import org.junit.runner.Result;
31  
32  import java.util.HashMap;
33  import java.util.concurrent.ExecutionException;
34  
35  import static org.apache.maven.surefire.junitcore.ConcurrentRunListener.createInstance;
36  import static org.apache.maven.surefire.report.ConsoleOutputCapture.startCapture;
37  
38  /**
39   * @author Kristian Rosenvold
40   */
41  public class JUnitCoreTester
42  {
43  
44      private final Computer computer;
45  
46      public JUnitCoreTester()
47      {
48          this( new Computer() );
49      }
50  
51      public JUnitCoreTester( Computer computer )
52      {
53          this.computer = computer;
54      }
55  
56      public Result run( boolean parallelClasses, Class<?>... classes )
57          throws TestSetFailedException, ExecutionException
58      {
59          ReporterFactory reporterManagerFactory = DefaultReporterFactory.defaultNoXml();
60  
61          try
62          {
63              final HashMap<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
64              RunListener reporter = createInstance( classMethodCounts, reporterManagerFactory, parallelClasses, false,
65                                                           new DefaultDirectConsoleReporter( System.out ) );
66              startCapture( (ConsoleOutputReceiver) reporter );
67  
68              JUnitCoreRunListener runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
69              JUnitCore junitCore = new JUnitCore();
70  
71              junitCore.addListener( runListener );
72              final Result run = junitCore.run( computer, classes );
73              junitCore.removeListener( runListener );
74              return run;
75          }
76          finally
77          {
78              reporterManagerFactory.close();
79              if ( computer instanceof ConfigurableParallelComputer )
80              {
81                  ( (ConfigurableParallelComputer) computer ).close();
82              }
83          }
84      }
85  
86  
87  }