diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java index 69eaba23..bb0b9876 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java @@ -18,15 +18,39 @@ */ package org.apache.maven.api.plugin.testing; +import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** - * Mojo parameters container + * Specifies the base directory for test resources in Maven plugin tests. + * This annotation can be applied to test methods to define where test resources are located. + * + **

Example usage:

+ *
+ * {@code
+ * @MojoTest
+ * class MyMojoTest {
+ *     @Test
+ *     @Basedir("src/test/resources/specific-test-case")
+ *     @InjectMojo(goal = "compile")
+ *     void testSpecificCase(MyMojo mojo) {
+ *         // Test resources will be loaded from src/test/resources/specific-test-case
+ *         mojo.execute();
+ *     }
+ * }
+ * }
+ * 
+ * + * @see MojoTest + * @see MojoExtension + * @since 3.4.0 */ @Retention(RetentionPolicy.RUNTIME) @Inherited +@Target(ElementType.METHOD) public @interface Basedir { String value() default ""; } diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java index 272eb9b4..24961bcb 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java @@ -18,15 +18,71 @@ */ package org.apache.maven.api.plugin.testing; +import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** + * Annotation used in Maven plugin tests to inject and configure a Mojo instance. + * This annotation can be applied to either test methods or parameters to specify + * which Mojo should be instantiated and how it should be configured. * + *

The annotation requires a {@code goal} attribute to specify which Mojo goal + * should be instantiated. Optionally, a custom {@code pom} file can be specified + * to provide specific configuration for the test.

+ * + *

Example usage on a test method:

+ *
+ * {@code
+ * @Test
+ * @InjectMojo(goal = "compile")
+ * void testCompileMojo(CompileMojo mojo) {
+ *     mojo.execute();
+ *     // verify compilation results
+ * }
+ * }
+ * 
+ * + *

Example usage with a custom POM:

+ *
+ * {@code
+ * @Test
+ * @InjectMojo(
+ *     goal = "compile",
+ *     pom = "src/test/resources/test-pom.xml"
+ * )
+ * void testCompileMojoWithCustomConfig(CompileMojo mojo) {
+ *     mojo.execute();
+ *     // verify compilation results
+ * }
+ * }
+ * 
+ * + *

The annotation can be used in conjunction with {@link MojoParameter} to provide + * specific parameter values for the Mojo:

