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