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