1 package org.apache.maven.surefire.booter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
30
31
32
33
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 }