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      private final ProcessCheckerType processChecker;
39  
40      public StartupConfiguration( @Nonnull String providerClassName,
41                                   @Nonnull AbstractPathConfiguration classpathConfiguration,
42                                   @Nonnull ClassLoaderConfiguration classLoaderConfiguration, boolean isForkRequested,
43                                   boolean inForkedVm, ProcessCheckerType processChecker )
44      {
45          this.classpathConfiguration = classpathConfiguration;
46          this.classLoaderConfiguration = classLoaderConfiguration;
47          this.isForkRequested = isForkRequested;
48          this.providerClassName = providerClassName;
49          isInForkedVm = inForkedVm;
50          this.processChecker = processChecker;
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                                                     ProcessCheckerType processChecker )
62      {
63          return new StartupConfiguration( providerClassName, classpathConfiguration, classLoaderConfiguration, true,
64                                           true, processChecker );
65      }
66  
67      public AbstractPathConfiguration getClasspathConfiguration()
68      {
69          return classpathConfiguration;
70      }
71  
72      @Deprecated
73      public boolean useSystemClassLoader()
74      {
75          // todo; I am not totally convinced this logic is as simple as it could be
76          return classLoaderConfiguration.isUseSystemClassLoader() && ( isInForkedVm || isForkRequested );
77      }
78  
79      public boolean isManifestOnlyJarRequestedAndUsable()
80      {
81          return classLoaderConfiguration.isManifestOnlyJarRequestedAndUsable();
82      }
83  
84      public String getProviderClassName()
85      {
86          return providerClassName;
87      }
88  
89      public String getActualClassName()
90      {
91          return isProviderMainClass() ? stripEnd( providerClassName, "#main" ) : providerClassName;
92      }
93  
94      /**
95       * <p>Strip any of a supplied String from the end of a String.</p>
96       * <br>
97       * <p>If the strip String is {@code null}, whitespace is
98       * stripped.</p>
99       *
100      * @param str   the String to remove characters from
101      * @param strip the String to remove
102      * @return the stripped String
103      */
104     private static String stripEnd( String str, String strip )
105     {
106         if ( str == null )
107         {
108             return null;
109         }
110         int end = str.length();
111 
112         if ( strip == null )
113         {
114             while ( ( end != 0 ) && Character.isWhitespace( str.charAt( end - 1 ) ) )
115             {
116                 end--;
117             }
118         }
119         else
120         {
121             while ( end != 0 && strip.indexOf( str.charAt( end - 1 ) ) != -1 )
122             {
123                 end--;
124             }
125         }
126         return str.substring( 0, end );
127     }
128 
129     public ClassLoaderConfiguration getClassLoaderConfiguration()
130     {
131         return classLoaderConfiguration;
132     }
133 
134     public boolean isShadefire()
135     {
136         return providerClassName.startsWith( "org.apache.maven.shadefire.surefire" );
137     }
138 
139     public void writeSurefireTestClasspathProperty()
140     {
141         getClasspathConfiguration().getTestClasspath().writeToSystemProperty( SUREFIRE_TEST_CLASSPATH );
142     }
143 
144     public ProcessCheckerType getProcessChecker()
145     {
146         return processChecker;
147     }
148 }