Author: jdcasey
Date: Tue Dec 5 22:05:43 2006
New Revision: 482918
URL: http://svn.apache.org/viewvc?view=rev&rev=482918
Log:
Working on a generator for junit wrappers for integration-test builds.
Added:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/GenerateJUnitWrapperMojo.java
(with props)
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/CodeGenerator.java
(with props)
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/ToolException.java
(with props)
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/AbstractJUnitWrapperTest.vm
(with props)
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/JUnitWrapperTest.vm
(with props)
Modified:
maven/sandbox/plugins/maven-plug-it-plugin/pom.xml
Modified: maven/sandbox/plugins/maven-plug-it-plugin/pom.xml
URL:
http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-plug-it-plugin/pom.xml?view=diff&rev=482918&r1=482917&r2=482918
==============================================================================
--- maven/sandbox/plugins/maven-plug-it-plugin/pom.xml (original)
+++ maven/sandbox/plugins/maven-plug-it-plugin/pom.xml Tue Dec 5 22:05:43 2006
@@ -25,6 +25,21 @@
<version>2.0.4</version>
</dependency>
<dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.shared</groupId>
+ <artifactId>file-management</artifactId>
+ <version>1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-velocity</artifactId>
+ <version>1.1.2</version>
+ </dependency>
+ <dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-plugin-testing-tools</artifactId>
<version>1.0-SNAPSHOT</version>
Added:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/GenerateJUnitWrapperMojo.java
URL:
http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/GenerateJUnitWrapperMojo.java?view=auto&rev=482918
==============================================================================
---
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/GenerateJUnitWrapperMojo.java
(added)
+++
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/GenerateJUnitWrapperMojo.java
Tue Dec 5 22:05:43 2006
@@ -0,0 +1,210 @@
+package org.apache.maven.plugin.plugit;
+
+import org.apache.maven.model.Dependency;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.plugit.tools.CodeGenerator;
+import org.apache.maven.plugin.plugit.tools.ToolException;
+import org.apache.maven.shared.model.fileset.FileSet;
+import org.apache.maven.shared.model.fileset.util.FileSetManager;
+import org.codehaus.plexus.velocity.VelocityComponent;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Generate JUnit wrappers for each integration-test build matched.
+ *
+ * @goal junit-wrappers
+ * @author jdcasey
+ *
+ */
+public class GenerateJUnitWrapperMojo
+ extends AbstractMojo
+{
+
+ private static final String JUNIT_GROUP_ID = "junit";
+
+ private static final String JUNIT_ARTIFACT_ID = "junit";
+
+ private static final String ABSTRACT_TEST_CLASS_TEMPLATE =
"AbstractJUnitWrapperTest.vm";
+
+ private static final String BUILD_WRAPPER_TEMPLATE = "JUnitWrapperTest.vm";
+
+ private static final String ABSTRACT_TEST_CLASS =
"AbstractJUnitWrapperTest";
+
+ private static final String BUILD_WRAPPER_CLASS_BASENAME =
"JUnitWrapperTest";
+
+ /**
+ * This is the Java package name for the generated wrapper classes.
+ *
+ * @parameter expression="wrapperPackage" default-value="integrationTests"
+ * @required
+ */
+ private String wrapperPackage;
+
+ /**
+ * List of direct dependencies of this project, to verify that junit is
present.
+ *
+ * @parameter default-value="${project.dependencies}"
+ * @required
+ * @readonly
+ */
+ private List dependencies;
+
+ /**
+ * Location of the integration-test project set's base directory.
+ * @parameter expression="itBasedir" default-value="src/it"
+ * @required
+ */
+ private File itBasedir;
+
+ /**
+ * List of included integration-test POM path patterns. By default,
includes all poms under the
+ * itBasedir directory.
+ *
+ * @parameter expression="itIncludes"
+ */
+ private List itPomIncludes = Collections.singletonList( "**/pom.xml" );
+
+ /**
+ * List of excluded integration-test POM path patterns, such as
child-projects of multimodule
+ * integration tests. By default, this will exclude all poms under a
child* directory structure.
+ *
+ * @parameter expression="itExcludes"
+ */
+ private List itPomExcludes = Collections.singletonList(
"**/child*/**/pom.xml" );
+
+ /**
+ * @component
+ */
+ private VelocityComponent velocity;
+
+ /**
+ * Source directory where generated wrapper classes will be located.
+ *
+ * @parameter expression="wrapperSourceDir"
default-value="${project.build.directory}/generated-sources/plug-it"
+ * @required
+ */
+ private File wrapperSourceDir;
+
+ // calculated.
+ private CodeGenerator codeGenerator;
+
+ private int wrapperCounter;
+
+ public void execute()
+ throws MojoExecutionException, MojoFailureException
+ {
+ verifyJUnitDependencyPresence();
+
+ String[] poms;
+ try
+ {
+ poms = collectITPoms();
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "Search for IT projects
failed.", e );
+ }
+
+ if ( poms != null && poms.length > 0 )
+ {
+ wrapperCounter = 0;
+
+ initializeCodeGenerator();
+
+ try
+ {
+ generateAbstractTestClass();
+ }
+ catch ( ToolException e )
+ {
+ throw new MojoExecutionException( "Error generating abstract
test-wrapper parent class.", e );
+ }
+
+ for ( int i = 0; i < poms.length; i++ )
+ {
+ String pom = poms[i];
+
+ File pomFile = new File( pom );
+
+ if ( !pomFile.isAbsolute() )
+ {
+ pomFile = new File( itBasedir, pom );
+ }
+
+ try
+ {
+ generateTestWrapper( pomFile );
+ }
+ catch ( ToolException e )
+ {
+ throw new MojoExecutionException( "Error generating
test-wrapper for: " + pomFile, e );
+ }
+ }
+ }
+ }
+
+ private void initializeCodeGenerator()
+ {
+ codeGenerator = new CodeGenerator( velocity, wrapperSourceDir,
wrapperPackage, Collections.EMPTY_MAP );
+ }
+
+ private void generateTestWrapper( File pomFile ) throws ToolException
+ {
+ String wrapperName = BUILD_WRAPPER_CLASS_BASENAME + wrapperCounter++;
+
+ codeGenerator.generateCode( pomFile.getPath(), wrapperName,
BUILD_WRAPPER_TEMPLATE );
+ }
+
+ private void generateAbstractTestClass() throws ToolException
+ {
+ codeGenerator.generateCode( ABSTRACT_TEST_CLASS,
ABSTRACT_TEST_CLASS_TEMPLATE );
+ }
+
+ private void verifyJUnitDependencyPresence()
+ throws MojoFailureException
+ {
+ boolean foundJUnit = false;
+
+ for ( Iterator it = dependencies.iterator(); it.hasNext(); )
+ {
+ Dependency dependency = (Dependency) it.next();
+
+ if ( JUNIT_GROUP_ID.equals( dependency.getGroupId() )
+ && JUNIT_ARTIFACT_ID.equals( dependency.getArtifactId() ) )
+ {
+ foundJUnit = true;
+ break;
+ }
+ }
+
+ if ( !foundJUnit )
+ {
+ throw new MojoFailureException( "plug-it:junit-wrappers", "JUnit
wrapper generation failed.",
+ "You need to add the JUnit
dependency to your project, and you'll probably need to set its scope to
\'test\'." );
+ }
+ }
+
+ private String[] collectITPoms()
+ throws IOException
+ {
+ final FileSet fs = new FileSet();
+
+ fs.setIncludes( itPomIncludes );
+ fs.setExcludes( itPomExcludes );
+ fs.setDirectory( itBasedir.getCanonicalPath() );
+ fs.setFollowSymlinks( false );
+ fs.setUseDefaultExcludes( true );
+
+ final FileSetManager fsm = new FileSetManager( getLog() );
+
+ return fsm.getIncludedFiles( fs );
+ }
+
+}
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/GenerateJUnitWrapperMojo.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/GenerateJUnitWrapperMojo.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/CodeGenerator.java
URL:
http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/CodeGenerator.java?view=auto&rev=482918
==============================================================================
---
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/CodeGenerator.java
(added)
+++
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/CodeGenerator.java
Tue Dec 5 22:05:43 2006
@@ -0,0 +1,95 @@
+package org.apache.maven.plugin.plugit.tools;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.velocity.VelocityComponent;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Map;
+
+public class CodeGenerator
+{
+ private VelocityComponent velocity;
+
+ private final File destDir;
+
+ private final String destPackage;
+
+ private final Map initialContext;
+
+ public CodeGenerator( VelocityComponent velocity, File destDir, String
destPackage, Map initialContext )
+ {
+ this.velocity = velocity;
+ this.destDir = destDir;
+ this.destPackage = destPackage;
+ this.initialContext = initialContext;
+ }
+
+ public void generateCode( String className, String templateName )
+ throws ToolException
+ {
+ generateCode( null, className, templateName );
+ }
+
+ public void generateCode( String pomPath, String className, String
templateName )
+ throws ToolException
+ {
+ File pkgDir = new File( destDir, destPackage.replace( '.', '/' ) );
+
+ pkgDir.mkdirs();
+
+ File classFile = new File( pkgDir, className + ".java" );
+
+ Context ctx = initialContext == null ? new VelocityContext() : new
VelocityContext( initialContext );
+
+ ctx.put( "package", destPackage );
+ ctx.put( "class", className );
+
+ if ( pomPath != null )
+ {
+ ctx.put( "pomPath", pomPath );
+ }
+
+ Writer writer = null;
+ try
+ {
+ writer = new BufferedWriter( new FileWriter( classFile ) );
+
+ velocity.getEngine().mergeTemplate( templateName, ctx, writer );
+
+ writer.flush();
+ }
+ catch ( ResourceNotFoundException e )
+ {
+ throw new ToolException( "Unable to locate Velocity template: " +
templateName, e );
+ }
+ catch ( ParseErrorException e )
+ {
+ throw new ToolException( "Unable to parse Velocity template: " +
templateName, e );
+ }
+ catch ( MethodInvocationException e )
+ {
+ throw new ToolException( "Error merging Velocity template: " +
templateName, e );
+ }
+ catch ( IOException e )
+ {
+ throw new ToolException( "Error generating class to: " +
classFile, e );
+ }
+ catch ( Exception e )
+ {
+ throw new ToolException( "Unknown error (probably from
VelocityComponent)", e );
+ }
+ finally
+ {
+ IOUtil.close( writer );
+ }
+ }
+}
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/CodeGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/CodeGenerator.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/ToolException.java
URL:
http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/ToolException.java?view=auto&rev=482918
==============================================================================
---
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/ToolException.java
(added)
+++
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/ToolException.java
Tue Dec 5 22:05:43 2006
@@ -0,0 +1,17 @@
+package org.apache.maven.plugin.plugit.tools;
+
+public class ToolException
+ extends Exception
+{
+
+ public ToolException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+
+ public ToolException( String message )
+ {
+ super( message );
+ }
+
+}
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/ToolException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/java/org/apache/maven/plugin/plugit/tools/ToolException.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/AbstractJUnitWrapperTest.vm
URL:
http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/AbstractJUnitWrapperTest.vm?view=auto&rev=482918
==============================================================================
---
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/AbstractJUnitWrapperTest.vm
(added)
+++
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/AbstractJUnitWrapperTest.vm
Tue Dec 5 22:05:43 2006
@@ -0,0 +1,7 @@
+package ${package};
+
+import org.codehaus.plexus.PlexusTestCase;
+
+public abstract class ${class} extends PlexusTestCase
+{
+}
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/AbstractJUnitWrapperTest.vm
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/AbstractJUnitWrapperTest.vm
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/JUnitWrapperTest.vm
URL:
http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/JUnitWrapperTest.vm?view=auto&rev=482918
==============================================================================
---
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/JUnitWrapperTest.vm
(added)
+++
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/JUnitWrapperTest.vm
Tue Dec 5 22:05:43 2006
@@ -0,0 +1,5 @@
+package ${package};
+
+public class ${class} extends AbstractJUnitWrapperTest
+{
+}
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/JUnitWrapperTest.vm
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
maven/sandbox/plugins/maven-plug-it-plugin/src/main/resources/JUnitWrapperTest.vm
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"