001    package org.apache.maven.tools.plugin.extractor.ant;
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 org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
023    import org.apache.maven.plugin.descriptor.MojoDescriptor;
024    import org.apache.maven.plugin.descriptor.Parameter;
025    import org.apache.maven.plugin.descriptor.PluginDescriptor;
026    import org.apache.maven.project.MavenProject;
027    import org.apache.maven.project.path.PathTranslator;
028    import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
029    import org.apache.maven.tools.plugin.PluginToolsRequest;
030    import org.apache.maven.tools.plugin.extractor.ExtractionException;
031    import org.codehaus.plexus.component.repository.ComponentRequirement;
032    import org.codehaus.plexus.util.StringUtils;
033    
034    import java.io.File;
035    import java.net.URL;
036    import java.util.HashMap;
037    import java.util.HashSet;
038    import java.util.Iterator;
039    import java.util.List;
040    import java.util.Map;
041    import java.util.Set;
042    
043    import junit.framework.TestCase;
044    
045    public class AntMojoDescriptorExtractorTest
046        extends TestCase
047    {
048        
049        public void testBasicMojoExtraction_CheckInjectedParametersAndRequirements()
050            throws InvalidPluginDescriptorException, ExtractionException
051        {
052            Map scriptMap = buildTestMap( "basic" );
053            
054            PluginDescriptor pd = new PluginDescriptor();
055            
056            pd.setArtifactId( "test-plugin" );
057            pd.setGroupId( "org.mytest" );
058            pd.setVersion( "1" );
059            pd.setGoalPrefix( "mytest" );
060            
061            PluginToolsRequest request = new DefaultPluginToolsRequest( new MavenProject(), pd );
062            
063            List metadata = new AntMojoDescriptorExtractor().extractMojoDescriptorsFromMetadata( scriptMap, request );
064            
065            assertEquals( 2, metadata.size() );
066            
067            for ( Iterator it = metadata.iterator(); it.hasNext(); )
068            {
069                MojoDescriptor desc = (MojoDescriptor) it.next();
070                
071                if ( "test".equals( desc.getGoal() ) )
072                {
073                    assertTrue( desc.getImplementation().indexOf( ":" ) < 0 );
074                }
075                else if ( "test2".equals( desc.getGoal() ) )
076                {
077                    assertTrue( desc.getImplementation().endsWith( ":test2" ) );
078                }
079                
080                List params = desc.getParameters();
081                Map paramMap = new HashMap();
082                for ( Iterator paramIterator = params.iterator(); paramIterator.hasNext(); )
083                {
084                    Parameter param = (Parameter) paramIterator.next();
085                    paramMap.put( param.getName(), param );
086                }
087                
088                assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'basedir' parameter.", paramMap.get( "basedir" ) );
089                assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'messageLevel' parameter.", paramMap.get( "messageLevel" ) );
090                assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'project' parameter.", paramMap.get( "project" ) );
091                assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'session' parameter.", paramMap.get( "session" ) );
092                assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'mojoExecution' parameter.", paramMap.get( "mojoExecution" ) );
093                
094                List components = desc.getRequirements();
095    
096                assertNotNull( components );
097                assertEquals( 1, components.size() );
098                
099                ComponentRequirement req = (ComponentRequirement) components.get( 0 );
100                assertEquals( "Mojo descriptor: " + desc.getGoal() + " is missing 'PathTranslator' component requirement.", PathTranslator.class.getName(), req.getRole() );
101            }
102        }
103        
104        private Map buildTestMap( String resourceDirName )
105        {
106            Map result = new HashMap();
107            
108            ClassLoader cloader = Thread.currentThread().getContextClassLoader();
109            URL mojosXmlUrl = cloader.getResource( resourceDirName + "/test.mojos.xml" );
110            
111            if ( mojosXmlUrl == null )
112            {
113                fail( "No classpath resource named: '" + resourceDirName + "/test.mojos.xml' could be found." );
114            }
115            
116            File mojosXml = new File( StringUtils.replace( mojosXmlUrl.getPath(), "%20", " " ) );
117            File dir = mojosXml.getParentFile();
118            
119            Set scripts = new HashSet();
120            String[] listing = dir.list();
121            for ( int i = 0; listing != null && i < listing.length; i++ )
122            {
123                if ( listing[i].endsWith( ".mojos.xml" ) )
124                {
125                    File f = new File( dir, listing[i] ).getAbsoluteFile();
126                    
127                    scripts.add( f );
128                }
129            }
130            
131            result.put( dir.getAbsolutePath(), scripts );
132            
133            return result;
134        }
135        
136        // TODO
137    
138    }