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 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   * Creates the surefire provider.
33   * <p/>
34   *
35   * @author Kristian Rosenvold
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 }