1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.testing;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.nio.file.Paths;
25
26 import org.apache.maven.api.plugin.testing.Basedir;
27 import org.apache.maven.api.plugin.testing.InjectMojo;
28 import org.apache.maven.api.plugin.testing.MojoExtension;
29 import org.apache.maven.api.plugin.testing.MojoParameter;
30 import org.apache.maven.api.plugin.testing.MojoParameters;
31 import org.apache.maven.api.plugin.testing.MojoTest;
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.plugin.logging.Log;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Nested;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.TestInfo;
38
39 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
40 import static org.junit.jupiter.api.Assertions.assertEquals;
41 import static org.junit.jupiter.api.Assertions.assertNotNull;
42 import static org.junit.jupiter.api.Assertions.assertTrue;
43
44 @MojoTest
45 public class ParametersMojoTest {
46
47 private static final String POM_DOT_XML_FILE = "pom.xml";
48
49 private static final String DEFAULT_POM_DIR = "src/test/projects/default/";
50
51 private static final String EXPLICIT_POM_DIR = "src/test/projects/explicit/";
52
53 private static final String PROPERTY_POM_DIR = "src/test/projects/property/";
54
55 @Inject
56 private Log log;
57
58 @Test
59 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
60 void testDefaultPom(ParametersMojo mojo) {
61 assertEquals("default", mojo.getWithDefault());
62 assertEquals("default", mojo.getWithPropertyAndDefault());
63
64 assertDoesNotThrow(mojo::execute);
65 }
66
67 @Test
68 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
69 void testExplicitPom(ParametersMojo mojo) {
70 assertEquals("explicitValue", mojo.getPlain());
71 assertEquals("explicitWithPropertyValue", mojo.getWithProperty());
72 assertEquals("explicitWithDefaultValue", mojo.getWithDefault());
73 assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault());
74
75 assertDoesNotThrow(mojo::execute);
76 }
77
78 @Test
79 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM_DIR + POM_DOT_XML_FILE)
80 void testPropertyPom(ParametersMojo mojo) {
81 assertDoesNotThrow(mojo::execute);
82 }
83
84 @Nested
85 class TestPropertyPom {
86
87 @Inject
88 private MavenSession mavenSession;
89
90 @BeforeEach
91 void setup() {
92 mavenSession.getUserProperties().setProperty("property", "testPropertyValue");
93 }
94
95 @Test
96 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM_DIR + POM_DOT_XML_FILE)
97 @MojoParameter(name = "plain", value = "test-${property}")
98 void testPropertyPom(ParametersMojo mojo) {
99
100 assertNotNull(log, "log from parent class should be injected properly");
101
102 assertEquals("test-testPropertyValue", mojo.getPlain());
103 assertEquals("testPropertyValue", mojo.getWithProperty());
104 assertEquals("default", mojo.getWithDefault());
105 assertEquals("testPropertyValue", mojo.getWithPropertyAndDefault());
106
107 assertDoesNotThrow(mojo::execute);
108 }
109 }
110
111 @Test
112 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
113 void simpleMojo(ParametersMojo mojo) {
114 assertEquals(log, mojo.getLog());
115 assertDoesNotThrow(mojo::execute);
116 }
117
118 @Test
119 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
120 @MojoParameter(name = "plain", value = "plainValue")
121 @MojoParameter(name = "withDefault", value = "withDefaultValue")
122 void simpleMojoWithParameters(ParametersMojo mojo) {
123 assertEquals("plainValue", mojo.getPlain());
124 assertEquals("withDefaultValue", mojo.getWithDefault());
125 assertDoesNotThrow(mojo::execute);
126 }
127
128 @Test
129 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
130 @MojoParameters({
131 @MojoParameter(name = "plain", value = "plainValue"),
132 @MojoParameter(name = "withDefault", value = "withDefaultValue")
133 })
134 void simpleMojoWithParametersGroupingAnnotation(ParametersMojo mojo) {
135 assertEquals("plainValue", mojo.getPlain());
136 assertEquals("withDefaultValue", mojo.getWithDefault());
137 assertDoesNotThrow(mojo::execute);
138 }
139
140 @Test
141 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
142 @MojoParameter(name = "plain", value = "plainValue")
143 void simpleMojoWithParameter(ParametersMojo mojo) {
144 assertEquals("plainValue", mojo.getPlain());
145 assertDoesNotThrow(mojo::execute);
146 }
147
148 @Test
149 @MojoParameter(name = "plain", value = "plainValue")
150 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
151 void simpleMojoWithParameterInjectionWinsOverConfig(ParametersMojo mojo) {
152 assertEquals("plainValue", mojo.getPlain());
153 assertDoesNotThrow(mojo::execute);
154 }
155
156 @Test
157 @Basedir("src/test/projects/basedir-set-by-annotation")
158 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM_DOT_XML_FILE)
159 void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) {
160 assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
161 assertDoesNotThrow(mojo::execute);
162 }
163
164 @Test
165 @Basedir("src/test/projects/basedir-set-by-annotation")
166 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM_DOT_XML_FILE)
167 @MojoParameter(name = "withDefault", value = "${basedir}/test-default-value.txt")
168 @MojoParameter(name = "withProperty", value = "${project.basedir}/test-default-value.txt")
169 void basedirInjectedWithBasedirAnnotationAndParams(ParametersMojo mojo) {
170 assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
171 assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithDefault());
172 assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithProperty());
173 assertDoesNotThrow(mojo::execute);
174 }
175
176 @Test
177 @Basedir("/projects/basedir-set-by-annotation-classpath")
178 @InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
179 void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
180 assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
181 assertDoesNotThrow(mojo::execute);
182 }
183
184 @Test
185 @Basedir("/projects/basedir-set-by-annotation-classpath")
186 @InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
187 @MojoParameter(name = "withDefault", value = "${basedir}/test-default-value.txt")
188 @MojoParameter(name = "withProperty", value = "${project.basedir}/test-default-value.txt")
189 void basedirInjectedWithBasedirFromClasspathAnnotationAndParams(ParametersMojo mojo) {
190 assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
191 assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithDefault());
192 assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithProperty());
193 assertDoesNotThrow(mojo::execute);
194 }
195
196 @Nested
197 class BaseDirInBeforeEach {
198
199 @BeforeEach
200 void setup() {
201
202 String fs = File.separator;
203 String endWith1 = fs + "src" + fs + "test" + fs + "projects" + fs + "basedir-set-by-annotation";
204 String endWith2 = fs + "projects" + fs + "basedir-set-by-annotation-classpath";
205
206 assertTrue(
207 MojoExtension.getBasedir().endsWith(endWith1)
208 || MojoExtension.getBasedir().endsWith(endWith2),
209 "Basedir: " + MojoExtension.getBasedir() + " is not ends with expected value '" + endWith1
210 + "' or '" + endWith2 + "'");
211 }
212
213 @Test
214 @Basedir("src/test/projects/basedir-set-by-annotation")
215 @InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
216 void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) {
217 assertDoesNotThrow(mojo::execute);
218 }
219
220 @Test
221 @Basedir("/projects/basedir-set-by-annotation-classpath")
222 @InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
223 void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
224 assertDoesNotThrow(mojo::execute);
225 }
226 }
227
228 @Test
229 @Basedir("src/test/projects/basedir-set-by-annotation")
230 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters")
231 void basedirInjectedWithBasedirAnnotationDefaultPom(ParametersMojo mojo) {
232 assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
233 assertDoesNotThrow(mojo::execute);
234 }
235
236 @Test
237 @Basedir("/projects/basedir-set-by-annotation-classpath")
238 @InjectMojo(goal = "parameters")
239 void basedirInjectedWithBasedirFromClasspathAnnotationDefaultPom(ParametersMojo mojo) {
240 assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
241 assertDoesNotThrow(mojo::execute);
242 }
243
244 @Test
245 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters")
246 void testMultipleResolvedParameters(ParametersMojo mojo, TestInfo testInfo) {
247 assertNotNull(mojo);
248 assertNotNull(testInfo);
249 }
250
251 @Test
252 @InjectMojo(goal = "parameters")
253 @MojoParameter(name = "plain", value = "${project.build.directory}/generated")
254 void projectBuildDirectoryShouldBeResolved(ParametersMojo mojo) {
255 assertEquals(
256 Paths.get(MojoExtension.getBasedir(), "target", "generated").normalize(),
257 Paths.get(mojo.getPlain()).normalize());
258 assertDoesNotThrow(mojo::execute);
259 }
260 }