-
Notifications
You must be signed in to change notification settings - Fork 36
/
ProjectInfoReportUtilsTest.java
270 lines (209 loc) · 9.78 KB
/
ProjectInfoReportUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.report.projectinfo;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.MavenProject;
import org.apache.maven.report.projectinfo.stubs.SettingsStub;
import org.apache.maven.settings.Settings;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.security.Constraint;
import org.mortbay.jetty.security.ConstraintMapping;
import org.mortbay.jetty.security.HashUserRealm;
import org.mortbay.jetty.security.SecurityHandler;
import org.mortbay.jetty.security.SslSocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import static org.apache.maven.report.projectinfo.ProjectInfoReportUtils.getArchiveServer;
/**
* @author <a href="mailto:vincent.siveton@crim.ca">Vincent Siveton</a>
* @version $Id$
*/
public class ProjectInfoReportUtilsTest extends AbstractMojoTestCase {
private static final int MAX_IDLE_TIME = 30000;
private int port = -1;
private Settings settingsStub;
private Server jettyServer;
protected void setUp() throws Exception {
super.setUp();
final List<org.apache.maven.settings.Server> servers = new ArrayList<>();
org.apache.maven.settings.Server server = new org.apache.maven.settings.Server();
server.setId("localhost");
server.setUsername("admin");
server.setPassword("admin");
servers.add(server);
settingsStub = new SettingsStub() {
private static final long serialVersionUID = 1L;
@Override
public org.apache.maven.settings.Server getServer(String serverId) {
for (org.apache.maven.settings.Server server : getServers()) {
if (server.getId().equals(serverId)) {
return server;
}
}
return null;
}
@Override
public List<org.apache.maven.settings.Server> getServers() {
return servers;
}
};
}
private MavenProject getMavenProjectStub(boolean https) {
final DistributionManagement distributionManagement = new DistributionManagement();
DeploymentRepository repository = new DeploymentRepository();
repository.setId("localhost");
repository.setUrl((https ? "https" : "http") + "://localhost:" + port);
distributionManagement.setRepository(repository);
distributionManagement.setSnapshotRepository(repository);
return new MavenProjectStub() {
@Override
public DistributionManagement getDistributionManagement() {
return distributionManagement;
}
};
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testGetInputStreamURL() throws Exception {
assertTrue(ProjectInfoReportUtils.isArtifactUrlValid("http://my.intern.domain:8080/test"));
// file
URL url = new File(getBasedir(), "/target/classes/project-info-reports.properties")
.toURI()
.toURL();
String content = ProjectInfoReportUtils.getContent(url, getMavenProjectStub(false), settingsStub, "ISO-8859-1");
assertNotNull(content);
assertTrue(content.contains("Licensed to the Apache Software Foundation"));
// file
url = new File(getBasedir(), "/src/test/resources/iso-8859-5-encoded.txt")
.toURI()
.toURL();
content = ProjectInfoReportUtils.getContent(url, getMavenProjectStub(false), settingsStub, "ISO-8859-5");
assertNotNull(content);
assertTrue(content.contains("Свобода всем народам!"));
// http + no auth
startJetty(false, false);
url = new URL("http://localhost:" + port + "/project-info-reports.properties");
content = ProjectInfoReportUtils.getContent(url, getMavenProjectStub(false), settingsStub, "ISO-8859-1");
assertNotNull(content);
assertTrue(content.contains("Licensed to the Apache Software Foundation"));
stopJetty();
// http + auth
startJetty(false, true);
url = new URL("http://localhost:" + port + "/project-info-reports.properties");
content = ProjectInfoReportUtils.getContent(url, getMavenProjectStub(false), settingsStub, "ISO-8859-1");
assertNotNull(content);
assertTrue(content.contains("Licensed to the Apache Software Foundation"));
stopJetty();
// https + no auth
startJetty(true, false);
url = new URL("https://localhost:" + port + "/project-info-reports.properties");
content = ProjectInfoReportUtils.getContent(url, getMavenProjectStub(true), settingsStub, "ISO-8859-1");
assertNotNull(content);
assertTrue(content.contains("Licensed to the Apache Software Foundation"));
stopJetty();
// https + auth
startJetty(true, true);
url = new URL("https://localhost:" + port + "/project-info-reports.properties");
content = ProjectInfoReportUtils.getContent(url, getMavenProjectStub(true), settingsStub, "ISO-8859-1");
assertNotNull(content);
assertTrue(content.contains("Licensed to the Apache Software Foundation"));
stopJetty();
// TODO need to test with a proxy
}
public void testGetArchiveServer() {
assertEquals("???UNKNOWN???", getArchiveServer(null));
assertNull(getArchiveServer(""));
assertEquals(
"mail-archives.apache.org",
getArchiveServer("http://mail-archives.apache.org/mod_mbox/maven-announce/"));
assertEquals(
"mail-archives.apache.org",
getArchiveServer("https://mail-archives.apache.org/mod_mbox/maven-announce/"));
assertEquals(
"mail-archives.apache.org",
getArchiveServer("http://mail-archives.apache.org/mod_mbox/maven-announce"));
assertEquals("www.mail-archive.com", getArchiveServer("http://www.mail-archive.com/announce@maven.apache.org"));
assertEquals("www.nabble.com", getArchiveServer("http://www.nabble.com/Maven-Announcements-f15617.html"));
assertEquals("maven.announce.markmail.org", getArchiveServer("http://maven.announce.markmail.org/"));
assertEquals("maven.announce.markmail.org", getArchiveServer("http://maven.announce.markmail.org"));
}
private void startJetty(boolean isSSL, boolean withAuth) throws Exception {
jettyServer = new Server();
jettyServer.setStopAtShutdown(true);
Connector connector = (isSSL ? getSSLConnector() : getDefaultConnector());
jettyServer.setConnectors(new Connector[] {connector});
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setResourceBase(getBasedir() + "/target/classes/");
webapp.setServer(jettyServer);
if (withAuth) {
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[] {"user", "admin"});
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
SecurityHandler sh = new SecurityHandler();
sh.setUserRealm(new HashUserRealm("MyRealm", getBasedir() + "/src/test/resources/realm.properties"));
sh.setConstraintMappings(new ConstraintMapping[] {cm});
webapp.addHandler(sh);
}
DefaultHandler defaultHandler = new DefaultHandler();
defaultHandler.setServer(jettyServer);
Handler[] handlers = new Handler[2];
handlers[0] = webapp;
handlers[1] = defaultHandler;
jettyServer.setHandlers(handlers);
jettyServer.start();
port = connector.getLocalPort();
}
private void stopJetty() throws Exception {
if (jettyServer != null) {
jettyServer.stop();
jettyServer = null;
port = -1;
}
}
private Connector getDefaultConnector() {
Connector connector = new SelectChannelConnector();
connector.setMaxIdleTime(MAX_IDLE_TIME);
return connector;
}
private Connector getSSLConnector() {
SslSocketConnector connector = new SslSocketConnector();
connector.setKeystore(getBasedir() + "/target/jetty.jks");
connector.setPassword("apache");
connector.setKeyPassword("apache");
connector.setTruststore(getBasedir() + "/target/jetty.jks");
connector.setTrustPassword("apache");
return connector;
}
}