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