1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.compiler;
20
21 import java.io.File;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.plugin.compiler.stubs.CompilerManagerStub;
29 import org.apache.maven.plugin.logging.Log;
30 import org.apache.maven.plugin.testing.junit5.InjectMojo;
31 import org.apache.maven.plugin.testing.junit5.MojoTest;
32 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
33 import org.junit.jupiter.api.Test;
34
35 import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenProject;
36 import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenSession;
37 import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMojoExecution;
38 import static org.apache.maven.plugin.compiler.MojoTestUtils.getVariableValueFromObject;
39 import static org.apache.maven.plugin.compiler.MojoTestUtils.setVariableValueToObject;
40 import static org.junit.jupiter.api.Assertions.*;
41 import static org.mockito.ArgumentMatchers.startsWith;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.never;
44 import static org.mockito.Mockito.verify;
45
46 @MojoTest
47 class CompilerMojoTest {
48
49 private static final String COMPILE = "compile";
50
51
52
53
54
55
56 @Test
57 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-basic-test/plugin-config.xml")
58 void testCompilerBasic(CompilerMojo compilerMojo) throws Exception {
59 setUpCompilerMojoTestEnv(compilerMojo);
60
61 Log log = mock(Log.class);
62
63 compilerMojo.setLog(log);
64
65 setVariableValueToObject(compilerMojo, "targetOrReleaseSet", false);
66 compilerMojo.execute();
67
68 Artifact projectArtifact = getVariableValueFromObject(compilerMojo, "projectArtifact");
69 assertNotNull(
70 projectArtifact.getFile(),
71 "MCOMPILER-94: artifact file should only be null if there is nothing to compile");
72
73 File testClass = new File(compilerMojo.getOutputDirectory(), "foo/TestCompile0.class");
74
75 verify(log).warn(startsWith("No explicit value set for target or release!"));
76
77 assertTrue(testClass::exists);
78 }
79
80 @Test
81 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-basic-sourcetarget/plugin-config.xml")
82 void testCompilerBasicSourceTarget(CompilerMojo compilerMojo) throws Exception {
83 setUpCompilerMojoTestEnv(compilerMojo);
84
85 Log log = mock(Log.class);
86
87 compilerMojo.setLog(log);
88
89 compilerMojo.execute();
90
91 verify(log, never()).warn(startsWith("No explicit value set for target or release!"));
92 }
93
94
95
96
97
98
99 @Test
100 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-empty-source-test/plugin-config.xml")
101 void testCompilerEmptySource(CompilerMojo compilerMojo) throws Exception {
102 setUpCompilerMojoTestEnv(compilerMojo);
103
104 compilerMojo.execute();
105
106 assertFalse(compilerMojo.getOutputDirectory().exists());
107
108 Artifact projectArtifact = getVariableValueFromObject(compilerMojo, "projectArtifact");
109 assertNull(
110 projectArtifact.getFile(), "MCOMPILER-94: artifact file should be null if there is nothing to compile");
111 }
112
113
114
115
116
117
118 @Test
119 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-includes-excludes-test/plugin-config.xml")
120 void testCompilerIncludesExcludes(CompilerMojo compilerMojo) throws Exception {
121 setUpCompilerMojoTestEnv(compilerMojo);
122
123 Set<String> includes = new HashSet<>();
124 includes.add("**/TestCompile4*.java");
125 setVariableValueToObject(compilerMojo, "includes", includes);
126
127 Set<String> excludes = new HashSet<>();
128 excludes.add("**/TestCompile2*.java");
129 excludes.add("**/TestCompile3*.java");
130 setVariableValueToObject(compilerMojo, "excludes", excludes);
131
132 compilerMojo.execute();
133
134 File testClass = new File(compilerMojo.getOutputDirectory(), "foo/TestCompile2.class");
135 assertFalse(testClass.exists());
136
137 testClass = new File(compilerMojo.getOutputDirectory(), "foo/TestCompile3.class");
138 assertFalse(testClass.exists());
139
140 testClass = new File(compilerMojo.getOutputDirectory(), "foo/TestCompile4.class");
141 assertTrue(testClass.exists());
142 }
143
144
145
146
147
148
149 @Test
150 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-fork-test/plugin-config.xml")
151 void testCompilerFork(CompilerMojo compilerMojo) throws Exception {
152 setUpCompilerMojoTestEnv(compilerMojo);
153
154 setVariableValueToObject(
155 compilerMojo, "executable", new File(System.getenv("JAVA_HOME"), "bin/javac").getPath());
156
157 compilerMojo.execute();
158
159 File testClass = new File(compilerMojo.getOutputDirectory(), "foo/TestCompile1.class");
160 assertTrue(testClass.exists());
161 }
162
163 @Test
164 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-one-output-file-test/plugin-config.xml")
165 void testOneOutputFileForAllInput(CompilerMojo compilerMojo) throws Exception {
166 setUpCompilerMojoTestEnv(compilerMojo);
167
168 setVariableValueToObject(compilerMojo, "compilerManager", new CompilerManagerStub());
169
170 compilerMojo.execute();
171
172 File testClass = new File(compilerMojo.getOutputDirectory(), "compiled.class");
173 assertTrue(testClass.exists());
174 }
175
176 @Test
177 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-args-test/plugin-config.xml")
178 void testCompilerArgs(CompilerMojo compilerMojo) throws Exception {
179 setUpCompilerMojoTestEnv(compilerMojo);
180
181 setVariableValueToObject(compilerMojo, "compilerManager", new CompilerManagerStub());
182
183 compilerMojo.execute();
184
185 File testClass = new File(compilerMojo.getOutputDirectory(), "compiled.class");
186 assertTrue(testClass.exists());
187 assertEquals(
188 Arrays.asList("key1=value1", "-Xlint", "-my&special:param-with+chars/not>allowed_in_XML_element_names"),
189 compilerMojo.compilerArgs);
190 }
191
192 @Test
193 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-implicit-test/plugin-config-none.xml")
194 void testImplicitFlagNone(CompilerMojo compilerMojo) throws Exception {
195 setUpCompilerMojoTestEnv(compilerMojo);
196
197 assertEquals("none", compilerMojo.getImplicit());
198 }
199
200 @Test
201 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-implicit-test/plugin-config-not-set.xml")
202 void testImplicitFlagNotSet(CompilerMojo compilerMojo) throws Exception {
203 setUpCompilerMojoTestEnv(compilerMojo);
204
205 assertNull(compilerMojo.getImplicit());
206 }
207
208 @Test
209 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-one-output-file-test2/plugin-config.xml")
210 void testOneOutputFileForAllInput2(CompilerMojo compilerMojo) throws Exception {
211 setUpCompilerMojoTestEnv(compilerMojo);
212
213 setVariableValueToObject(compilerMojo, "compilerManager", new CompilerManagerStub());
214
215 Set<String> includes = new HashSet<>();
216 includes.add("**/TestCompile4*.java");
217 setVariableValueToObject(compilerMojo, "includes", includes);
218
219 Set<String> excludes = new HashSet<>();
220 excludes.add("**/TestCompile2*.java");
221 excludes.add("**/TestCompile3*.java");
222 setVariableValueToObject(compilerMojo, "excludes", excludes);
223
224 compilerMojo.execute();
225
226 File testClass = new File(compilerMojo.getOutputDirectory(), "compiled.class");
227 assertTrue(testClass.exists());
228 }
229
230 @Test
231 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-fail-test/plugin-config.xml")
232 void testCompileFailure(CompilerMojo compilerMojo) throws Exception {
233 setUpCompilerMojoTestEnv(compilerMojo);
234
235 setVariableValueToObject(compilerMojo, "compilerManager", new CompilerManagerStub(true));
236
237 try {
238 compilerMojo.execute();
239
240 fail("Should throw an exception");
241 } catch (CompilationFailureException e) {
242
243 }
244 }
245
246 @Test
247 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-failonerror-test/plugin-config.xml")
248 void testCompileFailOnError(CompilerMojo compilerMojo) throws Exception {
249 setUpCompilerMojoTestEnv(compilerMojo);
250
251 setVariableValueToObject(compilerMojo, "compilerManager", new CompilerManagerStub(true));
252
253 try {
254 compilerMojo.execute();
255 assertTrue(true);
256 } catch (CompilationFailureException e) {
257 fail("The compilation error should have been consumed because failOnError = false");
258 }
259 }
260
261
262
263
264
265
266 @Test
267 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-skip-main/plugin-config.xml")
268 void testCompileSkipMain(CompilerMojo compilerMojo) throws Exception {
269 setUpCompilerMojoTestEnv(compilerMojo);
270 setVariableValueToObject(compilerMojo, "skipMain", true);
271 compilerMojo.execute();
272 File testClass = new File(compilerMojo.getOutputDirectory(), "foo/TestSkipMainCompile0.class");
273 assertFalse(testClass.exists());
274 }
275
276
277
278
279
280
281 @Test
282 @InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-skip-test/plugin-config.xml")
283 void testCompileSkipTest(CompilerMojo compilerMojo) throws Exception {
284 setUpCompilerMojoTestEnv(compilerMojo);
285
286 compilerMojo.execute();
287
288 File testClass = new File(compilerMojo.getOutputDirectory(), "foo/TestSkipTestCompile0.class");
289 assertTrue(testClass.exists());
290 }
291
292 private void setUpCompilerMojoTestEnv(CompilerMojo mojo) throws Exception {
293 setVariableValueToObject(mojo, "projectArtifact", new ArtifactStub());
294 setVariableValueToObject(mojo, "compilePath", Collections.EMPTY_LIST);
295 setVariableValueToObject(mojo, "session", getMockMavenSession());
296 setVariableValueToObject(mojo, "project", getMockMavenProject());
297 setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
298 setVariableValueToObject(mojo, "source", AbstractCompilerMojo.DEFAULT_SOURCE);
299 setVariableValueToObject(mojo, "target", AbstractCompilerMojo.DEFAULT_TARGET);
300 }
301 }