Debian Codegraph @

src/test/java/org/jboss/modules/maven/MavenSettingsTest.java

raw
1 package org.jboss.modules.maven;
2
3 import java.io.File;
4 import java.lang.reflect.Field;
5 import java.net.InetSocketAddress;
6 import java.net.Proxy;
7 import java.net.URL;
8 import java.nio.file.Path;
9 import java.nio.file.Paths;
10 import java.util.List;
11
12 import org.jboss.modules.Module;
13 import org.junit.Assert;
14 import org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.rules.TemporaryFolder;
17
18 /**
19 * @author Tomaz Cerar (c) 2015 Red Hat Inc.
20 */
21 public class MavenSettingsTest {
22
23 void clearCachedSettings() throws Exception {
24 Field mavenSettings = MavenSettings.class.getDeclaredField("mavenSettings");
25 mavenSettings.setAccessible(true);
26 mavenSettings.set(null, null);
27 }
28
29 @Rule
30 public TemporaryFolder tmpdir = new TemporaryFolder();
31
32 @Test
33 public void testLocalRepositoryOverriddenViaSystemProperty() throws Exception {
34 URL settingsXmlUrl = MavenSettingsTest.class.getResource("settings-with-local-repo-defined.xml");
35 System.setProperty("jboss.modules.settings.xml.url", settingsXmlUrl.toExternalForm());
36 String temporaryLocalRepository = tmpdir.newFolder("repository").getAbsolutePath();
37 System.setProperty("maven.repo.local", temporaryLocalRepository);
38
39 try {
40 clearCachedSettings();
41 MavenSettings settings = MavenSettings.getSettings();
42 Assert.assertEquals(temporaryLocalRepository, settings.getLocalRepository().toString());
43 } finally {
44 System.clearProperty("maven.repo.local");
45 System.clearProperty("jboss.modules.settings.xml.url");
46 }
47 }
48
49 @Test
50 public void testLocalRepositoryNotOverriddenViaSystemProperty() throws Exception {
51 URL settingsXmlUrl = MavenSettingsTest.class.getResource("settings-with-local-repo-defined.xml");
52 System.setProperty("jboss.modules.settings.xml.url", settingsXmlUrl.toExternalForm());
53
54 try {
55 clearCachedSettings();
56 MavenSettings settings = MavenSettings.getSettings();
57 Assert.assertEquals("/user/defined/path/in/settings/xml".replace('/', File.separatorChar), settings.getLocalRepository().toString());
58 } finally {
59 System.clearProperty("jboss.modules.settings.xml.url");
60 }
61 }
62
63 @Test
64 public void testWithPassedRepository() throws Exception {
65 System.setProperty("maven.repo.local", tmpdir.newFolder("repository").getAbsolutePath());
66 System.setProperty("remote.maven.repo", "http://repository.jboss.org/nexus/content/groups/public/,https://maven-central.storage.googleapis.com/");
67
68 try {
69 clearCachedSettings();
70 MavenSettings settings = MavenSettings.getSettings();
71 List<String> remoteRepos = settings.getRemoteRepositories();
72 Assert.assertTrue(remoteRepos.size() >= 3); //at least 3 must be present, other can come from settings.xml
73 Assert.assertTrue(remoteRepos.contains("https://repo1.maven.org/maven2/"));
74 Assert.assertTrue(remoteRepos.contains("http://repository.jboss.org/nexus/content/groups/public/"));
75 Assert.assertTrue(remoteRepos.contains("https://maven-central.storage.googleapis.com/"));
76
77 } finally {
78 System.clearProperty("maven.repo.local");
79 System.clearProperty("remote.repository");
80 }
81 }
82
83 @Test
84 public void testWithEmptyPassedRepository() throws Exception {
85 Path userRepo = tmpdir.newFolder(".m2", "repository").toPath();
86 String userHome = System.getProperty("user.home");
87 System.setProperty("user.home", tmpdir.getRoot().getAbsolutePath());
88 System.setProperty("maven.repo.local", "");
89
90 try {
91 clearCachedSettings();
92 MavenSettings settings = MavenSettings.getSettings();
93 Assert.assertEquals(userRepo, settings.getLocalRepository());
94 } finally {
95 System.setProperty("user.home", userHome);
96 System.clearProperty("maven.repo.local");
97 }
98 }
99
100 @Test
101 public void testEmptyLocalRepo() throws Exception {
102 MavenSettings settings = new MavenSettings();
103
104 MavenSettings.parseSettingsXml(Paths.get(MavenSettingsTest.class.getResource("settings-empty-local-repo.xml").toURI()), settings);
105 Assert.assertNull(settings.getLocalRepository());//local repo shouldn't be set
106
107 }
108
109 @Test
110 public void testInterpolatedLocalRepo() throws Exception {
111 Path userRepo = tmpdir.newFolder(".m2", "repository").toPath();
112 String userHome = System.getProperty("user.home");
113 System.setProperty("user.home", tmpdir.getRoot().getAbsolutePath());
114
115 try {
116 clearCachedSettings();
117 MavenSettings settings = new MavenSettings();
118
119 MavenSettings.parseSettingsXml(Paths.get(MavenSettingsTest.class.getResource("settings-interpolated-local-repo.xml").toURI()), settings);
120 Assert.assertEquals(Paths.get(tmpdir.getRoot().getAbsolutePath() + "/.mvnrepository"), settings.getLocalRepository());
121 } finally {
122 System.setProperty("user.home", userHome);
123 }
124 }
125
126
127 @Test
128 public void testProxies() throws Exception {
129 MavenSettings settings = new MavenSettings();
130
131 MavenSettings.parseSettingsXml(Paths.get(MavenSettingsTest.class.getResource("settings-empty-local-repo.xml").toURI()), settings);
132 List<MavenSettings.Proxy> proxies = settings.getProxies();
133 Assert.assertEquals(1, proxies.size());
134
135 MavenSettings.Proxy proxy = proxies.get(0);
136
137 Assert.assertEquals("my-proxy", proxy.getId());
138 Assert.assertEquals("myproxy.corp.com", proxy.getHost());
139 Assert.assertEquals(8080, proxy.getPort());
140 Assert.assertEquals("http", proxy.getProtocol());
141 Assert.assertEquals("bob", proxy.getUsername());
142 Assert.assertEquals("hunter2", proxy.getPassword());
143
144 Assert.assertTrue(proxy.canProxyFor(new URL("http://www.redhat.com/")));
145 Assert.assertFalse(proxy.canProxyFor(new URL("http://genius.apple.com/")));
146
147 Proxy netProxy = proxy.getProxy();
148
149 Assert.assertNotNull(netProxy);
150
151 Assert.assertEquals("myproxy.corp.com", ((InetSocketAddress) netProxy.address()).getHostName());
152 Assert.assertEquals(8080, ((InetSocketAddress) netProxy.address()).getPort());
153 }
154
155 @Test
156 public void testProxySelection() throws Exception {
157 MavenSettings settings = new MavenSettings();
158
159 MavenSettings.parseSettingsXml(Paths.get(MavenSettingsTest.class.getResource("settings-empty-local-repo.xml").toURI()), settings);
160 List<MavenSettings.Proxy> proxies = settings.getProxies();
161 Assert.assertEquals(1, proxies.size());
162
163 MavenSettings.Proxy proxy = settings.getProxyFor(new URL("http://genius.apple.com/foo/bar/baz"));
164 Assert.assertNull(proxy);
165
166 proxy = settings.getProxyFor(new URL("http://repository.jboss.org/foo/bar/baz"));
167 Assert.assertNotNull(proxy);
168
169 Assert.assertEquals("myproxy.corp.com", ((InetSocketAddress) proxy.getProxy().address()).getHostName());
170 Assert.assertEquals(8080, ((InetSocketAddress) proxy.getProxy().address()).getPort());
171 }
172
173 /**
174 * testing is snapshot resolving works properly, as in case of snapshot version, we need to use different path than exact version.
175 *
176 * @throws Exception
177 */
178 @Test
179 public void testSnapshotResolving() throws Exception {
180 ArtifactCoordinates coordinates = ArtifactCoordinates.fromString("org.wildfly.core:wildfly-version:2.0.5.Final-20151222.144931-1");
181 String path = coordinates.relativeArtifactPath('/');
182 Assert.assertEquals("org/wildfly/core/wildfly-version/2.0.5.Final-SNAPSHOT/wildfly-version-2.0.5.Final-20151222.144931-1", path);
183 }
184
185 @Test
186 public void testInterpolateVariablesOneVariable() throws Exception {
187 try {
188 System.setProperty( "test.user.home", "/home/bob" );
189 Assert.assertEquals("/home/bob/.m2/repository", MavenSettings.interpolateVariables("${test.user.home}/.m2/repository"));
190 } finally {
191 System.clearProperty("test.user.home");
192 }
193 }
194
195 @Test
196 public void testInterpolateVariablesTwoVariables() throws Exception {
197 try {
198 System.setProperty( "test.user.home", "/home/bob" );
199 System.setProperty( "test.repo.dir", "repository" );
200 Assert.assertEquals("/home/bob/.m2/repository", MavenSettings.interpolateVariables("${test.user.home}/.m2/${test.repo.dir}"));
201 } finally {
202 System.clearProperty("test.user.home");
203 }
204 }
205
206 @Test
207 public void testInterpolateVariablesInvalidExpression() throws Exception {
208 try {
209 System.setProperty( "test.user.home", "/home/bob" );
210 Assert.assertEquals("${test.user.home/.m2/repository", MavenSettings.interpolateVariables("${test.user.home/.m2/repository"));
211 } finally {
212 System.clearProperty("test.user.home");
213 }
214 }
215
216 @Test
217 public void testInterpolateVariablesInvalidExpression2() throws Exception {
218 try {
219 System.setProperty( "test.user.home", "/home/bob" );
220 Assert.assertEquals("/home/bob/.m2/${repoName", MavenSettings.interpolateVariables("${test.user.home}/.m2/${repoName"));
221 } finally {
222 System.clearProperty("test.user.home");
223 }
224 }
225 }