View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.plugin.compiler.stubs.CompilerManagerStub;
30  import org.apache.maven.plugin.testing.junit5.InjectMojo;
31  import org.apache.maven.plugin.testing.junit5.MojoTest;
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.plugin.compiler.MojoTestUtils.getMockMavenProject;
38  import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenSession;
39  import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMojoExecution;
40  import static org.apache.maven.plugin.compiler.MojoTestUtils.getVariableValueFromObject;
41  import static org.apache.maven.plugin.compiler.MojoTestUtils.setVariableValueToObject;
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  import static org.junit.jupiter.api.Assertions.assertFalse;
44  import static org.junit.jupiter.api.Assertions.assertTrue;
45  
46  @MojoTest
47  class TestCompilerMojoTest {
48  
49      private static final String TEST_COMPILE = "testCompile";
50  
51      @Test
52      @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-basic-test/plugin-config.xml")
53      void testCompilerBasic(TestCompilerMojo testCompilerMojo) throws Exception {
54          setUpCompilerMojoTestEnv(testCompilerMojo);
55  
56          testCompilerMojo.execute();
57  
58          File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile0Test.class");
59  
60          assertTrue(testClass::exists);
61      }
62  
63      @Test
64      @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-empty-source-test/plugin-config.xml")
65      public void testCompilerEmptySource(TestCompilerMojo testCompilerMojo) throws Exception {
66          setUpCompilerMojoTestEnv(testCompilerMojo);
67  
68          testCompilerMojo.execute();
69  
70          assertFalse(testCompilerMojo.getOutputDirectory().exists());
71      }
72  
73      @Test
74      @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-includes-excludes-test/plugin-config.xml")
75      void testCompilerIncludesExcludes(TestCompilerMojo testCompilerMojo) throws Exception {
76          setUpCompilerMojoTestEnv(testCompilerMojo);
77  
78          Set<String> includes = new HashSet<>();
79          includes.add("**/TestCompile4*.java");
80          setVariableValueToObject(testCompilerMojo, "testIncludes", includes);
81  
82          Set<String> excludes = new HashSet<>();
83          excludes.add("**/TestCompile2*.java");
84          excludes.add("**/TestCompile3*.java");
85          setVariableValueToObject(testCompilerMojo, "testExcludes", excludes);
86  
87          testCompilerMojo.execute();
88  
89          File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile2TestCase.class");
90          assertFalse(testClass.exists());
91  
92          testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile3TestCase.class");
93          assertFalse(testClass.exists());
94  
95          testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile4TestCase.class");
96          assertTrue(testClass.exists());
97      }
98  
99      @Test
100     @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-fork-test/plugin-config.xml")
101     void testCompilerFork(TestCompilerMojo testCompilerMojo) throws Exception {
102         setUpCompilerMojoTestEnv(testCompilerMojo);
103 
104         // JAVA_HOME doesn't have to be on the PATH.
105         setVariableValueToObject(
106                 testCompilerMojo, "executable", new File(System.getenv("JAVA_HOME"), "bin/javac").getPath());
107 
108         testCompilerMojo.execute();
109 
110         File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestCompile1TestCase.class");
111         assertTrue(testClass.exists());
112     }
113 
114     @Test
115     @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-one-output-file-test/plugin-config.xml")
116     void testOneOutputFileForAllInput(TestCompilerMojo testCompilerMojo) throws Exception {
117         setUpCompilerMojoTestEnv(testCompilerMojo);
118 
119         setVariableValueToObject(testCompilerMojo, "compilerManager", new CompilerManagerStub());
120 
121         testCompilerMojo.execute();
122 
123         File testClass = new File(testCompilerMojo.getOutputDirectory(), "compiled.class");
124         assertTrue(testClass.exists());
125     }
126 
127     @Test
128     @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-one-output-file-test2/plugin-config.xml")
129     void testOneOutputFileForAllInput2(TestCompilerMojo testCompilerMojo) throws Exception {
130         setUpCompilerMojoTestEnv(testCompilerMojo);
131 
132         setVariableValueToObject(testCompilerMojo, "compilerManager", new CompilerManagerStub());
133 
134         Set<String> includes = new HashSet<>();
135         includes.add("**/TestCompile4*.java");
136         setVariableValueToObject(testCompilerMojo, "testIncludes", includes);
137 
138         Set<String> excludes = new HashSet<>();
139         excludes.add("**/TestCompile2*.java");
140         excludes.add("**/TestCompile3*.java");
141         setVariableValueToObject(testCompilerMojo, "testExcludes", excludes);
142 
143         testCompilerMojo.execute();
144 
145         File testClass = new File(testCompilerMojo.getOutputDirectory(), "compiled.class");
146         assertTrue(testClass.exists());
147     }
148 
149     @Test
150     @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-skip-main/plugin-config.xml")
151     void testCompileSkipMain(TestCompilerMojo testCompilerMojo) throws Exception {
152         setUpCompilerMojoTestEnv(testCompilerMojo);
153 
154         testCompilerMojo.execute();
155 
156         File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestSkipMainCompile0Test.class");
157         assertTrue(testClass.exists());
158     }
159 
160     @Test
161     @InjectMojo(goal = TEST_COMPILE, pom = "classpath:/unit/compiler-skip-test/plugin-config.xml")
162     void testCompileSkipTest(TestCompilerMojo testCompilerMojo) throws Exception {
163         setUpCompilerMojoTestEnv(testCompilerMojo);
164 
165         setVariableValueToObject(testCompilerMojo, "skip", true);
166 
167         testCompilerMojo.execute();
168 
169         File testClass = new File(testCompilerMojo.getOutputDirectory(), "foo/TestSkipTestCompile0Test.class");
170         assertFalse(testClass.exists());
171     }
172 
173     private void setUpCompilerMojoTestEnv(TestCompilerMojo mojo) throws Exception {
174         File buildDir = getVariableValueFromObject(mojo, "buildDirectory");
175         File testClassesDir = new File(buildDir, "test-classes");
176         setVariableValueToObject(mojo, "outputDirectory", testClassesDir);
177 
178         setVariableValueToObject(mojo, "project", getMockMavenProject());
179 
180         Path baseDir = mojo.getOutputDirectory()
181                 .toPath()
182                 .resolveSibling(Paths.get("..", "..", "..", "..", "test-classes"))
183                 .normalize();
184         Path subpath = mojo.getOutputDirectory().toPath().subpath(baseDir.getNameCount(), baseDir.getNameCount() + 2);
185         String sourceRoot = baseDir.resolve(subpath) + "/src/main/java";
186         String testSourceRoot = baseDir.resolve(subpath) + "/src/test/java";
187 
188         setVariableValueToObject(mojo, "compileSourceRoots", Arrays.asList(sourceRoot, testSourceRoot));
189 
190         setVariableValueToObject(mojo, "session", getMockMavenSession());
191         setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
192         setVariableValueToObject(mojo, "source", AbstractCompilerMojo.DEFAULT_SOURCE);
193         setVariableValueToObject(mojo, "target", AbstractCompilerMojo.DEFAULT_TARGET);
194     }
195 
196     static Stream<Arguments> olderThanJDK9() {
197         return Stream.of(
198                 Arguments.of("1.8", true),
199                 Arguments.of("8", true),
200                 Arguments.of("1.9", false),
201                 Arguments.of("1.9", false),
202                 Arguments.of("9", false),
203                 Arguments.of("11", false));
204     }
205 
206     @ParameterizedTest
207     @MethodSource
208     void olderThanJDK9(String version, boolean expected) {
209         assertEquals(expected, TestCompilerMojo.isOlderThanJDK9(version));
210     }
211 }