001    package org.apache.maven.tools.plugin.scanner;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import junit.framework.TestCase;
023    import org.apache.maven.model.Build;
024    import org.apache.maven.model.Model;
025    import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
026    import org.apache.maven.plugin.descriptor.MojoDescriptor;
027    import org.apache.maven.plugin.descriptor.PluginDescriptor;
028    import org.apache.maven.project.MavenProject;
029    import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
030    import org.apache.maven.tools.plugin.extractor.ExtractionException;
031    import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
032    
033    import java.io.File;
034    import java.util.Arrays;
035    import java.util.Collection;
036    import java.util.Collections;
037    import java.util.HashMap;
038    import java.util.HashSet;
039    import java.util.List;
040    import java.util.Map;
041    import java.util.Set;
042    
043    /**
044     * @author jdcasey
045     */
046    public class DefaultMojoScannerTest
047        extends TestCase
048    {
049        private Map<String, MojoDescriptorExtractor> extractors;
050    
051        private Build build;
052    
053        private Model model;
054    
055        private MojoScanner scanner;
056    
057        private MavenProject project;
058    
059        protected void setUp()
060            throws Exception
061        {
062            extractors = new HashMap<String, MojoDescriptorExtractor>();
063            extractors.put( "one", new ScannerTestExtractor( "one" ) );
064            extractors.put( "two", new ScannerTestExtractor( "two" ) );
065            extractors.put( "three", new ScannerTestExtractor( "three" ) );
066    
067            scanner = new DefaultMojoScanner( extractors );
068    
069            build = new Build();
070            build.setSourceDirectory( "testdir" );
071    
072            model = new Model();
073            model.setBuild( build );
074    
075            project = new MavenProject( model );
076            project.setFile( new File( "." ) );
077        }
078    
079        public void testUnspecifiedExtractors()
080            throws Exception
081        {
082            PluginDescriptor pluginDescriptor = createPluginDescriptor();
083    
084            scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
085    
086            checkResult( pluginDescriptor, extractors.keySet() );
087        }
088    
089        public void testSpecifiedExtractors()
090            throws Exception
091        {
092            Set<String> activeExtractors = new HashSet<String>();
093            activeExtractors.add( "one" );
094            activeExtractors.add( "" );
095            activeExtractors.add( null );
096            activeExtractors.add( "three" );
097    
098            PluginDescriptor pluginDescriptor = createPluginDescriptor();
099    
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<String>();
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<String>( 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    }