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  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileReader;
25  import java.io.IOException;
26  import java.util.List;
27  import java.util.Locale;
28  
29  import org.apache.maven.execution.MavenSession;
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  import org.codehaus.plexus.util.FileUtils;
37  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
38  import org.sonatype.aether.util.DefaultRepositorySystemSession;
39  
40  public class AggregatorJavadocReportTest
41      extends AbstractMojoTestCase
42  {
43      private static final char LINE_SEPARATOR = ' ';
44  
45      /** flag to copy repo only one time */
46      private static boolean TEST_REPO_CREATED = false;
47  
48      private File unit;
49  
50      private File localRepo;
51  
52      /** {@inheritDoc} */
53      @Override
54      protected void setUp()
55          throws Exception
56      {
57          super.setUp();
58  
59          unit = new File( getBasedir(), "src/test/resources/unit" );
60  
61          localRepo = new File( getBasedir(), "target/local-repo/" );
62  
63          createTestRepo();
64      }
65  
66      private JavadocReport lookupMojo( File testPom )
67          throws Exception
68      {
69          JavadocReport mojo = (JavadocReport) lookupMojo( "aggregate", testPom );
70  
71          MojoExecution mojoExec = new MojoExecution( new Plugin(), "aggregate", null );
72          setVariableValueToObject( mojo, "mojo", mojoExec );
73          
74          MavenProject currentProject = new MavenProjectStub();
75          currentProject.setGroupId( "GROUPID" );
76          currentProject.setArtifactId( "ARTIFACTID" );
77          
78          MavenSession session = newMavenSession( currentProject );
79          DefaultRepositorySystemSession repoSysSession = (DefaultRepositorySystemSession) session.getRepositorySession();
80          repoSysSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
81          setVariableValueToObject( mojo, "session", session );
82  
83          return mojo;
84      }
85  
86      /**
87       * Create test repository in target directory.
88       *
89       * @throws IOException if any
90       */
91      private void createTestRepo()
92          throws IOException
93      {
94          if ( TEST_REPO_CREATED )
95          {
96              return;
97          }
98  
99          localRepo.mkdirs();
100 
101         // ----------------------------------------------------------------------
102         // UMLGraph
103         // ----------------------------------------------------------------------
104 
105         File sourceDir = new File( unit, "doclet-test/artifact-doclet" );
106         assertTrue( sourceDir.exists() );
107         FileUtils.copyDirectoryStructure( sourceDir, localRepo );
108 
109         // ----------------------------------------------------------------------
110         // UMLGraph-bis
111         // ----------------------------------------------------------------------
112 
113         sourceDir = new File( unit, "doclet-path-test/artifact-doclet" );
114         assertTrue( sourceDir.exists() );
115         FileUtils.copyDirectoryStructure( sourceDir, localRepo );
116 
117         // ----------------------------------------------------------------------
118         // commons-attributes-compiler
119         // http://www.tullmann.org/pat/taglets/
120         // ----------------------------------------------------------------------
121 
122         sourceDir = new File( unit, "taglet-test/artifact-taglet" );
123         assertTrue( sourceDir.exists() );
124         FileUtils.copyDirectoryStructure( sourceDir, localRepo );
125 
126         // ----------------------------------------------------------------------
127         // stylesheetfile-test
128         // ----------------------------------------------------------------------
129 
130         sourceDir = new File( unit, "stylesheetfile-test/artifact-stylesheetfile" );
131         assertTrue( sourceDir.exists() );
132         FileUtils.copyDirectoryStructure( sourceDir, localRepo );
133 
134         // ----------------------------------------------------------------------
135         // helpfile-test
136         // ----------------------------------------------------------------------
137 
138         sourceDir = new File( unit, "helpfile-test/artifact-helpfile" );
139         assertTrue( sourceDir.exists() );
140         FileUtils.copyDirectoryStructure( sourceDir, localRepo );
141 
142         // Remove SCM files
143         List<String> files = FileUtils.getFileAndDirectoryNames( localRepo, FileUtils.getDefaultExcludesAsString(),
144                                                                  null, true, true, true, true );
145         for ( String filename : files )
146         {
147             File file = new File( filename );
148 
149             if ( file.isDirectory() )
150             {
151                 FileUtils.deleteDirectory( file );
152             }
153             else
154             {
155                 file.delete();
156             }
157         }
158 
159         TEST_REPO_CREATED = true;
160     }
161 
162     /**
163      * Convenience method that reads the contents of the specified file object into a string with a <code>space</code>
164      * as line separator.
165      *
166      * @see #LINE_SEPARATOR
167      * @param file the file to be read
168      * @return a String object that contains the contents of the file
169      * @throws IOException if any
170      */
171     private static String readFile( File file )
172         throws IOException
173     {
174         StringBuilder str = new StringBuilder( (int) file.length() );
175 
176         try (BufferedReader in = new BufferedReader(new FileReader(file))) {
177 
178             for ( String strTmp ; ( strTmp = in.readLine() ) != null ; ) {
179                 str.append( LINE_SEPARATOR );
180                 str.append( strTmp );
181             }
182         }
183 
184         return str.toString();
185     }
186 
187     /**
188      * Method to test the aggregate parameter
189      *
190      * @throws Exception if any
191      */
192     public void testAggregate()
193         throws Exception
194     {
195         File testPom = new File( unit, "aggregate-test/aggregate-test-plugin-config.xml" );
196         JavadocReport mojo = lookupMojo( testPom );
197         mojo.execute();
198 
199         File apidocs = new File( getBasedir(), "target/test/unit/aggregate-test/target/site/apidocs/" );
200 
201         // check if project1 api files exist
202         assertTrue( new File( apidocs, "aggregate/test/project1/Project1App.html" ).exists() );
203         assertTrue( new File( apidocs, "aggregate/test/project1/Project1AppSample.html" ).exists() );
204         assertTrue( new File( apidocs, "aggregate/test/project1/Project1Sample.html" ).exists() );
205         assertTrue( new File( apidocs, "aggregate/test/project1/Project1Test.html" ).exists() );
206 
207         // check if project2 api files exist
208         assertTrue( new File( apidocs, "aggregate/test/project2/Project2App.html" ).exists() );
209         assertTrue( new File( apidocs, "aggregate/test/project2/Project2AppSample.html" ).exists() );
210         assertTrue( new File( apidocs, "aggregate/test/project2/Project2Sample.html" ).exists() );
211         assertTrue( new File( apidocs, "aggregate/test/project2/Project2Test.html" ).exists() );
212     }
213 
214     /**
215      * Test the javadoc resources in the aggregation case.
216      *
217      * @throws Exception if any
218      */
219     public void testAggregateJavadocResources()
220         throws Exception
221     {
222         File testPom = new File( unit, "aggregate-resources-test/aggregate-resources-test-plugin-config.xml" );
223         JavadocReport mojo = lookupMojo( testPom );
224         mojo.execute();
225 
226         File apidocs = new File( getBasedir(), "target/test/unit/aggregate-resources-test/target/site/apidocs" );
227 
228         // Test overview
229         File overviewSummary = getOverviewSummary(apidocs);
230         
231         assertTrue( overviewSummary.exists() );
232         String overview = readFile( overviewSummary ).toLowerCase( Locale.ENGLISH );
233         assertTrue( overview.contains( "<a href=\"resources/test/package-summary.html\">resources.test</a>" ) );
234         assertTrue( overview.contains( ">blabla</" ) );
235         assertTrue( overview.contains( "<a href=\"resources/test2/package-summary.html\">resources.test2</a>" ) );
236         assertTrue( overview.contains( "<a href=\"resources2/test/package-summary.html\">resources2.test</a>" ) );
237         assertTrue( overview.contains( "<a href=\"resources2/test2/package-summary.html\">resources2.test2</a>" ) );
238 
239         // Test doc-files
240         File app = new File( apidocs, "resources/test/App.html" );
241         assertTrue( app.exists() );
242         overview = readFile( app );
243         assertTrue( overview.contains( "<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">" ) );
244         assertTrue( new File( apidocs, "resources/test/doc-files/maven-feather.png" ).exists() );
245     }
246 
247     public void testAggregateWithModulsNotInSubFolders() throws Exception
248     {
249       File testPom = new File( unit, "aggregate-modules-not-in-subfolders-test/all/pom.xml");
250       JavadocReport mojo = lookupMojo( testPom );
251       mojo.execute();
252       
253       File apidocs = new File( getBasedir(), "target/test/unit/aggregate-modules-not-in-subfolders-test/target/site/apidocs" );
254       assertTrue( apidocs.isDirectory() );
255       assertTrue( getOverviewSummary( apidocs ).isFile() );
256     }
257     
258     private static File getOverviewSummary(File apidocs)
259     {
260       if ( JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore( "11" ) )
261       {
262           return new File( apidocs, "overview-summary.html" );
263       }
264       return new File( apidocs, "index.html" );
265     }
266 
267 }