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