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 javax.annotation.Nonnull;
23  
24  import static org.apache.maven.surefire.booter.Classpath.join;
25  
26  /**
27   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
28   * @since 2.21.0.Jigsaw
29   */
30  public abstract class AbstractPathConfiguration
31  {
32      public static final String CHILD_DELEGATION = "childDelegation";
33  
34      public static final String ENABLE_ASSERTIONS = "enableAssertions";
35  
36      public static final String CLASSPATH = "classPathUrl.";
37  
38      public static final String SUREFIRE_CLASSPATH = "surefireClassPathUrl.";
39  
40      private final Classpath surefireClasspathUrls;
41  
42      /**
43       * Whether to enable assertions or not
44       * (can be affected by the fork arguments, and the ability to do so based on the JVM).
45       */
46      private final boolean enableAssertions;
47  
48      // todo: @deprecated because the IsolatedClassLoader is really isolated - no parent.
49      private final boolean childDelegation;
50  
51      protected AbstractPathConfiguration( @Nonnull Classpath surefireClasspathUrls,
52                                           boolean enableAssertions, boolean childDelegation )
53      {
54          if ( isClassPathConfig() == isModularPathConfig() )
55          {
56              throw new IllegalStateException( "modular path and class path should be exclusive" );
57          }
58          this.surefireClasspathUrls = surefireClasspathUrls;
59          this.enableAssertions = enableAssertions;
60          this.childDelegation = childDelegation;
61      }
62  
63      public abstract Classpath getTestClasspath();
64  
65      /**
66       * Must be exclusive with {@link #isClassPathConfig()}.
67       *
68       * @return {@code true} if <tt>this</tt> is {@link ModularClasspathConfiguration}.
69       */
70      public abstract boolean isModularPathConfig();
71  
72      /**
73       * Must be exclusive with {@link #isModularPathConfig()}.
74       *
75       * @return {@code true} if <tt>this</tt> is {@link ClasspathConfiguration}.
76       */
77      public abstract boolean isClassPathConfig();
78  
79      protected abstract Classpath getInprocClasspath();
80  
81      public <T extends AbstractPathConfiguration> T toRealPath( Class<T> type )
82      {
83          if ( isClassPathConfig() && type == ClasspathConfiguration.class
84                  || isModularPathConfig() && type == ModularClasspathConfiguration.class )
85          {
86              return type.cast( this );
87          }
88          throw new IllegalStateException( "no target matched " + type );
89      }
90  
91      public ClassLoader createMergedClassLoader()
92              throws SurefireExecutionException
93      {
94          return createMergedClassLoader( getInprocTestClasspath() );
95      }
96  
97      public Classpath getProviderClasspath()
98      {
99          return surefireClasspathUrls;
100     }
101 
102     public boolean isEnableAssertions()
103     {
104         return enableAssertions;
105     }
106 
107     @Deprecated
108     public boolean isChildDelegation()
109     {
110         return childDelegation;
111     }
112 
113     final Classpath getInprocTestClasspath()
114     {
115         return join( getInprocClasspath(), getTestClasspath() );
116     }
117 
118     final ClassLoader createMergedClassLoader( Classpath cp )
119             throws SurefireExecutionException
120     {
121         return cp.createClassLoader( isChildDelegation(), isEnableAssertions(), "test" );
122     }
123 }