View Javadoc

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  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      {
51          this.providerConfiguration = providerConfiguration;
52          this.surefireClassLoader = surefireClassLoader;
53          this.startupConfiguration = startupConfiguration;
54          this.surefireReflector = new SurefireReflector( surefireClassLoader );
55          this.testsClassLoader = testsClassLoader;
56      }
57  
58      public SurefireProvider createProvider()
59      {
60          ClassLoader context = java.lang.Thread.currentThread().getContextClassLoader();
61          Thread.currentThread().setContextClassLoader( surefireClassLoader );
62  
63          StartupConfiguration starterConfiguration = startupConfiguration;
64          final Object o = surefireReflector.createBooterConfiguration();
65          surefireReflector.setTestSuiteDefinitionAware( o, providerConfiguration.getTestSuiteDefinition() );
66          surefireReflector.setProviderPropertiesAware( o, providerConfiguration.getProviderProperties() );
67          surefireReflector.setReporterConfigurationAware( o, providerConfiguration.getReporterConfiguration() );
68          surefireReflector.setTestClassLoaderAware( o, surefireClassLoader, testsClassLoader );
69          surefireReflector.setTestArtifactInfoAware( o, providerConfiguration.getTestArtifact() );
70          surefireReflector.setIfDirScannerAware( o, providerConfiguration.getDirScannerParams() );
71  
72          Object provider = surefireReflector.instantiateProvider( starterConfiguration.getProviderClassName(), o );
73          Thread.currentThread().setContextClassLoader( context );
74  
75          return createClassLoaderProxy( provider );
76      }
77  
78      private SurefireProvider createClassLoaderProxy( Object target )
79      {
80          return (SurefireProvider) Proxy.newProxyInstance( this.getClass().getClassLoader(),
81                                                            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          {
92              this.target = delegate;
93          }
94  
95          public Object invoke( Object proxy, Method method, Object[] args )
96              throws Throwable
97          {
98              ClassLoader original = java.lang.Thread.currentThread().getContextClassLoader();
99              Thread.currentThread().setContextClassLoader( testsClassLoader );
100             try
101             {
102                 Method delegateMethod = target.getClass().getMethod( method.getName(), method.getParameterTypes() );
103                 final Object result = delegateMethod.invoke( target, args );
104                 return surefireReflector.convertIfRunResult( result );
105             }
106             finally
107             {
108                 Thread.currentThread().setContextClassLoader( original );
109             }
110         }
111     }
112 
113 }