Skip to content

Commit c7ff3f9

Browse files
Update documentation
- javadocs for annotation - migration in getting-started - info about deprecation
1 parent 9db988a commit c7ff3f9

File tree

12 files changed

+334
-13
lines changed

12 files changed

+334
-13
lines changed

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,39 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import java.lang.annotation.ElementType;
2122
import java.lang.annotation.Inherited;
2223
import java.lang.annotation.Retention;
2324
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
2426

2527
/**
26-
* Mojo parameters container
28+
* Specifies the base directory for test resources in Maven plugin tests.
29+
* This annotation can be applied to test methods to define where test resources are located.
30+
*
31+
** <p>Example usage:</p>
32+
* <pre>
33+
* {@code
34+
* @MojoTest
35+
* class MyMojoTest {
36+
* @Test
37+
* @Basedir("src/test/resources/specific-test-case")
38+
* @InjectMojo(goal = "compile")
39+
* void testSpecificCase(MyMojo mojo) {
40+
* // Test resources will be loaded from src/test/resources/specific-test-case
41+
* mojo.execute();
42+
* }
43+
* }
44+
* }
45+
* </pre>
46+
*
47+
* @see MojoTest
48+
* @see MojoExtension
49+
* @since 3.4.0
2750
*/
2851
@Retention(RetentionPolicy.RUNTIME)
2952
@Inherited
53+
@Target(ElementType.METHOD)
3054
public @interface Basedir {
3155
String value() default "";
3256
}

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,71 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import java.lang.annotation.ElementType;
2122
import java.lang.annotation.Inherited;
2223
import java.lang.annotation.Retention;
2324
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
2426

