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