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