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