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.surefire.testset.TestSetFailedException;
23  import org.apache.maven.surefire.util.TestsToRun;
24  
25  import java.util.List;
26  import java.util.concurrent.ExecutionException;
27  
28  import org.junit.runner.Computer;
29  import org.junit.runner.JUnitCore;
30  import org.junit.runner.notification.RunListener;
31  
32  /**
33   * Encapsulates access to JUnitCore
34   *
35   * @author Kristian Rosenvold
36   */
37  
38  class JUnitCoreWrapper
39  {
40      public static void execute( TestsToRun testsToRun, JUnitCoreParameters jUnitCoreParameters,
41                                  List<RunListener> listeners )
42          throws TestSetFailedException
43      {
44          Computer computer = getComputer( jUnitCoreParameters );
45          JUnitCore junitCore = new JUnitCore();
46          for ( RunListener runListener : listeners )
47          {
48              junitCore.addListener( runListener );
49          }
50          try
51          {
52              junitCore.run( computer, testsToRun.getLocatedClasses() );
53          }
54          finally
55          {
56              closeIfConfigurable( computer );
57              for ( RunListener runListener : listeners )
58              {
59                  junitCore.removeListener( runListener );
60              }
61          }
62      }
63  
64      private static void closeIfConfigurable( Computer computer )
65          throws TestSetFailedException
66      {
67          if ( computer instanceof ConfigurableParallelComputer )
68          {
69              try
70              {
71                  ( (ConfigurableParallelComputer) computer ).close();
72              }
73              catch ( ExecutionException e )
74              {
75                  throw new TestSetFailedException( e );
76              }
77          }
78      }
79  
80      private static Computer getComputer( JUnitCoreParameters jUnitCoreParameters )
81          throws TestSetFailedException
82      {
83          if ( jUnitCoreParameters.isNoThreading() )
84          {
85              return new Computer();
86          }
87          return getConfigurableParallelComputer( jUnitCoreParameters );
88      }
89  
90      private static Computer getConfigurableParallelComputer( JUnitCoreParameters jUnitCoreParameters )
91          throws TestSetFailedException
92      {
93          if ( jUnitCoreParameters.isUseUnlimitedThreads() )
94          {
95              return new ConfigurableParallelComputer();
96          }
97          else
98          {
99              return new ConfigurableParallelComputer(
100                 jUnitCoreParameters.isParallelClasses() | jUnitCoreParameters.isParallelBoth(),
101                 jUnitCoreParameters.isParallelMethod() | jUnitCoreParameters.isParallelBoth(),
102                 jUnitCoreParameters.getThreadCount(), jUnitCoreParameters.isPerCoreThreadCount() );
103         }
104     }
105 }