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.List;
23  import java.util.concurrent.ExecutionException;
24  import org.apache.maven.surefire.testset.TestSetFailedException;
25  import org.apache.maven.surefire.util.TestsToRun;
26  
27  import org.junit.runner.Computer;
28  import org.junit.runner.JUnitCore;
29  import org.junit.runner.Result;
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              final Result run = junitCore.run( computer, testsToRun.getLocatedClasses() );
53  
54              if ( run.getFailureCount() > 0 )
55              {
56                  // There is something interesting going on here;
57                  // the "run" result can contain other exceptions that did not occur as
58                  // part of the test run, for instance if something bad happened in the
59                  // RunListener. But it also contains regular problems from the test-run.
60                  // I am not entirely sure of what to do with this; it might even be
61                  // that these errors are the correct errors to report back to the client.
62              }
63          }
64          finally
65          {
66              closeIfConfigurable( computer );
67              for ( RunListener runListener : listeners )
68              {
69                  junitCore.removeListener( runListener );
70              }
71          }
72      }
73  
74      private static void closeIfConfigurable( Computer computer )
75          throws TestSetFailedException
76      {
77          if ( computer instanceof ConfigurableParallelComputer )
78          {
79              try
80              {
81                  ( (ConfigurableParallelComputer) computer ).close();
82              }
83              catch ( ExecutionException e )
84              {
85                  throw new TestSetFailedException( e );
86              }
87          }
88      }
89  
90      private static Computer getComputer( JUnitCoreParameters jUnitCoreParameters )
91          throws TestSetFailedException
92      {
93          if ( jUnitCoreParameters.isNoThreading() )
94          {
95              return new Computer();
96          }
97          return getConfigurableParallelComputer( jUnitCoreParameters );
98      }
99  
100     private static Computer getConfigurableParallelComputer( JUnitCoreParameters jUnitCoreParameters )
101         throws TestSetFailedException
102     {
103         if ( jUnitCoreParameters.isUseUnlimitedThreads() )
104         {
105             return new ConfigurableParallelComputer();
106         }
107         else
108         {
109             return new ConfigurableParallelComputer(
110                 jUnitCoreParameters.isParallelClasses() | jUnitCoreParameters.isParallelBoth(),
111                 jUnitCoreParameters.isParallelMethod() | jUnitCoreParameters.isParallelBoth(),
112                 jUnitCoreParameters.getThreadCount(), jUnitCoreParameters.isPerCoreThreadCount() );
113         }
114     }
115 }