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            List dependencies = pluginDescriptor.getDependencies();
067    
068            checkDependency( "testGroup", "testArtifact", "0.0.0", (ComponentDependency) dependencies.get( 0 ) );
069    
070            assertEquals( 1, dependencies.size() );
071    
072            ComponentDependency dependency = (ComponentDependency) dependencies.get( 0 );
073            assertEquals( "testGroup", dependency.getGroupId() );
074            assertEquals( "testArtifact", dependency.getArtifactId() );
075            assertEquals( "0.0.0", dependency.getVersion() );
076        }
077    
078        private String readFile( File pluginDescriptorFile )
079            throws IOException
080        {
081            StringWriter sWriter = new StringWriter();
082            PrintWriter pWriter = new PrintWriter( sWriter );
083    
084            BufferedReader reader = new BufferedReader( ReaderFactory.newXmlReader( pluginDescriptorFile ) );
085    
086            String line = null;
087            while ( ( line = reader.readLine() ) != null )
088            {
089                pWriter.println( line );
090            }
091    
092            reader.close();
093    
094            return sWriter.toString();
095        }
096    
097        private void checkMojo( MojoDescriptor mojoDescriptor )
098        {
099            assertEquals( "test:testGoal", mojoDescriptor.getFullGoalName() );
100    
101            assertEquals( "org.apache.maven.tools.plugin.generator.TestMojo", mojoDescriptor.getImplementation() );
102    
103            // The following should be defaults
104            assertEquals( "per-lookup", mojoDescriptor.getInstantiationStrategy() );
105    
106            assertNotNull( mojoDescriptor.isDependencyResolutionRequired() );
107    
108            // check the parameter.
109            checkParameter( (Parameter) mojoDescriptor.getParameters().get( 0 ) );
110        }
111    
112        private void checkParameter( Parameter parameter )
113        {
114            assertEquals( "dir", parameter.getName() );
115            assertEquals( String.class.getName(), parameter.getType() );
116            assertTrue( parameter.isRequired() );
117        }
118    
119        private void checkDependency( String groupId, String artifactId, String version, ComponentDependency dependency )
120        {
121            assertNotNull( dependency );
122    
123            assertEquals( groupId, dependency.getGroupId() );
124    
125            assertEquals( artifactId, dependency.getArtifactId() );
126    
127            assertEquals( version, dependency.getVersion() );
128        }
129    }