1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.javadoc;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.net.SocketTimeoutException;
30 import java.net.URI;
31 import java.net.URL;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Set;
43 import java.util.regex.PatternSyntaxException;
44
45 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
46 import org.apache.maven.plugins.javadoc.ProxyServer.AuthAsyncProxyServlet;
47 import org.apache.maven.settings.Proxy;
48 import org.apache.maven.settings.Settings;
49 import org.codehaus.plexus.PlexusTestCase;
50 import org.codehaus.plexus.util.FileUtils;
51 import org.eclipse.jetty.server.Request;
52 import org.eclipse.jetty.server.Server;
53 import org.eclipse.jetty.server.ServerConnector;
54 import org.eclipse.jetty.server.handler.AbstractHandler;
55 import org.eclipse.jetty.server.handler.MovedContextHandler;
56 import org.eclipse.jetty.util.ByteArrayISO8859Writer;
57
58 import static org.assertj.core.api.Assertions.assertThat;
59
60
61
62
63 public class JavadocUtilTest extends PlexusTestCase {
64
65
66
67
68 public void testParseJavadocVersion() {
69 String version = null;
70 try {
71 JavadocUtil.extractJavadocVersion(version);
72 fail("Not catch null");
73 } catch (IllegalArgumentException e) {
74 assertTrue(true);
75 }
76
77
78 version = "java full version \"1.4.2_12-b03\"";
79 assertEquals("1.4.2", JavadocUtil.extractJavadocVersion(version));
80
81
82 version = "java full version \"1.5.0_07-164\"";
83 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
84
85
86 version = "java full version \"J2RE 1.4.2 IBM Windows 32 build cn1420-20040626\"";
87 assertEquals("1.4.2", JavadocUtil.extractJavadocVersion(version));
88
89
90 version = "javadoc version complète de \"J2RE 1.5.0 IBM Windows 32 build pwi32pdev-20070426a\"";
91 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
92
93
94 version =
95 "J2RE 1.5.0 IBM Windows 32 build pwi32devifx-20070323 (ifix 117674: SR4 + 116644 + 114941 + 116110 + 114881)";
96 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
97
98
99 version = "java full version \"diablo-1.5.0-b01\"";
100 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
101
102
103 version = "java full version \"1.5.0_11-b03\"";
104 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
105
106
107 version = "java full version \"1.5.0_07-164\"" + System.getProperty("line.separator");
108 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
109
110 version = System.getProperty("line.separator") + "java full version \"1.5.0_07-164\"";
111 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
112
113 version = System.getProperty("line.separator") + "java full version \"1.5.0_07-164\""
114 + System.getProperty("line.separator");
115 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
116
117 version = "java full" + System.getProperty("line.separator") + " version \"1.5.0_07-164\"";
118 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
119
120 version = "java full version \"1.99.123-b01\"";
121 assertEquals("1.99.123", JavadocUtil.extractJavadocVersion(version));
122
123 version = "java full version \"1.5.0.07-164\"";
124 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
125
126 version = "java full version \"1.4\"";
127 assertEquals("1.4", JavadocUtil.extractJavadocVersion(version));
128
129 version = "java full version \"1.A.B_07-164\"";
130 try {
131 JavadocUtil.extractJavadocVersion(version);
132
133
134 } catch (PatternSyntaxException e) {
135 assertTrue(true);
136 }
137
138 version = "SCO-UNIX-J2SE-1.5.0_09*FCS-UW714-OSR6*_20061114";
139 assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
140
141
142 version = "java full version \"9-ea+113\"";
143 assertEquals("9", JavadocUtil.extractJavadocVersion(version));
144
145
146 version = "java full version \"9-ea+113-2016-04-14-161743.javare.4852.nc\"";
147 assertEquals("9", JavadocUtil.extractJavadocVersion(version));
148
149 version = "java version \"9-ea\"";
150 assertEquals("9", JavadocUtil.extractJavadocVersion(version));
151
152
153 version = "java full version \"9+100\"";
154 assertEquals("9", JavadocUtil.extractJavadocVersion(version));
155
156 version = "java full version \"9.0.1+20\"";
157 assertEquals("9.0.1", JavadocUtil.extractJavadocVersion(version));
158
159 version = "java full version \"10+100\"";
160 assertEquals("10", JavadocUtil.extractJavadocVersion(version));
161
162 version = "java full version \"10.0.1+20\"";
163 assertEquals("10.0.1", JavadocUtil.extractJavadocVersion(version));
164 }
165
166
167
168
169
170 public void testParseJavadocMemory() {
171 String memory = null;
172 try {
173 JavadocUtil.parseJavadocMemory(memory);
174 fail("Not catch null");
175 } catch (IllegalArgumentException e) {
176 assertTrue(true);
177 }
178
179 memory = "128";
180 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
181
182 memory = "128k";
183 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128k");
184 memory = "128kb";
185 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128k");
186
187 memory = "128m";
188 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
189 memory = "128mb";
190 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
191
192 memory = "1g";
193 assertEquals(JavadocUtil.parseJavadocMemory(memory), "1024m");
194 memory = "1gb";
195 assertEquals(JavadocUtil.parseJavadocMemory(memory), "1024m");
196
197 memory = "1t";
198 assertEquals(JavadocUtil.parseJavadocMemory(memory), "1048576m");
199 memory = "1tb";
200 assertEquals(JavadocUtil.parseJavadocMemory(memory), "1048576m");
201
202 memory = System.getProperty("line.separator") + "128m";
203 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
204 memory = System.getProperty("line.separator") + "128m" + System.getProperty("line.separator");
205 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
206
207 memory = " 128m";
208 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
209 memory = " 128m ";
210 assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
211
212 memory = "1m28m";
213 try {
214 JavadocUtil.parseJavadocMemory(memory);
215 fail("Not catch wrong pattern");
216 } catch (IllegalArgumentException e) {
217 assertTrue(true);
218 }
219 memory = "ABC128m";
220 try {
221 JavadocUtil.parseJavadocMemory(memory);
222 fail("Not catch wrong pattern");
223 } catch (IllegalArgumentException e) {
224 assertTrue(true);
225 }
226 }
227
228
229
230
231
232 public void testValidateEncoding() {
233 assertFalse("Not catch null", JavadocUtil.validateEncoding(null));
234 assertTrue("UTF-8 not supported on this plateform", JavadocUtil.validateEncoding("UTF-8"));
235 assertTrue("ISO-8859-1 not supported on this plateform", JavadocUtil.validateEncoding("ISO-8859-1"));
236 assertFalse("latin is supported on this plateform???", JavadocUtil.validateEncoding("latin"));
237 assertFalse("WRONG is supported on this plateform???", JavadocUtil.validateEncoding("WRONG"));
238 }
239
240
241
242
243
244
245 public void testIsValidPackageList() throws Exception {
246 Settings settings = null;
247 Proxy proxy;
248
249 URL url = null;
250 URL wrongUrl;
251 try {
252 JavadocUtil.isValidPackageList(url, settings, false);
253 fail();
254 } catch (IllegalArgumentException e) {
255 assertTrue(true);
256 }
257
258 url = new File(getBasedir(), "/pom.xml").toURI().toURL();
259 assertTrue(JavadocUtil.isValidPackageList(url, settings, false));
260
261 try {
262 assertFalse(JavadocUtil.isValidPackageList(url, settings, true));
263 } catch (IOException e) {
264 assertTrue(true);
265 }
266
267 url = this.getClass()
268 .getResource("/JavadocUtilTest-package-list.txt")
269 .toURI()
270 .toURL();
271 assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
272
273 url = new URL("http://maven.apache.org/plugins-archives/maven-javadoc-plugin-3.5.0/apidocs/package-list");
274 assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
275
276 wrongUrl = new URL("http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list2");
277 try {
278 JavadocUtil.isValidPackageList(wrongUrl, settings, false);
279 fail();
280 } catch (IOException e) {
281 assertTrue(true);
282 }
283
284
285 ProxyServer proxyServer = null;
286 AuthAsyncProxyServlet proxyServlet;
287 try {
288 proxyServlet = new AuthAsyncProxyServlet();
289 proxyServer = new ProxyServer(proxyServlet);
290 proxyServer.start();
291
292 settings = new Settings();
293
294 assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
295
296 try {
297 JavadocUtil.isValidPackageList(wrongUrl, settings, false);
298 fail();
299 } catch (IOException e) {
300 assertTrue(true);
301 }
302 } finally {
303 if (proxyServer != null) {
304 proxyServer.stop();
305 }
306 }
307
308 Map<String, String> authentications = new HashMap<>();
309 authentications.put("foo", "bar");
310
311 try {
312 proxyServlet = new AuthAsyncProxyServlet(authentications);
313 proxyServer = new ProxyServer(proxyServlet);
314 proxyServer.start();
315
316 settings = new Settings();
317 proxy = new Proxy();
318 proxy.setActive(true);
319 proxy.setHost(proxyServer.getHostName());
320 proxy.setPort(proxyServer.getPort());
321 proxy.setProtocol("http");
322 settings.addProxy(proxy);
323
324 JavadocUtil.isValidPackageList(url, settings, false);
325 fail();
326 } catch (FileNotFoundException e) {
327 assertTrue(true);
328 } finally {
329 proxyServer.stop();
330 }
331
332
333 try {
334 proxyServlet = new AuthAsyncProxyServlet(authentications);
335 proxyServer = new ProxyServer(proxyServlet);
336 proxyServer.start();
337
338 settings = new Settings();
339 proxy = new Proxy();
340 proxy.setActive(true);
341 proxy.setHost(proxyServer.getHostName());
342 proxy.setPort(proxyServer.getPort());
343 proxy.setProtocol("http");
344 proxy.setUsername("foo");
345 proxy.setPassword("bar");
346 settings.addProxy(proxy);
347
348 assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
349
350 try {
351 JavadocUtil.isValidPackageList(wrongUrl, settings, false);
352 fail();
353 } catch (IOException e) {
354 assertTrue(true);
355 }
356 } finally {
357 proxyServer.stop();
358 }
359
360
361 try {
362 proxyServlet = new AuthAsyncProxyServlet(authentications, 3000);
363 proxyServer = new ProxyServer(proxyServlet);
364 proxyServer.start();
365
366 settings = new Settings();
367 proxy = new Proxy();
368 proxy.setActive(true);
369 proxy.setHost(proxyServer.getHostName());
370 proxy.setPort(proxyServer.getPort());
371 proxy.setProtocol("http");
372 proxy.setUsername("foo");
373 proxy.setPassword("bar");
374 settings.addProxy(proxy);
375
376 JavadocUtil.isValidPackageList(url, settings, true);
377 fail();
378 } catch (SocketTimeoutException e) {
379 assertTrue(true);
380 } finally {
381 proxyServer.stop();
382 }
383
384
385 try {
386 proxyServlet = new AuthAsyncProxyServlet(authentications);
387 proxyServer = new ProxyServer(proxyServlet);
388 proxyServer.start();
389
390 settings = new Settings();
391 proxy = new Proxy();
392 proxy.setActive(true);
393 proxy.setHost(proxyServer.getHostName());
394 proxy.setPort(proxyServer.getPort());
395 proxy.setProtocol("http");
396 proxy.setUsername("foo");
397 proxy.setPassword("bar");
398 proxy.setNonProxyHosts("maven.apache.org");
399 settings.addProxy(proxy);
400
401 assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
402 } finally {
403 proxyServer.stop();
404 }
405 }
406
407 public void testGetRedirectUrlNotHttp() throws Exception {
408 URL url = new URI("ftp://some.where").toURL();
409 assertEquals(
410 url.toString(), JavadocUtil.getRedirectUrl(url, new Settings()).toString());
411
412 url = new URI("file://some/where").toURL();
413 assertEquals(
414 url.toString(), JavadocUtil.getRedirectUrl(url, new Settings()).toString());
415 }
416
417
418
419
420 public void testGetRedirectUrl() throws Exception {
421 Server server = null, redirectServer = null;
422 try {
423 redirectServer = new Server(0);
424 redirectServer.setHandler(new AbstractHandler() {
425 @Override
426 public void handle(
427 String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
428 throws IOException, ServletException {
429 response.setStatus(HttpServletResponse.SC_OK);
430 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(100);
431 writer.write("<html>Hello world</html>");
432 writer.flush();
433 response.setContentLength(writer.size());
434 OutputStream out = response.getOutputStream();
435 writer.writeTo(out);
436 out.close();
437 writer.close();
438 }
439 });
440 redirectServer.start();
441
442 server = new Server(0);
443 MovedContextHandler handler = new MovedContextHandler();
444 int redirectPort = ((ServerConnector) redirectServer.getConnectors()[0]).getLocalPort();
445 handler.setNewContextURL("http://localhost:" + redirectPort);
446 server.setHandler(handler);
447 server.start();
448
449 URL url = new URI("http://localhost:"
450 + ((ServerConnector) redirectServer.getConnectors()[0]).getLocalPort())
451 .toURL();
452 URL redirectUrl = JavadocUtil.getRedirectUrl(url, new Settings());
453
454 assertTrue(redirectUrl.toString().startsWith("http://localhost:" + redirectPort));
455 } finally {
456 stopSilently(server);
457 stopSilently(redirectServer);
458 }
459 }
460
461
462
463
464 public void testGetRedirectUrlWithNoRedirects() throws Exception {
465 Server server = null;
466 try {
467 server = new Server(0);
468 server.setHandler(new AbstractHandler() {
469 @Override
470 public void handle(
471 String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
472 throws IOException, ServletException {
473 response.setStatus(HttpServletResponse.SC_OK);
474 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(100);
475 writer.write("<html>Hello world</html>");
476 writer.flush();
477 response.setContentLength(writer.size());
478 OutputStream out = response.getOutputStream();
479 writer.writeTo(out);
480 out.close();
481 writer.close();
482 }
483 });
484 server.start();
485
486 URL url =
487 new URI("http://localhost:" + ((ServerConnector) server.getConnectors()[0]).getLocalPort()).toURL();
488 URL redirectUrl = JavadocUtil.getRedirectUrl(url, new Settings());
489
490 assertEquals(url.toURI(), redirectUrl.toURI());
491 } finally {
492 stopSilently(server);
493 }
494 }
495
496
497
498
499
500 public void testGetRedirectUrlVerifyHeaders() throws Exception {
501 Server server = null;
502 try {
503 server = new Server(0);
504 server.setHandler(new AbstractHandler() {
505 @Override
506 public void handle(
507 String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
508 throws IOException, ServletException {
509
510 if (request.getHeader("Accept") == null) {
511 response.setStatus(HttpServletResponse.SC_FORBIDDEN);
512 } else {
513 response.setStatus(HttpServletResponse.SC_OK);
514 }
515 response.getOutputStream().close();
516 }
517 });
518 server.start();
519
520 URL url =
521 new URI("http://localhost:" + ((ServerConnector) server.getConnectors()[0]).getLocalPort()).toURL();
522 JavadocUtil.getRedirectUrl(url, new Settings());
523 } finally {
524 stopSilently(server);
525 }
526 }
527
528
529
530
531
532
533 public void testCopyJavadocResources() throws Exception {
534 File input = new File(getBasedir(), "src/test/resources/unit/docfiles-test/docfiles/");
535 assertThat(input).exists();
536
537 File output = new File(getBasedir(), "target/test/unit/docfiles-test/target/output");
538 if (output.exists()) {
539 FileUtils.deleteDirectory(output);
540 }
541 assertTrue(output.mkdirs());
542
543 JavadocUtil.copyJavadocResources(output, input, null);
544
545 assertThat(FileUtils.getFiles(output, null, null, false))
546 .containsExactlyInAnyOrder(
547 Paths.get("test", "doc-files", "excluded-dir1", "sample-excluded1.gif")
548 .toFile(),
549 Paths.get("test", "doc-files", "excluded-dir2", "sample-excluded2.gif")
550 .toFile(),
551 Paths.get("test", "doc-files", "included-dir1", "sample-included1.gif")
552 .toFile(),
553 Paths.get("test", "doc-files", "included-dir2", "sample-included2.gif")
554 .toFile());
555
556 assertThat(FileUtils.getDirectoryNames(new File(output, "test/doc-files"), null, null, false))
557 .containsExactlyInAnyOrder("", "excluded-dir1", "excluded-dir2", "included-dir1", "included-dir2");
558
559 input = new File(getBasedir(), "src/test/resources/unit/docfiles-test/docfiles/");
560 assertTrue(input.exists());
561
562 output = new File(getBasedir(), "target/test/unit/docfiles-test/target/output");
563 if (output.exists()) {
564 FileUtils.deleteDirectory(output);
565 }
566 assertTrue(output.mkdirs());
567
568 JavadocUtil.copyJavadocResources(output, input, "excluded-dir1:excluded-dir2");
569
570 assertThat(FileUtils.getFiles(output, null, null, false))
571 .containsExactlyInAnyOrder(
572 Paths.get("test", "doc-files", "included-dir1", "sample-included1.gif")
573 .toFile(),
574 Paths.get("test", "doc-files", "included-dir2", "sample-included2.gif")
575 .toFile());
576
577 assertThat(FileUtils.getDirectoryNames(new File(output, "test/doc-files"), null, null, false))
578 .containsExactlyInAnyOrder("", "included-dir1", "included-dir2");
579 }
580
581
582
583
584
585 public void testPruneDirs() {
586 List<String> list = new ArrayList<>();
587 String classesDir = getBasedir() + "/target/classes";
588 list.add(classesDir);
589 list.add(classesDir);
590 list.add(classesDir);
591
592 Set<Path> expected = Collections.singleton(Paths.get(classesDir));
593
594 MavenProjectStub project = new MavenProjectStub();
595 project.setFile(new File(getBasedir(), "pom.xml"));
596
597 assertEquals(expected, JavadocUtil.pruneDirs(project, list));
598 }
599
600
601
602
603
604 public void testPrunePaths() {
605 List<String> list = new ArrayList<>();
606 String classesDir = getBasedir() + "/target/classes";
607 String tagletJar = getBasedir()
608 + "/target/test-classes/unit/taglet-test/artifact-taglet/org/tullmann/taglets/1.0/taglets-1.0.jar";
609 list.add(classesDir);
610 list.add(classesDir);
611 list.add(classesDir);
612 list.add(tagletJar);
613 list.add(tagletJar);
614 list.add(tagletJar);
615
616 Set<Path> expectedNoJar = Collections.singleton(Paths.get(classesDir));
617 Set<Path> expectedWithJar = new HashSet<>(Arrays.asList(Paths.get(classesDir), Paths.get(tagletJar)));
618
619 MavenProjectStub project = new MavenProjectStub();
620 project.setFile(new File(getBasedir(), "pom.xml"));
621
622 assertEquals(expectedNoJar, JavadocUtil.prunePaths(project, list, false));
623 assertEquals(expectedWithJar, JavadocUtil.prunePaths(project, list, true));
624 }
625
626
627
628
629
630 public void testUnifyPathSeparator() {
631 assertNull(JavadocUtil.unifyPathSeparator(null));
632
633 final String ps = File.pathSeparator;
634
635
636 String path1 = "C:\\maven-javadoc-plugin\\src\\main\\java";
637 String path2 = "C:\\maven-javadoc-plugin\\src\\main\\javadoc";
638 assertEquals(path1 + ps + path2, JavadocUtil.unifyPathSeparator(path1 + ";" + path2));
639 assertEquals(path1 + ps + path2, JavadocUtil.unifyPathSeparator(path1 + ":" + path2));
640
641 path1 = "C:/maven-javadoc-plugin/src/main/java";
642 path2 = "C:/maven-javadoc-plugin/src/main/javadoc";
643 assertEquals(path1 + ps + path2, JavadocUtil.unifyPathSeparator(path1 + ";" + path2));
644 assertEquals(path1 + ps + path2, JavadocUtil.unifyPathSeparator(path1 + ":" + path2));
645 assertEquals(
646 path1 + ps + path2 + ps + path1 + ps + path2,
647 JavadocUtil.unifyPathSeparator(path1 + ";" + path2 + ";" + path1 + ":" + path2));
648
649
650 path1 = "/tmp/maven-javadoc-plugin/src/main/java";
651 path2 = "/tmp/maven-javadoc-plugin/src/main/javadoc";
652 assertEquals(path1 + ps + path2, JavadocUtil.unifyPathSeparator(path1 + ";" + path2));
653 assertEquals(path1 + ps + path2, JavadocUtil.unifyPathSeparator(path1 + ":" + path2));
654 assertEquals(
655 path1 + ps + path2 + ps + path1 + ps + path2,
656 JavadocUtil.unifyPathSeparator(path1 + ";" + path2 + ":" + path1 + ":" + path2));
657 }
658
659 public void testGetIncludedFiles() {
660 File sourceDirectory = new File("target/it").getAbsoluteFile();
661 String[] fileList = new String[] {"Main.java"};
662 Collection<String> excludePackages = Collections.singleton("*.it");
663
664 List<String> includedFiles = JavadocUtil.getIncludedFiles(sourceDirectory, fileList, excludePackages);
665
666 assertThat(includedFiles.toArray(new String[0])).isEqualTo(fileList);
667 }
668
669 private void stopSilently(Server server) {
670 try {
671 if (server != null) {
672 server.stop();
673 }
674 } catch (Exception e) {
675
676 }
677 }
678 }