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.File;
22  import java.io.IOException;
23  import java.net.HttpURLConnection;
24  import java.net.URL;
25  import java.nio.charset.Charset;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.nio.file.Paths;
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Objects;
35  
36  import org.apache.commons.lang3.StringUtils;
37  import org.apache.maven.execution.MavenSession;
38  import org.apache.maven.model.Plugin;
39  import org.apache.maven.plugin.LegacySupport;
40  import org.apache.maven.plugin.MojoExecution;
41  import org.apache.maven.plugin.MojoExecutionException;
42  import org.apache.maven.plugin.MojoFailureException;
43  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
44  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
45  import org.apache.maven.plugins.javadoc.ProxyServer.AuthAsyncProxyServlet;
46  import org.apache.maven.project.MavenProject;
47  import org.apache.maven.project.ProjectBuildingRequest;
48  import org.apache.maven.project.ProjectBuildingRequest.RepositoryMerging;
49  import org.apache.maven.settings.Proxy;
50  import org.apache.maven.settings.Settings;
51  import org.apache.maven.shared.utils.io.FileUtils;
52  import org.codehaus.plexus.languages.java.version.JavaVersion;
53  import org.eclipse.aether.DefaultRepositorySystemSession;
54  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
55  import org.eclipse.aether.repository.LocalRepository;
56  import org.hamcrest.MatcherAssert;
57  import org.junit.AssumptionViolatedException;
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  import static org.apache.commons.io.FileUtils.copyDirectory;
62  import static org.apache.commons.io.FileUtils.deleteDirectory;
63  import static org.assertj.core.api.Assertions.assertThat;
64  import static org.hamcrest.CoreMatchers.anyOf;
65  import static org.hamcrest.CoreMatchers.containsString;
66  import static org.hamcrest.CoreMatchers.is;
67  import static org.junit.Assume.assumeThat;
68  import static org.mockito.Mockito.mock;
69  import static org.mockito.Mockito.spy;
70  import static org.mockito.Mockito.when;
71  
72  /**
73   * Test {@link org.apache.maven.plugins.javadoc.JavadocReport} class.
74   *
75   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
76   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
77   */
78  public class JavadocReportTest extends AbstractMojoTestCase {
79  
80      private static final char LINE_SEPARATOR = ' ';
81  
82      public static final String OPTIONS_UMLAUT_ENCODING = "Options Umlaut Encoding ö ä ü ß";
83  
84      private Path unit;
85  
86      private File localRepo;
87  
88      private static final Logger LOGGER = LoggerFactory.getLogger(JavadocReportTest.class);
89  
90      @Override
91      protected void setUp() throws Exception {
92          super.setUp();
93  
94          Path tempDirectory = Files.createTempDirectory("JavadocReportTest");
95          localRepo = tempDirectory.resolve(Paths.get("target/local-repo/")).toFile();
96          unit = new File(getBasedir(), "src/test/resources/unit").toPath();
97  
98          createTestRepo();
99      }
100 
101     @Override
102     protected void tearDown() throws Exception {
103         try {
104             deleteDirectory(localRepo);
105         } catch (IOException ex) {
106             // CI servers can have problems deleting files.
107             // It will get cleared out eventually, and since
108             // temporary directories have unique names,
109             // it shouldn't affect subsequent tests.
110         }
111 
112         super.tearDown();
113     }
114 
115     private JavadocReport lookupMojo(Path testPom) throws Exception {
116         JavadocReport mojo = (JavadocReport) lookupMojo("javadoc", testPom.toFile());
117 
118         Plugin p = new Plugin();
119         p.setGroupId("org.apache.maven.plugins");
120         p.setArtifactId("maven-javadoc-plugin");
121         MojoExecution mojoExecution = new MojoExecution(p, "javadoc", null);
122 
123         setVariableValueToObject(mojo, "mojoExecution", mojoExecution);
124 
125         MavenProject currentProject = new MavenProjectStub();
126         currentProject.setGroupId("GROUPID");
127         currentProject.setArtifactId("ARTIFACTID");
128 
129         List<MavenProject> reactorProjects =
130                 mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
131         MavenSession session = newMavenSession(currentProject);
132         setVariableValueToObject(mojo, "session", session);
133         setVariableValueToObject(mojo, "repoSession", session.getRepositorySession());
134         setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
135         return mojo;
136     }
137 
138     /**
139      * Create test repository in target directory.
140      *
141      * @throws IOException if any
142      */
143     private void createTestRepo() throws IOException {
144         assertTrue(localRepo.mkdirs());
145 
146         // ----------------------------------------------------------------------
147         // UMLGraph
148         // ----------------------------------------------------------------------
149 
150         Path sourceDir = unit.resolve("doclet-test/artifact-doclet");
151         assertThat(sourceDir).exists();
152         copyDirectory(sourceDir.toFile(), localRepo);
153 
154         // ----------------------------------------------------------------------
155         // UMLGraph-bis
156         // ----------------------------------------------------------------------
157 
158         sourceDir = unit.resolve("doclet-path-test/artifact-doclet");
159         assertThat(sourceDir).exists();
160         copyDirectory(sourceDir.toFile(), localRepo);
161 
162         // ----------------------------------------------------------------------
163         // commons-attributes-compiler
164         // https://www.tullmann.org/pat/taglets/
165         // ----------------------------------------------------------------------
166 
167         sourceDir = unit.resolve("taglet-test/artifact-taglet");
168         assertThat(sourceDir).exists();
169         copyDirectory(sourceDir.toFile(), localRepo);
170 
171         // ----------------------------------------------------------------------
172         // stylesheetfile-test
173         // ----------------------------------------------------------------------
174 
175         sourceDir = unit.resolve("stylesheetfile-test/artifact-stylesheetfile");
176         assertThat(sourceDir).exists();
177         copyDirectory(sourceDir.toFile(), localRepo);
178 
179         // ----------------------------------------------------------------------
180         // helpfile-test
181         // ----------------------------------------------------------------------
182 
183         sourceDir = unit.resolve("helpfile-test/artifact-helpfile");
184         assertThat(sourceDir).exists();
185         copyDirectory(sourceDir.toFile(), localRepo);
186 
187         // Remove SCM files
188         List<String> files = FileUtils.getFileAndDirectoryNames(
189                 localRepo, FileUtils.getDefaultExcludesAsString(), null, true, true, true, true);
190         for (String filename : files) {
191             File file = new File(filename);
192 
193             if (file.isDirectory()) {
194                 deleteDirectory(file);
195             } else {
196                 file.delete();
197             }
198         }
199     }
200 
201     /**
202      * Convenience method that reads the contents of the specified file object into a string with a
203      * <code>space</code> as line separator.
204      *
205      * @see #LINE_SEPARATOR
206      * @param file the file to be read
207      * @return a String object that contains the contents of the file
208      * @throws IOException if any
209      */
210     private static String readFile(Path file) throws IOException {
211         return readFile(file, StandardCharsets.UTF_8);
212     }
213 
214     /**
215      * Convenience method that reads the contents of the specified file object into a string with a
216      * <code>space</code> as line separator.
217      *
218      * @see #LINE_SEPARATOR
219      * @param file the file to be read
220      * @param cs charset to use
221      * @return a String object that contains the contents of the file
222      * @throws IOException if any
223      */
224     private static String readFile(Path file, Charset cs) throws IOException {
225         StringBuilder str = new StringBuilder((int) Files.size(file));
226 
227         for (String strTmp : Files.readAllLines(file, cs)) {
228             str.append(LINE_SEPARATOR);
229             str.append(strTmp);
230         }
231 
232         return str.toString();
233     }
234 
235     /**
236      * Test when default configuration is provided for the plugin
237      *
238      * @throws Exception if any
239      */
240     public void testDefaultConfiguration() throws Exception {
241         Path testPom = unit.resolve("default-configuration/default-configuration-plugin-config.xml");
242         JavadocReport mojo = lookupMojo(testPom);
243         mojo.execute();
244 
245         // package level generated javadoc files
246         Path apidocs = new File(getBasedir(), "target/test/unit/default-configuration/target/site/apidocs").toPath();
247 
248         String appHtml = "def/configuration/App.html";
249         Path generatedFile = apidocs.resolve(appHtml);
250         assertThat(generatedFile).exists();
251 
252         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("16")) {
253             String url = Objects.requireNonNull(mojo.getDefaultJavadocApiLink()).getUrl();
254             HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
255             connection.setRequestMethod("HEAD");
256             try {
257                 // only test when URL can be reached
258                 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
259                     try {
260                         assumeThat(connection.getURL().toString(), is(url));
261 
262                         // https://bugs.openjdk.java.net/browse/JDK-8216497
263                         MatcherAssert.assertThat(
264                                 url + " available, but " + appHtml + " is missing link to java.lang.Object",
265                                 new String(Files.readAllBytes(generatedFile), StandardCharsets.UTF_8),
266                                 anyOf(
267                                         containsString("/docs/api/java/lang/Object.html"),
268                                         containsString("/docs/api/java.base/java/lang/Object.html")));
269                     } catch (AssumptionViolatedException e) {
270                         LOGGER.warn("ignoring defaultAPI check: {}", e.getMessage());
271                     }
272                 }
273             } catch (Exception e) {
274                 LOGGER.error("error connecting to javadoc URL: {}", url);
275                 throw e;
276             }
277         } else {
278             MatcherAssert.assertThat(
279                     new String(Files.readAllBytes(generatedFile), StandardCharsets.UTF_8),
280                     containsString("/docs/api/java.base/java/lang/Object.html"));
281         }
282 
283         assertThat(apidocs.resolve("def/configuration/AppSample.html")).exists();
284         assertThat(apidocs.resolve("def/configuration/package-summary.html")).exists();
285         assertThat(apidocs.resolve("def/configuration/package-tree.html")).exists();
286         assertThat(apidocs.resolve("def/configuration/package-use.html")).exists();
287 
288         // package-frame and allclasses-(no)frame not generated anymore since Java 11
289         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("11")) {
290             assertThat(apidocs.resolve("def/configuration/package-frame.html")).exists();
291             assertThat(apidocs.resolve("allclasses-frame.html"))
292                     .exists()
293                     .content()
294                     .containsOnlyOnce("def/configuration/App.html")
295                     .containsOnlyOnce("def/configuration/AppSample.html");
296             assertThat(apidocs.resolve("allclasses-noframe.html"))
297                     .exists()
298                     .content()
299                     .containsOnlyOnce("def/configuration/App.html")
300                     .containsOnlyOnce("def/configuration/AppSample.html");
301         }
302 
303         // class level generated javadoc files
304         assertThat(apidocs.resolve("def/configuration/class-use/App.html")).exists();
305         assertThat(apidocs.resolve("def/configuration/class-use/AppSample.html"))
306                 .exists();
307 
308         // project level generated javadoc files
309         assertThat(apidocs.resolve("constant-values.html")).exists();
310         assertThat(apidocs.resolve("deprecated-list.html")).exists();
311         assertThat(apidocs.resolve("help-doc.html")).exists();
312         assertThat(apidocs.resolve("index-all.html")).exists();
313         assertThat(apidocs.resolve("index.html")).exists();
314         assertThat(apidocs.resolve("overview-tree.html")).exists();
315         assertThat(apidocs.resolve("stylesheet.css")).exists();
316 
317         if (JavaVersion.JAVA_VERSION.isAtLeast("10")) {
318             assertThat(apidocs.resolve("element-list")).exists();
319         } else {
320             assertThat(apidocs.resolve("package-list")).exists();
321         }
322     }
323 
324     /**
325      * Method for testing the subpackages and excludePackageNames parameter
326      *
327      * @throws Exception if any
328      */
329     public void testSubpackages() throws Exception {
330         Path testPom = unit.resolve("subpackages-test/subpackages-test-plugin-config.xml");
331         JavadocReport mojo = lookupMojo(testPom);
332         mojo.execute();
333 
334         Path apidocs = new File(getBasedir(), "target/test/unit/subpackages-test/target/site/apidocs").toPath();
335 
336         // check the excluded packages
337         assertThat(apidocs.resolve("subpackages/test/excluded")).doesNotExist();
338         assertThat(apidocs.resolve("subpackages/test/included/exclude")).doesNotExist();
339 
340         // check if the classes in the specified subpackages were included
341         assertThat(apidocs.resolve("subpackages/test/App.html")).exists();
342         assertThat(apidocs.resolve("subpackages/test/AppSample.html")).exists();
343         assertThat(apidocs.resolve("subpackages/test/included/IncludedApp.html"))
344                 .exists();
345         assertThat(apidocs.resolve("subpackages/test/included/IncludedAppSample.html"))
346                 .exists();
347     }
348 
349     public void testIncludesExcludes() throws Exception {
350         Path testPom = unit.resolve("file-include-exclude-test/file-include-exclude-plugin-config.xml");
351         JavadocReport mojo = lookupMojo(testPom);
352         mojo.execute();
353 
354         Path apidocs =
355                 new File(getBasedir(), "target/test/unit/file-include-exclude-test/target/site/apidocs").toPath();
356 
357         // check if the classes in the specified subpackages were included
358         assertThat(apidocs.resolve("subpackages/test/App.html")).exists();
359         assertThat(apidocs.resolve("subpackages/test/AppSample.html")).exists();
360         assertThat(apidocs.resolve("subpackages/test/included/IncludedApp.html"))
361                 .exists();
362         assertThat(apidocs.resolve("subpackages/test/included/IncludedAppSample.html"))
363                 .exists();
364         assertThat(apidocs.resolve("subpackages/test/PariahApp.html")).doesNotExist();
365     }
366 
367     /**
368      * Test the recursion and exclusion of the doc-files subdirectories.
369      *
370      * @throws Exception if any
371      */
372     public void testDocfiles() throws Exception {
373         // Should be an assumption, but not supported by TestCase
374         // Seems like a bug in Javadoc 9 and above
375         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")) {
376             return;
377         }
378 
379         Path testPom = unit.resolve("docfiles-test/docfiles-test-plugin-config.xml");
380         JavadocReport mojo = lookupMojo(testPom);
381         mojo.execute();
382 
383         Path apidocs = new File(getBasedir(), "target/test/unit/docfiles-test/target/site/apidocs/").toPath();
384 
385         // check if the doc-files subdirectories were copied
386         assertThat(apidocs.resolve("docfiles/test/doc-files")).exists();
387         assertThat(apidocs.resolve("docfiles/test/doc-files/included-dir1/sample-included1.gif"))
388                 .exists();
389         assertThat(apidocs.resolve("docfiles/test/doc-files/included-dir2/sample-included2.gif"))
390                 .exists();
391         assertThat(apidocs.resolve("docfiles/test/doc-files/excluded-dir1")).doesNotExist();
392         assertThat(apidocs.resolve("docfiles/test/doc-files/excluded-dir2")).doesNotExist();
393 
394         testPom = unit.resolve("docfiles-with-java-test/docfiles-with-java-test-plugin-config.xml");
395         mojo = lookupMojo(testPom);
396         mojo.execute();
397     }
398 
399     /**
400      * Test javadoc plugin using custom configuration. noindex, notree and nodeprecated parameters
401      * were set to true.
402      *
403      * @throws Exception if any
404      */
405     public void testCustomConfiguration() throws Exception {
406         Path testPom = unit.resolve("custom-configuration/custom-configuration-plugin-config.xml");
407         JavadocReport mojo = lookupMojo(testPom);
408         mojo.execute();
409 
410         Path apidocs = new File(getBasedir(), "target/test/unit/custom-configuration/target/site/apidocs").toPath();
411 
412         // check if there is a tree page generated (notree == true)
413         assertThat(apidocs.resolve("overview-tree.html")).doesNotExist();
414         assertThat(apidocs.resolve("custom/configuration/package-tree.html")).doesNotExist();
415 
416         // check if the main index page was generated (noindex == true)
417         assertThat(apidocs.resolve("index-all.html")).doesNotExist();
418 
419         // check if the deprecated list and the deprecated api were generated (nodeprecated == true)
420         // @todo Fix: the class-use of the deprecated api is still created eventhough the deprecated api of that class
421         // is no longer generated
422         assertThat(apidocs.resolve("deprecated-list.html")).doesNotExist();
423         assertThat(apidocs.resolve("custom/configuration/App.html")).doesNotExist();
424 
425         // read the contents of the html files based on some of the parameter values
426         // author == false
427         String str = readFile(apidocs.resolve("custom/configuration/AppSample.html"));
428         assertFalse(str.toLowerCase().contains("author"));
429 
430         // bottom
431         assertTrue(str.toUpperCase().contains("SAMPLE BOTTOM CONTENT"));
432 
433         // offlineLinks
434         if (JavaVersion.JAVA_VERSION.isBefore("11.0.2")) {
435             assertThat(str)
436                     .containsIgnoringCase("href=\"http://java.sun.com/j2se/1.4.2/docs/api/java/lang/string.html");
437         } else {
438             assertTrue(str.toLowerCase()
439                     .contains("href=\"http://java.sun.com/j2se/1.4.2/docs/api/java.base/java/lang/string.html"));
440         }
441 
442         // header
443         assertTrue(str.toUpperCase().contains("MAVEN JAVADOC PLUGIN TEST"));
444 
445         // footer
446         if (JavaVersion.JAVA_VERSION.isBefore("16-ea")
447                 && !System.getProperty("java.vm.name").contains("OpenJ9")) {
448             assertTrue(str.toUpperCase().contains("MAVEN JAVADOC PLUGIN TEST FOOTER"));
449         }
450 
451         // nohelp == true
452         assertFalse(str.toUpperCase().contains("/HELP-DOC.HTML"));
453 
454         // check the wildcard (*) package exclusions -- excludePackageNames parameter
455         assertThat(apidocs.resolve("custom/configuration/exclude1/Exclude1App.html"))
456                 .exists();
457         assertThat(apidocs.resolve("custom/configuration/exclude1/subexclude/SubexcludeApp.html"))
458                 .doesNotExist();
459         assertThat(apidocs.resolve("custom/configuration/exclude2/Exclude2App.html"))
460                 .doesNotExist();
461 
462         assertThat(apidocs.resolve("options")).isRegularFile();
463 
464         String contentOptions = new String(Files.readAllBytes(apidocs.resolve("options")), StandardCharsets.UTF_8);
465 
466         assertNotNull(contentOptions);
467         assertThat(contentOptions).contains("-link").contains("http://java.sun.com/j2se/");
468     }
469 
470     /**
471      * Method to test the doclet artifact configuration
472      *
473      * @throws Exception if any
474      */
475     public void testDoclets() throws Exception {
476         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("13")) {
477             // As of JDK 13, the com.sun.javadoc API is no longer supported.
478             return;
479         }
480 
481         // ----------------------------------------------------------------------
482         // doclet-test: check if the file generated by UmlGraph exists and if
483         // doclet path contains the UmlGraph artifact
484         // ----------------------------------------------------------------------
485 
486         Path testPom = unit.resolve("doclet-test/doclet-test-plugin-config.xml");
487         JavadocReport mojo = lookupMojo(testPom);
488 
489         MavenSession session = spy(newMavenSession(mojo.project));
490         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
491         when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
492         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
493         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
494         repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
495                 .newInstance(repositorySession, new LocalRepository(localRepo)));
496         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
497         when(session.getRepositorySession()).thenReturn(repositorySession);
498         LegacySupport legacySupport = lookup(LegacySupport.class);
499         legacySupport.setSession(session);
500 
501         setVariableValueToObject(mojo, "session", session);
502         setVariableValueToObject(mojo, "repoSession", repositorySession);
503         mojo.execute();
504 
505         Path generatedFile =
506                 new File(getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot").toPath();
507         assertThat(generatedFile).exists();
508 
509         Path optionsFile = new File(mojo.getOutputDirectory(), "options").toPath();
510         assertThat(optionsFile).exists();
511         String options = readFile(optionsFile);
512         assertThat(options).contains("/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar");
513 
514         // ----------------------------------------------------------------------
515         // doclet-path: check if the file generated by UmlGraph exists and if
516         // doclet path contains the twice UmlGraph artifacts
517         // ----------------------------------------------------------------------
518 
519         testPom = unit.resolve("doclet-path-test/doclet-path-test-plugin-config.xml");
520         mojo = lookupMojo(testPom);
521         setVariableValueToObject(mojo, "session", session);
522         setVariableValueToObject(mojo, "repoSession", repositorySession);
523         mojo.execute();
524 
525         generatedFile = new File(getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot").toPath();
526         assertThat(generatedFile).exists();
527 
528         optionsFile = new File(mojo.getOutputDirectory(), "options").toPath();
529         assertThat(optionsFile).exists();
530         options = readFile(optionsFile);
531         assertThat(options)
532                 .contains("/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar")
533                 .contains("/target/local-repo/umlgraph/UMLGraph-bis/2.1/UMLGraph-bis-2.1.jar");
534     }
535 
536     /**
537      * Method to test when the path to the project sources has an apostrophe (')
538      *
539      * @throws Exception if any
540      */
541     public void testQuotedPath() throws Exception {
542         Path testPom = unit.resolve("quotedpath'test/quotedpath-test-plugin-config.xml");
543         JavadocReport mojo = lookupMojo(testPom);
544         mojo.execute();
545 
546         Path apidocs = new File(getBasedir(), "target/test/unit/quotedpath'test/target/site/apidocs").toPath();
547 
548         // package level generated javadoc files
549         assertThat(apidocs.resolve("quotedpath/test/App.html")).exists();
550         assertThat(apidocs.resolve("quotedpath/test/AppSample.html")).exists();
551 
552         // project level generated javadoc files
553         assertThat(apidocs.resolve("index-all.html")).exists();
554         assertThat(apidocs.resolve("index.html")).exists();
555         assertThat(apidocs.resolve("overview-tree.html")).exists();
556         assertThat(apidocs.resolve("stylesheet.css")).exists();
557 
558         if (JavaVersion.JAVA_VERSION.isBefore("10")) {
559             assertThat(apidocs.resolve("package-list")).exists();
560         } else {
561             assertThat(apidocs.resolve("element-list")).exists();
562         }
563     }
564 
565     /**
566      * Method to test when the options file has umlauts.
567      *
568      * @throws Exception if any
569      */
570     public void testOptionsUmlautEncoding() throws Exception {
571         Path testPom = unit.resolve("optionsumlautencoding-test/optionsumlautencoding-test-plugin-config.xml");
572         JavadocReport mojo = lookupMojo(testPom);
573         mojo.execute();
574 
575         Path optionsFile = new File(mojo.getOutputDirectory(), "options").toPath();
576         assertThat(optionsFile).exists();
577 
578         // check for a part of the window title
579         String content;
580         String expected;
581         if (JavaVersion.JAVA_VERSION.isAtLeast("9") && JavaVersion.JAVA_VERSION.isBefore("12")) {
582             content = readFile(optionsFile, StandardCharsets.UTF_8);
583             expected = OPTIONS_UMLAUT_ENCODING;
584         } else {
585             content = readFile(optionsFile, Charset.defaultCharset());
586             expected = new String(OPTIONS_UMLAUT_ENCODING.getBytes(Charset.defaultCharset()));
587         }
588 
589         assertThat(content).contains(expected);
590 
591         Path apidocs =
592                 new File(getBasedir(), "target/test/unit/optionsumlautencoding-test/target/site/apidocs").toPath();
593 
594         // package level generated javadoc files
595         assertThat(apidocs.resolve("optionsumlautencoding/test/App.html")).exists();
596         assertThat(apidocs.resolve("optionsumlautencoding/test/AppSample.html")).exists();
597 
598         // project level generated javadoc files
599         assertThat(apidocs.resolve("index-all.html")).exists();
600         assertThat(apidocs.resolve("index.html")).exists();
601         assertThat(apidocs.resolve("overview-tree.html")).exists();
602         assertThat(apidocs.resolve("stylesheet.css")).exists();
603 
604         if (JavaVersion.JAVA_VERSION.isBefore("10")) {
605             assertThat(apidocs.resolve("package-list")).exists();
606         } else {
607             assertThat(apidocs.resolve("element-list")).exists();
608         }
609     }
610 
611     /**
612      * Method to test the taglet artifact configuration
613      *
614      * @throws Exception if any
615      */
616     public void testTaglets() throws Exception {
617         // ----------------------------------------------------------------------
618         // taglet-test: check if a taglet is used
619         // ----------------------------------------------------------------------
620 
621         // Should be an assumption, but not supported by TestCase
622         // com.sun.tools.doclets.Taglet not supported by Java9 anymore
623         // Should be refactored with jdk.javadoc.doclet.Taglet
624         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("10")) {
625             return;
626         }
627 
628         Path testPom = unit.resolve("taglet-test/taglet-test-plugin-config.xml");
629         JavadocReport mojo = lookupMojo(testPom);
630 
631         MavenSession session = spy(newMavenSession(mojo.project));
632         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
633         when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
634         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
635         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
636         repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
637                 .newInstance(repositorySession, new LocalRepository(localRepo)));
638         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
639         when(session.getRepositorySession()).thenReturn(repositorySession);
640         LegacySupport legacySupport = lookup(LegacySupport.class);
641         legacySupport.setSession(session);
642 
643         setVariableValueToObject(mojo, "session", session);
644         setVariableValueToObject(mojo, "repoSession", repositorySession);
645 
646         mojo.execute();
647 
648         Path apidocs = new File(getBasedir(), "target/test/unit/taglet-test/target/site/apidocs").toPath();
649 
650         assertThat(apidocs.resolve("index.html")).exists();
651 
652         Path appFile = apidocs.resolve("taglet/test/App.html");
653         assertThat(appFile).exists();
654         String appString = readFile(appFile);
655         assertThat(appString).contains("<b>To Do:</b>");
656     }
657 
658     /**
659      * Method to test the jdk5 javadoc
660      *
661      * @throws Exception if any
662      */
663     public void testJdk5() throws Exception {
664         // Should be an assumption, but not supported by TestCase
665         // Java 5 not supported by Java9 anymore
666         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")) {
667             return;
668         }
669 
670         Path testPom = unit.resolve("jdk5-test/jdk5-test-plugin-config.xml");
671         JavadocReport mojo = lookupMojo(testPom);
672         mojo.execute();
673 
674         Path apidocs = new File(getBasedir(), "target/test/unit/jdk5-test/target/site/apidocs").toPath();
675 
676         assertThat(apidocs.resolve("index.html")).exists();
677 
678         Path overviewSummary = apidocs.resolve("overview-summary.html");
679         assertThat(overviewSummary).exists();
680         String content = readFile(overviewSummary);
681         assertThat(content).contains("<b>Test the package-info</b>");
682 
683         Path packageSummary = apidocs.resolve("jdk5/test/package-summary.html");
684         assertThat(packageSummary).exists();
685         content = readFile(packageSummary);
686         assertThat(content).contains("<b>Test the package-info</b>");
687     }
688 
689     /**
690      * Test to find the javadoc executable when <code>java.home</code> is not in the JDK_HOME. In this case, try to
691      * use the <code>JAVA_HOME</code> environment variable.
692      *
693      * @throws Exception if any
694      */
695     public void testToFindJavadoc() throws Exception {
696         String oldJreHome = System.getProperty("java.home");
697         System.setProperty("java.home", "foo/bar");
698 
699         Path testPom = unit.resolve("javaHome-test/javaHome-test-plugin-config.xml");
700         JavadocReport mojo = lookupMojo(testPom);
701         mojo.execute();
702 
703         System.setProperty("java.home", oldJreHome);
704     }
705 
706     /**
707      * Test the javadoc resources.
708      *
709      * @throws Exception if any
710      */
711     public void testJavadocResources() throws Exception {
712         Path testPom = unit.resolve("resources-test/resources-test-plugin-config.xml");
713         JavadocReport mojo = lookupMojo(testPom);
714         mojo.execute();
715 
716         Path apidocs = new File(getBasedir(), "target/test/unit/resources-test/target/site/apidocs/").toPath();
717 
718         Path app = apidocs.resolve("resources/test/App.html");
719         assertThat(app).exists();
720         String content = readFile(app);
721         assertThat(content).contains("<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">");
722         assertThat(apidocs.resolve("resources/test/doc-files/maven-feather.png"))
723                 .exists();
724 
725         Path app2 = apidocs.resolve("resources/test2/App2.html");
726         assertThat(app2).exists();
727         content = readFile(app2);
728         assertThat(content).contains("<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">");
729         assertThat(apidocs.resolve("resources/test2/doc-files/maven-feather.png"))
730                 .doesNotExist();
731 
732         // with excludes
733         testPom = unit.resolve("resources-with-excludes-test/resources-with-excludes-test-plugin-config.xml");
734         mojo = lookupMojo(testPom);
735         mojo.execute();
736 
737         apidocs = new File(getBasedir(), "target/test/unit/resources-with-excludes-test/target/site/apidocs").toPath();
738 
739         app = apidocs.resolve("resources/test/App.html");
740         assertThat(app).exists();
741         content = readFile(app);
742         assertThat(content).contains("<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">");
743 
744         JavaVersion javadocVersion = (JavaVersion) getVariableValueFromObject(mojo, "javadocRuntimeVersion");
745         if (javadocVersion.isAtLeast("1.8") /* && javadocVersion.isBefore( "14" ) */) {
746             // https://bugs.openjdk.java.net/browse/JDK-8032205
747             assertThat(apidocs.resolve("resources/test/doc-files/maven-feather.png"))
748                     .as("Javadoc runtime version: " + javadocVersion
749                             + "\nThis bug appeared in JDK8 and was planned to be fixed in JDK9, see JDK-8032205")
750                     .exists();
751         } else {
752             assertThat(apidocs.resolve("resources/test/doc-files/maven-feather.png"))
753                     .doesNotExist();
754         }
755         assertThat(apidocs.resolve("resources/test2/doc-files/maven-feather.png"))
756                 .exists();
757 
758         app2 = apidocs.resolve("resources/test2/App2.html");
759         assertThat(app2).exists();
760         content = readFile(app2);
761         assertThat(content).contains("<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">");
762         assertThat(apidocs.resolve("resources/test2/doc-files/maven-feather.png"))
763                 .exists();
764     }
765 
766     /**
767      * Test the javadoc for a POM project.
768      *
769      * @throws Exception if any
770      */
771     public void testPom() throws Exception {
772         Path testPom = unit.resolve("pom-test/pom-test-plugin-config.xml");
773         JavadocReport mojo = lookupMojo(testPom);
774         mojo.execute();
775 
776         assertThat(new File(getBasedir(), "target/test/unit/pom-test/target/site"))
777                 .doesNotExist();
778     }
779 
780     /**
781      * Test the javadoc with tag.
782      *
783      * @throws Exception if any
784      */
785     public void testTag() throws Exception {
786         Path testPom = unit.resolve("tag-test/tag-test-plugin-config.xml");
787         JavadocReport mojo = lookupMojo(testPom);
788         mojo.execute();
789 
790         Path app = new File(getBasedir(), "target/test/unit/tag-test/target/site/apidocs/tag/test/App.html").toPath();
791         assertThat(app).exists();
792         String readed = readFile(app);
793         assertThat(readed).contains(">To do something:</").contains(">Generator Class:</");
794 
795         // In javadoc-options-javadoc-resources.xml tag 'version' has only a name,
796         // which is not enough for Java 11 anymore
797         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("11")) {
798             assertThat(readed).contains(">Version:</");
799             assertTrue(readed.toLowerCase().contains("</dt>" + LINE_SEPARATOR + "  <dd>1.0</dd>")
800                     || readed.toLowerCase().contains("</dt>" + LINE_SEPARATOR + "<dd>1.0</dd>" /* JDK 8 */));
801         }
802     }
803 
804     /**
805      * Test newline in the header/footer parameter
806      *
807      * @throws Exception if any
808      */
809     public void testHeaderFooter() throws Exception {
810         Path testPom = unit.resolve("header-footer-test/header-footer-test-plugin-config.xml");
811         JavadocReport mojo = lookupMojo(testPom);
812         try {
813             mojo.execute();
814         } catch (MojoExecutionException e) {
815             fail("Doesnt handle correctly newline for header or footer parameter");
816         }
817     }
818 
819     /**
820      * Test newline in various string parameters
821      *
822      * @throws Exception if any
823      */
824     public void testNewline() throws Exception {
825         Path testPom = unit.resolve("newline-test/newline-test-plugin-config.xml");
826         JavadocReport mojo = lookupMojo(testPom);
827         try {
828             mojo.execute();
829         } catch (MojoExecutionException e) {
830             fail("Doesn't handle correctly newline for string parameters. See options and packages files.");
831         }
832     }
833 
834     /**
835      * Method to test the jdk6 javadoc
836      *
837      * @throws Exception if any
838      */
839     public void testJdk6() throws Exception {
840         // Should be an assumption, but not supported by TestCase
841         // Java 6 not supported by Java 12 anymore
842         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("12")) {
843             return;
844         }
845 
846         Path testPom = unit.resolve("jdk6-test/jdk6-test-plugin-config.xml");
847         JavadocReport mojo = lookupMojo(testPom);
848         mojo.execute();
849 
850         Path apidocs = new File(getBasedir(), "target/test/unit/jdk6-test/target/site/apidocs").toPath();
851         assertThat(apidocs.resolve("index.html")).exists();
852 
853         Path overview;
854         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("11")) {
855             overview = apidocs.resolve("overview-summary.html");
856         } else {
857             overview = apidocs.resolve("index.html");
858         }
859 
860         assertThat(overview).exists();
861         String content = readFile(overview);
862         assertThat(content)
863                 .contains("Top - Copyright &#169; All rights reserved.")
864                 .contains("Header - Copyright &#169; All rights reserved.");
865         // IBM dist of adopt-openj9 does not support the footer param
866         if (!System.getProperty("java.vm.name").contains("OpenJ9")) {
867             assertThat(content).contains("Footer - Copyright &#169; All rights reserved.");
868         }
869 
870         Path packageSummary = apidocs.resolve("jdk6/test/package-summary.html");
871         assertThat(packageSummary).exists();
872         content = readFile(packageSummary);
873         assertThat(content)
874                 .contains("Top - Copyright &#169; All rights reserved.")
875                 .contains("Header - Copyright &#169; All rights reserved.");
876 
877         // IBM dist of adopt-openj9 does not support the footer param
878         if (!System.getProperty("java.vm.name").contains("OpenJ9")) {
879             assertThat(content).contains("Footer - Copyright &#169; All rights reserved.");
880         }
881     }
882 
883     /**
884      * Method to test proxy support in the javadoc
885      *
886      * @throws Exception if any
887      */
888     public void testProxy() throws Exception {
889         Settings settings = new Settings();
890         Proxy proxy = new Proxy();
891 
892         // dummy proxy
893         proxy.setActive(true);
894         proxy.setHost("127.0.0.1");
895         proxy.setPort(80);
896         proxy.setProtocol("http");
897         proxy.setUsername("toto");
898         proxy.setPassword("toto");
899         proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
900         settings.addProxy(proxy);
901 
902         Path testPom =
903                 new File(getBasedir(), "src/test/resources/unit/proxy-test/proxy-test-plugin-config.xml").toPath();
904         JavadocReport mojo = lookupMojo(testPom);
905 
906         MavenSession session = spy(newMavenSession(mojo.project));
907         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
908         when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
909         when(buildingRequest.getRepositoryMerging()).thenReturn(RepositoryMerging.POM_DOMINANT);
910         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
911         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
912         repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
913                 .newInstance(repositorySession, new LocalRepository(localRepo)));
914         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
915         when(session.getRepositorySession()).thenReturn(repositorySession);
916         LegacySupport legacySupport = lookup(LegacySupport.class);
917         legacySupport.setSession(session);
918 
919         setVariableValueToObject(mojo, "settings", settings);
920         setVariableValueToObject(mojo, "session", session);
921         setVariableValueToObject(mojo, "repoSession", repositorySession);
922         mojo.execute();
923 
924         Path commandLine = new File(
925                         getBasedir(),
926                         "target/test/unit/proxy-test/target/site/apidocs/javadoc."
927                                 + (SystemUtils.IS_OS_WINDOWS ? "bat" : "sh"))
928                 .toPath();
929         assertThat(commandLine).exists();
930         String readed = readFile(commandLine);
931         assertThat(readed).contains("-J-Dhttp.proxyHost=127.0.0.1").contains("-J-Dhttp.proxyPort=80");
932         if (SystemUtils.IS_OS_WINDOWS) {
933             assertThat(readed).contains(" -J-Dhttp.nonProxyHosts=\"www.google.com^|*.somewhere.com\" ");
934         } else {
935             assertThat(readed).contains(" \"-J-Dhttp.nonProxyHosts=\\\"www.google.com^|*.somewhere.com\\\"\" ");
936         }
937 
938         Path options = new File(getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/options").toPath();
939         assertThat(options).exists();
940         String optionsContent = readFile(options);
941         // NO -link expected
942         assertThat(optionsContent).doesNotContain("-link");
943 
944         // real proxy
945         ProxyServer proxyServer = null;
946         AuthAsyncProxyServlet proxyServlet;
947         try {
948             proxyServlet = new AuthAsyncProxyServlet();
949             proxyServer = new ProxyServer(proxyServlet);
950             proxyServer.start();
951 
952             settings = new Settings();
953             proxy = new Proxy();
954             proxy.setActive(true);
955             proxy.setHost(proxyServer.getHostName());
956             proxy.setPort(proxyServer.getPort());
957             proxy.setProtocol("http");
958             settings.addProxy(proxy);
959 
960             mojo = lookupMojo(testPom);
961             setVariableValueToObject(mojo, "settings", settings);
962             setVariableValueToObject(mojo, "session", session);
963             setVariableValueToObject(mojo, "repoSession", repositorySession);
964             mojo.execute();
965             readed = readFile(commandLine);
966             assertTrue(readed.contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName()));
967             assertTrue(readed.contains("-J-Dhttp.proxyPort=" + proxyServer.getPort()));
968 
969             optionsContent = readFile(options);
970             // -link expected
971             // TODO: This got disabled for now!
972             // This test fails since the last commit but I actually think it only ever worked by accident.
973             // It did rely on a commons-logging-1.0.4.pom which got resolved by a test which did run previously.
974             // But after updating to commons-logging.1.1.1 there is no pre-resolved artifact available in
975             // target/local-repo anymore, thus the javadoc link info cannot get built and the test fails
976             // I'll for now just disable this line of code, because the test as far as I can see _never_
977             // did go upstream. The remoteRepository list used is always empty!.
978             //
979             //            assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
980         } finally {
981             if (proxyServer != null) {
982                 proxyServer.stop();
983             }
984         }
985 
986         // auth proxy
987         Map<String, String> authentications = new HashMap<>();
988         authentications.put("foo", "bar");
989         try {
990             proxyServlet = new AuthAsyncProxyServlet(authentications);
991             proxyServer = new ProxyServer(proxyServlet);
992             proxyServer.start();
993 
994             settings = new Settings();
995             proxy = new Proxy();
996             proxy.setActive(true);
997             proxy.setHost(proxyServer.getHostName());
998             proxy.setPort(proxyServer.getPort());
999             proxy.setProtocol("http");
1000             proxy.setUsername("foo");
1001             proxy.setPassword("bar");
1002             settings.addProxy(proxy);
1003 
1004             mojo = lookupMojo(testPom);
1005             setVariableValueToObject(mojo, "settings", settings);
1006             setVariableValueToObject(mojo, "session", session);
1007             setVariableValueToObject(mojo, "repoSession", repositorySession);
1008             mojo.execute();
1009             readed = readFile(commandLine);
1010             assertThat(readed)
1011                     .contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName())
1012                     .contains("-J-Dhttp.proxyPort=" + proxyServer.getPort());
1013 
1014             optionsContent = readFile(options);
1015             // -link expected
1016             // see comment above (line 829)
1017             //             assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
1018         } finally {
1019             if (proxyServer != null) {
1020                 proxyServer.stop();
1021             }
1022         }
1023     }
1024 
1025     /**
1026      * Method to test error or conflict in Javadoc options and in standard doclet options.
1027      *
1028      * @throws Exception if any
1029      */
1030     public void testValidateOptions() throws Exception {
1031         // encoding
1032         Path testPom = unit.resolve("validate-options-test/wrong-encoding-test-plugin-config.xml");
1033         JavadocReport mojo = lookupMojo(testPom);
1034         try {
1035             mojo.execute();
1036             fail("No wrong encoding catch");
1037         } catch (MojoExecutionException e) {
1038             assertTrue("No wrong encoding catch", e.getMessage().contains("Unsupported option <encoding/>"));
1039         }
1040         testPom = unit.resolve("validate-options-test/wrong-docencoding-test-plugin-config.xml");
1041         mojo = lookupMojo(testPom);
1042         try {
1043             mojo.execute();
1044             fail("No wrong docencoding catch");
1045         } catch (MojoExecutionException e) {
1046             assertTrue("No wrong docencoding catch", e.getMessage().contains("Unsupported option <docencoding/>"));
1047         }
1048         testPom = unit.resolve("validate-options-test/wrong-charset-test-plugin-config.xml");
1049         mojo = lookupMojo(testPom);
1050         try {
1051             mojo.execute();
1052             fail("No wrong charset catch");
1053         } catch (MojoExecutionException e) {
1054             assertTrue("No wrong charset catch", e.getMessage().contains("Unsupported option <charset/>"));
1055         }
1056 
1057         // locale
1058         testPom = unit.resolve("validate-options-test/wrong-locale-test-plugin-config.xml");
1059         mojo = lookupMojo(testPom);
1060         try {
1061             mojo.execute();
1062             fail("No wrong locale catch");
1063         } catch (MojoExecutionException e) {
1064             assertTrue("No wrong locale catch", e.getMessage().contains("Unsupported option <locale/>"));
1065         }
1066         testPom = unit.resolve("validate-options-test/wrong-locale-with-variant-test-plugin-config.xml");
1067         mojo = lookupMojo(testPom);
1068         mojo.execute();
1069         assertTrue("No wrong locale catch", true);
1070 
1071         // conflict options
1072         testPom = unit.resolve("validate-options-test/conflict-options-test-plugin-config.xml");
1073         mojo = lookupMojo(testPom);
1074         try {
1075             mojo.execute();
1076             fail("No conflict catch");
1077         } catch (MojoExecutionException e) {
1078             assertTrue("No conflict catch", e.getMessage().contains("Option <nohelp/> conflicts with <helpfile/>"));
1079         }
1080     }
1081 
1082     /**
1083      * Method to test the <code>&lt;tagletArtifacts/&gt;</code> parameter.
1084      *
1085      * @throws Exception if any
1086      */
1087     public void testTagletArtifacts() throws Exception {
1088         // Should be an assumption, but not supported by TestCase
1089         // com.sun.tools.doclets.Taglet not supported by Java 10 anymore
1090         if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("10")) {
1091             return;
1092         }
1093 
1094         Path testPom = unit.resolve("tagletArtifacts-test/tagletArtifacts-test-plugin-config.xml");
1095         JavadocReport mojo = lookupMojo(testPom);
1096 
1097         MavenSession session = newMavenSession(mojo.project);
1098         DefaultRepositorySystemSession repoSysSession = (DefaultRepositorySystemSession) session.getRepositorySession();
1099         repoSysSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
1100                 .newInstance(session.getRepositorySession(), new LocalRepository(new File("target/local-repo"))));
1101         // Ensure remote repo connection uses SSL
1102         LegacySupport legacySupport = lookup(LegacySupport.class);
1103         legacySupport.setSession(session);
1104         setVariableValueToObject(mojo, "session", session);
1105         setVariableValueToObject(mojo, "repoSession", repoSysSession);
1106         mojo.execute();
1107 
1108         Path optionsFile = new File(mojo.getOutputDirectory(), "options").toPath();
1109         assertThat(optionsFile).exists();
1110         String options = readFile(optionsFile);
1111         // count -taglet
1112         assertThat(StringUtils.countMatches(options, LINE_SEPARATOR + "-taglet" + LINE_SEPARATOR))
1113                 .isEqualTo(3);
1114         assertThat(options)
1115                 .contains("org.codehaus.plexus.javadoc.PlexusConfigurationTaglet")
1116                 .contains("org.codehaus.plexus.javadoc.PlexusRequirementTaglet")
1117                 .contains("org.codehaus.plexus.javadoc.PlexusComponentTaglet");
1118     }
1119 
1120     /**
1121      * Method to test the <code>&lt;stylesheetfile/&gt;</code> parameter.
1122      *
1123      * @throws Exception if any
1124      */
1125     public void testStylesheetfile() throws Exception {
1126         Path testPom = unit.resolve("stylesheetfile-test/pom.xml");
1127 
1128         JavadocReport mojo = lookupMojo(testPom);
1129         assertNotNull(mojo);
1130 
1131         MavenSession session = spy(newMavenSession(mojo.project));
1132         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
1133         when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
1134         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
1135         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
1136         repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
1137                 .newInstance(repositorySession, new LocalRepository(localRepo)));
1138         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
1139         when(session.getRepositorySession()).thenReturn(repositorySession);
1140         LegacySupport legacySupport = lookup(LegacySupport.class);
1141         legacySupport.setSession(session);
1142         setVariableValueToObject(mojo, "session", session);
1143         setVariableValueToObject(mojo, "repoSession", repositorySession);
1144 
1145         Path apidocs = new File(getBasedir(), "target/test/unit/stylesheetfile-test/target/site/apidocs").toPath();
1146 
1147         Path stylesheetfile = apidocs.resolve("stylesheet.css");
1148         Path options = apidocs.resolve("options");
1149 
1150         // stylesheet == maven OR java
1151         setVariableValueToObject(mojo, "stylesheet", "javamaven");
1152 
1153         try {
1154             mojo.execute();
1155             fail();
1156         } catch (MojoExecutionException | MojoFailureException e) {
1157         }
1158 
1159         // stylesheet == java
1160         setVariableValueToObject(mojo, "stylesheet", "java");
1161         mojo.execute();
1162 
1163         String content = readFile(stylesheetfile);
1164         if (JavaVersion.JAVA_VERSION.isAtLeast("13-ea")) {
1165             assertTrue(content.contains("/*" + LINE_SEPARATOR + " * Javadoc style sheet" + LINE_SEPARATOR + " */"));
1166         } else if (JavaVersion.JAVA_VERSION.isAtLeast("10")) {
1167             assertTrue(content.contains("/* " + LINE_SEPARATOR + " * Javadoc style sheet" + LINE_SEPARATOR + " */"));
1168         } else {
1169             assertTrue(content.contains("/* Javadoc style sheet */"));
1170         }
1171 
1172         String optionsContent = readFile(options);
1173         assertFalse(optionsContent.contains("-stylesheetfile"));
1174 
1175         // stylesheetfile defined as a project resource
1176         setVariableValueToObject(mojo, "stylesheet", null);
1177         setVariableValueToObject(mojo, "stylesheetfile", "com/mycompany/app/javadoc/css/stylesheet.css");
1178         mojo.execute();
1179 
1180         content = readFile(stylesheetfile);
1181         assertTrue(content.contains("/* Custom Javadoc style sheet in project */"));
1182 
1183         optionsContent = readFile(options);
1184         assertTrue(optionsContent.contains("-stylesheetfile"));
1185         Path stylesheetResource =
1186                 unit.resolve("stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css/stylesheet.css");
1187         assertTrue(optionsContent.contains(
1188                 "'" + stylesheetResource.toFile().getAbsolutePath().replaceAll("\\\\", "/") + "'"));
1189 
1190         // stylesheetfile defined in a javadoc plugin dependency
1191         setVariableValueToObject(mojo, "stylesheetfile", "com/mycompany/app/javadoc/css2/stylesheet.css");
1192         mojo.execute();
1193 
1194         content = readFile(stylesheetfile);
1195         assertTrue(content.contains("/* Custom Javadoc style sheet in artefact */"));
1196 
1197         optionsContent = readFile(options);
1198         assertTrue(optionsContent.contains("-stylesheetfile"));
1199         assertTrue(optionsContent.contains(
1200                 "'" + stylesheetfile.toFile().getAbsolutePath().replaceAll("\\\\", "/") + "'"));
1201 
1202         // stylesheetfile defined as file
1203         Path css = unit.resolve("stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css");
1204         setVariableValueToObject(mojo, "stylesheetfile", css.toFile().getAbsolutePath());
1205         mojo.execute();
1206 
1207         content = readFile(stylesheetfile);
1208         assertTrue(content.contains("/* Custom Javadoc style sheet as file */"));
1209 
1210         optionsContent = readFile(options);
1211         assertTrue(optionsContent.contains("-stylesheetfile"));
1212         stylesheetResource =
1213                 unit.resolve("stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css");
1214         assertTrue(optionsContent.contains(
1215                 "'" + stylesheetResource.toFile().getAbsolutePath().replaceAll("\\\\", "/") + "'"));
1216     }
1217 
1218     /**
1219      * Method to test the <code>&lt;helpfile/&gt;</code> parameter.
1220      *
1221      * @throws Exception if any
1222      */
1223     public void testHelpfile() throws Exception {
1224         Path testPom = unit.resolve("helpfile-test/pom.xml");
1225 
1226         JavadocReport mojo = lookupMojo(testPom);
1227         assertNotNull(mojo);
1228 
1229         MavenSession session = spy(newMavenSession(mojo.project));
1230         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
1231         when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
1232         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
1233         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
1234         repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
1235                 .newInstance(repositorySession, new LocalRepository(localRepo)));
1236         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
1237         when(session.getRepositorySession()).thenReturn(repositorySession);
1238         LegacySupport legacySupport = lookup(LegacySupport.class);
1239         legacySupport.setSession(session);
1240         setVariableValueToObject(mojo, "session", session);
1241         setVariableValueToObject(mojo, "repoSession", repositorySession);
1242 
1243         Path apidocs = new File(getBasedir(), "target/test/unit/helpfile-test/target/site/apidocs").toPath();
1244 
1245         Path helpfile = apidocs.resolve("help-doc.html");
1246         Path options = apidocs.resolve("options");
1247 
1248         // helpfile by default
1249         mojo.execute();
1250 
1251         String content = readFile(helpfile);
1252         assertTrue(content.contains("<!-- Generated by javadoc"));
1253 
1254         String optionsContent = readFile(options);
1255         assertFalse(optionsContent.contains("-helpfile"));
1256 
1257         // helpfile defined in a javadoc plugin dependency
1258         setVariableValueToObject(mojo, "helpfile", "com/mycompany/app/javadoc/helpfile/help-doc.html");
1259 
1260         setVariableValueToObject(mojo, "session", session);
1261         setVariableValueToObject(mojo, "repoSession", repositorySession);
1262 
1263         mojo.execute();
1264 
1265         content = readFile(helpfile);
1266         assertTrue(content.contains("<!--  Help file from artefact -->"));
1267 
1268         optionsContent = readFile(options);
1269         assertTrue(optionsContent.contains("-helpfile"));
1270         Path help = apidocs.resolve("help-doc.html");
1271         assertTrue(optionsContent.contains("'" + help.toFile().getAbsolutePath().replaceAll("\\\\", "/") + "'"));
1272 
1273         // helpfile defined as a project resource
1274         setVariableValueToObject(mojo, "helpfile", "com/mycompany/app/javadoc/helpfile2/help-doc.html");
1275         mojo.execute();
1276 
1277         content = readFile(helpfile);
1278         assertTrue(content.contains("<!--  Help file from file -->"));
1279 
1280         optionsContent = readFile(options);
1281         assertTrue(optionsContent.contains("-helpfile"));
1282         help = unit.resolve("helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html");
1283         assertTrue(optionsContent.contains("'" + help.toFile().getAbsolutePath().replaceAll("\\\\", "/") + "'"));
1284 
1285         // helpfile defined as file
1286         help = unit.resolve("helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html");
1287         setVariableValueToObject(mojo, "helpfile", help.toFile().getAbsolutePath());
1288         mojo.execute();
1289 
1290         content = readFile(helpfile);
1291         assertTrue(content.contains("<!--  Help file from file -->"));
1292 
1293         optionsContent = readFile(options);
1294         assertTrue(optionsContent.contains("-helpfile"));
1295         assertTrue(optionsContent.contains("'" + help.toFile().getAbsolutePath().replaceAll("\\\\", "/") + "'"));
1296     }
1297 }