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.buildcache.its;
20  
21  import java.io.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  
25  import com.google.common.collect.Lists;
26  import org.apache.commons.io.FileUtils;
27  import org.apache.maven.buildcache.its.junit.IntegrationTest;
28  import org.apache.maven.it.VerificationException;
29  import org.apache.maven.it.Verifier;
30  import org.junit.jupiter.api.AfterEach;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_LOCATION_PROPERTY_NAME;
35  import static org.junit.jupiter.api.Assertions.assertThrows;
36  
37  /**
38   * Runs project which contains plugin with forked execution - pmd
39   * The test checks that extensions receives expected events for forked executions and completes build successfully
40   */
41  @IntegrationTest("src/test/projects/forked-executions-core-extension")
42  public class ForkedExecutionCoreExtensionTest {
43  
44      private static final String PROJECT_NAME = "org.apache.maven.caching.test.simple:forked-executions-core-extension";
45      private Path tempDirectory;
46  
47      @BeforeEach
48      void setUp() throws IOException {
49          tempDirectory = Files.createTempDirectory("build-cache-test-");
50      }
51  
52      @AfterEach
53      void tearDown() throws IOException {
54          FileUtils.deleteDirectory(tempDirectory.toFile());
55      }
56  
57      @Test
58      void testForkedExecution(Verifier verifier) throws VerificationException {
59          verifier.setAutoclean(false);
60  
61          verifier.setLogFileName("../log-1.txt");
62          verifier.setMavenDebug(true);
63          verifier.setCliOptions(
64                  Lists.newArrayList("-D" + CACHE_LOCATION_PROPERTY_NAME + "=" + tempDirectory.toAbsolutePath()));
65          verifier.executeGoal("verify");
66          verifier.verifyTextInLog("Started forked project");
67          // forked execution actually runs
68          verifier.verifyTextInLog(
69                  "[DEBUG] Starting mojo execution: pmd:pmd:emptyLifecyclePhase:maven-pmd-plugin:org.apache.maven.plugins");
70          // checking that forked execution doesn't hook into lifecycle
71          assertThrows(
72                  VerificationException.class,
73                  () -> verifier.verifyTextInLog(
74                          "Mojo execution pmd:pmd:emptyLifecyclePhase:maven-pmd-plugin:org.apache.maven.plugins is forked,"
75                                  + " returning phase verify from originating mojo "
76                                  + "default:check:verify:maven-pmd-plugin:org.apache.maven.plugins"));
77          verifier.verifyTextInLog("[INFO] BUILD SUCCESS");
78  
79          verifier.setLogFileName("../log-2.txt");
80          verifier.executeGoal("verify");
81          verifier.verifyErrorFreeLog();
82          verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache");
83          // checking that fork originating mojo pmd:check is cached
84          verifier.verifyTextInLog("[INFO] Skipping plugin execution (cached): pmd:check");
85          // and because of that forked execution pmd:pmd didn't run
86          assertThrows(
87                  VerificationException.class,
88                  () -> verifier.verifyTextInLog(
89                          "[DEBUG] Starting mojo execution: pmd:pmd:emptyLifecyclePhase:maven-pmd-plugin:org.apache.maven.plugins"));
90          // and didn't appear in cache lifecycle
91          assertThrows(
92                  VerificationException.class,
93                  () -> verifier.verifyTextInLog("[INFO] Skipping plugin execution (cached): pmd:pmd"));
94          verifier.verifyTextInLog("[INFO] BUILD SUCCESS");
95      }
96  }