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  import java.util.List;
26  
27  import org.apache.maven.buildcache.its.junit.IntegrationTest;
28  import org.apache.maven.buildcache.util.LogFileUtils;
29  import org.apache.maven.buildcache.xml.CacheConfigImpl;
30  import org.apache.maven.it.VerificationException;
31  import org.apache.maven.it.Verifier;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_LOCATION_PROPERTY_NAME;
36  import static org.junit.jupiter.api.Assertions.assertThrows;
37  
38  /**
39   * Test the "mandatoryClean" parameter : saving in cache should be done only if a clean phase has been executed.
40   */
41  @IntegrationTest("src/test/projects/mandatory-clean")
42  class MandatoryCleanTest {
43  
44      private static final String MODULE_NAME_1 = "org.apache.maven.caching.test.simple:non-forked-module";
45      private static final String MODULE_NAME_2 = "org.apache.maven.caching.test.simple:forked-module";
46      private static final String CACHE_BUILD_LOG = "Found cached build, restoring %s from cache";
47  
48      @Test
49      void simple(Verifier verifier) throws VerificationException, IOException {
50  
51          verifier.setAutoclean(false);
52          Path tempDirectory = Files.createTempDirectory("simple-mandatory-clean");
53          verifier.getCliOptions().clear();
54          verifier.addCliOption("-D" + CACHE_LOCATION_PROPERTY_NAME + "=" + tempDirectory.toAbsolutePath());
55          verifier.addCliOption("--debug");
56  
57          verifier.setLogFileName("../log-1.txt");
58          verifier.executeGoal("verify");
59          verifier.verifyErrorFreeLog();
60          List<String> cacheSkippedBuild1 = LogFileUtils.findLinesContainingTextsInLogs(
61                  verifier, "Cache storing is skipped since there was no \"clean\" phase.");
62          Assertions.assertEquals(2, cacheSkippedBuild1.size(), "Expected 2 skipped module caching");
63  
64          assertThrows(
65                  VerificationException.class,
66                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
67                  "not expected to be loaded from the cache");
68          assertThrows(
69                  VerificationException.class,
70                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
71                  "not expected to be loaded from the cache");
72  
73          verifier.setLogFileName("../log-2.txt");
74          verifier.executeGoals(Arrays.asList("clean", "verify"));
75          verifier.verifyErrorFreeLog();
76          List<String> cacheSkippedBuild2 = LogFileUtils.findLinesContainingTextsInLogs(
77                  verifier, "Cache storing is skipped since there was no \"clean\" phase.");
78          Assertions.assertEquals(0, cacheSkippedBuild2.size(), "Expected 2 skipped module caching");
79          assertThrows(
80                  VerificationException.class,
81                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
82                  "not expected to be loaded from the cache");
83          assertThrows(
84                  VerificationException.class,
85                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
86                  "not expected to be loaded from the cache");
87  
88          verifier.setLogFileName("../log-3.txt");
89          verifier.executeGoal("verify");
90          verifier.verifyErrorFreeLog();
91          List<String> cacheSkippedBuild3 = LogFileUtils.findLinesContainingTextsInLogs(
92                  verifier, "Cache storing is skipped since there was no \"clean\" phase.");
93          Assertions.assertEquals(0, cacheSkippedBuild3.size(), "loading from cache, no more caching required");
94          // Expect to find and restore cached project
95          verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1));
96          verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2));
97      }
98  
99      @Test
100     void disabledViaProperty(Verifier verifier) throws VerificationException {
101 
102         verifier.setAutoclean(false);
103         verifier.addCliOption("--debug");
104 
105         verifier.setLogFileName("../log-1.txt");
106         verifier.executeGoal("verify");
107         verifier.verifyErrorFreeLog();
108         List<String> cacheSkippedBuild1 = LogFileUtils.findLinesContainingTextsInLogs(
109                 verifier, "Cache storing is skipped since there was no \"clean\" phase.");
110         Assertions.assertEquals(2, cacheSkippedBuild1.size(), "Expected 2 skipped module caching");
111 
112         assertThrows(
113                 VerificationException.class,
114                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
115                 "not expected to be loaded from the cache");
116         assertThrows(
117                 VerificationException.class,
118                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
119                 "not expected to be loaded from the cache");
120 
121         verifier.setLogFileName("../log-2.txt");
122         verifier.getCliOptions().clear();
123         verifier.addCliOption("--debug");
124         // With "true", we do not change the initially expected behaviour
125         verifier.addCliOption("-D" + CacheConfigImpl.MANDATORY_CLEAN + "=true");
126         verifier.executeGoal("verify");
127         verifier.verifyErrorFreeLog();
128         List<String> cacheSkippedBuild2 = LogFileUtils.findLinesContainingTextsInLogs(
129                 verifier, "Cache storing is skipped since there was no \"clean\" phase.");
130         Assertions.assertEquals(2, cacheSkippedBuild2.size(), "Expected 2 skipped module caching");
131 
132         assertThrows(
133                 VerificationException.class,
134                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
135                 "not expected to be loaded from the cache");
136         assertThrows(
137                 VerificationException.class,
138                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
139                 "not expected to be loaded from the cache");
140 
141         // With "false", we remove the need for the clean phase
142         verifier.getCliOptions().clear();
143         verifier.addCliOption("--debug");
144         verifier.addCliOption("-D" + CacheConfigImpl.MANDATORY_CLEAN + "=false");
145         verifier.setLogFileName("../log-3.txt");
146         verifier.executeGoal("verify");
147         verifier.verifyErrorFreeLog();
148         List<String> cacheSkippedBuild3 = LogFileUtils.findLinesContainingTextsInLogs(
149                 verifier, "Cache storing is skipped since there was no \"clean\" phase.");
150         Assertions.assertEquals(0, cacheSkippedBuild3.size(), "Expected 2 skipped module caching");
151         assertThrows(
152                 VerificationException.class,
153                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
154                 "not expected to be loaded from the cache");
155         assertThrows(
156                 VerificationException.class,
157                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
158                 "not expected to be loaded from the cache");
159     }
160 }