View Javadoc
1   package org.apache.maven.tools.plugin.scanner;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Build;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
25  import org.apache.maven.plugin.descriptor.MojoDescriptor;
26  import org.apache.maven.plugin.descriptor.PluginDescriptor;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
29  import org.apache.maven.tools.plugin.extractor.ExtractionException;
30  import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  import java.io.File;
35  import java.util.Arrays;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.HashMap;
39  import java.util.HashSet;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Set;
43  
44  import static org.junit.Assert.assertEquals;
45  import static org.junit.Assert.assertTrue;
46  import static org.junit.Assert.fail;
47  
48  /**
49   * @author jdcasey
50   */
51  public class DefaultMojoScannerTest
52  {
53      private Map<String, MojoDescriptorExtractor> extractors;
54  
55      private Build build;
56  
57      private Model model;
58  
59      private MojoScanner scanner;
60  
61      private MavenProject project;
62  
63      @Before
64      public void setUp()
65      {
66          extractors = new HashMap<>();
67          extractors.put( "one", new ScannerTestExtractor( "one" ) );
68          extractors.put( "two", new ScannerTestExtractor( "two" ) );
69          extractors.put( "three", new ScannerTestExtractor( "three" ) );
70  
71          scanner = new DefaultMojoScanner( extractors );
72  
73          build = new Build();
74          build.setSourceDirectory( "testdir" );
75  
76          model = new Model();
77          model.setBuild( build );
78  
79          project = new MavenProject( model );
80          project.setFile( new File( "." ) );
81      }
82  
83      @Test
84      public void testUnspecifiedExtractors()
85          throws Exception
86      {
87          PluginDescriptor pluginDescriptor = createPluginDescriptor();
88  
89          scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
90  
91          checkResult( pluginDescriptor, extractors.keySet() );
92      }
93  
94      @Test
95      public void testSpecifiedExtractors()
96          throws Exception
97      {
98          Set<String> activeExtractors = new HashSet<>();
99          activeExtractors.add( "one" );
100         activeExtractors.add( "" );
101         activeExtractors.add( null );
102         activeExtractors.add( "three" );
103 
104         PluginDescriptor pluginDescriptor = createPluginDescriptor();
105 
106         scanner.setActiveExtractors( activeExtractors );
107         scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
108 
109         checkResult( pluginDescriptor, Arrays.asList( "one", "three" ) );
110     }
111 
112     @Test
113     public void testAllExtractorsThroughNull()
114         throws Exception
115     {
116         PluginDescriptor pluginDescriptor = createPluginDescriptor();
117 
118         scanner.setActiveExtractors( null );
119         scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
120 
121         checkResult( pluginDescriptor, extractors.keySet() );
122     }
123 
124     @Test
125     public void testNoExtractorsThroughEmptySet()
126         throws Exception
127     {
128         PluginDescriptor pluginDescriptor = createPluginDescriptor();
129 
130         scanner.setActiveExtractors( Collections.<String>emptySet() );
131         try
132         {
133             scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
134             fail( "Expected exception" );
135         }
136         catch (InvalidPluginDescriptorException e)
137         {
138             // Ok
139         }
140 
141         checkResult( pluginDescriptor, Collections.<String>emptySet() );
142     }
143 
144     @Test
145     public void testUnknownExtractor()
146         throws Exception
147     {
148         Set<String> activeExtractors = new HashSet<>();
149         activeExtractors.add( "four" );
150 
151         PluginDescriptor pluginDescriptor = createPluginDescriptor();
152 
153         scanner.setActiveExtractors( activeExtractors );
154 
155         try
156         {
157             scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
158             fail( "No error for unknown extractor" );
159         }
160         catch ( ExtractionException e )
161         {
162             // Ok
163         }
164 
165         checkResult( pluginDescriptor, Collections.<String>emptySet() );
166     }
167 
168     private PluginDescriptor createPluginDescriptor()
169     {
170         PluginDescriptor pluginDescriptor = new PluginDescriptor();
171         pluginDescriptor.setGroupId( "groupId" );
172         pluginDescriptor.setArtifactId( "artifactId" );
173         pluginDescriptor.setVersion( "version" );
174         pluginDescriptor.setGoalPrefix( "testId" );
175         return pluginDescriptor;
176     }
177 
178     /**
179      * Checks if the {@link PluginDescriptor} contains exactly the {@link MojoDescriptor}s with the
180      * supplied goal names.
181      *
182      * @param pluginDescriptor The {@link PluginDescriptor} to check.
183      * @param expectedGoals    The goal names of the {@link MojoDescriptor}s.
184      */
185     protected void checkResult( PluginDescriptor pluginDescriptor, Collection<String> expectedGoals )
186     {
187         Set<String> remainingGoals = new HashSet<>( expectedGoals );
188         List<MojoDescriptor> descriptors = pluginDescriptor.getMojos();
189 
190         if ( descriptors == null )
191         {
192             // TODO Maybe getMojos should be more user friendly and not return null
193             descriptors = Collections.emptyList();
194         }
195 
196         for ( MojoDescriptor desc : descriptors )
197         {
198             assertEquals( pluginDescriptor, desc.getPluginDescriptor() );
199             assertTrue( "Unexpected goal in PluginDescriptor: " + desc.getGoal(),
200                     remainingGoals.remove( desc.getGoal() ) );
201         }
202 
203         assertEquals( "Expected goals missing from PluginDescriptor: " + remainingGoals, 0, remainingGoals.size() );
204     }
205 
206 }