001    package org.apache.maven.tools.plugin.generator;
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.apache.maven.plugin.descriptor.Parameter;
024    import org.apache.maven.plugin.descriptor.PluginDescriptor;
025    import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
026    import org.codehaus.plexus.component.repository.ComponentDependency;
027    import org.codehaus.plexus.util.ReaderFactory;
028    
029    import java.io.BufferedReader;
030    import java.io.File;
031    import java.io.IOException;
032    import java.io.PrintWriter;
033    import java.io.StringReader;
034    import java.io.StringWriter;
035    import java.util.List;
036    
037    /**
038     * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
039     * @version $Id: PluginDescriptorGeneratorTest.java,v 1.1.1.1 2004/08/09
040     *          18:43:10 jvanzyl Exp $
041     */
042    public class PluginDescriptorGeneratorTest
043        extends AbstractGeneratorTestCase
044    {
045        protected void validate( File destinationDirectory )
046            throws Exception
047        {
048            PluginDescriptorBuilder pdb = new PluginDescriptorBuilder();
049    
050            File pluginDescriptorFile = new File( destinationDirectory, "plugin.xml" );
051    
052            String pd = readFile( pluginDescriptorFile );
053    
054            PluginDescriptor pluginDescriptor = pdb.build( new StringReader( pd ) );
055    
056            assertEquals( 1, pluginDescriptor.getMojos().size() );
057    
058            MojoDescriptor mojoDescriptor = (MojoDescriptor) pluginDescriptor.getMojos().get( 0 );
059    
060            checkMojo( mojoDescriptor );
061    
062            // ----------------------------------------------------------------------
063            // Dependencies
064            // ----------------------------------------------------------------------
065    
066            @SuppressWarnings( "unchecked" )
067            List<ComponentDependency> dependencies = pluginDescriptor.getDependencies();
068    
069            checkDependency( "testGroup", "testArtifact", "0.0.0", dependencies.get( 0 ) );
070    
071            assertEquals( 1, dependencies.size() );
072    
073            ComponentDependency dependency = (ComponentDependency) dependencies.get( 0 );
074            assertEquals( "testGroup", dependency.getGroupId() );
075            assertEquals( "testArtifact", dependency.getArtifactId() );
076            assertEquals( "0.0.0", dependency.getVersion() );
077        }
078    
079        private String readFile( File pluginDescriptorFile )
080            throws IOException
081        {
082            StringWriter sWriter = new StringWriter();
083            PrintWriter pWriter = new PrintWriter( sWriter );
084    
085            BufferedReader reader = new BufferedReader( ReaderFactory.newXmlReader( pluginDescriptorFile ) );
086    
087            String line = null;
088            while ( ( line = reader.readLine() ) != null )
089            {
090                pWriter.println( line );
091            }
092    
093            reader.close();
094    
095            return sWriter.toString();
096        }
097    
098        private void checkMojo( MojoDescriptor mojoDescriptor )
099        {
100            assertEquals( "test:testGoal", mojoDescriptor.getFullGoalName() );
101    
102            assertEquals( "org.apache.maven.tools.plugin.generator.TestMojo", mojoDescriptor.getImplementation() );
103    
104            // The following should be defaults
105            assertEquals( "per-lookup", mojoDescriptor.getInstantiationStrategy() );
106    
107            assertNotNull( mojoDescriptor.isDependencyResolutionRequired() );
108    
109            // check the parameter.
110            checkParameter( (Parameter) mojoDescriptor.getParameters().get( 0 ) );
111        }
112    
113        private void checkParameter( Parameter parameter )
114        {
115            assertEquals( "dir", parameter.getName() );
116            assertEquals( String.class.getName(), parameter.getType() );
117            assertTrue( parameter.isRequired() );
118        }
119    
120        private void checkDependency( String groupId, String artifactId, String version, ComponentDependency dependency )
121        {
122            assertNotNull( dependency );
123    
124            assertEquals( groupId, dependency.getGroupId() );
125    
126            assertEquals( artifactId, dependency.getArtifactId() );
127    
128            assertEquals( version, dependency.getVersion() );
129        }
130    }