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.Before;
31  import org.junit.Test;
32  
33  import javax.annotation.Nonnull;
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Properties;
40  
41  import static org.apache.commons.lang3.JavaVersion.JAVA_1_7;
42  import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT;
43  import static java.io.File.separator;
44  import static java.io.File.pathSeparator;
45  import static java.nio.charset.StandardCharsets.UTF_8;
46  import static java.nio.file.Files.readAllLines;
47  import static java.util.Arrays.asList;
48  import static java.util.Collections.singleton;
49  import static org.fest.assertions.Assertions.assertThat;
50  import static org.junit.Assume.assumeTrue;
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      @Before
59      public void withJava7orHigher()
60      {
61          assumeTrue( JAVA_RECENT.atLeast( JAVA_1_7 ) );
62      }
63  
64      @Test
65      @SuppressWarnings( "ResultOfMethodCallIgnored" )
66      public void shouldCreateModularArgsFile() throws Exception
67      {
68          Classpath booter = new Classpath( asList( "booter.jar", "non-modular.jar" ) );
69          File target = new File( "target" ).getCanonicalFile();
70          File tmp = new File( target, "surefire" );
71          tmp.mkdirs();
72          File pwd = new File( "." ).getCanonicalFile();
73  
74          ModularClasspathForkConfiguration config = new ModularClasspathForkConfiguration( booter, tmp, "", pwd,
75                  new Properties(), "", new HashMap<String, String>(), true, 1, true, new Platform(),
76                  new NullConsoleLogger() )
77          {
78              @Nonnull
79              @Override
80              String toModuleName( @Nonnull File moduleDescriptor ) throws IOException
81              {
82                  return "abc";
83              }
84          };
85  
86          File patchFile = new File( "target" + separator + "test-classes" );
87          File descriptor = new File( tmp, "module-info.class" );
88          descriptor.createNewFile();
89          List<String> modulePath = asList( "modular.jar", "target/classes" );
90          List<String> classPath = asList( "booter.jar", "non-modular.jar", patchFile.getPath() );
91          Collection<String> packages = singleton( "org.apache.abc" );
92          String startClassName = ForkedBooter.class.getName();
93  
94          File jigsawArgsFile =
95                  config.createArgsFile( descriptor, modulePath, classPath, packages, patchFile, startClassName );
96  
97          assertThat( jigsawArgsFile ).isNotNull();
98          List<String> argsFileLines = readAllLines( jigsawArgsFile.toPath(), UTF_8 );
99          assertThat( argsFileLines ).hasSize( 13 );
100         assertThat( argsFileLines.get( 0 ) ).isEqualTo( "--module-path" );
101         assertThat( argsFileLines.get( 1 ) ).isEqualTo( "modular.jar" + pathSeparator + "target/classes" );
102         assertThat( argsFileLines.get( 2 ) ).isEqualTo( "--class-path" );
103         assertThat( argsFileLines.get( 3 ) )
104                 .isEqualTo( "booter.jar" + pathSeparator + "non-modular.jar" + pathSeparator + patchFile.getPath() );
105         assertThat( argsFileLines.get( 4 ) ).isEqualTo( "--patch-module" );
106         assertThat( argsFileLines.get( 5 ) ).isEqualTo( "abc=" + patchFile.getPath() );
107         assertThat( argsFileLines.get( 6 ) ).isEqualTo( "--add-exports" );
108         assertThat( argsFileLines.get( 7 ) ).isEqualTo( "abc/org.apache.abc=ALL-UNNAMED" );
109         assertThat( argsFileLines.get( 8 ) ).isEqualTo( "--add-modules" );
110         assertThat( argsFileLines.get( 9 ) ).isEqualTo( "abc" );
111         assertThat( argsFileLines.get( 10 ) ).isEqualTo( "--add-reads" );
112         assertThat( argsFileLines.get( 11 ) ).isEqualTo( "abc=ALL-UNNAMED" );
113         assertThat( argsFileLines.get( 12 ) ).isEqualTo( ForkedBooter.class.getName() );
114 
115         ModularClasspath modularClasspath = new ModularClasspath( descriptor, modulePath, packages, patchFile );
116         Classpath testClasspathUrls = new Classpath( singleton( "target" + separator + "test-classes" ) );
117         Classpath surefireClasspathUrls = Classpath.emptyClasspath();
118         ModularClasspathConfiguration modularClasspathConfiguration =
119                 new ModularClasspathConfiguration( modularClasspath, testClasspathUrls, surefireClasspathUrls,
120                         true, true );
121         ClassLoaderConfiguration clc = new ClassLoaderConfiguration( true, true );
122         StartupConfiguration startupConfiguration =
123                 new StartupConfiguration( "JUnitCoreProvider", modularClasspathConfiguration, clc, true, true );
124         OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
125         config.resolveClasspath( cli, ForkedBooter.class.getName(), startupConfiguration );
126 
127         assertThat( cli.getArguments() ).isNotNull();
128         assertThat( cli.getArguments() ).hasSize( 1 );
129         assertThat( cli.getArguments()[0] ).startsWith( "@" );
130         File argFile = new File( cli.getArguments()[0].substring( 1 ) );
131         assertThat( argFile ).isFile();
132         List<String> argsFileLines2 = readAllLines( argFile.toPath(), UTF_8 );
133         assertThat( argsFileLines2 ).hasSize( 13 );
134         for ( int i = 0; i < argsFileLines2.size(); i++ )
135         {
136             String line = argsFileLines2.get( i );
137             assertThat( line ).isEqualTo( argsFileLines.get( i ) );
138         }
139     }
140 }