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