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  /**
25   * Configuration that is used by the SurefireStarter but does not make it into the provider itself.
26   *
27   * @author Kristian Rosenvold
28   */
29  public class StartupConfiguration
30  {
31      private static final String SUREFIRE_TEST_CLASSPATH = "surefire.test.class.path";
32  
33      private final String providerClassName;
34      private final AbstractPathConfiguration classpathConfiguration;
35      private final ClassLoaderConfiguration classLoaderConfiguration;
36      private final boolean isForkRequested;
37      private final boolean isInForkedVm;
38  
39      public StartupConfiguration( @Nonnull String providerClassName,
40                                   @Nonnull AbstractPathConfiguration classpathConfiguration,
41                                   @Nonnull ClassLoaderConfiguration classLoaderConfiguration, boolean isForkRequested,
42                                   boolean inForkedVm )
43      {
44          this.classpathConfiguration = classpathConfiguration;
45          this.classLoaderConfiguration = classLoaderConfiguration;
46          this.isForkRequested = isForkRequested;
47          this.providerClassName = providerClassName;
48          isInForkedVm = inForkedVm;
49      }
50  
51      public boolean isProviderMainClass()
52      {
53          return providerClassName.endsWith( "#main" );
54      }
55  
56      public static StartupConfiguration inForkedVm( String providerClassName,
57                                                     ClasspathConfiguration classpathConfiguration,
58                                                     ClassLoaderConfiguration classLoaderConfiguration )
59      {
60          return new StartupConfiguration( providerClassName, classpathConfiguration, classLoaderConfiguration, true,
61                                           true );
62      }
63  
64      public AbstractPathConfiguration getClasspathConfiguration()
65      {
66          return classpathConfiguration;
67      }
68  
69      @Deprecated
70      public boolean useSystemClassLoader()
71      {
72          // todo; I am not totally convinced this logic is as simple as it could be
73          return classLoaderConfiguration.isUseSystemClassLoader() && ( isInForkedVm || isForkRequested );
74      }
75  
76      public boolean isManifestOnlyJarRequestedAndUsable()
77      {
78          return classLoaderConfiguration.isManifestOnlyJarRequestedAndUsable();
79      }
80  
81      public String getProviderClassName()
82      {
83          return providerClassName;
84      }
85  
86      public String getActualClassName()
87      {
88          return isProviderMainClass() ? stripEnd( providerClassName, "#main" ) : providerClassName;
89      }
90  
91      /**
92       * <p>Strip any of a supplied String from the end of a String.</p>
93       * <br>
94       * <p>If the strip String is {@code null}, whitespace is
95       * stripped.</p>
96       *
97       * @param str   the String to remove characters from
98       * @param strip the String to remove
99       * @return the stripped String
100      */
101     private static String stripEnd( String str, String strip )
102     {
103         if ( str == null )
104         {
105             return null;
106         }
107         int end = str.length();
108 
109         if ( strip == null )
110         {
111             while ( ( end != 0 ) && Character.isWhitespace( str.charAt( end - 1 ) ) )
112             {
113                 end--;
114             }
115         }
116         else
117         {
118             while ( end != 0 && strip.indexOf( str.charAt( end - 1 ) ) != -1 )
119             {
120                 end--;
121             }
122         }
123         return str.substring( 0, end );
124     }
125 
126     public ClassLoaderConfiguration getClassLoaderConfiguration()
127     {
128         return classLoaderConfiguration;
129     }
130 
131     public boolean isShadefire()
132     {
133         return providerClassName.startsWith( "org.apache.maven.surefire.shadefire" );
134     }
135 
136     public void writeSurefireTestClasspathProperty()
137     {
138         getClasspathConfiguration().getTestClasspath().writeToSystemProperty( SUREFIRE_TEST_CLASSPATH );
139     }
140 }