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.extractor.annotations;
20  
21  import java.io.IOException;
22  import java.net.URISyntaxException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.List;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.DefaultArtifact;
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
32  import org.apache.maven.plugin.descriptor.MojoDescriptor;
33  import org.apache.maven.plugin.descriptor.PluginDescriptor;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
36  import org.apache.maven.tools.plugin.extractor.ExtractionException;
37  import org.apache.maven.tools.plugin.extractor.annotations.scanner.DefaultMojoAnnotationsScanner;
38  import org.codehaus.plexus.logging.Logger;
39  import org.junit.jupiter.api.Test;
40  import org.junit.jupiter.api.io.TempDir;
41  
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  import static org.junit.jupiter.api.Assertions.assertThrows;
44  import static org.mockito.Mockito.mock;
45  
46  class JavaAnnotationsMojoDescriptorExtractorTest {
47      @TempDir
48      private Path targetDir;
49  
50      MojoDescriptor extractDescriptorFromMojoClass(Class<? extends AbstractMojo> mojoClass)
51              throws InvalidPluginDescriptorException, ExtractionException, IOException, URISyntaxException {
52          // copy class to an empty tmp directory
53          Path sourceClass = Paths.get(
54                  mojoClass.getResource(mojoClass.getSimpleName() + ".class").toURI());
55          Files.copy(sourceClass, targetDir.resolve(sourceClass.getFileName()));
56          JavaAnnotationsMojoDescriptorExtractor mojoDescriptorExtractor = new JavaAnnotationsMojoDescriptorExtractor();
57          DefaultMojoAnnotationsScanner scanner = new DefaultMojoAnnotationsScanner();
58          scanner.enableLogging(mock(Logger.class));
59          mojoDescriptorExtractor.mojoAnnotationsScanner = scanner;
60          PluginDescriptor pluginDescriptor = new PluginDescriptor();
61          MavenProject mavenProject = new MavenProject();
62          Artifact artifact = new DefaultArtifact("groupId", "artifactId", "1.0.0", null, "jar", "classifier", null);
63          mavenProject.setArtifact(artifact);
64          mavenProject.getBuild().setOutputDirectory(targetDir.toString());
65          List<MojoDescriptor> mojoDescriptors =
66                  mojoDescriptorExtractor.execute(new DefaultPluginToolsRequest(mavenProject, pluginDescriptor));
67          assertEquals(1, mojoDescriptors.size());
68          // there should be only one mojo contained in the one class
69          return mojoDescriptors.get(0);
70      }
71  
72      @Test
73      void assertFooMojo() throws InvalidPluginDescriptorException, ExtractionException, IOException, URISyntaxException {
74          MojoDescriptor mojoDescriptor = extractDescriptorFromMojoClass(FooMojo.class);
75          assertEquals("package", mojoDescriptor.getExecutePhase());
76          assertEquals("compiler", mojoDescriptor.getExecuteGoal());
77          assertEquals("my-lifecycle", mojoDescriptor.getExecuteLifecycle());
78      }
79  
80      @Test
81      void assertExecuteMojo()
82              throws InvalidPluginDescriptorException, ExtractionException, IOException, URISyntaxException {
83          MojoDescriptor mojoDescriptor = extractDescriptorFromMojoClass(ExecuteMojo.class);
84          assertEquals("my-phase-id", mojoDescriptor.getExecutePhase());
85          assertEquals("compiler", mojoDescriptor.getExecuteGoal());
86          assertEquals("my-lifecycle", mojoDescriptor.getExecuteLifecycle());
87      }
88  
89      @Test
90      void assertExecute2Mojo()
91              throws InvalidPluginDescriptorException, ExtractionException, IOException, URISyntaxException {
92          // two conflicting phase ids set
93          assertThrows(InvalidPluginDescriptorException.class, () -> extractDescriptorFromMojoClass(Execute2Mojo.class));
94      }
95  }