+ *
+ * {@code
+ * @Test
+ * @InjectMojo(goal = "compile")
+ * @MojoParameter(name = "source", value = "1.8")
+ * @MojoParameter(name = "target", value = "1.8")
+ * void testCompileMojoWithParameters(CompileMojo mojo) {
+ *     mojo.execute();
+ *     // verify compilation results
+ * }
+ * }
+ * 
+ * + * @see MojoTest + * @see MojoParameter + * @see MojoExtension + * @since 3.4.0 */ @Retention(RetentionPolicy.RUNTIME) @Inherited +@Target(ElementType.METHOD) public @interface InjectMojo { String goal(); diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java index 268a033f..2697dcb6 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java @@ -93,12 +93,45 @@ import static org.mockito.Mockito.mockingDetails; /** - * JUnit's extension to help testing Mojos. The extension should be automatically registered - * by adding the {@link MojoTest} annotation on the test class. + * JUnit Jupiter extension that provides support for testing Maven plugins (Mojos). + * This extension handles the lifecycle of Mojo instances in tests, including instantiation, + * configuration, and dependency injection. + * + *

The extension is automatically registered when using the {@link MojoTest} annotation + * on a test class. It provides the following features:

+ * + * + *

Example usage in a test class:

+ *
+ * {@code
+ * @MojoTest
+ * class MyMojoTest {
+ *     @Test
+ *     @InjectMojo(goal = "my-goal")
+ *     @MojoParameter(name = "outputDirectory", value = "${project.build.directory}/generated")
+ *     void testMojoExecution(MyMojo mojo) throws Exception {
+ *         mojo.execute();
+ *         // verify execution results
+ *     }
+ * }
+ * }
+ * 
+ ** + *

For custom POM configurations, you can specify a POM file using the {@link InjectMojo#pom()} + * attribute. The extension will merge this configuration with default test project settings.

* * * @see MojoTest * @see InjectMojo * @see MojoParameter + * @see Basedir + * @since 3.4.0 */ public class MojoExtension extends PlexusExtension implements ParameterResolver { diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java index 8c37804f..276ef659 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java @@ -18,17 +18,58 @@ */ package org.apache.maven.api.plugin.testing; +import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** - * Mojo parameter + * Specifies a parameter value for a Mojo in a Maven plugin test. + * This annotation can be used to configure individual Mojo parameters + * without requiring a full POM file. + * + *

The annotation is repeatable, allowing multiple parameters to be set + * on a single test method or parameter. For multiple parameters, you can + * either use multiple {@code @MojoParameter} annotations or a single + * {@link MojoParameters} annotation.

+ * + *

Example usage with a single parameter:

+ *
+ * {@code
+ * @Test
+ * @InjectMojo(goal = "compile")
+ * @MojoParameter(name = "source", value = "1.8")
+ * void testCompilation(CompileMojo mojo) {
+ *     mojo.execute();
+ * }
+ * }
+ * 
+ * + *

Example usage with multiple parameters:

+ *
+ * {@code
+ * @Test
+ * @InjectMojo(goal = "compile")
+ * @MojoParameter(name = "source", value = "1.8")
+ * @MojoParameter(name = "target", value = "1.8")
+ * @MojoParameter(name = "debug", value = "true")
+ * void testCompilation(CompileMojo mojo) {
+ *     mojo.execute();
+ * }
+ * }
+ * 
+ * + * @see MojoParameters + * @see InjectMojo + * @see MojoTest + * @since 3.4.0 */ @Retention(RetentionPolicy.RUNTIME) @Repeatable(MojoParameters.class) @Inherited +@Target(ElementType.METHOD) public @interface MojoParameter { String name(); diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java index 373c9260..ebe63bf8 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java @@ -18,15 +18,59 @@ */ package org.apache.maven.api.plugin.testing; +import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** - * Mojo parameters container + * Container annotation for multiple {@link MojoParameter} annotations. + * This annotation is automatically used by Java when multiple {@code @MojoParameter} + * annotations are applied to the same element. + * + *

While this annotation can be used directly, it's generally more convenient + * to use multiple {@code @MojoParameter} annotations, which Java will automatically + * wrap in this container annotation.

+ * + *

Example of direct usage:

+ *
+ * {@code
+ * @Test
+ * @InjectMojo(goal = "compile")
+ * @MojoParameters({
+ *     @MojoParameter(name = "source", value = "1.8"),
+ *     @MojoParameter(name = "target", value = "1.8"),
+ *     @MojoParameter(name = "debug", value = "true")
+ * })
+ * void testCompilation(CompileMojo mojo) {
+ *     mojo.execute();
+ * }
+ * }
+ * 
+ * + *

Equivalent usage with repeatable annotation:

+ *
+ * {@code
+ * @Test
+ * @InjectMojo(goal = "compile")
+ * @MojoParameter(name = "source", value = "1.8")
+ * @MojoParameter(name = "target", value = "1.8")
+ * @MojoParameter(name = "debug", value = "true")
+ * void testCompilation(CompileMojo mojo) {
+ *     mojo.execute();
+ * }
+ * }
+ * 
+ * + * @see MojoParameter + * @see InjectMojo + * @see MojoTest + * @since 3.4.0 */ @Retention(RetentionPolicy.RUNTIME) @Inherited +@Target(ElementType.METHOD) public @interface MojoParameters { MojoParameter[] value(); } diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java index eb94c091..4aa9bbfc 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java @@ -18,15 +18,67 @@ */ package org.apache.maven.api.plugin.testing; +import javax.inject.Inject; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.apache.maven.api.di.Provides; import org.junit.jupiter.api.extension.ExtendWith; /** + * Annotation that enables Maven plugin (Mojo) testing support in JUnit tests. + * When applied to a test class, it automatically sets up the testing environment + * for Maven plugins, including dependency injection and parameter resolution. + * + *

This annotation works in conjunction with {@link InjectMojo} and {@link MojoParameter} + * to provide a comprehensive testing framework for Maven plugins. It automatically registers + * the {@link MojoExtension} which handles the plugin lifecycle and dependency injection.

+ * + *

Example usage:

+ *
+ * {@code
+ * @MojoTest
+ * class MyMojoTest {
+ *     @Inject
+ *     private SomeComponent component;
+ *
+ *     @Test
+ *     @InjectMojo(goal = "my-goal")
+ *     @MojoParameter(name = "parameter", value = "value")
+ *     void testMojoExecution(MyMojo mojo) {
+ *         // mojo is instantiated with the specified parameters
+ *         // component is automatically injected
+ *         mojo.execute();
+ *         // verify execution results
+ *     }
+ *
+ *     @Provides
+ *     SomeComponent provideMockedComponent() {
+ *         return mock(SomeComponent.class);
+ *     }
+ * }
+ * }
+ * 
+ * + *

The annotation supports:

+ * + * * + * @see MojoExtension + * @see InjectMojo + * @see MojoParameter + * @see Provides + * @since 4.0.0 */ @Retention(RetentionPolicy.RUNTIME) @ExtendWith(MojoExtension.class) diff --git a/maven-plugin-testing-harness/src/site/markdown/examples/artifact.md b/maven-plugin-testing-harness/src/site/markdown/examples/artifact.md index c6061050..31689070 100644 --- a/maven-plugin-testing-harness/src/site/markdown/examples/artifact.md +++ b/maven-plugin-testing-harness/src/site/markdown/examples/artifact.md @@ -21,6 +21,12 @@ date: February 2008 ## Testing Project Artifact +### NOTE + +`JUnit 3` based tests are deprecated since `3.4.0`. + +Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples. + **Note**: This example improves the [cookbook](../getting-started/index.html) to play with artifact handler. diff --git a/maven-plugin-testing-harness/src/site/markdown/examples/complex-mojo-parameters.md b/maven-plugin-testing-harness/src/site/markdown/examples/complex-mojo-parameters.md index a354007a..fd76055e 100644 --- a/maven-plugin-testing-harness/src/site/markdown/examples/complex-mojo-parameters.md +++ b/maven-plugin-testing-harness/src/site/markdown/examples/complex-mojo-parameters.md @@ -20,6 +20,11 @@ date: February 2008 ## Testing Complex Mojo Parameters +### NOTE + +`JUnit 3` based tests are deprecated since `3.4.0`. + +Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples. **Note**: This example improves the [cookbook](../getting-started/index.html) for testing complex Mojo parameters. diff --git a/maven-plugin-testing-harness/src/site/markdown/examples/multiproject.md b/maven-plugin-testing-harness/src/site/markdown/examples/multiproject.md index f1580566..7b762605 100644 --- a/maven-plugin-testing-harness/src/site/markdown/examples/multiproject.md +++ b/maven-plugin-testing-harness/src/site/markdown/examples/multiproject.md @@ -20,6 +20,11 @@ date: February 2008 ## Testing Multiproject +### NOTE + +`JUnit 3` based tests are deprecated since `3.4.0`. + +Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples. **Note**: This example improves the [cookbook](../getting-started/index.html) for multi-project testing. diff --git a/maven-plugin-testing-harness/src/site/markdown/examples/repositories.md b/maven-plugin-testing-harness/src/site/markdown/examples/repositories.md index d1ec4c32..1098a4a8 100644 --- a/maven-plugin-testing-harness/src/site/markdown/examples/repositories.md +++ b/maven-plugin-testing-harness/src/site/markdown/examples/repositories.md @@ -20,6 +20,11 @@ date: February 2008 ## Testing Using Repositories +### NOTE + +`JUnit 3` based tests are deprecated since `3.4.0`. + +Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples. **Note**: This example improves the [cookbook](../getting-started/index.html) for testing repositories. diff --git a/maven-plugin-testing-harness/src/site/markdown/getting-started/index.md b/maven-plugin-testing-harness/src/site/markdown/getting-started/index.md index 73f74ce8..2b943dbc 100644 --- a/maven-plugin-testing-harness/src/site/markdown/getting-started/index.md +++ b/maven-plugin-testing-harness/src/site/markdown/getting-started/index.md @@ -23,6 +23,63 @@ date: 2008-08-27 This guide is intended as a reference for those developing Maven plugins, with self-contained references and solutions for common testing cases. +### NOTE + +`AbstractMojoTestCase` is based on `JUnit 3` and is deprecated since `3.4.0`. + +Instead of it use `@MojoTest` and other JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples. + +#### Migration receipts + +Replace + +```java +public class MyMojoTest extends AbstractMojoTestCase { +} +``` + +by + +```java +@MojoTest +class MyMojoTest { +} +``` + +--- + + Replace + +```java +MyMojo myMojo = (MyMojo) lookupMojo("goal", pom); +``` + +by + +```java +@InjectMojo(goal = "goal", pom = "src/test/resources/unit/project-to-test/pom.xml") +``` + +--- + +Replace + +```java +public void test() {} + MyCoponent myCoponent = lookup(MyCoponent.class); +} +``` + +by + +```java +@MojoTest +class MyMojoTest { + + @Inject + private MyCoponent myCoponent; +} +``` ### Prerequisites @@ -30,7 +87,6 @@ date: 2008-08-27 We assume that you have already created a plugin. In this cookbook, we make reference to `MyMojo` in `maven-my-plugin` which is generated by the Maven Archetype Plugin, i.e.: - ``` mvn archetype:create \ -DgroupId=org.apache.maven.plugin.my \ @@ -40,8 +96,6 @@ mvn archetype:create \ The generated structure should be: - - ``` maven-my-plugin |- pom.xml @@ -66,7 +120,6 @@ maven-my-plugin As usual, just add `maven-plugin-testing-harness` as following in your pom. Be sure to specify `test` scope. - ``` ... @@ -88,8 +141,6 @@ maven-my-plugin Create a `MyMojoTest` (by convention) class in `src/test/java/org/apache/maven/plugin/my` directory. This class should extend `AbstractMojoTestCase` from `maven-plugin-testing-harness`. - - ``` import org.apache.maven.plugin.testing.AbstractMojoTestCase; @@ -254,7 +305,6 @@ public class MyMojoTest As usual, just call: - ``` mvn test ``` diff --git a/maven-plugin-testing-harness/src/site/markdown/index.md b/maven-plugin-testing-harness/src/site/markdown/index.md index b3c9461f..00dddda0 100644 --- a/maven-plugin-testing-harness/src/site/markdown/index.md +++ b/maven-plugin-testing-harness/src/site/markdown/index.md @@ -30,7 +30,7 @@ date: February 2008 ### Examples - The following examples shows how to use the Testing Harness in more advanced usecases: + The following examples shows how to use the Testing Harness in more advanced use cases: