diff --git a/integration-tests/maven-core-it-plugin/pom.xml b/integration-tests/maven-core-it-plugin/pom.xml index cfa00d90bb..2da63b2df5 100644 --- a/integration-tests/maven-core-it-plugin/pom.xml +++ b/integration-tests/maven-core-it-plugin/pom.xml @@ -32,5 +32,10 @@ jline 0.9.1 + + bsh + bsh + 1.3.0 + diff --git a/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java b/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java new file mode 100644 index 0000000000..a8752e5635 --- /dev/null +++ b/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java @@ -0,0 +1,59 @@ +package org.apache.maven.plugin.coreit; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; + +/** + * @goal loadable + * @requiresDependencyResolution test + */ +public class LoadableMojo + extends AbstractMojo +{ + /** + * @parameter + * @required + */ + private String className; + + public void execute() throws MojoFailureException + { + if ( !load( true ) || !load( false ) ) + { + throw new MojoFailureException( this, "Class-loading test failed..", "Failed to load class: " + className + " using one or more methods." ); + } + } + + private boolean load( boolean useContextClassloader ) throws MojoFailureException + { + getLog().info( "Executing in java version: " + System.getProperty( "java.version" ) ); + + ClassLoader cl; + if ( useContextClassloader ) + { + cl = Thread.currentThread().getContextClassLoader(); + } + else + { + cl = this.getClass().getClassLoader(); + } + + getLog().info( "Attepting to load: " + className + " from: " + cl + (useContextClassloader ? " (context classloader)" : "" ) ); + + try + { + Class result = cl.loadClass( className ); + + getLog().info( "Load succeeded." ); + + return true; + } + catch ( ClassNotFoundException e ) + { + getLog().info( "Failed to load class: " + className + + (useContextClassloader ? " using context classloader" : "") ); + + return false; + } + } +} diff --git a/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java b/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java new file mode 100644 index 0000000000..4d73d1d8de --- /dev/null +++ b/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java @@ -0,0 +1,54 @@ +package org.apache.maven.plugin.coreit; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; + +import java.net.URL; + +/** + * @goal reachable + * @requiresDependencyResolution test + */ +public class ReachableMojo extends AbstractMojo +{ + /** + * @parameter + * @required + */ + private String resource; + + public void execute() + throws MojoFailureException + { + if ( !reach( true ) || !reach( false ) ) + { + throw new MojoFailureException( this, "Resource reachability test failed..", "Failed to reach resource: " + resource + " using one or more methods." ); + } + } + + public boolean reach( boolean useContextClassloader ) throws MojoFailureException + { + ClassLoader cl; + if ( useContextClassloader ) + { + cl = Thread.currentThread().getContextClassLoader(); + } + else + { + cl = this.getClass().getClassLoader(); + } + + URL result = cl.getResource( resource ); + + getLog().info( "Attepting to reach: " + resource + " from: " + cl + (useContextClassloader ? " (context classloader)" : "" ) + ( result == null ? ": FAILED" : ":SUCCEEDED" ) ); + + if ( result == null ) + { + getLog().info( "Cannot find resource: " + resource + (useContextClassloader?" in context classloader":"") ); + + return false; + } + + return true; + } +} diff --git a/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java b/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java new file mode 100644 index 0000000000..1d91dd24ab --- /dev/null +++ b/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java @@ -0,0 +1,40 @@ +package org.apache.maven.plugin.coreit; + +import bsh.EvalError; +import bsh.Interpreter; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; + +/** + * @goal runnable + * @requiresDependencyResolution test + */ +public class RunnableMojo + extends AbstractMojo +{ + /** + * @parameter + * @required + */ + private String script; + + public void execute() throws MojoFailureException + { + Interpreter terp = new Interpreter(); + + try + { + getLog().info( "Executing in java version: " + System.getProperty( "java.version" ) ); + + Class result = (Class) terp.eval( script ); + + getLog().info( "Result of script evaluation was: " + result + "\nLoaded from: " + result.getClassLoader() ); + } + catch ( EvalError e ) + { + throw new MojoFailureException( this, "Failed to evaluate script.", "Script: \n\n" + script + + "\n\nfailed to evaluate. Error: " + e.getMessage() + "\nLine: " + e.getErrorLineNumber() ); + } + } +}