1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 private Path file(String path) {
109 return Paths.get(path);
110 }
111
112 private String currentDirPath() {
113 return System.getProperty("user.dir");
114 }
115
116 public static <T extends CharSequence> Matcher<T> matchesRegexp(final String pattern) {
117 return new BaseMatcher<T>() {
118 @Override
119 public boolean matches(Object o) {
120 return Pattern.compile(pattern).matcher((CharSequence) o).matches();
121 }
122
123 @Override
124 public void describeTo(Description description) {
125 description.appendText("a CharSequence that matches regexp ").appendValue(pattern);
126 }
127 };
128 }
129 }