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.lang.reflect.Constructor;
23  import java.nio.file.Files;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.maven.model.Build;
29  import org.apache.maven.plugin.descriptor.DuplicateParameterException;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.Parameter;
32  import org.apache.maven.plugin.descriptor.PluginDescriptor;
33  import org.apache.maven.plugin.logging.Log;
34  import org.apache.maven.plugin.logging.SystemStreamLog;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
37  import org.codehaus.plexus.component.repository.ComponentDependency;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.junit.jupiter.api.Test;
40  
41  /**
42   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
43   */
44  public abstract class AbstractGeneratorTestCase {
45      protected Generator generator;
46  
47      protected String basedir = System.getProperty("basedir");
48  
49      @Test
50      public void testGenerator() throws Exception {
51          setupGenerator();
52  
53          MojoDescriptor mojoDescriptor = new MojoDescriptor();
54          mojoDescriptor.setGoal("testGoal");
55          mojoDescriptor.setImplementation("org.apache.maven.tools.plugin.generator.TestMojo");
56          mojoDescriptor.setDependencyResolutionRequired("compile");
57          mojoDescriptor.setSince("mojoSince");
58  
59          List<Parameter> params = new ArrayList<>();
60  
61          Parameter param = new Parameter();
62          param.setExpression("${project.build.directory}");
63          param.setDefaultValue("</markup-must-be-escaped>");
64          param.setName("dir");
65          param.setRequired(true);
66          param.setType("java.lang.String");
67          param.setDescription("Test parameter description");
68          param.setAlias("some.alias");
69          param.setSince("paramDirSince");
70          params.add(param);
71  
72          param = new Parameter();
73          param.setName("withoutSince");
74          param.setType("java.lang.String");
75          params.add(param);
76  
77          mojoDescriptor.setParameters(params);
78  
79          PluginDescriptor pluginDescriptor = new PluginDescriptor();
80          mojoDescriptor.setPluginDescriptor(pluginDescriptor);
81  
82          pluginDescriptor.addMojo(mojoDescriptor);
83  
84          pluginDescriptor.setArtifactId("maven-unitTesting-plugin");
85          pluginDescriptor.setGoalPrefix("test");
86  
87          ComponentDependency dependency = new ComponentDependency();
88          dependency.setGroupId("testGroup");
89          dependency.setArtifactId("testArtifact");
90          dependency.setVersion("0.0.0");
91  
92          pluginDescriptor.setDependencies(Collections.singletonList(dependency));
93  
94          File destinationDirectory =
95                  Files.createTempDirectory("testGenerator-outDir").toFile();
96          destinationDirectory.mkdir();
97  
98          MavenProject mavenProject = new MavenProject();
99          mavenProject.setGroupId("foo");
100         mavenProject.setArtifactId("bar");
101         Build build = new Build();
102         build.setDirectory(basedir + "/target");
103         build.setOutputDirectory(basedir + "/target");
104         mavenProject.setBuild(build);
105         extendPluginDescriptor(pluginDescriptor);
106         generator.execute(destinationDirectory, new DefaultPluginToolsRequest(mavenProject, pluginDescriptor));
107 
108         validate(destinationDirectory);
109 
110         FileUtils.deleteDirectory(destinationDirectory);
111     }
112 
113     protected void extendPluginDescriptor(PluginDescriptor pluginDescriptor) throws DuplicateParameterException {
114         // may be overwritten
115     }
116 
117     // ----------------------------------------------------------------------
118     //
119     // ----------------------------------------------------------------------
120 
121     protected void setupGenerator() throws Exception {
122         String generatorClassName = getClass().getName();
123 
124         generatorClassName = generatorClassName.substring(0, generatorClassName.length() - 4);
125 
126         try {
127             Class<?> generatorClass =
128                     Thread.currentThread().getContextClassLoader().loadClass(generatorClassName);
129 
130             Log log = new SystemStreamLog();
131             try {
132                 Constructor<?> constructor = generatorClass.getConstructor(Log.class);
133                 generator = (Generator) constructor.newInstance(log);
134             } catch (NoSuchMethodException ignore) {
135                 generator = (Generator) generatorClass.newInstance();
136             }
137         } catch (Exception e) {
138             throw new Exception("Cannot find " + generatorClassName
139                     + "! Make sure your test case is named in the form ${generatorClassName}Test "
140                     + "or override the setupPlugin() method to instantiate the mojo yourself.");
141         }
142     }
143 
144     // ----------------------------------------------------------------------
145     //
146     // ----------------------------------------------------------------------
147 
148     protected void validate(File destinationDirectory) throws Exception {
149         // empty
150     }
151 }