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  /**
23   * Represents the classpaths for the BooterConfiguration.
24   * <p/>
25   *
26   * @author Jason van Zyl
27   * @author Emmanuel Venisse
28   * @author Kristian Rosenvold
29   * @version $Id$
30   */
31  public class ClasspathConfiguration
32  {
33      private static final String CHILD_DELEGATION = "childDelegation";
34  
35      private static final String ENABLE_ASSERTIONS = "enableAssertions";
36  
37      private static final String CLASSPATH = "classPathUrl.";
38  
39      private static final String SUREFIRE_CLASSPATH = "surefireClassPathUrl.";
40  
41      private final Classpath classpathUrls;
42  
43      private final Classpath surefireClasspathUrls;
44  
45      /**
46       * The surefire classpath to use when invoking in-process with the plugin
47       */
48      private final Classpath inprocClasspath;
49  
50      /**
51       * Whether to enable assertions or not (can be affected by the fork arguments, and the ability to do so based on the
52       * JVM).
53       */
54      private final boolean enableAssertions;
55  
56      // todo: @deprecated because the IsolatedClassLoader is really isolated - no parent.
57      private final boolean childDelegation;
58  
59      public ClasspathConfiguration( boolean enableAssertions, boolean childDelegation )
60      {
61          this( new Classpath(), new Classpath(), new Classpath(), enableAssertions, childDelegation );
62      }
63  
64      ClasspathConfiguration( PropertiesWrapper properties )
65      {
66          this( properties.getClasspath( CLASSPATH ), properties.getClasspath( SUREFIRE_CLASSPATH ), new Classpath(),
67                properties.getBooleanProperty( ENABLE_ASSERTIONS ), properties.getBooleanProperty( CHILD_DELEGATION ) );
68      }
69  
70      public ClasspathConfiguration( Classpath testClasspath, Classpath surefireClassPathUrls, Classpath inprocClasspath,
71                                     boolean enableAssertions, boolean childDelegation )
72      {
73          this.enableAssertions = enableAssertions;
74          this.childDelegation = childDelegation;
75          this.inprocClasspath = inprocClasspath;
76          this.classpathUrls = testClasspath;
77          this.surefireClasspathUrls = surefireClassPathUrls;
78      }
79  
80      public void setForkProperties( PropertiesWrapper properties )
81      {
82          properties.setClasspath( CLASSPATH, classpathUrls );
83          properties.setClasspath( SUREFIRE_CLASSPATH, surefireClasspathUrls );
84          properties.setProperty( ENABLE_ASSERTIONS, String.valueOf( enableAssertions ) );
85          properties.setProperty( CHILD_DELEGATION, String.valueOf( childDelegation ) );
86      }
87  
88      public ClassLoader createTestClassLoader( boolean childDelegation )
89          throws SurefireExecutionException
90      {
91          return classpathUrls.createClassLoader( null, childDelegation, enableAssertions );
92      }
93  
94      public ClassLoader createTestClassLoader()
95          throws SurefireExecutionException
96      {
97          return classpathUrls.createClassLoader( null, this.childDelegation, enableAssertions );
98      }
99  
100     public ClassLoader createSurefireClassLoader( ClassLoader parent )
101         throws SurefireExecutionException
102     {
103         return surefireClasspathUrls.createClassLoader( parent, false, enableAssertions );
104     }
105 
106     public ClassLoader createInprocSurefireClassLoader( ClassLoader parent )
107         throws SurefireExecutionException
108     {
109         return inprocClasspath.createClassLoader( parent, false, enableAssertions );
110     }
111 
112     public Classpath getTestClasspath()
113     {
114         return classpathUrls;
115     }
116 
117     public ClassLoader createForkingTestClassLoader( boolean manifestOnlyJarRequestedAndUsable )
118         throws SurefireExecutionException
119     {
120         if ( manifestOnlyJarRequestedAndUsable )
121         {
122             System.setProperty( "surefire.real.class.path", System.getProperty( "java.class.path" ) );
123             getTestClasspath().writeToSystemProperty( "java.class.path" );
124             // this.getClass.getClassLoader() is always loaded in system classloader if forking
125             // this.getClass().getClassLoader() is plugin classloder if in-process
126             // "this" must refer to a class within the booter module
127             return this.getClass().getClassLoader();   // SUREFIRE-459, trick the app under test into thinking its classpath was conventional;
128         }
129         else
130         {
131             return createTestClassLoader();
132         }
133     }
134 }