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