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