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  public 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  
56          verifier.setLogFileName("../log-1.txt");
57          verifier.executeGoal("verify");
58          verifier.verifyErrorFreeLog();
59          List<String> cacheSkippedBuild1 = LogFileUtils.findLinesContainingTextsInLogs(
60                  verifier, "Cache storing is skipped since there was no \"clean\" phase.");
61          Assertions.assertEquals(2, cacheSkippedBuild1.size(), "Expected 2 skipped module caching");
62  
63          assertThrows(
64                  VerificationException.class,
65                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
66                  "not expected to be loaded from the cache");
67          assertThrows(
68                  VerificationException.class,
69                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
70                  "not expected to be loaded from the cache");
71  
72          verifier.setLogFileName("../log-2.txt");
73          verifier.executeGoals(Arrays.asList("clean", "verify"));
74          verifier.verifyErrorFreeLog();
75          List<String> cacheSkippedBuild2 = LogFileUtils.findLinesContainingTextsInLogs(
76                  verifier, "Cache storing is skipped since there was no \"clean\" phase.");
77          Assertions.assertEquals(0, cacheSkippedBuild2.size(), "Expected 2 skipped module caching");
78          assertThrows(
79                  VerificationException.class,
80                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
81                  "not expected to be loaded from the cache");
82          assertThrows(
83                  VerificationException.class,
84                  () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
85                  "not expected to be loaded from the cache");
86  
87          verifier.setLogFileName("../log-3.txt");
88          verifier.executeGoal("verify");
89          verifier.verifyErrorFreeLog();
90          List<String> cacheSkippedBuild3 = LogFileUtils.findLinesContainingTextsInLogs(
91                  verifier, "Cache storing is skipped since there was no \"clean\" phase.");
92          Assertions.assertEquals(0, cacheSkippedBuild3.size(), "loading from cache, no more caching required");
93          // Expect to find and restore cached project
94          verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1));
95          verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2));
96      }
97  
98      @Test
99      void disabledViaProperty(Verifier verifier) throws VerificationException {
100 
101         verifier.setAutoclean(false);
102 
103         verifier.setLogFileName("../log-1.txt");
104         verifier.executeGoal("verify");
105         verifier.verifyErrorFreeLog();
106         List<String> cacheSkippedBuild1 = LogFileUtils.findLinesContainingTextsInLogs(
107                 verifier, "Cache storing is skipped since there was no \"clean\" phase.");
108         Assertions.assertEquals(2, cacheSkippedBuild1.size(), "Expected 2 skipped module caching");
109 
110         assertThrows(
111                 VerificationException.class,
112                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
113                 "not expected to be loaded from the cache");
114         assertThrows(
115                 VerificationException.class,
116                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
117                 "not expected to be loaded from the cache");
118 
119         verifier.setLogFileName("../log-2.txt");
120         verifier.getCliOptions().clear();
121         // With "true", we do not change the initially expected behaviour
122         verifier.addCliOption("-D" + CacheConfigImpl.MANDATORY_CLEAN + "=true");
123         verifier.executeGoal("verify");
124         verifier.verifyErrorFreeLog();
125         List<String> cacheSkippedBuild2 = LogFileUtils.findLinesContainingTextsInLogs(
126                 verifier, "Cache storing is skipped since there was no \"clean\" phase.");
127         Assertions.assertEquals(2, cacheSkippedBuild2.size(), "Expected 2 skipped module caching");
128 
129         assertThrows(
130                 VerificationException.class,
131                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
132                 "not expected to be loaded from the cache");
133         assertThrows(
134                 VerificationException.class,
135                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
136                 "not expected to be loaded from the cache");
137 
138         // With "false", we remove the need for the clean phase
139         verifier.getCliOptions().clear();
140         verifier.addCliOption("-D" + CacheConfigImpl.MANDATORY_CLEAN + "=false");
141         verifier.setLogFileName("../log-3.txt");
142         verifier.executeGoal("verify");
143         verifier.verifyErrorFreeLog();
144         List<String> cacheSkippedBuild3 = LogFileUtils.findLinesContainingTextsInLogs(
145                 verifier, "Cache storing is skipped since there was no \"clean\" phase.");
146         Assertions.assertEquals(0, cacheSkippedBuild3.size(), "Expected 2 skipped module caching");
147         assertThrows(
148                 VerificationException.class,
149                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
150                 "not expected to be loaded from the cache");
151         assertThrows(
152                 VerificationException.class,
153                 () -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
154                 "not expected to be loaded from the cache");
155     }
156 }