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.tools.plugin.generator;
20  
21  import java.io.File;
22  import java.io.InputStream;
23  import java.util.Locale;
24  
25  import org.codehaus.plexus.util.ReaderFactory;
26  import org.codehaus.plexus.util.xml.Xpp3Dom;
27  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  
33  /**
34   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
35   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
36   */
37  public class PluginXdocGeneratorTest extends AbstractGeneratorTestCase {
38  
39      // inherits tests from base class
40  
41      @Override
42      protected void validate(File destinationDirectory) throws Exception {
43          File docFile = new File(destinationDirectory, "testGoal-mojo.xml");
44          Xpp3Dom actual = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(docFile));
45  
46          InputStream expectedAsStream = getClass().getResourceAsStream("/expected-testGoal-mojo.xml");
47  
48          Xpp3Dom expected = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(expectedAsStream));
49  
50          Assertions.assertEquals(expected, actual);
51      }
52  
53      @Test
54      void testGetShortType() {
55          assertEquals("String", PluginXdocGenerator.getShortType("java.lang.String"));
56          assertEquals("List<String>", PluginXdocGenerator.getShortType("java.util.List<java.lang.String>"));
57          assertEquals(
58                  "Map<String,Integer>",
59                  PluginXdocGenerator.getShortType("java.util.Map<java.lang.String,java.lang.Integer>"));
60          assertEquals("List<...>", PluginXdocGenerator.getShortType("java.util.List<java.util.List<java.lang.String>>"));
61      }
62  
63      @Test
64      void testGetXhtmlWithValidatedLinks() {
65          File baseDir = new File(this.getClass().getResource("").getFile());
66          PluginXdocGenerator xdocGenerator = new PluginXdocGenerator(null, Locale.ROOT, baseDir, false);
67          PluginXdocGenerator xdocGeneratorWithDisabledLinkValidator =
68                  new PluginXdocGenerator(null, Locale.ROOT, baseDir, true);
69          String externalLink =
70                  "test<a href=\"http://example.com/test\">External Link</a>..and a second<a href=\"http://localhost/example\">link</a>end";
71          assertEquals(externalLink, xdocGenerator.getXhtmlWithValidatedLinks(externalLink, "test"));
72          assertEquals(
73                  externalLink, xdocGeneratorWithDisabledLinkValidator.getXhtmlWithValidatedLinks(externalLink, "test"));
74          String validInternalLink =
75                  "test<a href=\"PluginXdocGeneratorTest.class\">Internal Link</a>..and a second<a href=\"http://localhost/example\">link</a>end";
76          assertEquals(validInternalLink, xdocGenerator.getXhtmlWithValidatedLinks(validInternalLink, "test"));
77          assertEquals(
78                  validInternalLink,
79                  xdocGeneratorWithDisabledLinkValidator.getXhtmlWithValidatedLinks(validInternalLink, "test"));
80          String invalidInternalLink =
81                  "test<a href=\"PluginXdocGeneratorTestinvalid.class\">Internal Link</a>..and a second<a href=\"http://localhost/example\">link</a>end";
82          String sanitizedInvalidInternalLink =
83                  "testInternal Link..and a second<a href=\"http://localhost/example\">link</a>end";
84          assertEquals(
85                  sanitizedInvalidInternalLink, xdocGenerator.getXhtmlWithValidatedLinks(invalidInternalLink, "test"));
86          assertEquals(
87                  invalidInternalLink,
88                  xdocGeneratorWithDisabledLinkValidator.getXhtmlWithValidatedLinks(invalidInternalLink, "test"));
89      }
90  }