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.net.URI;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.regex.Pattern;
25  
26  import org.hamcrest.BaseMatcher;
27  import org.hamcrest.Description;
28  import org.hamcrest.Matcher;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.hamcrest.MatcherAssert.assertThat;
33  import static org.hamcrest.Matchers.equalTo;
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.fail;
36  
37  /**
38   * @author Hans Dockter
39   */
40  public class PathAssemblerTest {
41      public static final String TEST_MAVEN_USER_HOME = "someUserHome";
42  
43      private PathAssembler pathAssembler = new PathAssembler(Paths.get(TEST_MAVEN_USER_HOME));
44  
45      final WrapperConfiguration configuration = new WrapperConfiguration();
46  
47      @BeforeEach
48      void setup() {
49          configuration.setDistributionBase(PathAssembler.MAVEN_USER_HOME_STRING);
50          configuration.setDistributionPath(Paths.get("somePath"));
51          configuration.setZipBase(PathAssembler.MAVEN_USER_HOME_STRING);
52          configuration.setZipPath(Paths.get("somePath"));
53      }
54  
55      @Test
56      void distributionDirWithMavenUserHomeBase() throws Exception {
57          configuration.setDistribution(new URI("http://server/dist/maven-0.9-bin.zip"));
58  
59          Path distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir();
60          assertThat(distributionDir.getFileName().toString(), matchesRegexp("[a-z0-9]+"));
61          assertThat(distributionDir.getParent(), equalTo(file(TEST_MAVEN_USER_HOME + "/somePath/maven-0.9-bin")));
62      }
63  
64      @Test
65      void distributionDirWithProjectBase() throws Exception {
66          configuration.setDistributionBase(PathAssembler.PROJECT_STRING);
67          configuration.setDistribution(new URI("http://server/dist/maven-0.9-bin.zip"));
68  
69          Path distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir();
70          assertThat(distributionDir.getFileName().toString(), matchesRegexp("[a-z0-9]+"));
71          assertThat(distributionDir.getParent(), equalTo(file(currentDirPath() + "/somePath/maven-0.9-bin")));
72      }
73  
74      @Test
75      void distributionDirWithUnknownBase() throws Exception {
76          configuration.setDistribution(new URI("http://server/dist/maven-1.0.zip"));
77          configuration.setDistributionBase("unknownBase");
78  
79          try {
80              pathAssembler.getDistribution(configuration);
81              fail();
82          } catch (RuntimeException e) {
83              assertEquals("Base: unknownBase is unknown", e.getMessage());
84          }
85      }
86  
87      @Test
88      void distZipWithMavenUserHomeBase() throws Exception {
89          configuration.setDistribution(new URI("http://server/dist/maven-1.0.zip"));
90  
91          Path dist = pathAssembler.getDistribution(configuration).getZipFile();
92          assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip"));
93          assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+"));
94          assertThat(dist.getParent().getParent(), equalTo(file(TEST_MAVEN_USER_HOME + "/somePath/maven-1.0")));
95      }
96  
97      @Test
98      void distZipWithProjectBase() throws Exception {
99          configuration.setZipBase(PathAssembler.PROJECT_STRING);
100         configuration.setDistribution(new URI("http://server/dist/maven-1.0.zip"));
101 
102         Path dist = pathAssembler.getDistribution(configuration).getZipFile();
103         assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip"));
104         assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+"));
105         assertThat(dist.getParent().getParent(), equalTo(file(currentDirPath() + "/somePath/maven-1.0")));
106     }
107 
108     @Test
109     void distZipWithLocalWindowsPath() throws Exception {
110         configuration.setZipBase(PathAssembler.PROJECT_STRING);
111         configuration.setDistribution(new URI("file:///C:/maven-1.0.zip"));
112 
113         Path dist = pathAssembler.getDistribution(configuration).getZipFile();
114         assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip"));
115         assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+"));
116         assertThat(dist.getParent().getParent(), equalTo(file(currentDirPath() + "/somePath/maven-1.0")));
117     }
118 
119     private Path file(String path) {
120         return Paths.get(path);
121     }
122 
123     private String currentDirPath() {
124         return System.getProperty("user.dir");
125     }
126 
127     public static <T extends CharSequence> Matcher<T> matchesRegexp(final String pattern) {
128         return new BaseMatcher<T>() {
129             @Override
130             public boolean matches(Object o) {
131                 return Pattern.compile(pattern).matcher((CharSequence) o).matches();
132             }
133 
134             @Override
135             public void describeTo(Description description) {
136                 description.appendText("a CharSequence that matches regexp ").appendValue(pattern);
137             }
138         };
139     }
140 }