View Javadoc
1   package org.apache.maven.plugin.surefire.booterclient;
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 org.apache.maven.plugin.surefire.booterclient.lazytestprovider.OutputStreamFlushableCommandline;
23  import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger;
24  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
25  import org.apache.maven.surefire.booter.Classpath;
26  import org.apache.maven.surefire.booter.ForkedBooter;
27  import org.apache.maven.surefire.booter.ModularClasspath;
28  import org.apache.maven.surefire.booter.ModularClasspathConfiguration;
29  import org.apache.maven.surefire.booter.StartupConfiguration;
30  import org.junit.Test;
31  
32  import javax.annotation.Nonnull;
33  import java.io.File;
34  import java.util.Collection;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Properties;
38  
39  import static java.io.File.createTempFile;
40  import static java.io.File.pathSeparatorChar;
41  import static java.io.File.separator;
42  import static java.io.File.separatorChar;
43  import static java.nio.charset.StandardCharsets.UTF_8;
44  import static java.nio.file.Files.readAllLines;
45  import static java.util.Arrays.asList;
46  import static java.util.Collections.singleton;
47  import static org.apache.maven.shared.utils.StringUtils.replace;
48  import static org.fest.assertions.Assertions.assertThat;
49  
50  /**
51   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
52   * @since 2.21.0.Jigsaw
53   */
54  public class ModularClasspathForkConfigurationTest
55  {
56      @Test
57      @SuppressWarnings( "ResultOfMethodCallIgnored" )
58      public void shouldCreateModularArgsFile() throws Exception
59      {
60          Classpath booter = new Classpath( asList( "booter.jar", "non-modular.jar" ) );
61          File target = new File( "target" ).getCanonicalFile();
62          File tmp = new File( target, "surefire" );
63          tmp.mkdirs();
64          File pwd = new File( "." ).getCanonicalFile();
65  
66          ModularClasspathForkConfiguration config = new ModularClasspathForkConfiguration( booter, tmp, "", pwd,
67                  new Properties(), "", new HashMap<String, String>(), true, 1, true, new Platform(),
68                  new NullConsoleLogger() )
69          {
70              @Nonnull
71              @Override
72              String toModuleName( @Nonnull File moduleDescriptor )
73              {
74                  return "abc";
75              }
76          };
77  
78          File patchFile = new File( "target" + separatorChar + "test-classes" );
79          File descriptor = new File( tmp, "module-info.class" );
80          descriptor.createNewFile();
81          List<String> modulePath =
82                  asList( "modular.jar", "target" + separatorChar + "classes" );
83          List<String> classPath = asList( "booter.jar", "non-modular.jar", patchFile.getPath() );
84          Collection<String> packages = singleton( "org.apache.abc" );
85          String startClassName = ForkedBooter.class.getName();
86  
87          File jigsawArgsFile =
88                  config.createArgsFile( descriptor, modulePath, classPath, packages, patchFile, startClassName );
89  
90          assertThat( jigsawArgsFile )
91                  .isNotNull();
92  
93          List<String> argsFileLines = readAllLines( jigsawArgsFile.toPath(), UTF_8 );
94  
95          assertThat( argsFileLines )
96                  .hasSize( 13 );
97  
98          assertThat( argsFileLines.get( 0 ) )
99                  .isEqualTo( "--module-path" );
100 
101         assertThat( argsFileLines.get( 1 ) )
102                 .isEqualTo( "\"modular.jar"
103                         + pathSeparatorChar
104                         + "target" + ( separatorChar == '\\' ? "\\\\" : "/" ) + "classes\"" );
105 
106         assertThat( argsFileLines.get( 2 ) )
107                 .isEqualTo( "--class-path" );
108 
109         assertThat( argsFileLines.get( 3 ) )
110                 .isEqualTo( "\"booter.jar"
111                         + pathSeparatorChar
112                         + "non-modular.jar"
113                         + pathSeparatorChar
114                         + replace( patchFile.getPath(), "\\", "\\\\" )
115                         + "\"" );
116 
117         assertThat( argsFileLines.get( 4 ) )
118                 .isEqualTo( "--patch-module" );
119 
120         assertThat( argsFileLines.get( 5 ) )
121                 .isEqualTo( "abc=\"" + replace( patchFile.getPath(), "\\", "\\\\" ) + "\"" );
122 
123         assertThat( argsFileLines.get( 6 ) )
124                 .isEqualTo( "--add-exports" );
125 
126         assertThat( argsFileLines.get( 7 ) )
127                 .isEqualTo( "abc/org.apache.abc=ALL-UNNAMED" );
128 
129         assertThat( argsFileLines.get( 8 ) )
130                 .isEqualTo( "--add-modules" );
131 
132         assertThat( argsFileLines.get( 9 ) )
133                 .isEqualTo( "abc" );
134 
135         assertThat( argsFileLines.get( 10 ) )
136                 .isEqualTo( "--add-reads" );
137 
138         assertThat( argsFileLines.get( 11 ) )
139                 .isEqualTo( "abc=ALL-UNNAMED" );
140 
141         assertThat( argsFileLines.get( 12 ) )
142                 .isEqualTo( ForkedBooter.class.getName() );
143 
144         ModularClasspath modularClasspath = new ModularClasspath( descriptor, modulePath, packages, patchFile );
145         Classpath testClasspathUrls = new Classpath( singleton( "target" + separator + "test-classes" ) );
146         Classpath surefireClasspathUrls = Classpath.emptyClasspath();
147         ModularClasspathConfiguration modularClasspathConfiguration =
148                 new ModularClasspathConfiguration( modularClasspath, testClasspathUrls, surefireClasspathUrls,
149                         true, true );
150         ClassLoaderConfiguration clc = new ClassLoaderConfiguration( true, true );
151         StartupConfiguration startupConfiguration =
152                 new StartupConfiguration( "JUnitCoreProvider", modularClasspathConfiguration, clc, true, true );
153         OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
154         config.resolveClasspath( cli, ForkedBooter.class.getName(), startupConfiguration,
155                 createTempFile( "surefire", "surefire-reports" ) );
156 
157         assertThat( cli.getArguments() ).isNotNull();
158         assertThat( cli.getArguments() ).hasSize( 1 );
159         assertThat( cli.getArguments()[0] ).startsWith( "@" );
160         File argFile = new File( cli.getArguments()[0].substring( 1 ) );
161         assertThat( argFile ).isFile();
162         List<String> argsFileLines2 = readAllLines( argFile.toPath(), UTF_8 );
163         assertThat( argsFileLines2 ).hasSize( 13 );
164         for ( int i = 0; i < argsFileLines2.size(); i++ )
165         {
166             String line = argsFileLines2.get( i );
167             assertThat( line ).isEqualTo( argsFileLines.get( i ) );
168         }
169     }
170 }