View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.javadoc;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.util.List;
26  import java.util.Locale;
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.eclipse.aether.DefaultRepositorySystemSession;
37  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
38  import org.eclipse.aether.repository.LocalRepository;
39  
40  public class AggregatorJavadocReportTest extends AbstractMojoTestCase {
41      private static final char LINE_SEPARATOR = ' ';
42  
43      /** flag to copy repo only one time */
44      private static boolean TEST_REPO_CREATED = false;
45  
46      private File unit;
47  
48      private File localRepo;
49  
50      /** {@inheritDoc} */
51      @Override
52      protected void setUp() throws Exception {
53          super.setUp();
54  
55          unit = new File(getBasedir(), "src/test/resources/unit");
56  
57          localRepo = new File(getBasedir(), "target/local-repo/");
58  
59          createTestRepo();
60      }
61  
62      private JavadocReport lookupMojo(File testPom) throws Exception {
63          JavadocReport mojo = (JavadocReport) lookupMojo("aggregate", testPom);
64  
65          MojoExecution mojoExec = new MojoExecution(new Plugin(), "aggregate", null);
66          setVariableValueToObject(mojo, "mojo", mojoExec);
67  
68          MavenProject currentProject = new MavenProjectStub();
69          currentProject.setGroupId("GROUPID");
70          currentProject.setArtifactId("ARTIFACTID");
71  
72          MavenSession session = newMavenSession(currentProject);
73          DefaultRepositorySystemSession repoSysSession = (DefaultRepositorySystemSession) session.getRepositorySession();
74          repoSysSession.setLocalRepositoryManager(
75                  new SimpleLocalRepositoryManagerFactory().newInstance(repoSysSession, new LocalRepository(localRepo)));
76          setVariableValueToObject(mojo, "session", session);
77          setVariableValueToObject(mojo, "repoSession", repoSysSession);
78          return mojo;
79      }
80  
81      /**
82       * Create test repository in target directory.
83       *
84       * @throws IOException if any
85       */
86      private void createTestRepo() throws IOException {
87          if (TEST_REPO_CREATED) {
88              return;
89          }
90  
91          localRepo.mkdirs();
92  
93          // ----------------------------------------------------------------------
94          // UMLGraph
95          // ----------------------------------------------------------------------
96  
97          File sourceDir = new File(unit, "doclet-test/artifact-doclet");
98          assertTrue(sourceDir.exists());
99          FileUtils.copyDirectoryStructure(sourceDir, localRepo);
100 
101         // ----------------------------------------------------------------------
102         // UMLGraph-bis
103         // ----------------------------------------------------------------------
104 
105         sourceDir = new File(unit, "doclet-path-test/artifact-doclet");
106         assertTrue(sourceDir.exists());
107         FileUtils.copyDirectoryStructure(sourceDir, localRepo);
108 
109         // ----------------------------------------------------------------------
110         // commons-attributes-compiler
111         // http://www.tullmann.org/pat/taglets/
112         // ----------------------------------------------------------------------
113 
114         sourceDir = new File(unit, "taglet-test/artifact-taglet");
115         assertTrue(sourceDir.exists());
116         FileUtils.copyDirectoryStructure(sourceDir, localRepo);
117 
118         // ----------------------------------------------------------------------
119         // stylesheetfile-test
120         // ----------------------------------------------------------------------
121 
122         sourceDir = new File(unit, "stylesheetfile-test/artifact-stylesheetfile");
123         assertTrue(sourceDir.exists());
124         FileUtils.copyDirectoryStructure(sourceDir, localRepo);
125 
126         // ----------------------------------------------------------------------
127         // helpfile-test
128         // ----------------------------------------------------------------------
129 
130         sourceDir = new File(unit, "helpfile-test/artifact-helpfile");
131         assertTrue(sourceDir.exists());
132         FileUtils.copyDirectoryStructure(sourceDir, localRepo);
133 
134         // Remove SCM files
135         List<String> files = FileUtils.getFileAndDirectoryNames(
136                 localRepo, FileUtils.getDefaultExcludesAsString(), null, true, true, true, true);
137         for (String filename : files) {
138             File file = new File(filename);
139 
140             if (file.isDirectory()) {
141                 FileUtils.deleteDirectory(file);
142             } else {
143                 file.delete();
144             }
145         }
146 
147         TEST_REPO_CREATED = true;
148     }
149 
150     /**
151      * Convenience method that reads the contents of the specified file object into a string with a <code>space</code>
152      * as line separator.
153      *
154      * @see #LINE_SEPARATOR
155      * @param file the file to be read
156      * @return a String object that contains the contents of the file
157      * @throws IOException if any
158      */
159     private static String readFile(File file) throws IOException {
160         StringBuilder str = new StringBuilder((int) file.length());
161 
162         try (BufferedReader in = new BufferedReader(new FileReader(file))) {
163 
164             for (String strTmp; (strTmp = in.readLine()) != null; ) {
165                 str.append(LINE_SEPARATOR);
166                 str.append(strTmp);
167             }
168         }
169 
170         return str.toString();
171     }
172 
173     /**
174      * Method to test the aggregate parameter
175      *
176      * @throws Exception if any
177      */
178     public void testAggregate() throws Exception {
179         File testPom = new File(unit, "aggregate-test/aggregate-test-plugin-config.xml");
180         JavadocReport mojo = lookupMojo(testPom);
181         mojo.execute();
182 
183         File apidocs = new File(getBasedir(), "target/test/unit/aggregate-test/target/site/apidocs/");
184 
185         // check if project1 api files exist
186         assertTrue(new File(apidocs, "aggregate/test/project1/Project1App.html").exists());
187         assertTrue(new File(apidocs, "aggregate/test/project1/Project1AppSample.html").exists());
188         assertTrue(new File(apidocs, "aggregate/test/project1/Project1Sample.html").exists());
189         assertTrue(new File(apidocs, "aggregate/test/project1/Project1Test.html").exists());
190 
191         // check if project2 api files exist
192         assertTrue(new File(apidocs, "aggregate/test/project2/Project2App.html").exists());
193         assertTrue(new File(apidocs, "aggregate/test/project2/Project2AppSample.html").exists());
194         assertTrue(new File(apidocs, "aggregate/test/project2/Project2Sample.html").exists());
195         assertTrue(new File(apidocs, "aggregate/test/project2/Project2Test.html").exists());
196     }
197 
198     /**
199      * Test the javadoc resources in the aggregation case.
200      *
201      * @throws Exception if any
202      */
203     public void testAggregateJavadocResources() throws Exception {
204         File testPom = new File(unit, "aggregate-resources-test/aggregate-resources-test-plugin-config.xml");
205         JavadocReport mojo = lookupMojo(testPom);
206         mojo.execute();
207 
208         File apidocs = new File(getBasedir(), "target/test/unit/aggregate-resources-test/target/site/apidocs");
209 
210         // Test overview
211         File overviewSummary = getOverviewSummary(apidocs);
212 
213         assertTrue(overviewSummary.exists());
214         String overview = readFile(overviewSummary).toLowerCase(Locale.ENGLISH);
215         assertTrue(overview.contains("<a href=\"resources/test/package-summary.html\">resources.test</a>"));
216         assertTrue(overview.contains(">blabla</"));
217         assertTrue(overview.contains("<a href=\"resources/test2/package-summary.html\">resources.test2</a>"));
218         assertTrue(overview.contains("<a href=\"resources2/test/package-summary.html\">resources2.test</a>"));
219         assertTrue(overview.contains("<a href=\"resources2/test2/package-summary.html\">resources2.test2</a>"));
220 
221         // Test doc-files
222         File app = new File(apidocs, "resources/test/App.html");
223         assertTrue(app.exists());
224         overview = readFile(app);
225         assertTrue(overview.contains("<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">"));
226         assertTrue(new File(apidocs, "resources/test/doc-files/maven-feather.png").exists());
227     }
228 
229     public void testAggregateWithModulsNotInSubFolders() throws Exception {
230         File testPom = new File(unit, "aggregate-modules-not-in-subfolders-test/all/pom.xml");
231         JavadocReport mojo = lookupMojo(testPom);
232         mojo.execute();
233 
234         File apidocs =
235                 new File(getBasedir(), "target/test/unit/aggregate-modules-not-in-subfolders-test/target/site/apidocs");
236         assertTrue(apidocs.isDirectory());
237         assertTrue(getOverviewSummary(apidocs).isFile());
238     }
239 
240     private static File getOverviewSummary(File apidocs) {
241         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("11")) {
242             return new File(apidocs, "overview-summary.html");
243         }
244         return new File(apidocs, "index.html");
245     }
246 }