View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.booterclient;
20  
21  import java.io.File;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.Commandline;
28  import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger;
29  import org.apache.maven.surefire.api.util.SureFireFileManager;
30  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
31  import org.apache.maven.surefire.booter.Classpath;
32  import org.apache.maven.surefire.booter.ForkedBooter;
33  import org.apache.maven.surefire.booter.ModularClasspath;
34  import org.apache.maven.surefire.booter.ModularClasspathConfiguration;
35  import org.apache.maven.surefire.booter.StartupConfiguration;
36  import org.apache.maven.surefire.extensions.ForkNodeFactory;
37  import org.junit.Test;
38  
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.surefire.booter.Classpath.emptyClasspath;
47  import static org.apache.maven.surefire.shared.utils.StringUtils.replace;
48  import static org.assertj.core.api.Assertions.assertThat;
49  import static org.mockito.Mockito.mock;
50  
51  /**
52   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
53   * @since 2.21.0.Jigsaw
54   */
55  public class ModularClasspathForkConfigurationTest {
56      @Test
57      @SuppressWarnings("ResultOfMethodCallIgnored")
58      public void shouldCreateModularArgsFile() throws Exception {
59          Classpath booter = new Classpath(asList("booter.jar", "non-modular.jar"));
60          File target = new File("target").getCanonicalFile();
61          File tmp = new File(target, "surefire");
62          tmp.mkdirs();
63          File pwd = new File(".").getCanonicalFile();
64  
65          ModularClasspathForkConfiguration config = new ModularClasspathForkConfiguration(
66                  booter,
67                  tmp,
68                  "",
69                  pwd,
70                  new Properties(),
71                  "",
72                  Collections.<String, String>emptyMap(),
73                  new String[0],
74                  true,
75                  1,
76                  true,
77                  new Platform(),
78                  new NullConsoleLogger(),
79                  mock(ForkNodeFactory.class));
80  
81          File patchFile = new File("target" + separatorChar + "test-classes");
82          File descriptor = new File(tmp, "module-info.class");
83          descriptor.createNewFile();
84          List<String> modulePath = asList("modular.jar", "target" + separatorChar + "classes");
85          List<String> classPath = asList("booter.jar", "non-modular.jar", patchFile.getPath());
86          Collection<String> packages = singleton("org.apache.abc");
87          String startClassName = ForkedBooter.class.getName();
88  
89          File jigsawArgsFile = config.createArgsFile(
90                  "abc",
91                  modulePath,
92                  classPath,
93                  packages,
94                  patchFile,
95                  startClassName,
96                  true,
97                  Collections.<String[]>emptyList());
98  
99          assertThat(jigsawArgsFile).isNotNull();
100 
101         List<String> argsFileLines = readAllLines(jigsawArgsFile.toPath(), UTF_8);
102 
103         assertThat(argsFileLines).hasSize(13);
104 
105         assertThat(argsFileLines.get(0)).isEqualTo("--module-path");
106 
107         assertThat(argsFileLines.get(1))
108                 .isEqualTo("\"modular.jar"
109                         + pathSeparatorChar
110                         + "target" + (separatorChar == '\\' ? "\\\\" : "/") + "classes\"");
111 
112         assertThat(argsFileLines.get(2)).isEqualTo("--class-path");
113 
114         assertThat(argsFileLines.get(3))
115                 .isEqualTo("\"booter.jar"
116                         + pathSeparatorChar
117                         + "non-modular.jar"
118                         + pathSeparatorChar
119                         + replace(patchFile.getPath(), "\\", "\\\\")
120                         + "\"");
121 
122         assertThat(argsFileLines.get(4)).isEqualTo("--patch-module");
123 
124         assertThat(argsFileLines.get(5)).isEqualTo("abc=\"" + replace(patchFile.getPath(), "\\", "\\\\") + "\"");
125 
126         assertThat(argsFileLines.get(6)).isEqualTo("--add-opens");
127 
128         assertThat(argsFileLines.get(7)).isEqualTo("abc/org.apache.abc=ALL-UNNAMED");
129 
130         assertThat(argsFileLines.get(8)).isEqualTo("--add-reads");
131 
132         assertThat(argsFileLines.get(9)).isEqualTo("abc=ALL-UNNAMED");
133 
134         assertThat(argsFileLines.get(10)).isEqualTo("--add-modules");
135 
136         assertThat(argsFileLines.get(11)).isEqualTo("ALL-MODULE-PATH");
137 
138         assertThat(argsFileLines.get(12)).isEqualTo(ForkedBooter.class.getName());
139 
140         ModularClasspath modularClasspath = new ModularClasspath("abc", modulePath, packages, patchFile, true);
141         Classpath testClasspathUrls = new Classpath(singleton("target" + separator + "test-classes"));
142         Classpath surefireClasspathUrls = Classpath.emptyClasspath();
143         ModularClasspathConfiguration modularClasspathConfiguration = new ModularClasspathConfiguration(
144                 modularClasspath, testClasspathUrls, surefireClasspathUrls, emptyClasspath(), true, true);
145         ClassLoaderConfiguration clc = new ClassLoaderConfiguration(true, true);
146         StartupConfiguration startupConfiguration = new StartupConfiguration(
147                 "JUnitCoreProvider", modularClasspathConfiguration, clc, null, Collections.<String[]>emptyList());
148         Commandline cli = new Commandline();
149         config.resolveClasspath(
150                 cli,
151                 ForkedBooter.class.getName(),
152                 startupConfiguration,
153                 SureFireFileManager.createTempFile("surefire", "surefire-reports"));
154 
155         assertThat(cli.getArguments()).isNotNull();
156         assertThat(cli.getArguments()).hasSize(1);
157         assertThat(cli.getArguments()[0]).startsWith("@");
158         File argFile = new File(cli.getArguments()[0].substring(1));
159         assertThat(argFile).isFile();
160         List<String> argsFileLines2 = readAllLines(argFile.toPath(), UTF_8);
161         assertThat(argsFileLines2).hasSize(13);
162         for (int i = 0; i < argsFileLines2.size(); i++) {
163             String line = argsFileLines2.get(i);
164             assertThat(line).isEqualTo(argsFileLines.get(i));
165         }
166     }
167 }