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