View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.junitcore;
20  
21  import java.io.File;
22  import java.io.PrintStream;
23  import java.util.HashMap;
24  import java.util.concurrent.ExecutionException;
25  
26  import org.apache.maven.plugin.surefire.StartupReportConfiguration;
27  import org.apache.maven.plugin.surefire.extensions.SurefireConsoleOutputReporter;
28  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
29  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
30  import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger;
31  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
32  import org.apache.maven.surefire.api.report.ReporterFactory;
33  import org.apache.maven.surefire.api.testset.TestSetFailedException;
34  import org.junit.runner.Computer;
35  import org.junit.runner.JUnitCore;
36  import org.junit.runner.Result;
37  
38  import static org.apache.maven.surefire.api.report.ConsoleOutputCapture.startCapture;
39  import static org.apache.maven.surefire.junitcore.ConcurrentRunListener.createInstance;
40  
41  /**
42   * @author Kristian Rosenvold
43   */
44  public class JUnitCoreTester {
45      private final Computer computer;
46  
47      public JUnitCoreTester() {
48          this(new Computer());
49      }
50  
51      public JUnitCoreTester(Computer computer) {
52          this.computer = computer;
53      }
54  
55      public Result run(boolean parallelClasses, Class<?>... classes) throws TestSetFailedException, ExecutionException {
56          ReporterFactory reporterManagerFactory = defaultNoXml();
57          PrintStream out = System.out;
58          PrintStream err = System.err;
59          try {
60              HashMap<String, TestSet> classMethodCounts = new HashMap<>();
61              ConcurrentRunListener reporter =
62                      createInstance(classMethodCounts, reporterManagerFactory, parallelClasses, false);
63              JUnitCoreRunListener runListener = new JUnitCoreRunListener(reporter, classMethodCounts);
64              startCapture(runListener);
65              JUnitCore junitCore = new JUnitCore();
66  
67              junitCore.addListener(runListener);
68              final Result run = junitCore.run(computer, classes);
69              junitCore.removeListener(runListener);
70              return run;
71          } finally {
72              System.setOut(out);
73              System.setErr(err);
74              reporterManagerFactory.close();
75              if (computer instanceof ConfigurableParallelComputer) {
76                  ((ConfigurableParallelComputer) computer).close();
77              }
78          }
79      }
80  
81      /**
82       * For testing purposes only.
83       *
84       * @return DefaultReporterFactory for testing purposes
85       */
86      public static DefaultReporterFactory defaultNoXml() {
87          return new DefaultReporterFactory(defaultStartupReportConfiguration(), new NullConsoleLogger());
88      }
89  
90      /**
91       * For testing purposes only.
92       *
93       * @return StartupReportConfiguration fo testing purposes
94       */
95      private static StartupReportConfiguration defaultStartupReportConfiguration() {
96          File target = new File("./target");
97          File statisticsFile = new File(target, "TESTHASHxXML");
98          return new StartupReportConfiguration(
99                  true,
100                 true,
101                 "PLAIN",
102                 false,
103                 target,
104                 false,
105                 null,
106                 statisticsFile,
107                 false,
108                 0,
109                 null,
110                 null,
111                 false,
112                 new SurefireStatelessReporter(),
113                 new SurefireConsoleOutputReporter(),
114                 new SurefireStatelessTestsetInfoReporter());
115     }
116 }