001    package org.apache.maven.plugin.tools.model;
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.MojoDescriptor;
023    import org.codehaus.plexus.util.StringUtils;
024    
025    import java.io.File;
026    import java.net.URL;
027    import java.util.Set;
028    
029    import junit.framework.TestCase;
030    
031    public class PluginMetadataParserTest
032        extends TestCase
033    {
034        
035        public void testBasicDeclarationWithoutCall()
036            throws PluginMetadataParseException
037        {
038            File metadataFile = getMetadataFile( "test.mojos.xml" );
039            Set<MojoDescriptor> descriptors = new PluginMetadataParser().parseMojoDescriptors( metadataFile );
040            
041            assertEquals( 1, descriptors.size() );
042            
043            MojoDescriptor desc = descriptors.iterator().next();
044            assertTrue( desc.getImplementation().indexOf( ":" ) < 0 );
045            assertEquals( "test", desc.getGoal() );
046        }
047        
048        public void testBasicDeclarationWithCall()
049            throws PluginMetadataParseException
050        {
051            File metadataFile = getMetadataFile( "test2.mojos.xml" );
052            Set<MojoDescriptor> descriptors = new PluginMetadataParser().parseMojoDescriptors( metadataFile );
053            
054            assertEquals( 1, descriptors.size() );
055            
056            MojoDescriptor desc = descriptors.iterator().next();
057            assertTrue( desc.getImplementation().endsWith( ":test2" ) );
058            assertEquals( "test2", desc.getGoal() );
059        }
060        
061        private File getMetadataFile( String name )
062        {
063            URL resource = Thread.currentThread().getContextClassLoader().getResource( name );
064            if ( resource == null )
065            {
066                fail( "Cannot find classpath resource: '" + name + "'." );
067            }
068            
069            return new File( StringUtils.replace( resource.getPath(), "%20", " " ) );
070        }
071    
072    }