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.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Set;
27 import java.util.stream.Stream;
28
29 import org.apache.maven.api.plugin.testing.InjectMojo;
30 import org.apache.maven.api.plugin.testing.MojoTest;
31 import org.apache.maven.plugin.compiler.stubs.CompilerManagerStub;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.Arguments;
35 import org.junit.jupiter.params.provider.MethodSource;
36
37 import static org.apache.maven.api.plugin.testing.MojoExtension.getVariableValueFromObject;
38 import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject;
39 import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenProject;
40 import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenSession;
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.junit.jupiter.api.Assertions.assertFalse;
43 import static org.junit.jupiter.api.Assertions.assertTrue;
44
45 @MojoTest
46 class TestCompilerMojoTest {
47
48 private static final String TEST_COMPILE = "testCompile";
49
50 @Test
51 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-basic-test/plugin-config.xml")
52 void testCompilerBasic(TestCompilerMojo testCompilerMojo) throws Exception {
53 setUpCompilerMojoTestEnv(testCompilerMojo);
54
55 testCompilerMojo.execute();
56
57 File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile0Test.class");
58
59 assertTrue(testClass::exists);
60 }
61
62 @Test
63 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-empty-source-test/plugin-config.xml")
64 public void testCompilerEmptySource(TestCompilerMojo testCompilerMojo) throws Exception {
65 setUpCompilerMojoTestEnv(testCompilerMojo);
66
67 testCompilerMojo.execute();
68
69 assertFalse(testCompilerMojo.getOutputDirectory().exists());
70 }
71
72 @Test
73 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-includes-excludes-test/plugin-config.xml")
74 void testCompilerIncludesExcludes(TestCompilerMojo testCompilerMojo) throws Exception {
75 setUpCompilerMojoTestEnv(testCompilerMojo);
76
77 Set<String> includes = new HashSet<>();
78 includes.add("**/TestCompile4*.java");
79 setVariableValueToObject(testCompilerMojo, "testIncludes", includes);
80
81 Set<String> excludes = new HashSet<>();
82 excludes.add("**/TestCompile2*.java");
83 excludes.add("**/TestCompile3*.java");
84 setVariableValueToObject(testCompilerMojo, "testExcludes", excludes);
85
86 testCompilerMojo.execute();
87
88 File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile2TestCase.class");
89 assertFalse(testClass.exists());
90
91 testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile3TestCase.class");
92 assertFalse(testClass.exists());
93
94 testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile4TestCase.class");
95 assertTrue(testClass.exists());
96 }
97
98 @Test
99 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-fork-test/plugin-config.xml")
100 void testCompilerFork(TestCompilerMojo testCompilerMojo) throws Exception {
101 setUpCompilerMojoTestEnv(testCompilerMojo);
102
103
104 setVariableValueToObject(
105 testCompilerMojo, "executable", new File(System.getenv("JAVA_HOME"), "bin/javac").getPath());
106
107 testCompilerMojo.execute();
108
109 File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile1TestCase.class");
110 assertTrue(testClass.exists());
111 }
112
113 @Test
114 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-one-output-file-test/plugin-config.xml")
115 void testOneOutputFileForAllInput(TestCompilerMojo testCompilerMojo) throws Exception {
116 setUpCompilerMojoTestEnv(testCompilerMojo);
117
118 setVariableValueToObject(testCompilerMojo, "compilerManager", new CompilerManagerStub());
119
120 testCompilerMojo.execute();
121
122 File testClass = new File(testCompilerMojo.getOutputDirectory(), "compiled.class");
123 assertTrue(testClass.exists());
124 }
125
126 @Test
127 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-one-output-file-test2/plugin-config.xml")
128 void testOneOutputFileForAllInput2(TestCompilerMojo testCompilerMojo) throws Exception {
129 setUpCompilerMojoTestEnv(testCompilerMojo);
130
131 setVariableValueToObject(testCompilerMojo, "compilerManager", new CompilerManagerStub());
132
133 Set<String> includes = new HashSet<>();
134 includes.add("**/TestCompile4*.java");
135 setVariableValueToObject(testCompilerMojo, "testIncludes", includes);
136
137 Set<String> excludes = new HashSet<>();
138 excludes.add("**/TestCompile2*.java");
139 excludes.add("**/TestCompile3*.java");
140 setVariableValueToObject(testCompilerMojo, "testExcludes", excludes);
141
142 testCompilerMojo.execute();
143
144 File testClass = new File(testCompilerMojo.getOutputDirectory(), "compiled.class");
145 assertTrue(testClass.exists());
146 }
147
148 @Test
149 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-skip-main/plugin-config.xml")
150 void testCompileSkipMain(TestCompilerMojo testCompilerMojo) throws Exception {
151 setUpCompilerMojoTestEnv(testCompilerMojo);
152
153 testCompilerMojo.execute();
154
155 File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestSkipMainCompile0Test.class");
156 assertTrue(testClass.exists());
157 }
158
159 @Test
160 @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-skip-test/plugin-config.xml")
161 void testCompileSkipTest(TestCompilerMojo testCompilerMojo) throws Exception {
162 setUpCompilerMojoTestEnv(testCompilerMojo);
163
164 setVariableValueToObject(testCompilerMojo, "skip", true);
165
166 testCompilerMojo.execute();
167
168 File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestSkipTestCompile0Test.class");
169 assertFalse(testClass.exists());
170 }
171
172 private void setUpCompilerMojoTestEnv(TestCompilerMojo mojo) throws Exception {
173 File buildDir = (File) getVariableValueFromObject(mojo, "buildDirectory");
174 File testClassesDir = new File(buildDir, "test-classes");
175 setVariableValueToObject(mojo, "outputDirectory", testClassesDir);
176
177 setVariableValueToObject(mojo, "project", getMockMavenProject());
178
179 Path baseDir = mojo.getOutputDirectory()
180 .toPath()
181 .resolveSibling(Paths.get("..", "..", "..", "..", "test-classes"))
182 .normalize();
183 Path subpath = mojo.getOutputDirectory().toPath().subpath(baseDir.getNameCount(), baseDir.getNameCount() + 2);
184 String sourceRoot = baseDir.resolve(subpath) + "/src/main/java";
185 String testSourceRoot = baseDir.resolve(subpath) + "/src/test/java";
186
187 setVariableValueToObject(mojo, "compileSourceRoots", Arrays.asList(sourceRoot, testSourceRoot));
188
189 setVariableValueToObject(mojo, "session", getMockMavenSession());
190 }
191
192 static Stream<Arguments> olderThanJDK9() {
193 return Stream.of(
194 Arguments.of("1.8", true),
195 Arguments.of("8", true),
196 Arguments.of("1.9", false),
197 Arguments.of("1.9", false),
198 Arguments.of("9", false),
199 Arguments.of("11", false));
200 }
201
202 @ParameterizedTest
203 @MethodSource
204 void olderThanJDK9(String version, boolean expected) {
205 assertEquals(expected, TestCompilerMojo.isOlderThanJDK9(version));
206 }
207 }