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 java.lang.reflect.Method;
23 import java.util.Iterator;
24 import org.apache.maven.surefire.providerapi.SurefireProvider;
25 import org.apache.maven.surefire.report.ReporterException;
26 import org.apache.maven.surefire.suite.RunResult;
27 import org.apache.maven.surefire.testset.TestSetFailedException;
28 import org.apache.maven.surefire.util.ReflectionUtils;
29
30
31
32
33
34
35
36
37 public class ProviderFactory
38 {
39 private final StartupConfiguration startupConfiguration;
40
41 private final ProviderConfiguration providerConfiguration;
42
43 private final ClassLoader surefireClassLoader;
44
45 private final ClassLoader testsClassLoader;
46
47 private final SurefireReflector surefireReflector;
48
49 private final Object reporterManagerFactory;
50
51 private static final Class[] invokeParamaters = new Class[]{ Object.class };
52
53
54 public ProviderFactory( StartupConfiguration startupConfiguration, ProviderConfiguration providerConfiguration,
55 ClassLoader surefireClassLoader, ClassLoader testsClassLoader,
56 Object reporterManagerFactory )
57 {
58 this.providerConfiguration = providerConfiguration;
59 this.surefireClassLoader = surefireClassLoader;
60 this.startupConfiguration = startupConfiguration;
61 this.surefireReflector = new SurefireReflector( surefireClassLoader );
62 this.testsClassLoader = testsClassLoader;
63 this.reporterManagerFactory = reporterManagerFactory;
64 }
65
66 public SurefireProvider createProvider()
67 {
68 ClassLoader context = java.lang.Thread.currentThread().getContextClassLoader();
69 Thread.currentThread().setContextClassLoader( surefireClassLoader );
70
71 StartupConfiguration starterConfiguration = startupConfiguration;
72
73 final Object o = surefireReflector.createBooterConfiguration( surefireClassLoader, reporterManagerFactory );
74 surefireReflector.setTestSuiteDefinitionAware( o, providerConfiguration.getTestSuiteDefinition() );
75 surefireReflector.setProviderPropertiesAware( o, providerConfiguration.getProviderProperties() );
76 surefireReflector.setReporterConfigurationAware( o, providerConfiguration.getReporterConfiguration() );
77 surefireReflector.setTestClassLoaderAware( o, surefireClassLoader, testsClassLoader );
78 surefireReflector.setTestArtifactInfoAware( o, providerConfiguration.getTestArtifact() );
79 surefireReflector.setIfDirScannerAware( o, providerConfiguration.getDirScannerParams() );
80
81 Object provider = surefireReflector.instantiateProvider( starterConfiguration.getProviderClassName(), o );
82 Thread.currentThread().setContextClassLoader( context );
83
84 return new ProviderProxy( provider );
85 }
86
87
88 private class ProviderProxy
89 implements SurefireProvider
90 {
91 private final Object providerInOtherClassLoader;
92
93
94 private ProviderProxy( Object providerInOtherClassLoader )
95 {
96 this.providerInOtherClassLoader = providerInOtherClassLoader;
97 }
98
99 public Iterator getSuites()
100 {
101 return (Iterator) ReflectionUtils.invokeGetter( providerInOtherClassLoader, "getSuites" );
102 }
103
104 public RunResult invoke( Object forkTestSet )
105 throws TestSetFailedException, ReporterException
106 {
107 final Method invoke =
108 ReflectionUtils.getMethod( providerInOtherClassLoader.getClass(), "invoke", invokeParamaters );
109
110 final Object result = ReflectionUtils.invokeMethodWithArray( providerInOtherClassLoader, invoke,
111 new Object[]{ forkTestSet } );
112 return (RunResult) surefireReflector.convertIfRunResult( result );
113 }
114
115 public void cancel()
116 {
117 final Method invoke =
118 ReflectionUtils.getMethod( providerInOtherClassLoader.getClass(), "cancel", new Class[]{ } );
119 ReflectionUtils.invokeMethodWithArray( providerInOtherClassLoader, invoke, null );
120 }
121 }
122 }