Coverage Report - org.apache.maven.surefire.junit4.JUnit4Provider
 
Classes in this File Line Coverage Branch Coverage Complexity
JUnit4Provider
0%
0/71
0%
0/24
2.6
 
 1  
 package org.apache.maven.surefire.junit4;
 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.lang.reflect.Method;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
 26  
 import org.apache.maven.surefire.common.junit4.JUnit4RunListenerFactory;
 27  
 import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
 28  
 import org.apache.maven.surefire.providerapi.AbstractProvider;
 29  
 import org.apache.maven.surefire.providerapi.ProviderParameters;
 30  
 import org.apache.maven.surefire.report.ConsoleOutputCapture;
 31  
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 32  
 import org.apache.maven.surefire.report.PojoStackTraceWriter;
 33  
 import org.apache.maven.surefire.report.ReportEntry;
 34  
 import org.apache.maven.surefire.report.ReporterException;
 35  
 import org.apache.maven.surefire.report.ReporterFactory;
 36  
 import org.apache.maven.surefire.report.RunListener;
 37  
 import org.apache.maven.surefire.report.SimpleReportEntry;
 38  
 import org.apache.maven.surefire.suite.RunResult;
 39  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 40  
 import org.apache.maven.surefire.util.DefaultDirectoryScanner;
 41  
 import org.apache.maven.surefire.util.DirectoryScanner;
 42  
 import org.apache.maven.surefire.util.RunOrderCalculator;
 43  
 import org.apache.maven.surefire.util.TestsToRun;
 44  
 
 45  
 import org.apache.maven.surefire.util.internal.StringUtils;
 46  
 import org.codehaus.plexus.util.SelectorUtils;
 47  
 import org.junit.runner.Request;
 48  
 import org.junit.runner.Result;
 49  
 import org.junit.runner.Runner;
 50  
 import org.junit.runner.notification.RunNotifier;
 51  
 
 52  
 /**
 53  
  * @author Kristian Rosenvold
 54  
  */
 55  
 public class JUnit4Provider
 56  
     extends AbstractProvider
 57  
 {
 58  
     private final ClassLoader testClassLoader;
 59  
 
 60  
     private final DirectoryScanner directoryScanner;
 61  
 
 62  
     private final List<org.junit.runner.notification.RunListener> customRunListeners;
 63  
 
 64  
     private final JUnit4TestChecker jUnit4TestChecker;
 65  
 
 66  
     private final String requestedTestMethod;
 67  
 
 68  
     private TestsToRun testsToRun;
 69  
 
 70  
     private final ProviderParameters providerParameters;
 71  
 
 72  
     private final RunOrderCalculator runOrderCalculator;
 73  
 
 74  
 
 75  
     public JUnit4Provider( ProviderParameters booterParameters )
 76  0
     {
 77  0
         this.providerParameters = booterParameters;
 78  0
         this.testClassLoader = booterParameters.getTestClassLoader();
 79  0
         this.directoryScanner = booterParameters.getDirectoryScanner();
 80  0
         this.runOrderCalculator = booterParameters.getRunOrderCalculator();
 81  0
         customRunListeners = JUnit4RunListenerFactory.
 82  
             createCustomListeners( booterParameters.getProviderProperties().getProperty( "listener" ) );
 83  0
         jUnit4TestChecker = new JUnit4TestChecker( testClassLoader );
 84  0
         requestedTestMethod = booterParameters.getTestRequest().getRequestedTestMethod();
 85  
 
 86  0
     }
 87  
 
 88  
     public RunResult invoke( Object forkTestSet )
 89  
         throws TestSetFailedException, ReporterException
 90  
     {
 91  0
         if ( testsToRun == null )
 92  
         {
 93  0
             testsToRun = forkTestSet == null ? scanClassPath() : TestsToRun.fromClass( (Class) forkTestSet );
 94  
         }
 95  
 
 96  0
         upgradeCheck();
 97  
 
 98  0
         final ReporterFactory reporterFactory = providerParameters.getReporterFactory();
 99  
 
 100  0
         final RunListener reporter = reporterFactory.createReporter();
 101  
 
 102  
 
 103  0
         ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
 104  
 
 105  0
         JUnit4RunListener jUnit4TestSetReporter = new JUnit4RunListener( reporter );
 106  
 
 107  0
         Result result = new Result();
 108  0
         RunNotifier runNotifer = getRunNotifer( jUnit4TestSetReporter, result, customRunListeners );
 109  
 
 110  0
         runNotifer.fireTestRunStarted( null );
 111  
 
 112  0
         for ( Class clazz : testsToRun.getLocatedClasses() )
 113  
         {
 114  0
             executeTestSet( clazz, reporter, runNotifer );
 115  
         }
 116  
 
 117  0
         runNotifer.fireTestRunFinished( result );
 118  
 
 119  0
         closeRunNotifer( jUnit4TestSetReporter, customRunListeners );
 120  
 
 121  0
         return reporterFactory.close();
 122  
     }
 123  
 
 124  
     private void executeTestSet( Class clazz, RunListener reporter, RunNotifier listeners )
 125  
         throws ReporterException, TestSetFailedException
 126  
     {
 127  0
         final ReportEntry report = new SimpleReportEntry( this.getClass().getName(), clazz.getName() );
 128  
 
 129  0
         reporter.testSetStarting( report );
 130  
 
 131  
         try
 132  
         {
 133  0
             execute( clazz, listeners, this.requestedTestMethod );
 134  
         }
 135  0
         catch ( TestSetFailedException e )
 136  
         {
 137  0
             throw e;
 138  
         }
 139  0
         catch ( Throwable e )
 140  
         {
 141  0
             reporter.testError( new SimpleReportEntry( report.getSourceName(), report.getName(),
 142  
                                                        new PojoStackTraceWriter( report.getSourceName(),
 143  
                                                                                  report.getName(), e ) ) );
 144  
         }
 145  
         finally
 146  
         {
 147  0
             reporter.testSetCompleted( report );
 148  0
         }
 149  0
     }
 150  
 
 151  
     private RunNotifier getRunNotifer( org.junit.runner.notification.RunListener main, Result result, List<org.junit.runner.notification.RunListener> others )
 152  
     {
 153  0
         RunNotifier fNotifier = new RunNotifier();
 154  0
         fNotifier.addListener( main );
 155  0
         fNotifier.addListener(  result.createListener() );
 156  0
         for ( org.junit.runner.notification.RunListener listener : others )
 157  
         {
 158  0
             fNotifier.addListener( listener );
 159  
         }
 160  0
         return fNotifier;
 161  
     }
 162  
 
 163  
     // I am not entierly sure as to why we do this explicit freeing, it's one of those
 164  
     // pieces of code that just seem to linger on in here ;)
 165  
     private void closeRunNotifer( org.junit.runner.notification.RunListener main,
 166  
                                   List<org.junit.runner.notification.RunListener> others )
 167  
     {
 168  0
         RunNotifier fNotifier = new RunNotifier();
 169  0
         fNotifier.removeListener( main );
 170  0
         for ( org.junit.runner.notification.RunListener listener : others )
 171  
         {
 172  0
             fNotifier.removeListener( listener );
 173  
         }
 174  0
     }
 175  
 
 176  
     public Iterator getSuites()
 177  
     {
 178  0
         testsToRun = scanClassPath();
 179  0
         return testsToRun.iterator();
 180  
     }
 181  
 
 182  
     private TestsToRun scanClassPath()
 183  
     {
 184  0
         final TestsToRun scannedClasses = directoryScanner.locateTestClasses( testClassLoader, jUnit4TestChecker );
 185  0
         return runOrderCalculator.orderTestClasses(  scannedClasses );
 186  
     }
 187  
 
 188  
     private void upgradeCheck()
 189  
         throws TestSetFailedException
 190  
     {
 191  0
         if ( isJunit4UpgradeCheck()
 192  
             && ( (DefaultDirectoryScanner) directoryScanner ).getClassesSkippedByValidation().size() > 0 )
 193  
         {
 194  0
             StringBuilder reason = new StringBuilder();
 195  0
             reason.append( "Updated check failed\n" );
 196  0
             reason.append( "There are tests that would be run with junit4 / surefire 2.6 but not with [2.7,):\n" );
 197  
             //noinspection unchecked
 198  0
             for ( Class testClass : (List<Class>) ( (DefaultDirectoryScanner) directoryScanner ).getClassesSkippedByValidation() )
 199  
             {
 200  0
                 reason.append( "   " );
 201  0
                 reason.append( testClass.getCanonicalName() );
 202  0
                 reason.append( "\n" );
 203  
             }
 204  0
             throw new TestSetFailedException( reason.toString() );
 205  
         }
 206  0
     }
 207  
 
 208  
     private boolean isJunit4UpgradeCheck()
 209  
     {
 210  0
         final String property = System.getProperty( "surefire.junit4.upgradecheck" );
 211  0
         return property != null;
 212  
     }
 213  
 
 214  
 
 215  
     private static void execute( Class testClass, RunNotifier fNotifier, String testMethod )
 216  
         throws TestSetFailedException
 217  
     {
 218  0
         if ( !StringUtils.isBlank( testMethod ) )
 219  
         {
 220  0
             Method[] methods = testClass.getMethods();
 221  0
             for (int i = 0,size = methods.length;i<size;i++)
 222  
             {
 223  0
                 if ( SelectorUtils.match( testMethod, methods[i].getName() ) )
 224  
                 {
 225  0
                     Runner junitTestRunner = Request.method( testClass, methods[i].getName() ).getRunner();
 226  0
                     junitTestRunner.run( fNotifier );
 227  
                 }
 228  
             }
 229  0
             return;
 230  
         }
 231  
 
 232  0
         Runner junitTestRunner = Request.aClass( testClass ).getRunner();
 233  
 
 234  0
         junitTestRunner.run( fNotifier );
 235  0
     }
 236  
 }