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 org.apache.maven.plugin.testing.AbstractMojoTestCase;
23  import org.codehaus.plexus.archiver.zip.ZipEntry;
24  import org.codehaus.plexus.archiver.zip.ZipFile;
25  import org.codehaus.plexus.util.FileUtils;
26  
27  import java.io.File;
28  import java.util.Enumeration;
29  import java.util.Set;
30  import java.util.HashSet;
31  
32  /**
33   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
34   */
35  public class JavadocJarTest
36      extends AbstractMojoTestCase
37  {
38      /**
39       * Test when default configuration is provided
40       *
41       * @throws Exception if any
42       */
43      public void testDefaultConfig()
44          throws Exception
45      {
46          File testPom =
47              new File( getBasedir(), "src/test/resources/unit/javadocjar-default/javadocjar-default-plugin-config.xml" );
48          JavadocJar mojo = (JavadocJar) lookupMojo( "jar", testPom );
49          mojo.execute();
50  
51          //check if the javadoc jar file was generated
52          File generatedFile =
53              new File( getBasedir(), "target/test/unit/javadocjar-default/target/javadocjar-default-javadoc.jar" );
54          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
55  
56          //validate contents of jar file
57          ZipFile jar = new ZipFile( generatedFile );
58          Set set = new HashSet();
59          for( Enumeration entries = jar.getEntries(); entries.hasMoreElements(); )
60          {
61              ZipEntry entry = ( ZipEntry ) entries.nextElement();
62              set.add( entry.getName() );
63          }
64  
65          assertTrue( set.contains( "stylesheet.css" ) );
66          assertTrue( set.contains( "resources/inherit.gif" ) );
67          assertTrue( set.contains( "javadocjar/def/package-use.html" ) );
68          assertTrue( set.contains( "javadocjar/def/package-tree.html" ) );
69          assertTrue( set.contains( "javadocjar/def/package-summary.html" ) );
70          assertTrue( set.contains( "javadocjar/def/package-frame.html" ) );
71          assertTrue( set.contains( "javadocjar/def/class-use/AppSample.html" ) );
72          assertTrue( set.contains( "index.html" ) );
73          assertTrue( set.contains( "javadocjar/def/App.html" ) );
74          assertTrue( set.contains( "javadocjar/def/AppSample.html" ) );
75          assertTrue( set.contains( "javadocjar/def/class-use/App.html" ) );
76  
77          assertFalse( set.contains( AbstractJavadocMojo.ARGFILE_FILE_NAME ) );
78          assertFalse( set.contains( AbstractJavadocMojo.FILES_FILE_NAME ) );
79          assertFalse( set.contains( AbstractJavadocMojo.OPTIONS_FILE_NAME ) );
80          assertFalse( set.contains( AbstractJavadocMojo.PACKAGES_FILE_NAME ) );
81  
82          //check if the javadoc files were created
83          generatedFile =
84              new File( getBasedir(), "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/App.html" );
85          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
86  
87          generatedFile = new File( getBasedir(),
88                                    "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/AppSample.html" );
89          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
90      }
91  
92      /**
93       * Test when the specified destDir parameter has an invalid value
94       *
95       * @throws Exception if any
96       */
97      public void testInvalidDestdir()
98          throws Exception
99      {
100         File testPom = new File( getBasedir(),
101                                  "src/test/resources/unit/javadocjar-invalid-destdir/javadocjar-invalid-destdir-plugin-config.xml" );
102         JavadocJar mojo = (JavadocJar) lookupMojo( "jar", testPom );
103         mojo.execute();
104 
105         //check if the javadoc jar file was generated
106         File generatedFile = new File( getBasedir(),
107                                        "target/test/unit/javadocjar-invalid-destdir/target/javadocjar-invalid-destdir-javadoc.jar" );
108         assertTrue( !FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
109     }
110 }