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