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 junit.framework.TestCase;
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.tools.plugin.DefaultPluginToolsRequest;
027    import org.codehaus.plexus.component.repository.ComponentDependency;
028    import org.codehaus.plexus.util.FileUtils;
029    
030    import java.io.File;
031    import java.util.ArrayList;
032    import java.util.Collections;
033    import java.util.List;
034    
035    /**
036     * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
037     * @version $Id: AbstractGeneratorTestCase.java,v 1.1 2005/02/20 16:25:21
038     *          jdcasey Exp $
039     */
040    public abstract class AbstractGeneratorTestCase
041        extends TestCase
042    {
043        protected Generator generator;
044    
045        protected String basedir;
046    
047        protected void setUp()
048            throws Exception
049        {
050            basedir = System.getProperty( "basedir" );
051        }
052    
053        public void testGenerator()
054            throws Exception
055        {
056            setupGenerator();
057    
058            MojoDescriptor mojoDescriptor = new MojoDescriptor();
059            mojoDescriptor.setGoal( "testGoal" );
060            mojoDescriptor.setImplementation( "org.apache.maven.tools.plugin.generator.TestMojo" );
061            mojoDescriptor.setDependencyResolutionRequired( "compile" );
062    
063            List params = new ArrayList();
064    
065            Parameter param = new Parameter();
066            param.setExpression( "${project.build.directory}" );
067            param.setDefaultValue( "</markup-must-be-escaped>" );
068            param.setName( "dir" );
069            param.setRequired( true );
070            param.setType( "java.lang.String" );
071            param.setDescription( "Test parameter description" );
072    
073            params.add( param );
074    
075            mojoDescriptor.setParameters( params );
076    
077            PluginDescriptor pluginDescriptor = new PluginDescriptor();
078            mojoDescriptor.setPluginDescriptor( pluginDescriptor );
079    
080            pluginDescriptor.addMojo( mojoDescriptor );
081    
082            pluginDescriptor.setArtifactId( "maven-unitTesting-plugin" );
083            pluginDescriptor.setGoalPrefix( "test" );
084    
085            ComponentDependency dependency = new ComponentDependency();
086            dependency.setGroupId( "testGroup" );
087            dependency.setArtifactId( "testArtifact" );
088            dependency.setVersion( "0.0.0" );
089    
090            pluginDescriptor.setDependencies( Collections.singletonList( dependency ) );
091    
092            File destinationDirectory = new File( System.getProperty( "java.io.tmpdir" ), "testGenerator-outDir" );
093            FileUtils.deleteDirectory( destinationDirectory );
094            destinationDirectory.mkdir();
095    
096            generator.execute( destinationDirectory, new DefaultPluginToolsRequest( null, pluginDescriptor ) );
097    
098            validate( destinationDirectory );
099    
100            FileUtils.deleteDirectory( destinationDirectory );
101        }
102    
103        // ----------------------------------------------------------------------
104        //
105        // ----------------------------------------------------------------------
106    
107        protected void setupGenerator()
108            throws Exception
109        {
110            String generatorClassName = getClass().getName();
111    
112            generatorClassName = generatorClassName.substring( 0, generatorClassName.length() - 4 );
113    
114            try
115            {
116                Class generatorClass = Thread.currentThread().getContextClassLoader().loadClass( generatorClassName );
117    
118                generator = (Generator) generatorClass.newInstance();
119            }
120            catch ( Exception e )
121            {
122                throw new Exception( "Cannot find " + generatorClassName +
123                    "! Make sure your test case is named in the form ${generatorClassName}Test " +
124                    "or override the setupPlugin() method to instantiate the mojo yourself." );
125            }
126        }
127    
128        // ----------------------------------------------------------------------
129        //
130        // ----------------------------------------------------------------------
131    
132        protected void validate( File destinationDirectory )
133            throws Exception
134        {
135            // empty
136        }
137    }