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 java.io.File;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.List;
36  import java.util.Properties;
37  
38  import static java.io.File.createTempFile;
39  import static java.io.File.pathSeparatorChar;
40  import static java.io.File.separator;
41  import static java.io.File.separatorChar;
42  import static java.nio.charset.StandardCharsets.UTF_8;
43  import static java.nio.file.Files.readAllLines;
44  import static java.util.Arrays.asList;
45  import static java.util.Collections.singleton;
46  import static org.apache.maven.shared.utils.StringUtils.replace;
47  import static org.apache.maven.surefire.booter.Classpath.emptyClasspath;
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(), "",
68                  Collections.<String, String>emptyMap(), new String[0], true, 1, true,
69                  new Platform(), new NullConsoleLogger() );
70  
71          File patchFile = new File( "target" + separatorChar + "test-classes" );
72          File descriptor = new File( tmp, "module-info.class" );
73          descriptor.createNewFile();
74          List<String> modulePath =
75                  asList( "modular.jar", "target" + separatorChar + "classes" );
76          List<String> classPath = asList( "booter.jar", "non-modular.jar", patchFile.getPath() );
77          Collection<String> packages = singleton( "org.apache.abc" );
78          String startClassName = ForkedBooter.class.getName();
79  
80          File jigsawArgsFile =
81                  config.createArgsFile( "abc", modulePath, classPath, packages, patchFile, startClassName );
82  
83          assertThat( jigsawArgsFile )
84                  .isNotNull();
85  
86          List<String> argsFileLines = readAllLines( jigsawArgsFile.toPath(), UTF_8 );
87  
88          assertThat( argsFileLines )
89                  .hasSize( 13 );
90  
91          assertThat( argsFileLines.get( 0 ) )
92                  .isEqualTo( "--module-path" );
93  
94          assertThat( argsFileLines.get( 1 ) )
95                  .isEqualTo( "\"modular.jar"
96                          + pathSeparatorChar
97                          + "target" + ( separatorChar == '\\' ? "\\\\" : "/" ) + "classes\"" );
98  
99          assertThat( argsFileLines.get( 2 ) )
100                 .isEqualTo( "--class-path" );
101 
102         assertThat( argsFileLines.get( 3 ) )
103                 .isEqualTo( "\"booter.jar"
104                         + pathSeparatorChar
105                         + "non-modular.jar"
106                         + pathSeparatorChar
107                         + replace( patchFile.getPath(), "\\", "\\\\" )
108                         + "\"" );
109 
110         assertThat( argsFileLines.get( 4 ) )
111                 .isEqualTo( "--patch-module" );
112 
113         assertThat( argsFileLines.get( 5 ) )
114                 .isEqualTo( "abc=\"" + replace( patchFile.getPath(), "\\", "\\\\" ) + "\"" );
115 
116         assertThat( argsFileLines.get( 6 ) )
117                 .isEqualTo( "--add-exports" );
118 
119         assertThat( argsFileLines.get( 7 ) )
120                 .isEqualTo( "abc/org.apache.abc=ALL-UNNAMED" );
121 
122         assertThat( argsFileLines.get( 8 ) )
123                 .isEqualTo( "--add-modules" );
124 
125         assertThat( argsFileLines.get( 9 ) )
126                 .isEqualTo( "abc" );
127 
128         assertThat( argsFileLines.get( 10 ) )
129                 .isEqualTo( "--add-reads" );
130 
131         assertThat( argsFileLines.get( 11 ) )
132                 .isEqualTo( "abc=ALL-UNNAMED" );
133 
134         assertThat( argsFileLines.get( 12 ) )
135                 .isEqualTo( ForkedBooter.class.getName() );
136 
137         ModularClasspath modularClasspath = new ModularClasspath( "abc", modulePath, packages, patchFile );
138         Classpath testClasspathUrls = new Classpath( singleton( "target" + separator + "test-classes" ) );
139         Classpath surefireClasspathUrls = Classpath.emptyClasspath();
140         ModularClasspathConfiguration modularClasspathConfiguration =
141                 new ModularClasspathConfiguration( modularClasspath, testClasspathUrls, surefireClasspathUrls,
142                         emptyClasspath(), true, true );
143         ClassLoaderConfiguration clc = new ClassLoaderConfiguration( true, true );
144         StartupConfiguration startupConfiguration =
145                 new StartupConfiguration( "JUnitCoreProvider", modularClasspathConfiguration, clc, true, true, null );
146         OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
147         config.resolveClasspath( cli, ForkedBooter.class.getName(), startupConfiguration,
148                 createTempFile( "surefire", "surefire-reports" ) );
149 
150         assertThat( cli.getArguments() ).isNotNull();
151         assertThat( cli.getArguments() ).hasSize( 1 );
152         assertThat( cli.getArguments()[0] ).startsWith( "@" );
153         File argFile = new File( cli.getArguments()[0].substring( 1 ) );
154         assertThat( argFile ).isFile();
155         List<String> argsFileLines2 = readAllLines( argFile.toPath(), UTF_8 );
156         assertThat( argsFileLines2 ).hasSize( 13 );
157         for ( int i = 0; i < argsFileLines2.size(); i++ )
158         {
159             String line = argsFileLines2.get( i );
160             assertThat( line ).isEqualTo( argsFileLines.get( i ) );
161         }
162     }
163 }