Coverage Report - org.apache.maven.surefire.junitcore.JUnitCoreWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
JUnitCoreWrapper
0%
0/41
0%
0/16
2,4
JUnitCoreWrapper$FilteringRequest
0%
0/9
N/A
2,4
 
 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  0
 class JUnitCoreWrapper
 45  
 {
 46  0
     private static class FilteringRequest
 47  
         extends Request
 48  
     {
 49  
         private Runner filteredRunner;
 50  
 
 51  
         public FilteringRequest( Request req, Filter filter )
 52  0
         {
 53  
             try
 54  
             {
 55  0
                 Runner runner = req.getRunner();
 56  0
                 filter.apply( runner );
 57  0
                 filteredRunner = runner;
 58  
             }
 59  0
             catch ( NoTestsRemainException e )
 60  
             {
 61  0
                 filteredRunner = null;
 62  0
             }
 63  0
         }
 64  
 
 65  
         @Override
 66  
         public Runner getRunner()
 67  
         {
 68  0
             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  0
         Computer computer = getComputer( jUnitCoreParameters );
 77  
 
 78  0
         JUnitCore junitCore = createJUnitCore( listeners );
 79  
 
 80  
         try
 81  
         {
 82  0
             if ( testsToRun.allowEagerReading() )
 83  
             {
 84  0
                 executeEager( testsToRun, filter, computer, junitCore );
 85  
             }
 86  
             else
 87  
             {
 88  0
                 exeuteLazy( testsToRun, filter, computer, junitCore );
 89  
             }
 90  
         }
 91  
         finally
 92  
         {
 93  0
             closeIfConfigurable( computer );
 94  0
         }
 95  0
     }
 96  
 
 97  
     private static JUnitCore createJUnitCore( List<RunListener> listeners )
 98  
     {
 99  0
         JUnitCore junitCore = new JUnitCore();
 100  0
         for ( RunListener runListener : listeners )
 101  
         {
 102  0
             junitCore.addListener( runListener );
 103  
         }
 104  0
         return junitCore;
 105  
     }
 106  
 
 107  
     private static void executeEager(TestsToRun testsToRun, Filter filter, Computer computer, JUnitCore junitCore)
 108  
             throws TestSetFailedException 
 109  
     {
 110  0
         Class[] tests = testsToRun.getLocatedClasses();
 111  0
         createReqestAndRun( filter, computer, junitCore, tests );
 112  0
     }
 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  0
         Iterator<?> classIter = testsToRun.iterator();
 119  0
         while ( classIter.hasNext() )
 120  
         {
 121  0
             createReqestAndRun( filter, computer, junitCore, new Class[]{ (Class<?>) classIter.next() } );
 122  
         }
 123  0
     }
 124  
 
 125  
     private static void createReqestAndRun( Filter filter, Computer computer, JUnitCore junitCore, Class<?>[] classesToRun )
 126  
             throws TestSetFailedException
 127  
     {
 128  0
         Request req = Request.classes( computer, classesToRun );
 129  0
         if ( filter != null )
 130  
         {
 131  0
             req = new FilteringRequest( req, filter );
 132  0
             if ( req.getRunner() == null )
 133  
             {
 134  
                 // nothing to run
 135  0
                 return;
 136  
             }
 137  
         }
 138  
 
 139  0
         final Result run = junitCore.run( req );
 140  0
         JUnit4RunListener.rethrowAnyTestMechanismFailures( run );
 141  0
     }
 142  
 
 143  
     private static void closeIfConfigurable( Computer computer )
 144  
         throws TestSetFailedException
 145  
     {
 146  0
         if ( computer instanceof ConfigurableParallelComputer )
 147  
         {
 148  
             try
 149  
             {
 150  0
                 ( (ConfigurableParallelComputer) computer ).close();
 151  
             }
 152  0
             catch ( ExecutionException e )
 153  
             {
 154  0
                 throw new TestSetFailedException( e );
 155  0
             }
 156  
         }
 157  0
     }
 158  
 
 159  
     private static Computer getComputer( JUnitCoreParameters jUnitCoreParameters )
 160  
         throws TestSetFailedException
 161  
     {
 162  0
         if ( jUnitCoreParameters.isNoThreading() )
 163  
         {
 164  0
             return new Computer();
 165  
         }
 166  0
         return getConfigurableParallelComputer( jUnitCoreParameters );
 167  
     }
 168  
 
 169  
     private static Computer getConfigurableParallelComputer( JUnitCoreParameters jUnitCoreParameters )
 170  
         throws TestSetFailedException
 171  
     {
 172  0
         if ( jUnitCoreParameters.isUseUnlimitedThreads() )
 173  
         {
 174  0
             return new ConfigurableParallelComputer();
 175  
         }
 176  
         else
 177  
         {
 178  0
             return new ConfigurableParallelComputer(
 179  
                 jUnitCoreParameters.isParallelClasses() | jUnitCoreParameters.isParallelBoth(),
 180  
                 jUnitCoreParameters.isParallelMethod() | jUnitCoreParameters.isParallelBoth(),
 181  
                 jUnitCoreParameters.getThreadCount(), jUnitCoreParameters.isPerCoreThreadCount() );
 182  
         }
 183  
     }
 184  
 
 185  
 }