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