1   package org.apache.maven.plugin.javadoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
42   * @version $Id$
43   */
44  public class JavadocUtilTest
45      extends PlexusTestCase
46  {
47      /**
48       * Method to test the javadoc version parsing.
49       *
50       * @throws Exception if any
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          // Sun JDK 1.4
67          version = "java full version \"1.4.2_12-b03\"";
68          assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.42f, 0 );
69  
70          // Sun JDK 1.5
71          version = "java full version \"1.5.0_07-164\"";
72          assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
73  
74          // IBM JDK 1.4
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          // IBM JDK 1.5
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          // IBM JDK 1.5
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          // FreeBSD
87          version = "java full version \"diablo-1.5.0-b01\"";
88          assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
89  
90          // BEA
91          version = "java full version \"1.5.0_11-b03\"";
92          assertEquals( JavadocUtil.parseJavadocVersion( version ), 1.5f, 0 );
93  
94          // Other tests
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      * Method to test the javadoc memory parsing.
131      *
132      * @throws Exception if any
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      * Method to test the validate encoding parsing.
205      *
206      * @throws Exception if any
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      * Method to test the hiding proxy password.
220      *
221      * @throws Exception if any
222      */
223     public void testHideProxyPassword()
224         throws Exception
225     {
226         String cmdLine = "javadoc.exe " + "-J-Dhttp.proxySet=true " + "-J-Dhttp.proxyHost=http://localhost "
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( "http://localhost" );
236         proxy.setPort( 80 );
237         proxy.setUsername( "toto" );
238         proxy.setPassword( "toto" );
239         proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
240         settings.addProxy( proxy );
241 
242         cmdLine = "javadoc.exe " + "-J-Dhttp.proxySet=true " + "-J-Dhttp.proxyHost=http://localhost "
243             + "-J-Dhttp.proxyPort=80 " + "-J-Dhttp.nonProxyHosts=\"www.google.com|*.somewhere.com\" "
244             + "-J-Dhttp.proxyUser=\"toto\" " + "-J-Dhttp.proxyPassword=\"toto\" " + "@options @packages";
245         cmdLine = JavadocUtil.hideProxyPassword( cmdLine, settings );
246         assertTrue( cmdLine.indexOf( "-J-Dhttp.proxyPassword=\"****\"" ) != -1 );
247 
248         settings = new Settings();
249         proxy = new Proxy();
250         proxy.setActive( true );
251         proxy.setHost( "http://localhost" );
252         proxy.setPort( 80 );
253         proxy.setUsername( "toto" );
254         proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
255         settings.addProxy( proxy );
256 
257         cmdLine = "javadoc.exe " + "-J-Dhttp.proxySet=true " + "-J-Dhttp.proxyHost=http://localhost "
258         + "-J-Dhttp.proxyPort=80 " + "-J-Dhttp.nonProxyHosts=\"www.google.com|*.somewhere.com\" "
259         + "-J-Dhttp.proxyUser=\"toto\" " + "-J-Dhttp.proxyPassword=\"toto\" " + "@options @packages";
260         cmdLine = JavadocUtil.hideProxyPassword( cmdLine, null );
261         assertFalse( cmdLine.indexOf( "-J-Dhttp.proxyPassword=\"****\"" ) != -1 );
262     }
263 
264     /**
265      * Method to test fetchURL()
266      *
267      * @throws Exception if any
268      */
269     public void testFetchURL()
270         throws Exception
271     {
272         Settings settings = null;
273         Proxy proxy = null;
274 
275         URL url = null;
276         URL wrongUrl = null;
277         try
278         {
279             JavadocUtil.fetchURL( settings, url );
280             assertTrue( false );
281         }
282         catch ( IllegalArgumentException e )
283         {
284             assertTrue( true );
285         }
286 
287         url = new File( getBasedir(), "/pom.xml" ).toURL();
288         JavadocUtil.fetchURL( settings, url );
289         assertTrue( true );
290 
291         url = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list" );
292         JavadocUtil.fetchURL( settings, url );
293         assertTrue( true );
294 
295         wrongUrl = new URL( "http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list2" );
296         try
297         {
298             JavadocUtil.fetchURL( settings, wrongUrl );
299             assertTrue( false );
300         }
301         catch ( IOException e )
302         {
303             assertTrue( true );
304         }
305 
306         // real proxy
307         ProxyServer proxyServer = null;
308         AuthAsyncProxyServlet proxyServlet = null;
309         try
310         {
311             proxyServlet = new AuthAsyncProxyServlet();
312             proxyServer = new ProxyServer( proxyServlet );
313             proxyServer.start();
314 
315             settings = new Settings();
316 
317             JavadocUtil.fetchURL( settings, url );
318             assertTrue( true );
319 
320             try
321             {
322                 JavadocUtil.fetchURL( settings, wrongUrl );
323                 assertTrue( false );
324             }
325             catch ( IOException e )
326             {
327                 assertTrue( true );
328             }
329         }
330         catch ( Exception e )
331         {
332             assertTrue( false );
333         }
334         finally
335         {
336             if ( proxyServer != null )
337             {
338                 proxyServer.stop();
339             }
340         }
341 
342         Map authentications = new HashMap();
343         authentications.put( "foo", "bar" );
344         // wrong auth
345         try
346         {
347             proxyServlet = new AuthAsyncProxyServlet( authentications );
348             proxyServer = new ProxyServer( proxyServlet );
349             proxyServer.start();
350 
351             settings = new Settings();
352             proxy = new Proxy();
353             proxy.setActive( true );
354             proxy.setHost( proxyServer.getHostName() );
355             proxy.setPort( proxyServer.getPort() );
356             settings.addProxy( proxy );
357 
358             JavadocUtil.fetchURL( settings, url );
359             assertTrue( false );
360         }
361         catch ( FileNotFoundException e )
362         {
363             assertTrue( true );
364         }
365         catch ( Exception e )
366         {
367             assertTrue( false );
368         }
369         finally
370         {
371             if ( proxyServer != null )
372             {
373                 proxyServer.stop();
374             }
375         }
376 
377         // auth proxy
378         try
379         {
380             proxyServlet = new AuthAsyncProxyServlet( authentications );
381             proxyServer = new ProxyServer( proxyServlet );
382             proxyServer.start();
383 
384             settings = new Settings();
385             proxy = new Proxy();
386             proxy.setActive( true );
387             proxy.setHost( proxyServer.getHostName() );
388             proxy.setPort( proxyServer.getPort() );
389             proxy.setUsername( "foo" );
390             proxy.setPassword( "bar" );
391             settings.addProxy( proxy );
392 
393             JavadocUtil.fetchURL( settings, url );
394             assertTrue( true );
395 
396             try
397             {
398                 JavadocUtil.fetchURL( settings, wrongUrl );
399                 assertTrue( false );
400             }
401             catch ( IOException e )
402             {
403                 assertTrue( true );
404             }
405         }
406         catch ( FileNotFoundException e )
407         {
408             assertTrue( false );
409         }
410         finally
411         {
412             if ( proxyServer != null )
413             {
414                 proxyServer.stop();
415             }
416         }
417 
418         // timeout
419         try
420         {
421             proxyServlet = new AuthAsyncProxyServlet( authentications, 3000 ); // more than 2000, see fetchURL
422             proxyServer = new ProxyServer( proxyServlet );
423             proxyServer.start();
424 
425             settings = new Settings();
426             proxy = new Proxy();
427             proxy.setActive( true );
428             proxy.setHost( proxyServer.getHostName() );
429             proxy.setPort( proxyServer.getPort() );
430             proxy.setUsername( "foo" );
431             proxy.setPassword( "bar" );
432             settings.addProxy( proxy );
433             JavadocUtil.fetchURL( settings, url );
434             assertTrue( false );
435         }
436         catch ( SocketTimeoutException e )
437         {
438             assertTrue( true );
439         }
440         finally
441         {
442             if ( proxyServer != null )
443             {
444                 proxyServer.stop();
445             }
446         }
447     }
448 
449     /**
450      * Method to test copyJavadocResources()
451      *
452      * @throws Exception if any
453      */
454     public void testCopyJavadocResources()
455         throws Exception
456     {
457         File input = new File( getBasedir(), "src/test/resources/unit/docfiles-test/docfiles/" );
458         assertTrue( input.exists() );
459 
460         File output = new File( getBasedir(), "target/test/unit/docfiles-test/target/output" );
461         if ( output.exists() )
462         {
463             FileUtils.deleteDirectory( output );
464         }
465         assertTrue( output.mkdirs() );
466 
467         JavadocUtil.copyJavadocResources( output, input, null );
468         List expected = new ArrayList();
469         expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir1" + File.separator
470             + "sample-excluded1.gif" );
471         expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir2" + File.separator
472             + "sample-excluded2.gif" );
473         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" + File.separator
474             + "sample-included1.gif" );
475         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" + File.separator
476             + "sample-included2.gif" );
477         assertTrue( EqualsBuilder.reflectionEquals( expected, FileUtils.getFiles( output, null, null, false ) ) );
478         expected = new ArrayList();
479         expected.add( "" );
480         expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir1" );
481         expected.add( "test" + File.separator + "doc-files" + File.separator + "excluded-dir1" );
482         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" );
483         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" );
484         assertTrue( EqualsBuilder.reflectionEquals( expected,
485                                                     FileUtils.getDirectoryNames( new File( output,
486                                                                                            "test/doc-files" ),
487                                                                                  null, null, false ) ) );
488 
489         input = new File( getBasedir(), "src/test/resources/unit/docfiles-test/docfiles/" );
490         assertTrue( input.exists() );
491 
492         output = new File( getBasedir(), "target/test/unit/docfiles-test/target/output" );
493         if ( output.exists() )
494         {
495             FileUtils.deleteDirectory( output );
496         }
497         assertTrue( output.mkdirs() );
498 
499         JavadocUtil.copyJavadocResources( output, input, "excluded-dir1:excluded-dir2" );
500         expected = new ArrayList();
501         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" + File.separator
502             + "sample-included1.gif" );
503         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" + File.separator
504             + "sample-included2.gif" );
505         assertTrue( EqualsBuilder.reflectionEquals( expected, FileUtils.getFiles( output, null, null, false ) ) );
506         expected = new ArrayList();
507         expected.add( "" );
508         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir1" );
509         expected.add( "test" + File.separator + "doc-files" + File.separator + "included-dir2" );
510         assertTrue( EqualsBuilder.reflectionEquals( expected,
511                                                     FileUtils.getDirectoryNames( new File( output,
512                                                                                            "test/doc-files" ),
513                                                                                  null, null, false ) ) );
514     }
515 
516     /**
517      * Method to test copyJavadocResources()
518      *
519      * @throws Exception if any
520      */
521     public void testPruneDirs()
522         throws Exception
523     {
524         List list = new ArrayList();
525         list.add( getBasedir() + "/target/classes" );
526         list.add( getBasedir() + "/target/classes" );
527         list.add( getBasedir() + "/target/classes" );
528 
529         List expected = new ArrayList();
530         expected.add( getBasedir() + "/target/classes" );
531 
532         assertTrue( EqualsBuilder.reflectionEquals( expected, JavadocUtil.pruneDirs( null, list ) ) );
533     }
534 }