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