2527
/**
28+
* Annotation used in Maven plugin tests to inject and configure a Mojo instance.
29+
* This annotation can be applied to either test methods or parameters to specify
30+
* which Mojo should be instantiated and how it should be configured.
2631
*
32+
* <p>The annotation requires a {@code goal} attribute to specify which Mojo goal
33+
* should be instantiated. Optionally, a custom {@code pom} file can be specified
34+
* to provide specific configuration for the test.</p>
35+
*
36+
* <p>Example usage on a test method:</p>
37+
* <pre>
38+
* {@code
39+
* @Test
40+
* @InjectMojo(goal = "compile")
41+
* void testCompileMojo(CompileMojo mojo) {
42+
* mojo.execute();
43+
* // verify compilation results
44+
* }
45+
* }
46+
* </pre>
47+
*
48+
* <p>Example usage with a custom POM:</p>
49+
* <pre>
50+
* {@code
51+
* @Test
52+
* @InjectMojo(
53+
* goal = "compile",
54+
* pom = "src/test/resources/test-pom.xml"
55+
* )
56+
* void testCompileMojoWithCustomConfig(CompileMojo mojo) {
57+
* mojo.execute();
58+
* // verify compilation results
59+
* }
60+
* }
61+
* </pre>
62+
*
63+
* <p>The annotation can be used in conjunction with {@link MojoParameter} to provide
64+
* specific parameter values for the Mojo:</p>
65+
* <pre>
66+
* {@code
67+
* @Test
68+
* @InjectMojo(goal = "compile")
69+
* @MojoParameter(name = "source", value = "1.8")
70+
* @MojoParameter(name = "target", value = "1.8")
71+
* void testCompileMojoWithParameters(CompileMojo mojo) {
72+
* mojo.execute();
73+
* // verify compilation results
74+
* }
75+
* }
76+
* </pre>
77+
*
78+
* @see MojoTest
79+
* @see MojoParameter
80+
* @see MojoExtension
81+
* @since 3.4.0
2782
*/
2883
@Retention(RetentionPolicy.RUNTIME)
2984
@Inherited
85+
@Target(ElementType.METHOD)
3086
public @interface InjectMojo {
3187

3288
String goal();

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,45 @@
9393
import static org.mockito.Mockito.mockingDetails;
9494

9595
/**
96-
* JUnit's extension to help testing Mojos. The extension should be automatically registered
97-
* by adding the {@link MojoTest} annotation on the test class.
96+
* JUnit Jupiter extension that provides support for testing Maven plugins (Mojos).
97+
* This extension handles the lifecycle of Mojo instances in tests, including instantiation,
98+
* configuration, and dependency injection.
99+
*
100+
* <p>The extension is automatically registered when using the {@link MojoTest} annotation
101+
* on a test class. It provides the following features:</p>
102+
* <ul>
103+
* <li>Automatic Mojo instantiation based on {@link InjectMojo} annotations</li>
104+
* <li>Parameter injection using {@link MojoParameter} annotations</li>
105+
* <li>POM configuration handling</li>
106+
* <li>Project stub creation and configuration</li>
107+
* <li>Maven session and build context setup</li>
108+
* <li>Component dependency injection</li>
109+
* </ul>
110+
*
111+
* <p>Example usage in a test class:</p>
112+
* <pre>
113+
* {@code
114+
* @MojoTest
115+
* class MyMojoTest {
116+
* @Test
117+
* @InjectMojo(goal = "my-goal")
118+
* @MojoParameter(name = "outputDirectory", value = "${project.build.directory}/generated")
119+
* void testMojoExecution(MyMojo mojo) throws Exception {
120+
* mojo.execute();
121+
* // verify execution results
122+
* }
123+
* }
124+
* }
125+
* </pre>
126+
**
127+
* <p>For custom POM configurations, you can specify a POM file using the {@link InjectMojo#pom()}
128+
* attribute. The extension will merge this configuration with default test project settings.</p>*
98129
*
99130
* @see MojoTest
100131
* @see InjectMojo
101132
* @see MojoParameter
133+
* @see Basedir
134+
* @since 3.4.0
102135
*/
103136
public class MojoExtension extends PlexusExtension implements ParameterResolver {
104137

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,58 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import java.lang.annotation.ElementType;
2122
import java.lang.annotation.Inherited;
2223
import java.lang.annotation.Repeatable;
2324
import java.lang.annotation.Retention;
2425
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
2527

2628
/**
27-
* Mojo parameter
29+
* Specifies a parameter value for a Mojo in a Maven plugin test.
30+
* This annotation can be used to configure individual Mojo parameters
31+
* without requiring a full POM file.
32+
*
33+
* <p>The annotation is repeatable, allowing multiple parameters to be set
34+
* on a single test method or parameter. For multiple parameters, you can
35+
* either use multiple {@code @MojoParameter} annotations or a single
36+
* {@link MojoParameters} annotation.</p>
37+
*
38+
* <p>Example usage with a single parameter:</p>
39+
* <pre>
40+
* {@code
41+
* @Test
42+
* @InjectMojo(goal = "compile")
43+
* @MojoParameter(name = "source", value = "1.8")
44+
* void testCompilation(CompileMojo mojo) {
45+
* mojo.execute();
46+
* }
47+
* }
48+
* </pre>
49+
*
50+
* <p>Example usage with multiple parameters:</p>
51+
* <pre>
52+
* {@code
53+
* @Test
54+
* @InjectMojo(goal = "compile")
55+
* @MojoParameter(name = "source", value = "1.8")
56+
* @MojoParameter(name = "target", value = "1.8")
57+
* @MojoParameter(name = "debug", value = "true")
58+
* void testCompilation(CompileMojo mojo) {
59+
* mojo.execute();
60+
* }
61+
* }
62+
* </pre>
63+
*
64+
* @see MojoParameters
65+
* @see InjectMojo
66+
* @see MojoTest
67+
* @since 3.4.0
2868
*/
2969
@Retention(RetentionPolicy.RUNTIME)
3070
@Repeatable(MojoParameters.class)
3171
@Inherited
72+
@Target(ElementType.METHOD)
3273
public @interface MojoParameter {
3374
String name();
3475

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,59 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import java.lang.annotation.ElementType;
2122
import java.lang.annotation.Inherited;
2223
import java.lang.annotation.Retention;
2324
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
2426

2527
/**
26-
* Mojo parameters container
28+
* Container annotation for multiple {@link MojoParameter} annotations.
29+
* This annotation is automatically used by Java when multiple {@code @MojoParameter}
30+
* annotations are applied to the same element.
31+
*
32+
* <p>While this annotation can be used directly, it's generally more convenient
33+
* to use multiple {@code @MojoParameter} annotations, which Java will automatically
34+
* wrap in this container annotation.</p>
35+
*
36+
* <p>Example of direct usage:</p>
37+
* <pre>
38+
* {@code
39+
* @Test
40+
* @InjectMojo(goal = "compile")
41+
* @MojoParameters({
42+
* @MojoParameter(name = "source", value = "1.8"),
43+
* @MojoParameter(name = "target", value = "1.8"),
44+
* @MojoParameter(name = "debug", value = "true")
45+
* })
46+
* void testCompilation(CompileMojo mojo) {
47+
* mojo.execute();
48+
* }
49+
* }
50+
* </pre>
51+
*
52+
* <p>Equivalent usage with repeatable annotation:</p>
53+
* <pre>
54+
* {@code
55+
* @Test
56+
* @InjectMojo(goal = "compile")
57+
* @MojoParameter(name = "source", value = "1.8")
58+
* @MojoParameter(name = "target", value = "1.8")
59+
* @MojoParameter(name = "debug", value = "true")
60+
* void testCompilation(CompileMojo mojo) {
61+
* mojo.execute();
62+
* }
63+
* }
64+
* </pre>
65+
*
66+
* @see MojoParameter
67+
* @see InjectMojo
68+
* @see MojoTest
69+
* @since 3.4.0
2770
*/
2871
@Retention(RetentionPolicy.RUNTIME)
2972
@Inherited
73+
@Target(ElementType.METHOD)
3074
public @interface MojoParameters {
3175
MojoParameter[] value();
3276
}

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,67 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import javax.inject.Inject;
22+
2123
import java.lang.annotation.ElementType;
2224
import java.lang.annotation.Retention;
2325
import java.lang.annotation.RetentionPolicy;
2426
import java.lang.annotation.Target;
2527

28+
import org.apache.maven.api.di.Provides;
2629
import org.junit.jupiter.api.extension.ExtendWith;
2730

2831
/**
32+
* Annotation that enables Maven plugin (Mojo) testing support in JUnit tests.
33+
* When applied to a test class, it automatically sets up the testing environment
34+
* for Maven plugins, including dependency injection and parameter resolution.
35+
*
36+
* <p>This annotation works in conjunction with {@link InjectMojo} and {@link MojoParameter}
37+
* to provide a comprehensive testing framework for Maven plugins. It automatically registers
38+
* the {@link MojoExtension} which handles the plugin lifecycle and dependency injection.</p>
39+
*
40+
* <p>Example usage:</p>
41+
* <pre>
42+
* {@code
43+
* @MojoTest
44+
* class MyMojoTest {
45+
* @Inject
46+
* private SomeComponent component;
47+
*
48+
* @Test
49+
* @InjectMojo(goal = "my-goal")
50+
* @MojoParameter(name = "parameter", value = "value")
51+
* void testMojoExecution(MyMojo mojo) {
52+
* // mojo is instantiated with the specified parameters
53+
* // component is automatically injected
54+
* mojo.execute();
55+
* // verify execution results
56+
* }
57+
*
58+
* @Provides
59+
* SomeComponent provideMockedComponent() {
60+
* return mock(SomeComponent.class);
61+
* }
62+
* }
63+
* }
64+
* </pre>
65+
*
66+
* <p>The annotation supports:</p>
67+
* <ul>
68+
* <li>Automatic Mojo instantiation and configuration</li>
69+
* <li>Parameter injection via {@link MojoParameter}</li>
70+
* <li>Component injection via {@link Inject}</li>
71+
* <li>Mock component injection via {@link Provides}</li>
72+
* <li>Custom POM configuration via {@link InjectMojo#pom()}</li>
73+
* <li>Base directory configuration for test resources via {@link Basedir}</li>
74+
* </ul>
75+
*
2976
*
77+
* @see MojoExtension
78+
* @see InjectMojo
79+
* @see MojoParameter
80+
* @see Provides
81+
* @since 4.0.0
3082
*/
3183
@Retention(RetentionPolicy.RUNTIME)
3284
@ExtendWith(MojoExtension.class)

maven-plugin-testing-harness/src/site/markdown/examples/artifact.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ date: February 2008
2121
## Testing Project Artifact
2222

2323

24+
### NOTE
25+
26+
`JUnit 3` based tests are deprecated since `3.4.0`.
27+
28+
Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples.
29+
2430
**Note**: This example improves the [cookbook](../getting-started/index.html) to play with artifact handler.
2531

2632

maven-plugin-testing-harness/src/site/markdown/examples/complex-mojo-parameters.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ date: February 2008
2020
<!-- under the License. -->
2121
## Testing Complex Mojo Parameters
2222

23+
### NOTE
24+
25+
`JUnit 3` based tests are deprecated since `3.4.0`.
26+
27+
Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples.
2328

2429
**Note**: This example improves the [cookbook](../getting-started/index.html) for testing complex Mojo parameters.
2530

maven-plugin-testing-harness/src/site/markdown/examples/multiproject.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ date: February 2008
2020
<!-- under the License. -->
2121
## Testing Multiproject
2222

23+
### NOTE
24+
25+
`JUnit 3` based tests are deprecated since `3.4.0`.
26+
27+
Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples.
2328

2429
**Note**: This example improves the [cookbook](../getting-started/index.html) for multi-project testing.
2530

maven-plugin-testing-harness/src/site/markdown/examples/repositories.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ date: February 2008
2020
<!-- under the License. -->
2121
## Testing Using Repositories
2222

23+
### NOTE
24+
25+
`JUnit 3` based tests are deprecated since `3.4.0`.
26+
27+
Use JUnit 5 annotations, consult [javadocs](../apidocs/org/apache/maven/api/plugin/testing/package-summary.html) for examples.
2328

2429
**Note**: This example improves the [cookbook](../getting-started/index.html) for testing repositories.
2530

0 commit comments

Comments
 (0)