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  import java.util.Arrays;
25  
26  import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
27  import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
28  import com.github.tomakehurst.wiremock.matching.UrlPathPattern;
29  import org.apache.commons.io.FileUtils;
30  import org.apache.maven.buildcache.its.junit.IntegrationTest;
31  import org.apache.maven.it.VerificationException;
32  import org.apache.maven.it.Verifier;
33  import org.junit.jupiter.api.AfterEach;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  import org.junit.jupiter.api.extension.RegisterExtension;
37  
38  import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
39  import static com.github.tomakehurst.wiremock.client.WireMock.get;
40  import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
41  import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
42  import static com.github.tomakehurst.wiremock.client.WireMock.ok;
43  import static com.github.tomakehurst.wiremock.client.WireMock.put;
44  import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
45  import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
46  import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
47  
48  /**
49   * Verifies MBUILDCACHE-25 - build cache calculated and saved exactly once in presence of forked executions
50   */
51  @IntegrationTest("src/test/projects/forked-executions-core-extension-remote")
52  public class ForkedExecutionsTest {
53  
54      private static final String PROJECT_NAME = "org.apache.maven.caching.test.simple:forked-executions-core-extension";
55  
56      @RegisterExtension
57      static WireMockExtension wm = WireMockExtension.newInstance()
58              .options(wireMockConfig().dynamicPort().notifier(new ConsoleNotifier(true)))
59              .build();
60  
61      private Path tempDirectory;
62  
63      @BeforeEach
64      void setUp() throws IOException {
65          tempDirectory = Files.createTempDirectory("build-cache-test-");
66      }
67  
68      @AfterEach
69      void tearDown() throws IOException {
70          FileUtils.deleteDirectory(tempDirectory.toFile());
71      }
72  
73      @Test
74      void testForkedExecution(Verifier verifier) throws VerificationException {
75  
76          UrlPathPattern buildInfoPath = urlPathMatching(".*/buildinfo.xml");
77          wm.stubFor(get(buildInfoPath).willReturn(notFound()));
78          wm.stubFor(put(buildInfoPath).willReturn(ok()));
79  
80          UrlPathPattern jarPath = urlPathMatching(".*/forked-executions-core-extension-remote.jar");
81          wm.stubFor(put(jarPath).willReturn(ok()));
82  
83          UrlPathPattern cacheReportPath = urlPathMatching(".*/build-cache-report.xml");
84          wm.stubFor(put(cacheReportPath).willReturn(ok()));
85  
86          verifier.setAutoclean(false);
87  
88          verifier.setLogFileName("../log-1.txt");
89          verifier.setMavenDebug(true);
90          verifier.setCliOptions(Arrays.asList(
91                  "-Dmaven.build.cache.location=" + tempDirectory.toAbsolutePath(),
92                  "-Dmaven.build.cache.remote.url=http:////localhost:"
93                          + wm.getRuntimeInfo().getHttpPort(),
94                  "-Dmaven.build.cache.remote.save.enabled=true"));
95          verifier.executeGoal("verify");
96          verifier.verifyTextInLog("Started forked project");
97          verifier.verifyTextInLog("BUILD SUCCESS");
98  
99          wm.verify(exactly(1), getRequestedFor(buildInfoPath));
100         wm.verify(exactly(1), putRequestedFor(buildInfoPath));
101         wm.verify(exactly(1), putRequestedFor(jarPath));
102         wm.verify(exactly(1), putRequestedFor(cacheReportPath));
103     }
104 }