diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java index b75b492..fb69c6e 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java @@ -36,6 +36,7 @@ import java.util.Map; import java.util.Properties; +import com.google.inject.Module; import org.apache.commons.io.input.XmlStreamReader; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; @@ -81,8 +82,6 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomBuilder; -import com.google.inject.Module; - /** * TODO: add a way to use the plugin POM for the lookup so that the user doesn't have to provide the a:g:v:goal * as the role hint for the mojo lookup. @@ -304,7 +303,7 @@ protected PlexusContainer getContainer() * @return a Mojo instance * @throws Exception */ - protected Mojo lookupMojo( String goal, String pluginPom ) + protected T lookupMojo( String goal, String pluginPom ) throws Exception { return lookupMojo( goal, new File( pluginPom ) ); @@ -318,7 +317,7 @@ protected Mojo lookupMojo( String goal, String pluginPom ) * @return a Mojo instance * @throws Exception */ - protected Mojo lookupEmptyMojo( String goal, String pluginPom ) + protected T lookupEmptyMojo( String goal, String pluginPom ) throws Exception { return lookupEmptyMojo( goal, new File( pluginPom ) ); @@ -332,7 +331,7 @@ protected Mojo lookupEmptyMojo( String goal, String pluginPom ) * @return a Mojo instance * @throws Exception */ - protected Mojo lookupMojo( String goal, File pom ) + protected T lookupMojo( String goal, File pom ) throws Exception { File pluginPom = new File( getBasedir(), "pom.xml" ); @@ -358,7 +357,7 @@ protected Mojo lookupMojo( String goal, File pom ) * @return a Mojo instance * @throws Exception */ - protected Mojo lookupEmptyMojo( String goal, File pom ) + protected T lookupEmptyMojo( String goal, File pom ) throws Exception { File pluginPom = new File( getBasedir(), "pom.xml" ); @@ -394,7 +393,7 @@ protected Mojo lookupMojo( String groupId, String artifactId, String version, St * @return a Mojo instance * @throws Exception */ - protected Mojo lookupMojo( String groupId, String artifactId, String version, String goal, + protected T lookupMojo( String groupId, String artifactId, String version, String goal, PlexusConfiguration pluginConfiguration ) throws Exception { @@ -402,7 +401,7 @@ protected Mojo lookupMojo( String groupId, String artifactId, String version, St // pluginkey = groupId : artifactId : version : goal - Mojo mojo = lookup( Mojo.class, groupId + ":" + artifactId + ":" + version + ":" + goal ); + T mojo = (T) lookup( Mojo.class, groupId + ":" + artifactId + ":" + version + ":" + goal ); LoggerManager loggerManager = getContainer().lookup( LoggerManager.class ); @@ -425,21 +424,21 @@ protected Mojo lookupMojo( String groupId, String artifactId, String version, St } /** - * + * * @param project * @param goal * @return * @throws Exception * @since 2.0 */ - protected Mojo lookupConfiguredMojo( MavenProject project, String goal ) + protected T lookupConfiguredMojo( MavenProject project, String goal ) throws Exception { return lookupConfiguredMojo( newMavenSession( project ), newMojoExecution( goal ) ); } /** - * + * * @param session * @param execution * @return @@ -447,13 +446,13 @@ protected Mojo lookupConfiguredMojo( MavenProject project, String goal ) * @throws ComponentConfigurationException * @since 2.0 */ - protected Mojo lookupConfiguredMojo( MavenSession session, MojoExecution execution ) + protected T lookupConfiguredMojo( MavenSession session, MojoExecution execution ) throws Exception, ComponentConfigurationException { MavenProject project = session.getCurrentProject(); MojoDescriptor mojoDescriptor = execution.getMojoDescriptor(); - Mojo mojo = (Mojo) lookup( mojoDescriptor.getRole(), mojoDescriptor.getRoleHint() ); + T mojo = (T) lookup( mojoDescriptor.getRole(), mojoDescriptor.getRoleHint() ); ExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator( session, execution ); @@ -639,7 +638,7 @@ protected PlexusConfiguration extractPluginConfiguration( String artifactId, Xpp * @return a Mojo instance * @throws Exception */ - protected Mojo configureMojo( Mojo mojo, String artifactId, File pom ) + protected T configureMojo( T mojo, String artifactId, File pom ) throws Exception { validateContainerStatus(); @@ -661,7 +660,7 @@ protected Mojo configureMojo( Mojo mojo, String artifactId, File pom ) * @return a Mojo instance * @throws Exception */ - protected Mojo configureMojo( Mojo mojo, PlexusConfiguration pluginConfiguration ) + protected T configureMojo( T mojo, PlexusConfiguration pluginConfiguration ) throws Exception { validateContainerStatus(); @@ -683,14 +682,14 @@ protected Mojo configureMojo( Mojo mojo, PlexusConfiguration pluginConfiguration * @return object value of variable * @throws IllegalArgumentException */ - protected Object getVariableValueFromObject( Object object, String variable ) + protected T getVariableValueFromObject( Object object, String variable ) throws IllegalAccessException { Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( variable, object.getClass() ); field.setAccessible( true ); - return field.get( object ); + return (T) field.get( object ); } /** @@ -748,7 +747,7 @@ protected Map getVariablesAndValuesFromObject( Class clazz, O * @param value * @throws IllegalAccessException */ - protected void setVariableValueToObject( Object object, String variable, Object value ) + protected void setVariableValueToObject( Object object, String variable, T value ) throws IllegalAccessException { Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( variable, object.getClass() ); diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java index 2fef3db..04d2564 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java @@ -132,7 +132,7 @@ public PlexusContainer getContainer() * @return a Mojo instance * @throws Exception */ - public Mojo lookupMojo( String goal, String pluginPom ) + public T lookupMojo( String goal, String pluginPom ) throws Exception { return testCase.lookupMojo( goal, pluginPom ); @@ -146,7 +146,7 @@ public Mojo lookupMojo( String goal, String pluginPom ) * @return a Mojo instance * @throws Exception */ - public Mojo lookupEmptyMojo( String goal, String pluginPom ) + public T lookupEmptyMojo( String goal, String pluginPom ) throws Exception { return testCase.lookupEmptyMojo( goal, new File( pluginPom ) ); @@ -160,7 +160,7 @@ public Mojo lookupEmptyMojo( String goal, String pluginPom ) * @return a Mojo instance * @throws Exception */ - public Mojo lookupMojo( String goal, File pom ) + public T lookupMojo( String goal, File pom ) throws Exception { return testCase.lookupMojo( goal, pom ); @@ -174,26 +174,26 @@ public Mojo lookupMojo( String goal, File pom ) * @return a Mojo instance * @throws Exception */ - public Mojo lookupEmptyMojo( String goal, File pom ) + public T lookupEmptyMojo( String goal, File pom ) throws Exception { return testCase.lookupEmptyMojo( goal, pom ); } - public Mojo lookupMojo( String groupId, String artifactId, String version, String goal, + public T lookupMojo( String groupId, String artifactId, String version, String goal, PlexusConfiguration pluginConfiguration ) throws Exception { return testCase.lookupMojo( groupId, artifactId, version, goal, pluginConfiguration ); } - public Mojo lookupConfiguredMojo( MavenProject project, String goal ) + public T lookupConfiguredMojo( MavenProject project, String goal ) throws Exception { return testCase.lookupConfiguredMojo( project, goal ); } - public Mojo lookupConfiguredMojo( MavenSession session, MojoExecution execution ) + public T lookupConfiguredMojo( MavenSession session, MojoExecution execution ) throws Exception, ComponentConfigurationException { return testCase.lookupConfiguredMojo( session, execution ); @@ -221,13 +221,13 @@ public PlexusConfiguration extractPluginConfiguration( String artifactId, Xpp3Do return testCase.extractPluginConfiguration( artifactId, pomDom ); } - public Mojo configureMojo( Mojo mojo, String artifactId, File pom ) + public T configureMojo( T mojo, String artifactId, File pom ) throws Exception { return testCase.configureMojo( mojo, artifactId, pom ); } - public Mojo configureMojo( Mojo mojo, PlexusConfiguration pluginConfiguration ) + public T configureMojo( T mojo, PlexusConfiguration pluginConfiguration ) throws Exception { return testCase.configureMojo( mojo, pluginConfiguration ); @@ -243,7 +243,7 @@ public Mojo configureMojo( Mojo mojo, PlexusConfiguration pluginConfiguration ) * @return object value of variable * @throws IllegalArgumentException */ - public Object getVariableValueFromObject( Object object, String variable ) + public T getVariableValueFromObject( Object object, String variable ) throws IllegalAccessException { return testCase.getVariableValueFromObject( object, variable ); @@ -286,7 +286,7 @@ public Map getVariablesAndValuesFromObject( Class clazz, Obje * @param value * @throws IllegalAccessException */ - public void setVariableValueToObject( Object object, String variable, Object value ) + public void setVariableValueToObject( Object object, String variable, T value ) throws IllegalAccessException { testCase.setVariableValueToObject( object, variable, value ); @@ -350,7 +350,7 @@ public void executeMojo( File basedir, String goal ) /** * @since 3.1.0 */ - public Mojo lookupConfiguredMojo( File basedir, String goal ) + public T lookupConfiguredMojo( File basedir, String goal ) throws Exception, ComponentConfigurationException { MavenProject project = readMavenProject( basedir ); diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoRuleTest.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoRuleTest.java index 8a11b8e..0c7bc58 100644 --- a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoRuleTest.java +++ b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoRuleTest.java @@ -19,18 +19,21 @@ * under the License. */ -import org.codehaus.plexus.configuration.PlexusConfiguration; -import org.codehaus.plexus.util.xml.Xpp3Dom; -import org.codehaus.plexus.util.xml.Xpp3DomBuilder; - +import java.io.File; import java.io.StringReader; import java.util.Map; -import org.junit.Rule; -import static org.junit.Assert.*; +import org.codehaus.plexus.configuration.PlexusConfiguration; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + /** * @author Mirko Friedenhagen */ @@ -95,7 +98,7 @@ public void testMojoConfiguration() { SimpleMojo mojo = new SimpleMojo(); - mojo = (SimpleMojo) rule.configureMojo( mojo, pluginConfiguration ); + mojo = rule.configureMojo( mojo, pluginConfiguration ); assertEquals( "valueOne", mojo.getKeyOne() ); @@ -111,7 +114,7 @@ public void testVariableAccessWithoutGetter() { SimpleMojo mojo = new SimpleMojo(); - mojo = (SimpleMojo) rule.configureMojo( mojo, pluginConfiguration ); + mojo = rule.configureMojo( mojo, pluginConfiguration ); assertEquals( "valueOne", rule.getVariableValueFromObject( mojo, "keyOne" ) ); @@ -127,7 +130,7 @@ public void testVariableAccessWithoutGetter2() { SimpleMojo mojo = new SimpleMojo(); - mojo = (SimpleMojo) rule.configureMojo( mojo, pluginConfiguration ); + mojo = rule.configureMojo( mojo, pluginConfiguration ); Map map = rule.getVariablesAndValuesFromObject( mojo ); @@ -145,7 +148,7 @@ public void testSettingMojoVariables() { SimpleMojo mojo = new SimpleMojo(); - mojo = (SimpleMojo) rule.configureMojo( mojo, pluginConfiguration ); + mojo = rule.configureMojo( mojo, pluginConfiguration ); rule.setVariableValueToObject( mojo, "keyOne", "myValueOne" ); @@ -165,4 +168,19 @@ public void testWithRuleWrapper() { assertTrue( "before executed because WithMojo annotation was not added", beforeWasCalled ); } + + /** + * @throws Exception if any + */ + @Test + public void testLookupInitializedMojo() + throws Exception + { + File pomBaseDir = new File( "src/test/projects/property" ); + ParametersMojo mojo = rule.lookupConfiguredMojo( pomBaseDir, "parameters" ); + assertEquals( "default", rule.getVariableValueFromObject( mojo, "withDefault" ) ); + assertEquals( "propertyValue", rule.getVariableValueFromObject( mojo, "withProperty" ) ); + assertEquals( "propertyValue", rule.getVariableValueFromObject( mojo, "withPropertyAndDefault" ) ); + } + } diff --git a/maven-plugin-testing-harness/src/test/projects/property/pom.xml b/maven-plugin-testing-harness/src/test/projects/property/pom.xml new file mode 100644 index 0000000..167b598 --- /dev/null +++ b/maven-plugin-testing-harness/src/test/projects/property/pom.xml @@ -0,0 +1,52 @@ + + + + + + 4.0.0 + + test + test-test + 1.0-SNAPSHOT + jar + + + propertyValue + + + + + + test + test-plugin + 0.0.1-SNAPSHOT + + + test + + test + + compile + + + + + +