1 package org.apache.maven.tools.plugin.generator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
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
136 }
137 }