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