Coverage Report - org.apache.maven.surefire.booter.ProviderFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ProviderFactory
0%
0/23
0%
0/2
1.2
ProviderFactory$ClassLoaderProxy
0%
0/9
N/A
1.2
 
 1  
 package org.apache.maven.surefire.booter;
 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.providerapi.SurefireProvider;
 23  
 
 24  
 import java.lang.reflect.InvocationHandler;
 25  
 import java.lang.reflect.Method;
 26  
 import java.lang.reflect.Proxy;
 27  
 
 28  
 /**
 29  
  * Creates the surefire provider.
 30  
  * <p/>
 31  
  * Todo: This class does a little bit too little ;)
 32  
  *
 33  
  * @author Kristian Rosenvold
 34  
  */
 35  0
 public class ProviderFactory
 36  
 {
 37  
     private final StartupConfiguration startupConfiguration;
 38  
 
 39  
     private final ProviderConfiguration providerConfiguration;
 40  
 
 41  
     private final ClassLoader surefireClassLoader;
 42  
 
 43  
     private final ClassLoader testsClassLoader;
 44  
 
 45  
     private final SurefireReflector surefireReflector;
 46  
 
 47  
 
 48  
     public ProviderFactory( StartupConfiguration startupConfiguration, ProviderConfiguration providerConfiguration,
 49  
                             ClassLoader surefireClassLoader, ClassLoader testsClassLoader )
 50  0
     {
 51  0
         this.providerConfiguration = providerConfiguration;
 52  0
         this.surefireClassLoader = surefireClassLoader;
 53  0
         this.startupConfiguration = startupConfiguration;
 54  0
         this.surefireReflector = new SurefireReflector( surefireClassLoader );
 55  0
         this.testsClassLoader = testsClassLoader;
 56  0
     }
 57  
 
 58  
     public SurefireProvider createProvider()
 59  
     {
 60  0
         ClassLoader context = java.lang.Thread.currentThread().getContextClassLoader();
 61  0
         Thread.currentThread().setContextClassLoader( surefireClassLoader );
 62  
 
 63  0
         StartupConfiguration starterConfiguration = startupConfiguration;
 64  0
         final Object o = surefireReflector.createBooterConfiguration();
 65  0
         surefireReflector.setTestSuiteDefinitionAware( o, providerConfiguration.getTestSuiteDefinition() );
 66  0
         surefireReflector.setProviderPropertiesAware( o, providerConfiguration.getProviderProperties() );
 67  0
         surefireReflector.setReporterConfigurationAware( o, providerConfiguration.getReporterConfiguration() );
 68  0
         surefireReflector.setTestClassLoaderAware( o, surefireClassLoader, testsClassLoader );
 69  0
         surefireReflector.setTestArtifactInfoAware( o, providerConfiguration.getTestArtifact() );
 70  0
         surefireReflector.setIfDirScannerAware( o, providerConfiguration.getDirScannerParams() );
 71  
 
 72  0
         Object provider = surefireReflector.instantiateProvider( starterConfiguration.getProviderClassName(), o );
 73  0
         Thread.currentThread().setContextClassLoader( context );
 74  
 
 75  0
         return createClassLoaderProxy( provider );
 76  
     }
 77  
 
 78  
     private SurefireProvider createClassLoaderProxy( Object target )
 79  
     {
 80  0
         return (SurefireProvider) Proxy.newProxyInstance( this.getClass().getClassLoader(),
 81  0
                                                           new Class[]{ SurefireProvider.class },
 82  
                                                           new ClassLoaderProxy( target ) );
 83  
     }
 84  
 
 85  
     private class ClassLoaderProxy
 86  
         implements InvocationHandler
 87  
     {
 88  
         private final Object target;
 89  
 
 90  
         public ClassLoaderProxy( Object delegate )
 91  0
         {
 92  0
             this.target = delegate;
 93  0
         }
 94  
 
 95  
         public Object invoke( Object proxy, Method method, Object[] args )
 96  
             throws Throwable
 97  
         {
 98  0
             ClassLoader original = java.lang.Thread.currentThread().getContextClassLoader();
 99  0
             Thread.currentThread().setContextClassLoader( testsClassLoader );
 100  
             try
 101  
             {
 102  0
                 Method delegateMethod = target.getClass().getMethod( method.getName(), method.getParameterTypes() );
 103  0
                 final Object result = delegateMethod.invoke( target, args );
 104  0
                 return surefireReflector.convertIfRunResult( result );
 105  
             }
 106  
             finally
 107  
             {
 108  0
                 Thread.currentThread().setContextClassLoader( original );
 109  
             }
 110  
         }
 111  
     }
 112  
 
 113  
 }