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