View Javadoc
1   package org.apache.maven.plugins.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  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Set;
29  import java.util.zip.ZipEntry;
30  import java.util.zip.ZipFile;
31  
32  import org.apache.maven.model.Plugin;
33  import org.apache.maven.plugin.MojoExecution;
34  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.languages.java.version.JavaVersion;
38  import org.codehaus.plexus.util.FileUtils;
39  
40  /**
41   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
42   */
43  public class JavadocJarTest
44      extends AbstractMojoTestCase
45  {
46      
47      private JavadocJar lookupMojo( File testPom )
48                      throws Exception
49      {
50          JavadocJar mojo = (JavadocJar) lookupMojo( "jar", testPom );
51  
52          MojoExecution mojoExec = new MojoExecution( new Plugin(), "javadoc", null );
53  
54          setVariableValueToObject( mojo, "mojo", mojoExec );
55          
56          MavenProject currentProject = new MavenProjectStub();
57          currentProject.setGroupId( "GROUPID" );
58          currentProject.setArtifactId( "ARTIFACTID" );
59          
60          setVariableValueToObject( mojo, "session", newMavenSession( currentProject ) );
61          
62          return mojo;
63      }
64  
65  
66      /**
67       * Test when default configuration is provided
68       *
69       * @throws Exception if any
70       */
71      public void testDefaultConfig()
72          throws Exception
73      {
74          File testPom =
75              new File( getBasedir(), "src/test/resources/unit/javadocjar-default/javadocjar-default-plugin-config.xml" );
76          JavadocJar mojo = lookupMojo( testPom );
77          mojo.execute();
78  
79          //check if the javadoc jar file was generated
80          File generatedFile =
81              new File( getBasedir(), "target/test/unit/javadocjar-default/target/javadocjar-default-javadoc.jar" );
82          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
83  
84          Set<String> set = new HashSet<>();
85  
86          //validate contents of jar file
87          try ( ZipFile jar = new ZipFile( generatedFile ) )
88          {
89              for( Enumeration<? extends ZipEntry> entries = jar.entries(); entries.hasMoreElements(); )
90              {
91                  ZipEntry entry = entries.nextElement();
92                  set.add( entry.getName() );
93              }
94          }
95  
96          assertTrue( set.contains( "stylesheet.css" ) );
97          JavaVersion javadocVersion = (JavaVersion) getVariableValueFromObject( mojo, "javadocRuntimeVersion" );
98          if ( javadocVersion.isBefore( "1.7" ) )
99          {
100             assertTrue( set.contains( "resources/inherit.gif" ) );
101         }
102         else if ( javadocVersion.isBefore( "1.8" ) )
103         {
104             assertTrue( set.contains( "resources/background.gif" ) /* JDK7 */);
105         }
106         else
107         {
108             // JDK8 has no resources anymore
109             assertFalse( set.contains( "resources" ) );
110         }
111 
112         assertTrue( set.contains( "javadocjar/def/package-use.html" ) );
113         assertTrue( set.contains( "javadocjar/def/package-tree.html" ) );
114         assertTrue( set.contains( "javadocjar/def/package-summary.html" ) );
115         assertTrue( set.contains( "javadocjar/def/package-frame.html" ) );
116         assertTrue( set.contains( "javadocjar/def/class-use/AppSample.html" ) );
117         assertTrue( set.contains( "index.html" ) );
118         assertTrue( set.contains( "javadocjar/def/App.html" ) );
119         assertTrue( set.contains( "javadocjar/def/AppSample.html" ) );
120         assertTrue( set.contains( "javadocjar/def/class-use/App.html" ) );
121 
122         assertFalse( set.contains( AbstractJavadocMojo.ARGFILE_FILE_NAME ) );
123         assertFalse( set.contains( AbstractJavadocMojo.FILES_FILE_NAME ) );
124         assertFalse( set.contains( AbstractJavadocMojo.OPTIONS_FILE_NAME ) );
125         assertFalse( set.contains( AbstractJavadocMojo.PACKAGES_FILE_NAME ) );
126 
127         //check if the javadoc files were created
128         generatedFile =
129             new File( getBasedir(), "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/App.html" );
130         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
131 
132         generatedFile = new File( getBasedir(),
133                                   "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/AppSample.html" );
134         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
135     }
136 
137     /**
138      * Test when the specified destDir parameter has an invalid value
139      *
140      * @throws Exception if any
141      */
142     public void testInvalidDestdir()
143         throws Exception
144     {
145         File testPom = new File( getBasedir(),
146                                  "src/test/resources/unit/javadocjar-invalid-destdir/javadocjar-invalid-destdir-plugin-config.xml" );
147         JavadocJar mojo = lookupMojo( testPom );
148         mojo.execute();
149 
150         //check if the javadoc jar file was generated
151         File generatedFile = new File( getBasedir(),
152                                        "target/test/unit/javadocjar-invalid-destdir/target/javadocjar-invalid-destdir-javadoc.jar" );
153         assertTrue( !FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
154     }
155 
156     public void testContinueIfFailOnErrorIsFalse() throws Exception
157     {
158         File testPom =
159                 new File( getBasedir(), "src/test/resources/unit/javadocjar-failonerror/javadocjar-failonerror-plugin-config.xml" );
160         JavadocJar mojo = lookupMojo( testPom );
161         mojo.execute();
162 
163         //check if the javadoc jar file was generated
164         File generatedFile =
165                 new File( getBasedir(), "target/test/unit/javadocjar-failonerror/target/javadocjar-failonerror-javadoc.jar" );
166         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
167     }
168 
169     public void testIncludeMavenDescriptorWhenExplicitlyConfigured() throws Exception
170     {
171         File testPom =
172                 new File( getBasedir(), "src/test/resources/unit/javadocjar-archive-config/javadocjar-archive-config.xml" );
173         JavadocJar mojo = lookupMojo( testPom );
174         mojo.execute();
175 
176         //check if the javadoc jar file was generated
177         File generatedFile =
178                 new File( getBasedir(), "target/test/unit/javadocjar-archive-config/target/javadocjar-archive-config-javadoc.jar" );
179         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
180 
181         //validate contents of jar file
182         ZipFile jar = new ZipFile( generatedFile );
183         Set<String> set = new HashSet<>();
184         for (Enumeration<? extends ZipEntry> entries = jar.entries(); entries.hasMoreElements(); )
185         {
186             ZipEntry entry = entries.nextElement();
187             set.add( entry.getName() );
188         }
189         jar.close();
190 
191         List<String> expected = new ArrayList<>();
192         expected.add( "META-INF/" );
193         expected.add( "META-INF/maven/" );
194         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/" );
195         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/" );
196         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.xml" );
197         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.properties" );
198 
199         for (int i = 0; i < expected.size(); i++)
200         {
201             String entry = expected.get( i );
202             assertTrue( "Expected jar to contain " + entry, set.contains( entry ) );
203         }
204     }
205 }