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