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.Iterator;
23  import java.util.List;
24  import java.util.concurrent.ExecutionException;
25  import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
26  import org.apache.maven.surefire.testset.TestSetFailedException;
27  import org.apache.maven.surefire.util.TestsToRun;
28  
29  import org.junit.runner.Computer;
30  import org.junit.runner.JUnitCore;
31  import org.junit.runner.Request;
32  import org.junit.runner.Result;
33  import org.junit.runner.Runner;
34  import org.junit.runner.manipulation.Filter;
35  import org.junit.runner.manipulation.NoTestsRemainException;
36  import org.junit.runner.notification.RunListener;
37  
38  /**
39   * Encapsulates access to JUnitCore
40   *
41   * @author Kristian Rosenvold
42   */
43  
44  class JUnitCoreWrapper
45  {
46      private static class FilteringRequest
47          extends Request
48      {
49          private Runner filteredRunner;
50  
51          public FilteringRequest( Request req, Filter filter )
52          {
53              try
54              {
55                  Runner runner = req.getRunner();
56                  filter.apply( runner );
57                  filteredRunner = runner;
58              }
59              catch ( NoTestsRemainException e )
60              {
61                  filteredRunner = null;
62              }
63          }
64  
65          @Override
66          public Runner getRunner()
67          {
68              return filteredRunner;
69          }
70      }
71  
72      public static void execute( TestsToRun testsToRun, JUnitCoreParameters jUnitCoreParameters,
73                                  List<RunListener> listeners, Filter filter )
74          throws TestSetFailedException
75      {
76          Computer computer = getComputer( jUnitCoreParameters );
77  
78          JUnitCore junitCore = createJUnitCore( listeners );
79  
80          try
81          {
82              if ( testsToRun.allowEagerReading() )
83              {
84                  executeEager( testsToRun, filter, computer, junitCore );
85              }
86              else
87              {
88                  exeuteLazy( testsToRun, filter, computer, junitCore );
89              }
90          }
91          finally
92          {
93              closeIfConfigurable( computer );
94          }
95      }
96  
97      private static JUnitCore createJUnitCore( List<RunListener> listeners )
98      {
99          JUnitCore junitCore = new JUnitCore();
100         for ( RunListener runListener : listeners )
101         {
102             junitCore.addListener( runListener );
103         }
104         return junitCore;
105     }
106 
107     private static void executeEager(TestsToRun testsToRun, Filter filter, Computer computer, JUnitCore junitCore)
108             throws TestSetFailedException 
109     {
110         Class[] tests = testsToRun.getLocatedClasses();
111         createReqestAndRun( filter, computer, junitCore, tests );
112     }
113 
114     private static void exeuteLazy(TestsToRun testsToRun, Filter filter, Computer computer, JUnitCore junitCore)
115             throws TestSetFailedException
116     {
117         // in order to support LazyTestsToRun, the iterator must be used
118         Iterator<?> classIter = testsToRun.iterator();
119         while ( classIter.hasNext() )
120         {
121             createReqestAndRun( filter, computer, junitCore, new Class[]{ (Class<?>) classIter.next() } );
122         }
123     }
124 
125     private static void createReqestAndRun( Filter filter, Computer computer, JUnitCore junitCore, Class<?>[] classesToRun )
126             throws TestSetFailedException
127     {
128         Request req = Request.classes( computer, classesToRun );
129         if ( filter != null )
130         {
131             req = new FilteringRequest( req, filter );
132             if ( req.getRunner() == null )
133             {
134                 // nothing to run
135                 return;
136             }
137         }
138 
139         final Result run = junitCore.run( req );
140         JUnit4RunListener.rethrowAnyTestMechanismFailures( run );
141     }
142 
143     private static void closeIfConfigurable( Computer computer )
144         throws TestSetFailedException
145     {
146         if ( computer instanceof ConfigurableParallelComputer )
147         {
148             try
149             {
150                 ( (ConfigurableParallelComputer) computer ).close();
151             }
152             catch ( ExecutionException e )
153             {
154                 throw new TestSetFailedException( e );
155             }
156         }
157     }
158 
159     private static Computer getComputer( JUnitCoreParameters jUnitCoreParameters )
160         throws TestSetFailedException
161     {
162         if ( jUnitCoreParameters.isNoThreading() )
163         {
164             return new Computer();
165         }
166         return getConfigurableParallelComputer( jUnitCoreParameters );
167     }
168 
169     private static Computer getConfigurableParallelComputer( JUnitCoreParameters jUnitCoreParameters )
170         throws TestSetFailedException
171     {
172         if ( jUnitCoreParameters.isUseUnlimitedThreads() )
173         {
174             return new ConfigurableParallelComputer();
175         }
176         else
177         {
178             return new ConfigurableParallelComputer(
179                 jUnitCoreParameters.isParallelClasses() | jUnitCoreParameters.isParallelBoth(),
180                 jUnitCoreParameters.isParallelMethod() | jUnitCoreParameters.isParallelBoth(),
181                 jUnitCoreParameters.getThreadCount(), jUnitCoreParameters.isPerCoreThreadCount() );
182         }
183     }
184 
185 }