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