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.scanner;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.plugins.annotations.Execute;
30  import org.apache.maven.plugins.annotations.LifecyclePhase;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.tools.plugin.extractor.ExtractionException;
34  import org.apache.maven.tools.plugin.extractor.annotations.AbstractFooMojo;
35  import org.apache.maven.tools.plugin.extractor.annotations.DeprecatedMojo;
36  import org.apache.maven.tools.plugin.extractor.annotations.FooMojo;
37  import org.apache.maven.tools.plugin.extractor.annotations.ParametersWithGenericsMojo;
38  import org.apache.maven.tools.plugin.extractor.annotations.datamodel.ComponentAnnotationContent;
39  import org.apache.maven.tools.plugin.extractor.annotations.datamodel.ParameterAnnotationContent;
40  import org.codehaus.plexus.logging.Logger;
41  import org.junit.jupiter.api.Test;
42  
43  import static org.assertj.core.api.Assertions.assertThat;
44  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
45  import static org.junit.jupiter.api.Assertions.assertEquals;
46  import static org.junit.jupiter.api.Assertions.assertFalse;
47  import static org.junit.jupiter.api.Assertions.assertNotNull;
48  import static org.junit.jupiter.api.Assertions.assertTrue;
49  import static org.mockito.Mockito.mock;
50  
51  class DefaultMojoAnnotationsScannerTest {
52      private DefaultMojoAnnotationsScanner scanner = new DefaultMojoAnnotationsScanner();
53  
54      @Test
55      void testSkipModuleInfoClassInArchive() throws Exception {
56          scanner.scanArchive(new File("target/test-classes/java9-module.jar"), null, false);
57      }
58  
59      @Test
60      void testJava8Annotations() throws Exception {
61          scanner.enableLogging(mock(Logger.class));
62          scanner.scanArchive(new File("target/test-classes/java8-annotations.jar"), null, false);
63      }
64  
65      @Test
66      void scanDeprecatedMojoAnnotatins() throws ExtractionException, IOException {
67          File directoryToScan = new File(DeprecatedMojo.class.getResource("").getFile());
68  
69          scanner.enableLogging(mock(Logger.class));
70          Map<String, MojoAnnotatedClass> result =
71                  scanner.scanDirectory(directoryToScan, Collections.singletonList("DeprecatedMojo.class"), null, false);
72  
73          assertThat(result).hasSize(1);
74  
75          MojoAnnotatedClass annotatedClass = result.get(DeprecatedMojo.class.getName());
76          assertThat(annotatedClass.getClassName()).isEqualTo(DeprecatedMojo.class.getName());
77          assertThat(annotatedClass.getMojo().getDeprecated()).isEmpty();
78  
79          assertThat(annotatedClass.getParameters()).containsOnlyKeys("deprecatedParameters", "anotherNotDeprecated");
80  
81          assertThat(annotatedClass.getParameters().get("deprecatedParameters").getDeprecated())
82                  .isEmpty();
83          assertThat(annotatedClass.getParameters().get("deprecatedParameters").alias())
84                  .isEqualTo("deprecatedParametersAlias");
85  
86          assertThat(annotatedClass.getParameters().get("anotherNotDeprecated").getDeprecated())
87                  .isNull();
88          assertThat(annotatedClass.getParameters().get("anotherNotDeprecated").property())
89                  .isEqualTo("property.anotherNotDeprecated");
90      }
91  
92      @Test
93      void scanParametersWithGenerics() throws ExtractionException, IOException {
94          File directoryToScan =
95                  new File(ParametersWithGenericsMojo.class.getResource("").getFile());
96  
97          scanner.enableLogging(mock(Logger.class));
98          Map<String, MojoAnnotatedClass> result = scanner.scanDirectory(
99                  directoryToScan, Collections.singletonList("ParametersWithGenericsMojo**.class"), null, false);
100 
101         assertThat(result).hasSize(2); // mojo and nested class
102 
103         MojoAnnotatedClass annotatedClass = result.get(ParametersWithGenericsMojo.class.getName());
104         assertThat(annotatedClass.getClassName()).isEqualTo(ParametersWithGenericsMojo.class.getName());
105 
106         ParameterAnnotationContent parameter = annotatedClass.getParameters().get("string");
107         assertNotNull(parameter);
108         assertEquals("java.lang.String", parameter.getClassName());
109         assertThat(parameter.getTypeParameters()).isEmpty();
110 
111         parameter = annotatedClass.getParameters().get("stringBooleanMap");
112         assertNotNull(parameter);
113         assertEquals("java.util.Map", parameter.getClassName());
114         assertThat(parameter.getTypeParameters()).containsExactly("java.lang.String", "java.lang.Boolean");
115 
116         parameter = annotatedClass.getParameters().get("integerCollection");
117         assertNotNull(parameter);
118         assertEquals("java.util.Collection", parameter.getClassName());
119         assertThat(parameter.getTypeParameters()).containsExactly("java.lang.Integer");
120 
121         parameter = annotatedClass.getParameters().get("nestedStringCollection");
122         assertNotNull(parameter);
123         assertEquals("java.util.Collection", parameter.getClassName());
124         assertThat(parameter.getTypeParameters()).containsExactly("java.util.Collection<java.lang.String>");
125 
126         parameter = annotatedClass.getParameters().get("integerArrayCollection");
127         assertNotNull(parameter);
128         assertEquals("java.util.Collection", parameter.getClassName());
129         assertThat(parameter.getTypeParameters()).containsExactly("java.lang.Integer[]");
130 
131         parameter = annotatedClass.getParameters().get("numberList");
132         assertNotNull(parameter);
133         assertEquals("java.util.List", parameter.getClassName());
134         assertThat(parameter.getTypeParameters()).containsExactly("java.lang.Number");
135     }
136 
137     @Test
138     void scanFooMojoClass() throws Exception {
139         MojoAnnotationsScannerRequest request = new MojoAnnotationsScannerRequest();
140         request.setClassesDirectories(Collections.singletonList(new File(getBasedir(), "target/test-classes")));
141         request.setIncludePatterns(Arrays.asList("**/FooMojo.class"));
142         request.setProject(new MavenProject());
143 
144         scanner.enableLogging(mock(Logger.class));
145         Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = scanner.scan(request);
146 
147         System.out.println("mojoAnnotatedClasses:" + mojoAnnotatedClasses);
148 
149         assertThat(mojoAnnotatedClasses).isNotNull().isNotEmpty().hasSize(1);
150 
151         MojoAnnotatedClass mojoAnnotatedClass =
152                 mojoAnnotatedClasses.values().iterator().next();
153 
154         assertEquals(FooMojo.class.getName(), mojoAnnotatedClass.getClassName());
155         assertEquals(AbstractFooMojo.class.getName(), mojoAnnotatedClass.getParentClassName());
156 
157         Mojo mojo = mojoAnnotatedClass.getMojo();
158 
159         assertEquals("foo", mojo.name());
160         assertTrue(mojo.threadSafe());
161         assertFalse(mojo.aggregator());
162         assertEquals(LifecyclePhase.COMPILE, mojo.defaultPhase());
163 
164         Execute execute = mojoAnnotatedClass.getExecute();
165 
166         assertEquals("compiler", execute.goal());
167         assertEquals("my-lifecycle", execute.lifecycle());
168         assertEquals(LifecyclePhase.PACKAGE, execute.phase());
169 
170         Collection<ComponentAnnotationContent> components =
171                 mojoAnnotatedClass.getComponents().values();
172         assertThat(components).isNotNull().isNotEmpty().hasSize(1);
173 
174         Collection<ParameterAnnotationContent> parameters =
175                 mojoAnnotatedClass.getParameters().values();
176         assertThat(parameters)
177                 .isNotNull()
178                 .isNotEmpty()
179                 .hasSize(5)
180                 .containsExactlyInAnyOrder(
181                         new ParameterAnnotationContent(
182                                 "bar",
183                                 null,
184                                 "thebar",
185                                 "coolbar",
186                                 true,
187                                 false,
188                                 String.class.getName(),
189                                 Collections.emptyList(),
190                                 false),
191                         new ParameterAnnotationContent(
192                                 "beer",
193                                 null,
194                                 "thebeer",
195                                 "coolbeer",
196                                 false,
197                                 false,
198                                 String.class.getName(),
199                                 Collections.emptyList(),
200                                 false),
201                         new ParameterAnnotationContent(
202                                 "paramFromSetter",
203                                 null,
204                                 "props.paramFromSetter",
205                                 null,
206                                 false,
207                                 false,
208                                 String.class.getName(),
209                                 Collections.emptyList(),
210                                 true),
211                         new ParameterAnnotationContent(
212                                 "paramFromAdd",
213                                 null,
214                                 "props.paramFromAdd",
215                                 null,
216                                 false,
217                                 false,
218                                 String.class.getName(),
219                                 Collections.emptyList(),
220                                 true),
221                         new ParameterAnnotationContent(
222                                 "paramFromSetterDeprecated",
223                                 null,
224                                 "props.paramFromSetterDeprecated",
225                                 null,
226                                 false,
227                                 false,
228                                 List.class.getName(),
229                                 Collections.singletonList("java.lang.String"),
230                                 true));
231     }
232 }