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.wrapper;
20  
21  import java.io.OutputStream;
22  import java.net.URI;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.junit.jupiter.api.Test;
31  import org.mockito.Mockito;
32  
33  import static org.apache.maven.wrapper.MavenWrapperMain.MVNW_REPOURL;
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.assertNotEquals;
36  import static org.junit.jupiter.api.Assertions.assertNull;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  import static org.junit.jupiter.api.Assertions.fail;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.verify;
41  import static org.mockito.Mockito.when;
42  
43  public class WrapperExecutorTest {
44      private final Installer install;
45  
46      private final BootstrapMainStarter start;
47  
48      private Path propertiesFile;
49  
50      private Properties properties = new Properties();
51  
52      private Path testDir = Paths.get("target/test-files/SystemPropertiesHandlerTest-" + System.currentTimeMillis());
53  
54      private Path mockInstallDir = testDir.resolve("mock-dir");
55  
56      public WrapperExecutorTest() throws Exception {
57          install = mock(Installer.class);
58          when(install.createDist(Mockito.any(WrapperConfiguration.class))).thenReturn(mockInstallDir);
59          start = mock(BootstrapMainStarter.class);
60  
61          Files.createDirectories(testDir);
62          propertiesFile = testDir.resolve("maven/wrapper/maven-wrapper.properties");
63  
64          properties.put("distributionUrl", "http://server/test/maven.zip");
65          properties.put("distributionBase", "testDistBase");
66          properties.put("distributionPath", "testDistPath");
67          properties.put("zipStoreBase", "testZipBase");
68          properties.put("zipStorePath", "testZipPath");
69  
70          writePropertiesFile(properties, propertiesFile, "header");
71      }
72  
73      @Test
74      void loadWrapperMetadataFromFile() throws Exception {
75          WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile(propertiesFile);
76  
77          assertEquals(new URI("http://server/test/maven.zip"), wrapper.getDistribution());
78          assertEquals(
79                  new URI("http://server/test/maven.zip"),
80                  wrapper.getConfiguration().getDistribution());
81          assertEquals("testDistBase", wrapper.getConfiguration().getDistributionBase());
82          assertEquals(
83                  "testDistPath", wrapper.getConfiguration().getDistributionPath().toString());
84          assertEquals("testZipBase", wrapper.getConfiguration().getZipBase());
85          assertEquals("testZipPath", wrapper.getConfiguration().getZipPath().toString());
86      }
87  
88      @Test
89      void loadWrapperMetadataFromDirectory() throws Exception {
90          WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory(testDir);
91  
92          assertEquals(new URI("http://server/test/maven.zip"), wrapper.getDistribution());
93          assertEquals(
94                  new URI("http://server/test/maven.zip"),
95                  wrapper.getConfiguration().getDistribution());
96          assertEquals("testDistBase", wrapper.getConfiguration().getDistributionBase());
97          assertEquals(
98                  "testDistPath", wrapper.getConfiguration().getDistributionPath().toString());
99          assertEquals("testZipBase", wrapper.getConfiguration().getZipBase());
100         assertEquals("testZipPath", wrapper.getConfiguration().getZipPath().toString());
101     }
102 
103     @Test
104     void useDefaultMetadataNoProeprtiesFile() throws Exception {
105         WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory(testDir.resolve("unknown"));
106 
107         assertNull(wrapper.getDistribution());
108         assertNull(wrapper.getConfiguration().getDistribution());
109         assertEquals(
110                 PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase());
111         assertEquals(
112                 Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath());
113         assertEquals(
114                 PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase());
115         assertEquals(
116                 Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath());
117     }
118 
119     @Test
120     void propertiesFileOnlyContainsDistURL() throws Exception {
121 
122         properties = new Properties();
123         properties.put("distributionUrl", "http://server/test/maven.zip");
124         writePropertiesFile(properties, propertiesFile, "header");
125 
126         WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile(propertiesFile);
127 
128         assertEquals(new URI("http://server/test/maven.zip"), wrapper.getDistribution());
129         assertEquals(
130                 new URI("http://server/test/maven.zip"),
131                 wrapper.getConfiguration().getDistribution());
132         assertEquals(
133                 PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase());
134         assertEquals(
135                 Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath());
136         assertEquals(
137                 PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase());
138         assertEquals(
139                 Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath());
140     }
141 
142     @Test
143     void executeInstallAndLaunch() throws Exception {
144         WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory(propertiesFile);
145 
146         wrapper.execute(new String[] {"arg"}, install, start);
147         verify(install).createDist(Mockito.any(WrapperConfiguration.class));
148         verify(start).start(new String[] {"arg"}, mockInstallDir);
149     }
150 
151     @Test
152     void failWhenDistNotSetInProperties() throws Exception {
153         properties = new Properties();
154         writePropertiesFile(properties, propertiesFile, "header");
155 
156         try {
157             WrapperExecutor.forWrapperPropertiesFile(propertiesFile);
158             fail("Expected RuntimeException");
159         } catch (RuntimeException e) {
160             assertEquals("Could not load wrapper properties from '" + propertiesFile + "'.", e.getMessage());
161             assertEquals(
162                     "No value with key 'distributionUrl' specified in wrapper properties file '" + propertiesFile
163                             + "'.",
164                     e.getCause().getMessage());
165         }
166     }
167 
168     @Test
169     void failWhenPropertiesFileDoesNotExist() {
170         propertiesFile = testDir.resolve("unknown.properties");
171 
172         try {
173             WrapperExecutor.forWrapperPropertiesFile(propertiesFile);
174             fail("Expected RuntimeException");
175         } catch (RuntimeException e) {
176             assertEquals("Wrapper properties file '" + propertiesFile + "' does not exist.", e.getMessage());
177         }
178     }
179 
180     @Test
181     void testRelativeDistUrl() throws Exception {
182 
183         properties = new Properties();
184         properties.put("distributionUrl", "some/relative/url/to/bin.zip");
185         writePropertiesFile(properties, propertiesFile, "header");
186 
187         WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile(propertiesFile);
188         assertNotEquals(
189                 "some/relative/url/to/bin.zip", wrapper.getDistribution().getSchemeSpecificPart());
190         assertTrue(wrapper.getDistribution().getSchemeSpecificPart().endsWith("some/relative/url/to/bin.zip"));
191     }
192 
193     @Test
194     void testEnvironmentVariableOverwriteSimpleCase() throws Exception {
195         final Map<String, String> environmentVariables = new HashMap<>();
196         environmentVariables.put(MVNW_REPOURL, "https://repo/test");
197 
198         properties = new Properties();
199         properties.put("distributionUrl", "https://server/path/to/bin.zip");
200         writePropertiesFile(properties, propertiesFile, "header");
201 
202         WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
203 
204         assertEquals(
205                 "https://repo/test/path/to/bin.zip", wrapper.getDistribution().toString());
206     }
207 
208     @Test
209     void testEnvironmentVariableOverwriteMvnwRepoUrlTrailingSlash() throws Exception {
210         final Map<String, String> environmentVariables = new HashMap<>();
211         environmentVariables.put(MVNW_REPOURL, "https://repo/test/");
212         properties = new Properties();
213         properties.put("distributionUrl", "https://server/path/to/bin.zip");
214         writePropertiesFile(properties, propertiesFile, "header");
215 
216         WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
217 
218         assertEquals(
219                 "https://repo/test/path/to/bin.zip", wrapper.getDistribution().toString());
220     }
221 
222     @Test
223     void testEnvironmentVariableOverwritePackageName() throws Exception {
224         final Map<String, String> environmentVariables = new HashMap<>();
225         environmentVariables.put(MVNW_REPOURL, "https://repo/test");
226         properties = new Properties();
227         properties.put("distributionUrl", "https://server/org/apache/maven/to/bin.zip");
228         writePropertiesFile(properties, propertiesFile, "header");
229 
230         WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
231 
232         assertEquals(
233                 "https://repo/test/org/apache/maven/to/bin.zip",
234                 wrapper.getDistribution().toString());
235     }
236 
237     @Test
238     void testEnvironmentVariableOverwritePackageNameTrailingSpace() throws Exception {
239         final Map<String, String> environmentVariables = new HashMap<>();
240         environmentVariables.put(MVNW_REPOURL, "https://repo/test/");
241         properties = new Properties();
242         properties.put("distributionUrl", "https://server/whatever/org/apache/maven/to/bin.zip");
243         writePropertiesFile(properties, propertiesFile, "header");
244 
245         WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
246 
247         assertEquals(
248                 "https://repo/test/org/apache/maven/to/bin.zip",
249                 wrapper.getDistribution().toString());
250     }
251 
252     private WrapperExecutor prepareWrapperExecutorWithEnvironmentVariables(
253             final Map<String, String> environmentVariables) {
254         return new WrapperExecutor(propertiesFile, new Properties()) {
255             @Override
256             protected String getEnv(String key) {
257                 return environmentVariables.get(key);
258             }
259         };
260     }
261 
262     private void writePropertiesFile(Properties properties, Path propertiesFile, String message) throws Exception {
263         Files.createDirectories(propertiesFile.getParent());
264         try (OutputStream outStream = Files.newOutputStream(propertiesFile)) {
265             properties.store(outStream, message);
266         }
267     }
268 }