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.ConsoleOutputCapture;
24  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
25  import org.apache.maven.surefire.report.DefaultConsoleReporter;
26  import org.apache.maven.surefire.report.ReporterFactory;
27  import org.apache.maven.surefire.report.RunListener;
28  import org.apache.maven.surefire.testset.TestSetFailedException;
29  import org.junit.runner.Computer;
30  import org.junit.runner.JUnitCore;
31  import org.junit.runner.Result;
32  
33  import java.util.HashMap;
34  import java.util.concurrent.ExecutionException;
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          try
60          {
61  
62              final HashMap<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
63              RunListener reporter =
64                  ConcurrentRunListener.createInstance( classMethodCounts, reporterManagerFactory, parallelClasses, false,
65                                                        new DefaultConsoleReporter( System.out ) );
66              ConsoleOutputCapture.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  }