|
18 | 18 | */ |
19 | 19 | package org.apache.maven.plugin.testing; |
20 | 20 |
|
21 | | -import java.io.File; |
22 | | - |
23 | | -import org.apache.maven.execution.DefaultMavenExecutionRequest; |
24 | | -import org.apache.maven.execution.MavenExecutionRequest; |
25 | | -import org.apache.maven.execution.MavenSession; |
26 | | -import org.apache.maven.plugin.MojoExecution; |
27 | | -import org.apache.maven.project.MavenProject; |
28 | | -import org.apache.maven.project.ProjectBuilder; |
29 | | -import org.apache.maven.project.ProjectBuildingException; |
30 | | -import org.apache.maven.project.ProjectBuildingRequest; |
31 | | -import org.eclipse.aether.DefaultRepositorySystemSession; |
32 | | - |
33 | | -public class ParametersMojoTest extends AbstractMojoTestCase { |
34 | | - public void testDefault() throws Exception { |
35 | | - MavenProject project = readMavenProject(new File("src/test/projects/default")); |
36 | | - |
37 | | - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); |
38 | | - |
39 | | - assertNull(mojo.getPlain()); |
40 | | - assertNull(mojo.getWithProperty()); |
41 | | - assertEquals("default", mojo.getWithDefault()); |
42 | | - assertEquals("default", mojo.getWithPropertyAndDefault()); |
43 | | - } |
| 21 | +import javax.inject.Inject; |
| 22 | + |
| 23 | +import org.apache.maven.plugin.logging.Log; |
| 24 | +import org.apache.maven.plugin.testing.junit5.InjectMojo; |
| 25 | +import org.apache.maven.plugin.testing.junit5.MojoParameter; |
| 26 | +import org.apache.maven.plugin.testing.junit5.MojoParameters; |
| 27 | +import org.apache.maven.plugin.testing.junit5.MojoTest; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.*; |
| 33 | + |
| 34 | +@MojoTest |
| 35 | +public class ParametersMojoTest { |
| 36 | + |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(ParametersMojoTest.class); |
| 38 | + |
| 39 | + private static final String DEFAULT_POM = "src/test/projects/default/pom.xml"; |
44 | 40 |
|
45 | | - public void testExplicit() throws Exception { |
46 | | - MavenProject project = readMavenProject(new File("src/test/projects/explicit")); |
| 41 | + private static final String EXPLICIT_POM = "src/test/projects/explicit/pom.xml"; |
47 | 42 |
|
48 | | - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); |
| 43 | + private static final String PROPERTY_POM = "src/test/projects/property/pom.xml"; |
49 | 44 |
|
50 | | - assertEquals("explicitValue", mojo.getPlain()); |
51 | | - assertEquals("explicitWithPropertyValue", mojo.getWithProperty()); |
52 | | - assertEquals("explicitWithDefaultValue", mojo.getWithDefault()); |
53 | | - assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault()); |
| 45 | + @Inject |
| 46 | + private Log log; |
| 47 | + |
| 48 | + @Test |
| 49 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) |
| 50 | + void testDefaultPom(ParametersMojo mojo) { |
| 51 | + assertDoesNotThrow(mojo::execute); |
54 | 52 | } |
55 | 53 |
|
56 | | - public void testDefaultWithProperty() throws Exception { |
57 | | - MavenProject project = readMavenProject(new File("src/test/projects/default")); |
58 | | - MavenSession session = newMavenSession(project); |
59 | | - MojoExecution execution = newMojoExecution("parameters"); |
| 54 | + @Test |
| 55 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM) |
| 56 | + void testExplicitPom(ParametersMojo mojo) { |
| 57 | + assertEquals("explicitValue", mojo.plain); |
| 58 | + assertDoesNotThrow(mojo::execute); |
| 59 | + } |
60 | 60 |
|
61 | | - session.getUserProperties().put("property", "propertyValue"); |
62 | | - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution); |
| 61 | + @Test |
| 62 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM) |
| 63 | + void testPropertyPom(ParametersMojo mojo) { |
| 64 | + assertDoesNotThrow(mojo::execute); |
| 65 | + } |
63 | 66 |
|
64 | | - assertNull(mojo.getPlain()); |
65 | | - assertEquals("propertyValue", mojo.getWithProperty()); |
66 | | - assertEquals("default", mojo.getWithDefault()); |
67 | | - assertEquals("propertyValue", mojo.getWithPropertyAndDefault()); |
| 67 | + @Test |
| 68 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) |
| 69 | + void simpleMojo(ParametersMojo mojo) { |
| 70 | + assertEquals(log, mojo.getLog()); |
| 71 | + assertDoesNotThrow(mojo::execute); |
68 | 72 | } |
69 | 73 |
|
70 | | - public void testExplicitWithProperty() throws Exception { |
71 | | - MavenProject project = readMavenProject(new File("src/test/projects/explicit")); |
72 | | - MavenSession session = newMavenSession(project); |
73 | | - MojoExecution execution = newMojoExecution("parameters"); |
| 74 | + @Test |
| 75 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) |
| 76 | + @MojoParameter(name = "plain", value = "plainValue") |
| 77 | + @MojoParameter(name = "withDefault", value = "withDefaultValue") |
| 78 | + void simpleMojoWithParameters(ParametersMojo mojo) { |
| 79 | + assertEquals("plainValue", mojo.plain); |
| 80 | + assertEquals("withDefaultValue", mojo.withDefault); |
| 81 | + assertDoesNotThrow(mojo::execute); |
| 82 | + } |
74 | 83 |
|
75 | | - session.getUserProperties().put("property", "propertyValue"); |
76 | | - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution); |
| 84 | + @Test |
| 85 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) |
| 86 | + @MojoParameters({ |
| 87 | + @MojoParameter(name = "plain", value = "plainValue"), |
| 88 | + @MojoParameter(name = "withDefault", value = "withDefaultValue") |
| 89 | + }) |
| 90 | + void simpleMojoWithParametersGroupingAnnotation(ParametersMojo mojo) { |
| 91 | + assertEquals("plainValue", mojo.plain); |
| 92 | + assertEquals("withDefaultValue", mojo.withDefault); |
| 93 | + assertDoesNotThrow(mojo::execute); |
| 94 | + } |
77 | 95 |
|
78 | | - assertEquals("explicitValue", mojo.getPlain()); |
79 | | - assertEquals("explicitWithPropertyValue", mojo.getWithProperty()); |
80 | | - assertEquals("explicitWithDefaultValue", mojo.getWithDefault()); |
81 | | - assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault()); |
| 96 | + @Test |
| 97 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) |
| 98 | + @MojoParameter(name = "plain", value = "plainValue") |
| 99 | + void simpleMojoWithParameter(ParametersMojo mojo) { |
| 100 | + assertEquals("plainValue", mojo.plain); |
| 101 | + assertDoesNotThrow(mojo::execute); |
82 | 102 | } |
83 | 103 |
|
84 | | - protected MavenProject readMavenProject(File basedir) throws ProjectBuildingException, Exception { |
85 | | - File pom = new File(basedir, "pom.xml"); |
86 | | - MavenExecutionRequest request = new DefaultMavenExecutionRequest(); |
87 | | - request.setBaseDirectory(basedir); |
88 | | - ProjectBuildingRequest configuration = request.getProjectBuildingRequest(); |
89 | | - configuration.setRepositorySession(new DefaultRepositorySystemSession()); |
90 | | - MavenProject project = |
91 | | - lookup(ProjectBuilder.class).build(pom, configuration).getProject(); |
92 | | - assertNotNull(project); |
93 | | - return project; |
| 104 | + @Test |
| 105 | + @MojoParameter(name = "plain", value = "plainValue") |
| 106 | + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM) |
| 107 | + void simpleMojoWithParameterInjectionWinsOverConfig(ParametersMojo mojo) { |
| 108 | + assertEquals("plainValue", mojo.plain); |
| 109 | + assertDoesNotThrow(mojo::execute); |
94 | 110 | } |
95 | 111 | } |
0 commit comments