|
12 | 12 | import java.io.File;
|
13 | 13 | import java.io.IOException;
|
14 | 14 | import java.io.OutputStream;
|
| 15 | +import java.nio.charset.StandardCharsets; |
15 | 16 | import java.nio.file.Files;
|
| 17 | +import java.nio.file.Path; |
16 | 18 | import java.nio.file.Paths;
|
17 | 19 | import java.util.ArrayList;
|
18 | 20 | import java.util.Arrays;
|
|
28 | 30 | import org.apache.maven.execution.MavenSession;
|
29 | 31 | import org.apache.maven.plugin.MojoExecutionException;
|
30 | 32 | import org.apache.maven.plugin.testing.AbstractMojoTestCase;
|
31 |
| -import org.mockito.Mock; |
| 33 | +import org.apache.maven.toolchain.ToolchainManager; |
32 | 34 |
|
33 | 35 | import static java.util.Collections.emptyMap;
|
| 36 | +import static org.mockito.ArgumentMatchers.any; |
| 37 | +import static org.mockito.ArgumentMatchers.eq; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.when; |
34 | 40 |
|
35 | 41 | /**
|
36 | 42 | * @author Jerome Lacoste
|
37 | 43 | * @version $Id$
|
38 | 44 | */
|
39 | 45 | public class ExecMojoTest extends AbstractMojoTestCase {
|
40 |
| - @Mock |
41 |
| - private MavenSession session; |
| 46 | + private MavenSession session = mock(MavenSession.class); |
| 47 | + private ToolchainManager toolchainManager = mock(ToolchainManager.class); |
42 | 48 |
|
43 | 49 | private static final File LOCAL_REPO = new File("src/test/repository");
|
44 | 50 |
|
@@ -287,6 +293,30 @@ public void test_exec_receives_all_parameters() throws MojoExecutionException {
|
287 | 293 | Paths.get("target", "dist", "mails").toFile().exists());
|
288 | 294 | }
|
289 | 295 |
|
| 296 | + public void testToolchainJavaHomePropertySetWhenToolchainIsUsed() throws Exception { |
| 297 | + // given |
| 298 | + String testJavaPath = "/path/to/java/home"; |
| 299 | + |
| 300 | + File pom = new File(getBasedir(), "src/test/projects/project20/pom.xml"); |
| 301 | + ExecMojo mojo = (ExecMojo) lookupMojo("exec", pom); |
| 302 | + |
| 303 | + setVariableValueToObject(mojo, "session", session); |
| 304 | + setVariableValueToObject(mojo, "toolchainManager", toolchainManager); |
| 305 | + when(toolchainManager.getToolchainFromBuildContext(any(), eq(session))) |
| 306 | + .thenReturn(new DummyToolchain(testJavaPath + "/bin/java")); |
| 307 | + |
| 308 | + File basedir = new File("target"); |
| 309 | + mojo.setBasedir(basedir); |
| 310 | + |
| 311 | + // when |
| 312 | + mojo.execute(); |
| 313 | + |
| 314 | + // then |
| 315 | + Path resultFilePath = basedir.toPath().resolve("testfile.txt"); |
| 316 | + String result = new String(Files.readAllBytes(resultFilePath), StandardCharsets.UTF_8); |
| 317 | + assertTrue(result.contains(testJavaPath)); |
| 318 | + } |
| 319 | + |
290 | 320 | private void checkMojo(String expectedCommandLine) {
|
291 | 321 | assertEquals(1, mojo.getAmountExecutedCommandLines());
|
292 | 322 | CommandLine commandline = mojo.getExecutedCommandline(0);
|
|
0 commit comments