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.common.junit4.JUnit4RunListener;
23  import org.apache.maven.surefire.junitcore.pc.ParallelComputer;
24  import org.apache.maven.surefire.junitcore.pc.ParallelComputerBuilder;
25  import org.apache.maven.surefire.report.ConsoleLogger;
26  import org.apache.maven.surefire.testset.TestSetFailedException;
27  import org.apache.maven.surefire.util.TestsToRun;
28  import org.junit.runner.Computer;
29  import org.junit.runner.JUnitCore;
30  import org.junit.runner.Request;
31  import org.junit.runner.Result;
32  import org.junit.runner.Runner;
33  import org.junit.runner.manipulation.Filter;
34  import org.junit.runner.manipulation.NoTestsRemainException;
35  import org.junit.runner.notification.RunListener;
36  
37  import java.util.List;
38  
39  /**
40   * Encapsulates access to JUnitCore
41   *
42   * @author Kristian Rosenvold
43   */
44  
45  class JUnitCoreWrapper
46  {
47      public static void execute( ConsoleLogger logger, TestsToRun testsToRun, JUnitCoreParameters jUnitCoreParameters,
48                                  List<RunListener> listeners, Filter filter )
49          throws TestSetFailedException
50      {
51          JUnitCore junitCore = createJUnitCore( listeners );
52          if ( testsToRun.allowEagerReading() )
53          {
54              executeEager( logger, testsToRun, filter, jUnitCoreParameters, junitCore );
55          }
56          else
57          {
58              executeLazy( logger, testsToRun, filter, jUnitCoreParameters, junitCore );
59          }
60      }
61  
62      private static JUnitCore createJUnitCore( List<RunListener> listeners )
63      {
64          JUnitCore junitCore = new JUnitCore();
65          for ( RunListener runListener : listeners )
66          {
67              junitCore.addListener( runListener );
68          }
69          return junitCore;
70      }
71  
72      private static void executeEager( ConsoleLogger logger, TestsToRun testsToRun, Filter filter,
73                                        JUnitCoreParameters jUnitCoreParameters, JUnitCore junitCore )
74          throws TestSetFailedException
75      {
76          Class[] tests = testsToRun.getLocatedClasses();
77          Computer computer = createComputer( logger, jUnitCoreParameters );
78          createRequestAndRun( filter, computer, junitCore, tests );
79      }
80  
81      private static void executeLazy( ConsoleLogger logger, TestsToRun testsToRun, Filter filter,
82                                       JUnitCoreParameters jUnitCoreParameters, JUnitCore junitCore )
83          throws TestSetFailedException
84      {
85          // in order to support LazyTestsToRun, the iterator must be used
86          for ( Class clazz : testsToRun )
87          {
88              Computer computer = createComputer( logger, jUnitCoreParameters );
89              createRequestAndRun( filter, computer, junitCore, clazz );
90          }
91      }
92  
93      private static void createRequestAndRun( Filter filter, Computer computer, JUnitCore junitCore,
94                                               Class<?>... classesToRun )
95          throws TestSetFailedException
96      {
97          Request req = Request.classes( computer, classesToRun );
98          if ( filter != null )
99          {
100             req = new FilteringRequest( req, filter );
101             if ( req.getRunner() == null )
102             {
103                 // nothing to run
104                 return;
105             }
106         }
107 
108         Result run = junitCore.run( req );
109         JUnit4RunListener.rethrowAnyTestMechanismFailures( run );
110 
111         if ( computer instanceof ParallelComputer )
112         {
113             String timeoutMessage = ( (ParallelComputer) computer ).describeElapsedTimeout();
114             if ( timeoutMessage.length() != 0 )
115             {
116                 throw new TestSetFailedException( timeoutMessage );
117             }
118         }
119     }
120 
121     private static Computer createComputer( ConsoleLogger logger, JUnitCoreParameters parameters )
122         throws TestSetFailedException
123     {
124         return parameters.isNoThreading()
125             ? Computer.serial()
126             : new ParallelComputerBuilder( logger, parameters ).buildComputer();
127     }
128 
129     private static class FilteringRequest
130         extends Request
131     {
132         private Runner filteredRunner;
133 
134         public FilteringRequest( Request req, Filter filter )
135         {
136             try
137             {
138                 Runner runner = req.getRunner();
139                 filter.apply( runner );
140                 filteredRunner = runner;
141             }
142             catch ( NoTestsRemainException e )
143             {
144                 filteredRunner = null;
145             }
146         }
147 
148         @Override
149         public Runner getRunner()
150         {
151             return filteredRunner;
152         }
153     }
154 }