Skip to content

Instantly share code, notes, and snippets.

@ifedorenko
Created October 12, 2011 15:53
Show Gist options
  • Save ifedorenko/1281612 to your computer and use it in GitHub Desktop.
Save ifedorenko/1281612 to your computer and use it in GitHub Desktop.
package org.antlr.mojo.antlr3.tests;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import org.antlr.mojo.antlr3.Antlr3Mojo;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.tesla.incremental.BuildContext;
import org.eclipse.tesla.incremental.maven.MavenBuildContextManager;
import org.eclipse.tesla.incremental.maven.internal.MojoExecutionScoped;
import org.eclipse.tesla.incremental.maven.internal.SimpleScope;
import org.sonatype.guice.plexus.config.PlexusBeanModule;
import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.name.Names;
abstract class AbstractBuildAvoidanceTest
extends AbstractMojoTestCase
{
@Override
protected ContainerConfiguration setupContainerConfiguration()
{
return super.setupContainerConfiguration().setClassPathScanning( PlexusConstants.SCANNING_INDEX ).setAutoWiring( true );
}
@Override
protected void setupContainer()
{
super.setupContainer();
Module module = new Module()
{
@Override
public void configure( Binder binder )
{
SimpleScope batchScope = new SimpleScope();
// tell Guice about the scope
binder.bindScope( MojoExecutionScoped.class, batchScope );
// make our scope instance injectable
binder.bind( SimpleScope.class ).annotatedWith( Names.named( "mojoExecution" ) ).toInstance( batchScope );
}
};
( (DefaultPlexusContainer) getContainer() ).addPlexusInjector( Collections.<PlexusBeanModule> emptyList(),
module );
}
protected MavenProject readMavenProject( File basedir )
throws ProjectBuildingException, Exception
{
File pom = new File( basedir, "pom.xml" );
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory( basedir );
ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
MavenProject project = lookup( ProjectBuilder.class ).build( pom, configuration ).getProject();
assertNotNull( project );
return project;
}
protected File getBasedir( String location )
throws IOException
{
File src = new File( location );
File basedir = new File( "target/ut/", src.getName() ).getCanonicalFile();
FileUtils.deleteDirectory( basedir );
assertTrue( basedir.mkdirs() );
FileUtils.copyDirectoryStructure( src, basedir );
return basedir;
}
protected void executeMojo( File basedir )
throws Exception
{
MavenProject project = readMavenProject( basedir );
MojoExecution execution = newMojoExecution( "antlr" );
MavenSession session = newMavenSession( project );
SimpleScope scope = lookup( SimpleScope.class, "mojoExecution" );
try
{
scope.enter();
BuildContext buildContext = lookup( MavenBuildContextManager.class ).newContext( session, execution );
scope.seed( Key.get( BuildContext.class ), buildContext );
try
{
Antlr3Mojo mojo = (Antlr3Mojo) lookupConfiguredMojo( session, execution );
mojo.execute();
}
finally
{
buildContext.close();
}
}
finally
{
scope.exit();
}
}
protected void copyFile( File basedir, String from, String to )
throws IOException
{
FileUtils.copyFile( new File( basedir, from ), new File( basedir, to ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment