View Javadoc

1   package org.apache.maven.tools.plugin.generator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.maven.plugin.descriptor.MojoDescriptor;
24  import org.apache.maven.plugin.descriptor.Parameter;
25  import org.apache.maven.plugin.descriptor.PluginDescriptor;
26  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
27  import org.codehaus.plexus.component.repository.ComponentDependency;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  import java.io.File;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  
35  /**
36   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
37   * @version $Id: AbstractGeneratorTestCase.java,v 1.1 2005/02/20 16:25:21
38   *          jdcasey Exp $
39   */
40  public abstract class AbstractGeneratorTestCase
41      extends TestCase
42  {
43      protected Generator generator;
44  
45      protected String basedir;
46  
47      protected void setUp()
48          throws Exception
49      {
50          basedir = System.getProperty( "basedir" );
51      }
52  
53      public void testGenerator()
54          throws Exception
55      {
56          setupGenerator();
57  
58          MojoDescriptor mojoDescriptor = new MojoDescriptor();
59          mojoDescriptor.setGoal( "testGoal" );
60          mojoDescriptor.setImplementation( "org.apache.maven.tools.plugin.generator.TestMojo" );
61          mojoDescriptor.setDependencyResolutionRequired( "compile" );
62  
63          List params = new ArrayList();
64  
65          Parameter param = new Parameter();
66          param.setExpression( "${project.build.directory}" );
67          param.setDefaultValue( "</markup-must-be-escaped>" );
68          param.setName( "dir" );
69          param.setRequired( true );
70          param.setType( "java.lang.String" );
71          param.setDescription( "Test parameter description" );
72  
73          params.add( param );
74  
75          mojoDescriptor.setParameters( params );
76  
77          PluginDescriptor pluginDescriptor = new PluginDescriptor();
78          mojoDescriptor.setPluginDescriptor( pluginDescriptor );
79  
80          pluginDescriptor.addMojo( mojoDescriptor );
81  
82          pluginDescriptor.setArtifactId( "maven-unitTesting-plugin" );
83          pluginDescriptor.setGoalPrefix( "test" );
84  
85          ComponentDependency dependency = new ComponentDependency();
86          dependency.setGroupId( "testGroup" );
87          dependency.setArtifactId( "testArtifact" );
88          dependency.setVersion( "0.0.0" );
89  
90          pluginDescriptor.setDependencies( Collections.singletonList( dependency ) );
91  
92          File destinationDirectory = new File( System.getProperty( "java.io.tmpdir" ), "testGenerator-outDir" );
93          FileUtils.deleteDirectory( destinationDirectory );
94          destinationDirectory.mkdir();
95  
96          generator.execute( destinationDirectory, new DefaultPluginToolsRequest( null, pluginDescriptor ) );
97  
98          validate( destinationDirectory );
99  
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 }