View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.booter;
20  
21  import javax.annotation.Nonnull;
22  
23  import static org.apache.maven.surefire.booter.Classpath.join;
24  
25  /**
26   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
27   * @since 2.21.0.Jigsaw
28   */
29  public abstract class AbstractPathConfiguration {
30      public static final String CHILD_DELEGATION = "childDelegation";
31  
32      public static final String ENABLE_ASSERTIONS = "enableAssertions";
33  
34      public static final String CLASSPATH = "classPathUrl.";
35  
36      public static final String SUREFIRE_CLASSPATH = "surefireClassPathUrl.";
37  
38      private final Classpath surefireClasspathUrls;
39  
40      /**
41       * Whether to enable assertions or not
42       * (can be affected by the fork arguments, and the ability to do so based on the JVM).
43       */
44      private final boolean enableAssertions;
45  
46      // todo: @deprecated because the IsolatedClassLoader is really isolated - no parent.
47      private final boolean childDelegation;
48  
49      protected AbstractPathConfiguration(
50              @Nonnull Classpath surefireClasspathUrls, boolean enableAssertions, boolean childDelegation) {
51          if (isClassPathConfig() == isModularPathConfig()) {
52              throw new IllegalStateException("modular path and class path should be exclusive");
53          }
54          this.surefireClasspathUrls = surefireClasspathUrls;
55          this.enableAssertions = enableAssertions;
56          this.childDelegation = childDelegation;
57      }
58  
59      public abstract Classpath getTestClasspath();
60  
61      /**
62       * Must be exclusive with {@link #isClassPathConfig()}.
63       *
64       * @return {@code true} if <code>this</code> is {@link ModularClasspathConfiguration}.
65       */
66      public abstract boolean isModularPathConfig();
67  
68      /**
69       * Must be exclusive with {@link #isModularPathConfig()}.
70       *
71       * @return {@code true} if <code>this</code> is {@link ClasspathConfiguration}.
72       */
73      public abstract boolean isClassPathConfig();
74  
75      protected abstract Classpath getInprocClasspath();
76  
77      public <T extends AbstractPathConfiguration> T toRealPath(Class<T> type) {
78          if (isClassPathConfig() && type == ClasspathConfiguration.class
79                  || isModularPathConfig() && type == ModularClasspathConfiguration.class) {
80              return type.cast(this);
81          }
82          throw new IllegalStateException("no target matched " + type);
83      }
84  
85      public ClassLoader createMergedClassLoader() throws SurefireExecutionException {
86          return createMergedClassLoader(getInprocTestClasspath());
87      }
88  
89      public Classpath getProviderClasspath() {
90          return surefireClasspathUrls;
91      }
92  
93      public boolean isEnableAssertions() {
94          return enableAssertions;
95      }
96  
97      @Deprecated
98      public boolean isChildDelegation() {
99          return childDelegation;
100     }
101 
102     final Classpath getInprocTestClasspath() {
103         return join(getInprocClasspath(), getTestClasspath());
104     }
105 
106     final ClassLoader createMergedClassLoader(Classpath cp) throws SurefireExecutionException {
107         return cp.createClassLoader(isChildDelegation(), isEnableAssertions(), "test");
108     }
109 }