KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > tools > plugin > generator > AbstractGeneratorTestCase


1 package org.apache.maven.tools.plugin.generator;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20 import org.apache.maven.plugin.descriptor.MojoDescriptor;
21 import org.apache.maven.plugin.descriptor.Parameter;
22 import org.apache.maven.plugin.descriptor.PluginDescriptor;
23 import org.codehaus.plexus.component.repository.ComponentDependency;
24 import org.codehaus.plexus.util.FileUtils;
25
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl </a>
33  * @version $Id: AbstractGeneratorTestCase.java,v 1.1 2005/02/20 16:25:21
34  * jdcasey Exp $
35  */

36 public abstract class AbstractGeneratorTestCase
37     extends TestCase
38 {
39     protected Generator generator;
40
41     protected String JavaDoc basedir;
42
43     protected void setUp()
44         throws Exception JavaDoc
45     {
46         basedir = System.getProperty( "basedir" );
47     }
48
49     public void testGenerator()
50         throws Exception JavaDoc
51     {
52         setupGenerator();
53
54         MojoDescriptor mojoDescriptor = new MojoDescriptor();
55         mojoDescriptor.setGoal( "testGoal" );
56         mojoDescriptor.setImplementation( "org.apache.maven.tools.plugin.generator.TestMojo" );
57         mojoDescriptor.setDependencyResolutionRequired( "compile" );
58
59         List JavaDoc params = new ArrayList JavaDoc();
60
61         Parameter param = new Parameter();
62         param.setExpression( "${project.build.directory}" );
63         param.setName( "dir" );
64         param.setRequired( true );
65         param.setType( "java.lang.String" );
66         param.setDescription( "Test parameter description" );
67
68         params.add( param );
69
70         mojoDescriptor.setParameters( params );
71
72         PluginDescriptor pluginDescriptor = new PluginDescriptor();
73         mojoDescriptor.setPluginDescriptor( pluginDescriptor );
74
75         pluginDescriptor.addMojo( mojoDescriptor );
76
77         pluginDescriptor.setArtifactId( "maven-unitTesting-plugin" );
78         pluginDescriptor.setGoalPrefix( "test" );
79
80         ComponentDependency dependency = new ComponentDependency();
81         dependency.setGroupId( "testGroup" );
82         dependency.setArtifactId( "testArtifact" );
83         dependency.setVersion( "0.0.0" );
84
85         pluginDescriptor.setDependencies( Collections.singletonList( dependency ) );
86
87         File JavaDoc destinationDirectory = new File JavaDoc( System.getProperty( "java.io.tmpdir" ), "testGenerator-outDir" );
88         FileUtils.deleteDirectory( destinationDirectory );
89         destinationDirectory.mkdir();
90
91         generator.execute( destinationDirectory, pluginDescriptor );
92
93         validate( destinationDirectory );
94
95         FileUtils.deleteDirectory( destinationDirectory );
96     }
97
98     // ----------------------------------------------------------------------
99
//
100
// ----------------------------------------------------------------------
101

102     protected void setupGenerator()
103         throws Exception JavaDoc
104     {
105         String JavaDoc generatorClassName = getClass().getName();
106
107         generatorClassName = generatorClassName.substring( 0, generatorClassName.length() - 4 );
108
109         try
110         {
111             Class JavaDoc generatorClass = Thread.currentThread().getContextClassLoader().loadClass( generatorClassName );
112
113             generator = (Generator) generatorClass.newInstance();
114         }
115         catch ( Exception JavaDoc e )
116         {
117             throw new Exception JavaDoc( "Cannot find " + generatorClassName +
118                                  "! Make sure your test case is named in the form ${generatorClassName}Test " +
119                                  "or override the setupPlugin() method to instantiate the mojo yourself." );
120         }
121     }
122
123     // ----------------------------------------------------------------------
124
//
125
// ----------------------------------------------------------------------
126

127     protected void validate( File JavaDoc destinationDirectory )
128         throws Exception JavaDoc
129     {
130         // empty
131
}
132 }
133
Popular Tags