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.BufferedReader;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  import java.net.URI;
28  import java.net.URISyntaxException;
29  import java.util.List;
30  
31  import org.apache.maven.plugin.descriptor.DuplicateParameterException;
32  import org.apache.maven.plugin.descriptor.MojoDescriptor;
33  import org.apache.maven.plugin.descriptor.Parameter;
34  import org.apache.maven.plugin.descriptor.PluginDescriptor;
35  import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
36  import org.apache.maven.tools.plugin.javadoc.JavadocLinkGenerator;
37  import org.codehaus.plexus.component.repository.ComponentDependency;
38  import org.codehaus.plexus.configuration.PlexusConfiguration;
39  import org.codehaus.plexus.util.ReaderFactory;
40  import org.junit.jupiter.api.Test;
41  
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  import static org.junit.jupiter.api.Assertions.assertNotNull;
44  import static org.junit.jupiter.api.Assertions.assertTrue;
45  
46  /**
47   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
48   */
49  public class PluginDescriptorFilesGeneratorTest extends AbstractGeneratorTestCase {
50      @Override
51      protected void extendPluginDescriptor(PluginDescriptor pluginDescriptor) throws DuplicateParameterException {
52          Parameter parameterWithGenerics = new Parameter();
53          parameterWithGenerics.setName("parameterWithGenerics");
54          parameterWithGenerics.setType("java.util.Collection<java.lang.String>");
55          parameterWithGenerics.setExpression("${customParam}");
56          parameterWithGenerics.setDefaultValue("a,b,c");
57          pluginDescriptor.getMojos().get(0).addParameter(parameterWithGenerics);
58      }
59  
60      @Override
61      protected void validate(File destinationDirectory) throws Exception {
62          PluginDescriptorBuilder pdb = new PluginDescriptorBuilder();
63  
64          File pluginDescriptorFile = new File(destinationDirectory, "plugin.xml");
65  
66          String pd = readFile(pluginDescriptorFile);
67  
68          PluginDescriptor pluginDescriptor = pdb.build(new StringReader(pd));
69  
70          assertEquals(1, pluginDescriptor.getMojos().size());
71  
72          MojoDescriptor mojoDescriptor = pluginDescriptor.getMojos().get(0);
73  
74          checkMojo(mojoDescriptor);
75  
76          // ----------------------------------------------------------------------
77          // Dependencies
78          // ----------------------------------------------------------------------
79  
80          List<ComponentDependency> dependencies = pluginDescriptor.getDependencies();
81  
82          checkDependency("testGroup", "testArtifact", "0.0.0", dependencies.get(0));
83  
84          assertEquals(1, dependencies.size());
85  
86          ComponentDependency dependency = dependencies.get(0);
87          assertEquals("testGroup", dependency.getGroupId());
88          assertEquals("testArtifact", dependency.getArtifactId());
89          assertEquals("0.0.0", dependency.getVersion());
90      }
91  
92      private String readFile(File pluginDescriptorFile) throws IOException {
93          StringWriter sWriter = new StringWriter();
94  
95          try (PrintWriter pWriter = new PrintWriter(sWriter); //
96                  BufferedReader reader = new BufferedReader(ReaderFactory.newXmlReader(pluginDescriptorFile))) {
97              String line = null;
98              while ((line = reader.readLine()) != null) {
99                  pWriter.println(line);
100             }
101         }
102 
103         return sWriter.toString();
104     }
105 
106     private void checkMojo(MojoDescriptor mojoDescriptor) {
107         assertEquals("test:testGoal", mojoDescriptor.getFullGoalName());
108 
109         assertEquals("org.apache.maven.tools.plugin.generator.TestMojo", mojoDescriptor.getImplementation());
110 
111         // The following should be defaults
112         assertEquals("per-lookup", mojoDescriptor.getInstantiationStrategy());
113 
114         assertNotNull(mojoDescriptor.isDependencyResolutionRequired());
115 
116         // check the default parameter
117         checkParameter(mojoDescriptor.getParameters().get(0));
118 
119         // check another parameter with generics type information
120         Parameter parameterWithGenerics = mojoDescriptor.getParameters().get(2);
121         assertNotNull(parameterWithGenerics);
122         assertEquals("parameterWithGenerics", parameterWithGenerics.getName());
123         assertEquals("java.util.Collection", parameterWithGenerics.getType());
124 
125         PlexusConfiguration configurations = mojoDescriptor.getMojoConfiguration();
126         assertNotNull(configurations);
127         PlexusConfiguration configuration = configurations.getChild("parameterWithGenerics");
128         assertEquals("java.util.Collection", configuration.getAttribute("implementation"));
129         assertEquals("a,b,c", configuration.getAttribute("default-value"));
130         assertEquals("${customParam}", configuration.getValue());
131     }
132 
133     private void checkParameter(Parameter parameter) {
134         assertEquals("dir", parameter.getName());
135         assertEquals(String.class.getName(), parameter.getType());
136         assertTrue(parameter.isRequired());
137         assertEquals("some.alias", parameter.getAlias());
138     }
139 
140     private void checkDependency(String groupId, String artifactId, String version, ComponentDependency dependency) {
141         assertNotNull(dependency);
142 
143         assertEquals(groupId, dependency.getGroupId());
144 
145         assertEquals(artifactId, dependency.getArtifactId());
146 
147         assertEquals(version, dependency.getVersion());
148     }
149 
150     @Test
151     void testGetJavadocUrlForType() throws URISyntaxException {
152         URI javadocBaseUri = new URI("http://localhost/apidocs/");
153         JavadocLinkGenerator linkGenerator = new JavadocLinkGenerator(javadocBaseUri, "1.8");
154         assertEquals(
155                 javadocBaseUri.resolve("java/lang/String.html"),
156                 PluginDescriptorFilesGenerator.getJavadocUrlForType(linkGenerator, "java.lang.String"));
157         assertEquals(
158                 javadocBaseUri.resolve("java/lang/String.html"),
159                 PluginDescriptorFilesGenerator.getJavadocUrlForType(
160                         linkGenerator, "java.lang.Collection<java.lang.String>"));
161         assertEquals(
162                 javadocBaseUri.resolve("java/lang/Integer.html"),
163                 PluginDescriptorFilesGenerator.getJavadocUrlForType(
164                         linkGenerator, "java.lang.Map<java.lang.String,java.lang.Integer>"));
165         assertEquals(
166                 javadocBaseUri.resolve("java/lang/Integer.html"),
167                 PluginDescriptorFilesGenerator.getJavadocUrlForType(
168                         linkGenerator, "java.lang.Map<java.lang.String,java.util.List<java.lang.Integer>>"));
169         assertEquals(
170                 javadocBaseUri.resolve("java/util/function/BiFunction.html"),
171                 PluginDescriptorFilesGenerator.getJavadocUrlForType(
172                         linkGenerator,
173                         "java.util.function.BiFunction<java.lang.String,java.lang.String,java.lang.String>"));
174     }
175 }