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         // package frame not generated anymore since Java 11
116         if ( JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore( "11" ) )
117         {
118             assertTrue( set.contains( "javadocjar/def/package-frame.html" ) );
119         }
120         assertTrue( set.contains( "javadocjar/def/class-use/AppSample.html" ) );
121         assertTrue( set.contains( "index.html" ) );
122         assertTrue( set.contains( "javadocjar/def/App.html" ) );
123         assertTrue( set.contains( "javadocjar/def/AppSample.html" ) );
124         assertTrue( set.contains( "javadocjar/def/class-use/App.html" ) );
125 
126         assertFalse( set.contains( AbstractJavadocMojo.ARGFILE_FILE_NAME ) );
127         assertFalse( set.contains( AbstractJavadocMojo.FILES_FILE_NAME ) );
128         assertFalse( set.contains( AbstractJavadocMojo.OPTIONS_FILE_NAME ) );
129         assertFalse( set.contains( AbstractJavadocMojo.PACKAGES_FILE_NAME ) );
130 
131         //check if the javadoc files were created
132         generatedFile =
133             new File( getBasedir(), "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/App.html" );
134         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
135 
136         generatedFile = new File( getBasedir(),
137                                   "target/test/unit/javadocjar-default/target/site/apidocs/javadocjar/def/AppSample.html" );
138         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
139     }
140 
141     /**
142      * Test when the specified destDir parameter has an invalid value
143      *
144      * @throws Exception if any
145      */
146     public void testInvalidDestdir()
147         throws Exception
148     {
149         File testPom = new File( getBasedir(),
150                                  "src/test/resources/unit/javadocjar-invalid-destdir/javadocjar-invalid-destdir-plugin-config.xml" );
151         JavadocJar mojo = lookupMojo( testPom );
152         mojo.execute();
153 
154         //check if the javadoc jar file was generated
155         File generatedFile = new File( getBasedir(),
156                                        "target/test/unit/javadocjar-invalid-destdir/target/javadocjar-invalid-destdir-javadoc.jar" );
157         assertTrue( !FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
158     }
159 
160     public void testContinueIfFailOnErrorIsFalse() throws Exception
161     {
162         File testPom =
163                 new File( getBasedir(), "src/test/resources/unit/javadocjar-failonerror/javadocjar-failonerror-plugin-config.xml" );
164         JavadocJar mojo = lookupMojo( testPom );
165         mojo.execute();
166 
167         //check if the javadoc jar file was generated
168         File generatedFile =
169                 new File( getBasedir(), "target/test/unit/javadocjar-failonerror/target/javadocjar-failonerror-javadoc.jar" );
170         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
171     }
172 
173     public void testIncludeMavenDescriptorWhenExplicitlyConfigured() throws Exception
174     {
175         File testPom =
176                 new File( getBasedir(), "src/test/resources/unit/javadocjar-archive-config/javadocjar-archive-config.xml" );
177         JavadocJar mojo = lookupMojo( testPom );
178         mojo.execute();
179 
180         //check if the javadoc jar file was generated
181         File generatedFile =
182                 new File( getBasedir(), "target/test/unit/javadocjar-archive-config/target/javadocjar-archive-config-javadoc.jar" );
183         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
184 
185         //validate contents of jar file
186         ZipFile jar = new ZipFile( generatedFile );
187         Set<String> set = new HashSet<>();
188         for (Enumeration<? extends ZipEntry> entries = jar.entries(); entries.hasMoreElements(); )
189         {
190             ZipEntry entry = entries.nextElement();
191             set.add( entry.getName() );
192         }
193         jar.close();
194 
195         List<String> expected = new ArrayList<>();
196         expected.add( "META-INF/" );
197         expected.add( "META-INF/maven/" );
198         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/" );
199         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/" );
200         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.xml" );
201         expected.add( "META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.properties" );
202 
203         for (int i = 0; i < expected.size(); i++)
204         {
205             String entry = expected.get( i );
206             assertTrue( "Expected jar to contain " + entry, set.contains( entry ) );
207         }
208     }
209 }