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       * @see org.apache.maven.plugin.testing.AbstractMojoTestCase#setUp()
40       */
41      protected void setUp()
42          throws Exception
43      {
44          // required for mojo lookups to work
45          super.setUp();
46      }
47  
48      /**
49       * @see org.codehaus.plexus.PlexusTestCase#tearDown()
50       */
51      protected void tearDown()
52          throws Exception
53      {
54          super.tearDown();
55      }
56  
57      /**
58       * Test when default configuration is provided
59       *
60       * @throws Exception
61       */
62      public void testDefaultConfig()
63          throws Exception
64      {
65          File testPom =
66              new File( getBasedir(), "src/test/resources/unit/javadocjar-default/javadocjar-default-plugin-config.xml" );
67          JavadocJar mojo = (JavadocJar) lookupMojo( "jar", testPom );
68          mojo.execute();
69  
70          //check if the javadoc jar file was generated
71          File generatedFile =
72              new File( getBasedir(), "target/test/unit/javadocjar-default/target/javadocjar-default-javadoc.jar" );
73          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
74  
75          //validate contents of jar file
76          ZipFile jar = new ZipFile( generatedFile );
77          Set set = new HashSet();
78          for( Enumeration entries = jar.getEntries(); entries.hasMoreElements(); )
79          {
80              ZipEntry entry = ( ZipEntry ) entries.nextElement();
81              set.add( entry.getName() );
82          }
83  
84          assertTrue( set.contains( "stylesheet.css" ) );
85          assertTrue( set.contains( "resources/inherit.gif" ) );
86          assertTrue( set.contains( "javadocjar/def/package-use.html" ) );
87          assertTrue( set.contains( "javadocjar/def/package-tree.html" ) );
88          assertTrue( set.contains( "javadocjar/def/package-summary.html" ) );
89          assertTrue( set.contains( "javadocjar/def/package-frame.html" ) );
90          assertTrue( set.contains( "javadocjar/def/class-use/AppSample.html" ) );
91          assertTrue( set.contains( "index.html" ) );
92          assertTrue( set.contains( "javadocjar/def/App.html" ) );
93          assertTrue( set.contains( "javadocjar/def/AppSample.html" ) );
94          assertTrue( set.contains( "javadocjar/def/class-use/App.html" ) );
95  
96          assertFalse( set.contains( AbstractJavadocMojo.ARGFILE_FILE_NAME ) );
97          assertFalse( set.contains( AbstractJavadocMojo.FILES_FILE_NAME ) );
98          assertFalse( set.contains( AbstractJavadocMojo.OPTIONS_FILE_NAME ) );
99          assertFalse( set.contains( AbstractJavadocMojo.PACKAGES_FILE_NAME ) );
100 
101         //check if the javadoc files were created
102         generatedFile =
103             new File( getBasedir(), "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/App.html" );
104         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
105 
106         generatedFile = new File( getBasedir(),
107                                   "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/AppSample.html" );
108         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
109     }
110 
111     /**
112      * Test when the specified destDir parameter has an invalid value
113      *
114      * @throws Exception
115      */
116     public void testInvalidDestdir()
117         throws Exception
118     {
119         File testPom = new File( getBasedir(),
120                                  "src/test/resources/unit/javadocjar-invalid-destdir/javadocjar-invalid-destdir-plugin-config.xml" );
121         JavadocJar mojo = (JavadocJar) lookupMojo( "jar", testPom );
122         mojo.execute();
123 
124         //check if the javadoc jar file was generated
125         File generatedFile = new File( getBasedir(),
126                                        "target/test/unit/javadocjar-invalid-destdir/target/javadocjar-invalid-destdir-javadoc.jar" );
127         assertTrue( !FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
128     }
129 }