1 package org.apache.maven.plugin.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.net.SocketTimeoutException;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.regex.PatternSyntaxException;
32
33 import org.apache.commons.lang.builder.EqualsBuilder;
34 import org.apache.maven.plugin.javadoc.ProxyServer.AuthAsyncProxyServlet;
35 import org.apache.maven.settings.Proxy;
36 import org.apache.maven.settings.Settings;
37 import org.codehaus.plexus.PlexusTestCase;
38 import org.codehaus.plexus.logging.Logger;
39 import org.codehaus.plexus.util.FileUtils;
40
41
42
43
44
45 public class JavadocUtilTest
46 extends PlexusTestCase
47 {
48
49
50
51
52
53 public void testParseJavadocVersion()
54 throws Exception
55 {
56 String version = null;
57 try
58 {
59 JavadocUtil.parseJavadocVersion( version );
60 assertTrue( "Not catch null", false );
61 }
62 catch ( IllegalArgumentException e )
63 {
64 assertTrue( true );
65 }
66
67
68 version = "java full version \"1.4.2_12-b03\"";
69 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.42f, 0 );
70
71
72 version = "java full version \"1.5.0_07-164\"";
73 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
74
75
76 version = "java full version \"J2RE 1.4.2 IBM Windows 32 build cn1420-20040626\"";
77 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.42f, 0 );
78
79
80 version = "javadoc version compl�te de \"J2RE 1.5.0 IBM Windows 32 build pwi32pdev-20070426a\"";
81 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
82
83
84 version = "J2RE 1.5.0 IBM Windows 32 build pwi32devifx-20070323 (ifix 117674: SR4 + 116644 + 114941 + 116110 + 114881)";
85 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
86
87
88 version = "java full version \"diablo-1.5.0-b01\"";
89 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
90
91
92 version = "java full version \"1.5.0_11-b03\"";
93 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
94
95
96 version = "java full version \"1.5.0_07-164\"" + System.getProperty( "line.separator" );
97 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
98 version = System.getProperty( "line.separator" ) + "java full version \"1.5.0_07-164\"";
99 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
100 version = System.getProperty( "line.separator" ) + "java full version \"1.5.0_07-164\""
101 + System.getProperty( "line.separator" );
102 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
103 version = "java full" + System.getProperty( "line.separator" ) + " version \"1.5.0_07-164\"";
104 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
105
106 version = "java full version \"1.99.123-b01\"";
107 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.99123f, 0 );
108
109 version = "java full version \"1.5.0.07-164\"";
110 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
111
112 version = "java full version \"1.4\"";
113 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.4f, 0 );
114
115 version = "java full version \"1.A.B_07-164\"";
116 try
117 {
118 JavadocUtil.parseJavadocVersion( version );
119 assertTrue( "Not catch wrong pattern", false );
120 }
121 catch ( PatternSyntaxException e )
122 {
123 assertTrue( true );
124 }
125
126 version = "SCO-UNIX-J2SE-1.5.0_09*FCS-UW714-OSR6*_20061114";
127 assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
128 }
129
130
131
132
133
134
135 public void testParseJavadocMemory()
136 throws Exception
137 {
138 String memory = null;
139 try
140 {
141 JavadocUtil.parseJavadocMemory( memory );
142 assertTrue( "Not catch null", false );
143 }
144 catch ( IllegalArgumentException e )
145 {
146 assertTrue( true );
147 }
148
149 memory = "128";
150 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128m" );
151
152 memory = "128k";
153 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128k" );
154 memory = "128kb";
155 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128k" );
156
157 memory = "128m";
158 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128m" );
159 memory = "128mb";
160 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128m" );
161
162 memory = "1g";
163 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "1024m" );
164 memory = "1gb";
165 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "1024m" );
166
167 memory = "1t";
168 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "1048576m" );
169 memory = "1tb";
170 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "1048576m" );
171
172 memory = System.getProperty( "line.separator" ) + "128m";
173 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128m" );
174 memory = System.getProperty( "line.separator" ) + "128m" + System.getProperty( "line.separator" );
175 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128m" );
176
177 memory = " 128m";
178 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128m" );
179 memory = " 128m ";
180 assertEquals( JavadocUtil.parseJavadocMemory( memory ), "128m" );
181
182 memory = "1m28m";
183 try
184 {
185 JavadocUtil.parseJavadocMemory( memory );
186 assertTrue( "Not catch wrong pattern", false );
187 }
188 catch ( IllegalArgumentException e )
189 {
190 assertTrue( true );
191 }
192 memory = "ABC128m";
193 try
194 {
195 JavadocUtil.parseJavadocMemory( memory );
196 assertTrue( "Not catch wrong pattern", false );
197 }
198 catch ( IllegalArgumentException e )
199 {
200 assertTrue( true );
201 }
202 }
203
204
205
206
207
208
209 public void testValidateEncoding()
210 throws Exception
211 {
212 assertFalse( "Not catch null", JavadocUtil.validateEncoding( null ) );
213 assertTrue( "UTF-8 not supported on this plateform", JavadocUtil.validateEncoding( "UTF-8" ) );
214 assertTrue( "ISO-8859-1 not supported on this plateform", JavadocUtil.validateEncoding( "ISO-8859-1" ) );
215 assertFalse( "latin is supported on this plateform???", JavadocUtil.validateEncoding( "latin" ) );
216 assertFalse( "WRONG is supported on this plateform???", JavadocUtil.validateEncoding( "WRONG" ) );
217 }
218
219
220
221
222
223
224 public void testHideProxyPassword()
225 throws Exception
226 {
227 String cmdLine = "javadoc.exe " + "-J-Dhttp.proxySet=true " + "-J-Dhttp.proxyHost=127.0.0.1 "
228 + "-J-Dhttp.proxyPort=80 " + "-J-Dhttp.nonProxyHosts=\"www.google.com|*.somewhere.com\" "
229 + "-J-Dhttp.proxyUser=\"toto\" " + "-J-Dhttp.proxyPassword=\"toto\" " + "@options @packages";
230 cmdLine = JavadocUtil.hideProxyPassword( cmdLine, null );
231 assertFalse( cmdLine.indexOf( "-J-Dhttp.proxyPassword=\"****\"" ) != -1 );
232
233 Settings settings = new Settings();
234 Proxy proxy = new Proxy();
235 proxy.setActive( true );
236 proxy.setHost( "127.0.0.1" );
237 proxy.setPort( 80 );
238 proxy.setProtocol( "http" );
239 proxy.setUsername( "toto" );
240 proxy.setPassword( "toto" );
241 proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
242 settings.addProxy( proxy );
243
244 cmdLine = "javadoc.exe " + "-J-Dhttp.proxySet=true " + "-J-Dhttp.proxyHost=127.0.0.1 "
245 + "-J-Dhttp.proxyPort=80 " + "-J-Dhttp.nonProxyHosts=\"www.google.com|*.somewhere.com\" "
246 + "-J-Dhttp.proxyUser=\"toto\" " + "-J-Dhttp.proxyPassword=\"toto\" " + "@options @packages";
247 cmdLine = JavadocUtil.hideProxyPassword( cmdLine, settings );
248 assertTrue( cmdLine.indexOf( "-J-Dhttp.proxyPassword=\"****\"" ) != -1 );
249
250 settings = new Settings();
251 proxy = new Proxy();
252 proxy.setActive( true );
253 proxy.setHost( "127.0.0.1" );
254 proxy.setPort( 80 );
255 proxy.setProtocol( "http" );
256 proxy.setUsername( "toto" );
257 proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
258 settings.addProxy( proxy );
259
260 cmdLine = "javadoc.exe " + "-J-Dhttp.proxySet=true " + "-J-Dhttp.proxyHost=127.0.0.1 "
261 + "-J-Dhttp.proxyPort=80 " + "-J-Dhttp.nonProxyHosts=\"www.google.com|*.somewhere.com\" "
262 + "-J-Dhttp.proxyUser=\"toto\" " + "-J-Dhttp.proxyPassword=\"toto\" " + "@options @packages";
263 cmdLine = JavadocUtil.hideProxyPassword( cmdLine, null );
264 assertFalse( cmdLine.indexOf( "-J-Dhttp.proxyPassword=\"****\"" ) != -1 );
265 }
266
267
268
269
270
271
272 public void testFetchURL()
273 throws Exception
274 {
275 Settings settings = null;
276 Proxy proxy = null;
277
278 URL url = null;
279 URL wrongUrl = null;
280 try
281 {
282 JavadocUtil.fetchURL( settings, url );
283 assertTrue( false );
284 }
285 catch ( IllegalArgumentException e )
286 {
287 assertTrue( true );
288 }
289
290 url = new File( getBasedir(), "/pom.xml" ).toURL();
291 JavadocUtil.fetchURL( settings, url );
292 assertTrue( true );
293
294 if ( !isWebSiteOnline( settings, getContainer().getLogger(), "http://maven.apache.org" ) )
295 {
296 getContainer().getLogger().warn(
297 "http://maven.apache.org should be on line to run '"
298 + getClass().getName() + "#" + getName() + "()'." );
299 return;
300 }
301
302 url = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list" );
303 JavadocUtil.fetchURL( settings, url );
304 assertTrue( true );
305
306 wrongUrl = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list2" );
307 try
308 {
309 JavadocUtil.fetchURL( settings, wrongUrl );
310 assertTrue( false );
311 }
312 catch ( IOException e )
313 {
314 assertTrue( true );
315 }
316
317
318 ProxyServer proxyServer = null;
319 AuthAsyncProxyServlet proxyServlet = null;
320 try
321 {
322 proxyServlet = new AuthAsyncProxyServlet();
323 proxyServer = new ProxyServer( proxyServlet );
324 proxyServer.start();
325
326 settings = new Settings();
327
328 JavadocUtil.fetchURL( settings, url );
329 assertTrue( true );
330
331 try
332 {
333 JavadocUtil.fetchURL( settings, wrongUrl );
334 assertTrue( false );
335 }
336 catch ( IOException e )
337 {
338 assertTrue( true );
339 }
340 }
341 catch ( Exception e )
342 {
343 assertTrue( false );
344 }
345 finally
346 {
347 if ( proxyServer != null )
348 {
349 proxyServer.stop();
350 }
351 }
352
353 Map authentications = new HashMap();
354 authentications.put( "foo", "bar" );
355
356 try
357 {
358 proxyServlet = new AuthAsyncProxyServlet( authentications );
359 proxyServer = new ProxyServer( proxyServlet );
360 proxyServer.start();
361
362 settings = new Settings();
363 proxy = new Proxy();
364 proxy.setActive( true );
365 proxy.setHost( proxyServer.getHostName() );
366 proxy.setPort( proxyServer.getPort() );
367 proxy.setProtocol( "http" );
368 settings.addProxy( proxy );
369
370 JavadocUtil.fetchURL( settings, url );
371 assertTrue( false );
372 }
373 catch ( FileNotFoundException e )
374 {
375 assertTrue( true );
376 }
377 catch ( Exception e )
378 {
379 assertTrue( false );
380 }
381 finally
382 {
383 if ( proxyServer != null )
384 {
385 proxyServer.stop();
386 }
387 }
388
389
390 try
391 {
392 proxyServlet = new AuthAsyncProxyServlet( authentications );
393 proxyServer = new ProxyServer( proxyServlet );
394 proxyServer.start();
395
396 settings = new Settings();
397 proxy = new Proxy();
398 proxy.setActive( true );
399 proxy.setHost( proxyServer.getHostName() );
400 proxy.setPort( proxyServer.getPort() );
401 proxy.setProtocol( "http" );
402 proxy.setUsername( "foo" );
403 proxy.setPassword( "bar" );
404 settings.addProxy( proxy );
405
406 JavadocUtil.fetchURL( settings, url );
407 assertTrue( true );
408
409 try
410 {
411 JavadocUtil.fetchURL( settings, wrongUrl );
412 assertTrue( false );
413 }
414 catch ( IOException e )
415 {
416 assertTrue( true );
417 }
418 }
419 catch ( FileNotFoundException e )
420 {
421 assertTrue( false );
422 }
423 finally
424 {
425 if ( proxyServer != null )
426 {
427 proxyServer.stop();
428 }
429 }
430
431
432 try
433 {
434 proxyServlet = new AuthAsyncProxyServlet( authentications, 3000 );
435 proxyServer = new ProxyServer( proxyServlet );
436 proxyServer.start();
437
438 settings = new Settings();
439 proxy = new Proxy();
440 proxy.setActive( true );
441 proxy.setHost( proxyServer.getHostName() );
442 proxy.setPort( proxyServer.getPort() );
443 proxy.setProtocol( "http" );
444 proxy.setUsername( "foo" );
445 proxy.setPassword( "bar" );
446 settings.addProxy( proxy );
447
448 JavadocUtil.fetchURL( settings, url );
449 assertTrue( false );
450 }
451 catch ( SocketTimeoutException e )
452 {
453 assertTrue( true );
454 }
455 finally
456 {
457 if ( proxyServer != null )
458 {
459 proxyServer.stop();
460 }
461 }
462
463
464 try
465 {
466 proxyServlet = new AuthAsyncProxyServlet( authentications );
467 proxyServer = new ProxyServer( proxyServlet );
468 proxyServer.start();
469
470 settings = new Settings();
471 proxy = new Proxy();
472 proxy.setActive( true );
473 proxy.setHost( proxyServer.getHostName() );
474 proxy.setPort( proxyServer.getPort() );
475 proxy.setProtocol( "http" );
476 proxy.setUsername( "foo" );
477 proxy.setPassword( "bar" );
478 proxy.setNonProxyHosts( "maven.apache.org" );
479 settings.addProxy( proxy );
480
481 url = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list" );
482
483 JavadocUtil.fetchURL( settings, url );
484 assertTrue( true );
485 }
486 finally
487 {
488 if ( proxyServer != null )
489 {
490 proxyServer.stop();
491 }
492 }
493 }
494
495
496
497
498
499
500 public void testCopyJavadocResources()
501 throws Exception
502 {
503 File input = new File( getBasedir(), "src/test/resources/unit/docfiles-test/docfiles/" );
504 assertTrue( input.exists() );
505
506 File output = new File( getBasedir(), "target/test/unit/docfiles-test/target/output" );
507 if ( output.exists() )
508 {
509 FileUtils.deleteDirectory( output );
510 }
511 assertTrue( output.mkdirs() );
512
513 JavadocUtil.copyJavadocResources( output, input, null );
514 List expected = new ArrayList();
515 expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir1" + File.separator
516 + "sample-excluded1.gif" );
517 expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir2" + File.separator
518 + "sample-excluded2.gif" );
519 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" + File.separator
520 + "sample-included1.gif" );
521 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" + File.separator
522 + "sample-included2.gif" );
523 assertTrue( EqualsBuilder.reflectionEquals( expected, FileUtils.getFiles( output, null, null, false ) ) );
524 expected = new ArrayList();
525 expected.add( "" );
526 expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir1" );
527 expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir1" );
528 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" );
529 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" );
530 assertTrue( EqualsBuilder.reflectionEquals( expected,
531 FileUtils.getDirectoryNames( new File( output,
532 "test/doc-files" ),
533 null, null, false ) ) );
534
535 input = new File( getBasedir(), "src/test/resources/unit/docfiles-test/docfiles/" );
536 assertTrue( input.exists() );
537
538 output = new File( getBasedir(), "target/test/unit/docfiles-test/target/output" );
539 if ( output.exists() )
540 {
541 FileUtils.deleteDirectory( output );
542 }
543 assertTrue( output.mkdirs() );
544
545 JavadocUtil.copyJavadocResources( output, input, "excluded-dir1:excluded-dir2" );
546 expected = new ArrayList();
547 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" + File.separator
548 + "sample-included1.gif" );
549 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" + File.separator
550 + "sample-included2.gif" );
551 assertTrue( EqualsBuilder.reflectionEquals( expected, FileUtils.getFiles( output, null, null, false ) ) );
552 expected = new ArrayList();
553 expected.add( "" );
554 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" );
555 expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" );
556 assertTrue( EqualsBuilder.reflectionEquals( expected,
557 FileUtils.getDirectoryNames( new File( output,
558 "test/doc-files" ),
559 null, null, false ) ) );
560 }
561
562
563
564
565
566
567 public void testPruneDirs()
568 throws Exception
569 {
570 List list = new ArrayList();
571 list.add( getBasedir() + "/target/classes" );
572 list.add( getBasedir() + "/target/classes" );
573 list.add( getBasedir() + "/target/classes" );
574
575 List expected = new ArrayList();
576 expected.add( getBasedir() + "/target/classes" );
577
578 assertTrue( EqualsBuilder.reflectionEquals( expected, JavadocUtil.pruneDirs( null, list ) ) );
579 }
580
581
582
583
584
585
586 public void testUnifyPathSeparator()
587 throws Exception
588 {
589 assertEquals( null, JavadocUtil.unifyPathSeparator( null ) );
590
591 final String ps = File.pathSeparator;
592
593
594 String path1 = "C:\\maven-javadoc-plugin\\src\\main\\java";
595 String path2 = "C:\\maven-javadoc-plugin\\src\\main\\javadoc";
596 assertEquals( path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ";" + path2 ) );
597 assertEquals( path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ":" + path2 ) );
598
599 path1 = "C:/maven-javadoc-plugin/src/main/java";
600 path2 = "C:/maven-javadoc-plugin/src/main/javadoc";
601 assertEquals( path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ";" + path2 ) );
602 assertEquals( path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ":" + path2 ) );
603 assertEquals( path1 + ps + path2 + ps + path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ";"
604 + path2 + ";" + path1 + ":" + path2 ) );
605
606
607 path1 = "/tmp/maven-javadoc-plugin/src/main/java";
608 path2 = "/tmp/maven-javadoc-plugin/src/main/javadoc";
609 assertEquals( path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ";" + path2 ) );
610 assertEquals( path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ":" + path2 ) );
611 assertEquals( path1 + ps + path2 + ps + path1 + ps + path2, JavadocUtil.unifyPathSeparator( path1 + ";"
612 + path2 + ":" + path1 + ":" + path2 ) );
613 }
614
615
616
617
618
619
620
621
622 protected static boolean isWebSiteOnline( Settings settings, Logger logger, String websiteUrl )
623 {
624 try
625 {
626 JavadocUtil.fetchURL( settings, new URL( websiteUrl ) );
627 return true;
628 }
629 catch ( IOException e )
630 {
631 if ( logger == null )
632 {
633 e.printStackTrace();
634 }
635 else
636 {
637 logger.fatalError( "Error when fetching " + websiteUrl + ". Is it available?" );
638 }
639 return false;
640 }
641 }
642 }