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.scanner;
20  
21  import java.io.File;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.maven.model.Build;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
34  import org.apache.maven.plugin.descriptor.MojoDescriptor;
35  import org.apache.maven.plugin.descriptor.PluginDescriptor;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
38  import org.apache.maven.tools.plugin.extractor.ExtractionException;
39  import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
40  import org.junit.jupiter.api.BeforeEach;
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.assertTrue;
45  import static org.junit.jupiter.api.Assertions.fail;
46  
47  /**
48   * @author jdcasey
49   */
50  class DefaultMojoScannerTest {
51      private Map<String, MojoDescriptorExtractor> extractors;
52  
53      private Build build;
54  
55      private Model model;
56  
57      private MojoScanner scanner;
58  
59      private MavenProject project;
60  
61      @BeforeEach
62      void setUp() {
63          extractors = new HashMap<>();
64          extractors.put("one", new ScannerTestExtractor("one"));
65          extractors.put("two", new ScannerTestExtractor("two"));
66          extractors.put("three", new ScannerTestExtractor("three"));
67  
68          scanner = new DefaultMojoScanner(extractors);
69  
70          build = new Build();
71          build.setSourceDirectory("testdir");
72  
73          model = new Model();
74          model.setBuild(build);
75  
76          project = new MavenProject(model);
77          project.setFile(new File("."));
78      }
79  
80      @Test
81      void testUnspecifiedExtractors() throws Exception {
82          PluginDescriptor pluginDescriptor = createPluginDescriptor();
83  
84          scanner.populatePluginDescriptor(new DefaultPluginToolsRequest(project, pluginDescriptor));
85  
86          checkResult(pluginDescriptor, extractors.keySet());
87      }
88  
89      @Test
90      void testSpecifiedExtractors() throws Exception {
91          Set<String> activeExtractors = new HashSet<>();
92          activeExtractors.add("one");
93          activeExtractors.add("");
94          activeExtractors.add(null);
95          activeExtractors.add("three");
96  
97          PluginDescriptor pluginDescriptor = createPluginDescriptor();
98  
99          scanner.setActiveExtractors(activeExtractors);
100         scanner.populatePluginDescriptor(new DefaultPluginToolsRequest(project, pluginDescriptor));
101 
102         checkResult(pluginDescriptor, Arrays.asList("one", "three"));
103     }
104 
105     @Test
106     void testAllExtractorsThroughNull() throws Exception {
107         PluginDescriptor pluginDescriptor = createPluginDescriptor();
108 
109         scanner.setActiveExtractors(null);
110         scanner.populatePluginDescriptor(new DefaultPluginToolsRequest(project, pluginDescriptor));
111 
112         checkResult(pluginDescriptor, extractors.keySet());
113     }
114 
115     @Test
116     void testNoExtractorsThroughEmptySet() throws Exception {
117         PluginDescriptor pluginDescriptor = createPluginDescriptor();
118 
119         scanner.setActiveExtractors(Collections.emptySet());
120         try {
121             scanner.populatePluginDescriptor(new DefaultPluginToolsRequest(project, pluginDescriptor));
122             fail("Expected exception");
123         } catch (InvalidPluginDescriptorException e) {
124             // Ok
125         }
126 
127         checkResult(pluginDescriptor, Collections.emptySet());
128     }
129 
130     @Test
131     void testUnknownExtractor() throws Exception {
132         Set<String> activeExtractors = new HashSet<>();
133         activeExtractors.add("four");
134 
135         PluginDescriptor pluginDescriptor = createPluginDescriptor();
136 
137         scanner.setActiveExtractors(activeExtractors);
138 
139         try {
140             scanner.populatePluginDescriptor(new DefaultPluginToolsRequest(project, pluginDescriptor));
141             fail("No error for unknown extractor");
142         } catch (ExtractionException e) {
143             // Ok
144         }
145 
146         checkResult(pluginDescriptor, Collections.emptySet());
147     }
148 
149     private PluginDescriptor createPluginDescriptor() {
150         PluginDescriptor pluginDescriptor = new PluginDescriptor();
151         pluginDescriptor.setGroupId("groupId");
152         pluginDescriptor.setArtifactId("artifactId");
153         pluginDescriptor.setVersion("version");
154         pluginDescriptor.setGoalPrefix("testId");
155         return pluginDescriptor;
156     }
157 
158     /**
159      * Checks if the {@link PluginDescriptor} contains exactly the {@link MojoDescriptor}s with the
160      * supplied goal names.
161      *
162      * @param pluginDescriptor The {@link PluginDescriptor} to check.
163      * @param expectedGoals    The goal names of the {@link MojoDescriptor}s.
164      */
165     protected void checkResult(PluginDescriptor pluginDescriptor, Collection<String> expectedGoals) {
166         Set<String> remainingGoals = new HashSet<>(expectedGoals);
167         List<MojoDescriptor> descriptors = pluginDescriptor.getMojos();
168 
169         if (descriptors == null) {
170             // TODO Maybe getMojos should be more user friendly and not return null
171             descriptors = Collections.emptyList();
172         }
173 
174         for (MojoDescriptor desc : descriptors) {
175             assertEquals(pluginDescriptor, desc.getPluginDescriptor());
176             assertTrue(remainingGoals.remove(desc.getGoal()), "Unexpected goal in PluginDescriptor: " + desc.getGoal());
177         }
178 
179         assertEquals(0, remainingGoals.size(), "Expected goals missing from PluginDescriptor: " + remainingGoals);
180     }
181 }