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