1 package org.apache.maven.tools.plugin.generator; 2 3 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 ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.List ; 30 31 36 public abstract class AbstractGeneratorTestCase 37 extends TestCase 38 { 39 protected Generator generator; 40 41 protected String basedir; 42 43 protected void setUp() 44 throws Exception 45 { 46 basedir = System.getProperty( "basedir" ); 47 } 48 49 public void testGenerator() 50 throws Exception 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 params = new ArrayList (); 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 destinationDirectory = new File ( 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 102 protected void setupGenerator() 103 throws Exception 104 { 105 String generatorClassName = getClass().getName(); 106 107 generatorClassName = generatorClassName.substring( 0, generatorClassName.length() - 4 ); 108 109 try 110 { 111 Class generatorClass = Thread.currentThread().getContextClassLoader().loadClass( generatorClassName ); 112 113 generator = (Generator) generatorClass.newInstance(); 114 } 115 catch ( Exception e ) 116 { 117 throw new Exception ( "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 127 protected void validate( File destinationDirectory ) 128 throws Exception 129 { 130 } 132 } 133 | Popular Tags |