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.File;
22  import java.io.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  
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.Assertions;
31  import org.junit.jupiter.api.Test;
32  
33  import static org.apache.maven.buildcache.util.LogFileUtils.findFirstLineContainingTextsInLogs;
34  
35  /**
36   * Check if a restoration restores build incrementally,i.e. package -> verify -> install -> deploy,
37   * so that the cached executions are not run again for builds with a higher goal.
38   */
39  @IntegrationTest("src/test/projects/mbuildcache-incremental")
40  class IncrementalRestoreTest {
41  
42      public static final String SAVED_BUILD_TO_LOCAL_FILE = "Saved Build to local file: ";
43      public static final String GENERATED_JAR = "target/mbuildcache-incremental-final.jar";
44      public static final String GENERATED_SOURCES_JAR = "target/mbuildcache-incremental-final-sources.jar";
45      public static final String GENERATED_JAVADOC_JAR = "target/mbuildcache-incremental-final-javadoc.jar";
46  
47      public static final String EXTRA_OUTPUT_1 =
48              "target" + File.separatorChar + "extra-resources" + File.separatorChar + "extra-readme-1.md";
49      public static final String EXTRA_OUTPUT_2 = "target/extra-resources/extra-readme-2.md";
50      public static final String EXTRA_OUTPUT_3 = "target/extra-resources/other-readme-1.md";
51      public static final String EXTRA_OUTPUT_4 = "target/other-resources/extra-readme-1.md";
52      public static final String EXTRA_OUTPUT_5 = "target/other-resources/extra-readme-2.md";
53      public static final String EXTRA_OUTPUT_6 = "target/other-resources/other-readme-1.md";
54      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_RESOURCES_RESOURCES =
55              "Skipping plugin execution (cached): resources:resources";
56      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_COMPILER_COMPILE =
57              "Skipping plugin execution (cached): compiler:compile";
58      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_RESOURCES_TEST_RESOURCES =
59              "Skipping plugin execution (cached): resources:testResources";
60      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_COMPILER_TEST_COMPILE =
61              "Skipping plugin execution (cached): compiler:testCompile";
62      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_SUREFIRE_TEST =
63              "Skipping plugin execution (cached): surefire:test";
64      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_JAR_JAR = "Skipping plugin execution (cached): jar:jar";
65      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_RESOURCES_COPY =
66              "Skipping plugin execution (cached): resources:copy-resources";
67      public static final String INSTALL_DEFAULT_INSTALL_MBUILDCACHE_INCREMENTAL =
68              "install (default-install) @ mbuildcache-incremental";
69      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_INSTALL_INSTALL =
70              "Skipping plugin execution (cached): install:install";
71      public static final String DEPLOY_DEFAULT_DEPLOY_MBUILDCACHE_INCREMENTAL =
72              "deploy (default-deploy) @ mbuildcache-incremental";
73      public static final String LOCAL_BUILD_WAS_NOT_FOUND_BY_CHECKSUM = "Local build was not found by checksum";
74      public static final String RESOURCES_DEFAULT_RESOURCES_MBUILDCACHE_INCREMENTAL =
75              "resources (default-resources) @ mbuildcache-incremental";
76      public static final String COMPILE_DEFAULT_COMPILE_MBUILDCACHE_INCREMENTAL =
77              "compile (default-compile) @ mbuildcache-incremental";
78      public static final String TEST_RESOURCES_DEFAULT_TEST_RESOURCES_MBUILDCACHE_INCREMENTAL =
79              "testResources (default-testResources) @ mbuildcache-incremental";
80      public static final String TEST_COMPILE_DEFAULT_TEST_COMPILE_MBUILDCACHE_INCREMENTAL =
81              "testCompile (default-testCompile) @ mbuildcache-incremental";
82      public static final String TEST_DEFAULT_TEST_MBUILDCACHE_INCREMENTAL =
83              "test (default-test) @ mbuildcache-incremental";
84      public static final String JAR_DEFAULT_JAR_MBUILDCACHE_INCREMENTAL = "jar (default-jar) @ mbuildcache-incremental";
85      public static final String
86              FOUND_CACHED_BUILD_RESTORING_ORG_APACHE_MAVEN_CACHING_TEST_MBUILDCACHE_INCREMENTAL_FROM_CACHE_BY_CHECKSUM =
87                      "Found cached build, restoring org.apache.maven.caching.test:mbuildcache-incremental from cache by checksum";
88      public static final String MBUILDCACHE_INCREMENTAL_JAR = "mbuildcache-incremental.jar";
89      public static final String MBUILDCACHE_INCREMENTAL_SOURCES_JAR = "mbuildcache-incremental-sources.jar";
90      public static final String MBUILDCACHE_INCREMENTAL_JAVADOC_JAR = "mbuildcache-incremental-javadoc.jar";
91      public static final String INTEGRATION_TEST_DEFAULT_MBUILDCACHE_INCREMENTAL =
92              "integration-test (default) @ mbuildcache-incremental";
93      public static final String VERIFY_DEFAULT_MBUILDCACHE_INCREMENTAL = "verify (default) @ mbuildcache-incremental";
94      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_INTEGRATION_TEST =
95              "Skipping plugin execution (cached): failsafe:integration-test";
96      public static final String SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_VERIFY =
97              "Skipping plugin execution (cached): failsafe:verify";
98  
99      private Path jarCacheFile;
100     private Path jarSourcesCacheFile;
101     private Path jarJavadocCacheFile;
102 
103     @Test
104     void simple(Verifier verifier) throws VerificationException, IOException {
105         verifier.setAutoclean(false);
106         verifier.setMavenDebug(true);
107 
108         initialBuild(verifier);
109         verifyPackageWithCache(verifier);
110         verifyWithCache(verifier);
111         installWithCache(verifier);
112         deployWithCache(verifier);
113         replayInstallWithCache(verifier);
114     }
115 
116     private void initialBuild(Verifier verifier) throws VerificationException, IOException {
117         // First build, nothing in cache
118         verifier.setLogFileName("../log-package.txt");
119         verifier.executeGoal("package");
120         verifier.verifyErrorFreeLog();
121         verifier.verifyTextInLog(LOCAL_BUILD_WAS_NOT_FOUND_BY_CHECKSUM);
122         verifier.verifyTextInLog(RESOURCES_DEFAULT_RESOURCES_MBUILDCACHE_INCREMENTAL);
123         verifier.verifyTextInLog(COMPILE_DEFAULT_COMPILE_MBUILDCACHE_INCREMENTAL);
124         verifier.verifyTextInLog(TEST_RESOURCES_DEFAULT_TEST_RESOURCES_MBUILDCACHE_INCREMENTAL);
125         verifier.verifyTextInLog(TEST_COMPILE_DEFAULT_TEST_COMPILE_MBUILDCACHE_INCREMENTAL);
126         verifier.verifyTextInLog(TEST_DEFAULT_TEST_MBUILDCACHE_INCREMENTAL);
127         verifier.verifyTextInLog(JAR_DEFAULT_JAR_MBUILDCACHE_INCREMENTAL);
128         verifier.verifyTextInLog(SAVED_BUILD_TO_LOCAL_FILE);
129         verifier.verifyFilePresent(GENERATED_JAR);
130 
131         // First build : all resources are present in the target folder
132         verifyAllExtraOutputsPresent(verifier);
133 
134         Path buildInfoPath = getSavedBuildInfoPath(verifier);
135         jarCacheFile = buildInfoPath.getParent().resolve(MBUILDCACHE_INCREMENTAL_JAR);
136         jarSourcesCacheFile = buildInfoPath.getParent().resolve(MBUILDCACHE_INCREMENTAL_SOURCES_JAR);
137         jarJavadocCacheFile = buildInfoPath.getParent().resolve(MBUILDCACHE_INCREMENTAL_JAVADOC_JAR);
138         Assertions.assertTrue(Files.exists(jarCacheFile), "Expected artifact saved in build cache.");
139         Assertions.assertFalse(
140                 Files.exists(jarSourcesCacheFile), "Not expected sources artifact saved in build cache.");
141         Assertions.assertFalse(
142                 Files.exists(jarJavadocCacheFile), "Not expected javadoc artifact saved in build cache.");
143     }
144 
145     private void verifyPackageWithCache(Verifier verifier) throws VerificationException {
146         // Verify clean build, with the same goal should be fully restored
147         cleanBuild(verifier);
148 
149         verifier.setLogFileName("../log-package-2.txt");
150         verifier.executeGoal("package");
151         verifier.verifyTextInLog(
152                 FOUND_CACHED_BUILD_RESTORING_ORG_APACHE_MAVEN_CACHING_TEST_MBUILDCACHE_INCREMENTAL_FROM_CACHE_BY_CHECKSUM);
153         verifier.verifyTextInLog(
154                 "Found cached build, restoring org.apache.maven.caching.test:mbuildcache-incremental from cache by checksum");
155         verifier.verifyErrorFreeLog();
156         verifySkippedPluginExecutions(verifier);
157         verifier.verifyFilePresent(GENERATED_JAR);
158         verifyCachedExtraOutputs(verifier);
159         Assertions.assertTrue(Files.exists(jarCacheFile), "Expected artifact saved in build cache.");
160     }
161 
162     private void verifyWithCache(Verifier verifier) throws VerificationException {
163         // Next step : verify
164         cleanBuild(verifier);
165 
166         verifier.setLogFileName("../log-verify.txt");
167         verifier.executeGoal("verify");
168         verifier.verifyTextInLog(
169                 FOUND_CACHED_BUILD_RESTORING_ORG_APACHE_MAVEN_CACHING_TEST_MBUILDCACHE_INCREMENTAL_FROM_CACHE_BY_CHECKSUM);
170         verifier.verifyTextInLog(
171                 "Project org.apache.maven.caching.test:mbuildcache-incremental restored partially. Highest cached goal: package, requested: verify");
172         verifier.verifyErrorFreeLog();
173         verifySkippedPluginExecutions(verifier);
174         verifier.verifyTextInLog(INTEGRATION_TEST_DEFAULT_MBUILDCACHE_INCREMENTAL);
175         verifier.verifyTextInLog(VERIFY_DEFAULT_MBUILDCACHE_INCREMENTAL);
176         verifier.verifyTextInLog(SAVED_BUILD_TO_LOCAL_FILE);
177         verifier.verifyFilePresent(GENERATED_JAR);
178         verifyCachedExtraOutputs(verifier);
179         Assertions.assertTrue(Files.exists(jarCacheFile), "Expected artifact saved in build cache.");
180     }
181 
182     private void installWithCache(Verifier verifier) throws VerificationException {
183         // Install with clean build, with a higher goal should restore cached mojo executions and apply increments
184         cleanBuild(verifier);
185 
186         verifier.setLogFileName("../log-install.txt");
187         verifier.executeGoal("install");
188         verifier.verifyErrorFreeLog();
189         verifier.verifyTextInLog(
190                 "Project org.apache.maven.caching.test:mbuildcache-incremental restored partially. Highest cached goal: verify, requested: install");
191         verifySkippedPluginExecutions(verifier);
192         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_INTEGRATION_TEST);
193         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_VERIFY);
194         verifyNoCachedPluginExecutions(verifier);
195         verifier.verifyTextInLog(INSTALL_DEFAULT_INSTALL_MBUILDCACHE_INCREMENTAL);
196         final String installToLocalRepoString =
197                 "Installing " + verifier.getBasedir() + File.separatorChar + EXTRA_OUTPUT_1 + " to ";
198         verifier.verifyTextInLog(installToLocalRepoString);
199         verifier.verifyTextInLog(SAVED_BUILD_TO_LOCAL_FILE);
200         verifier.verifyFilePresent(GENERATED_JAR);
201         verifyCachedExtraOutputs(verifier);
202         Assertions.assertTrue(Files.exists(jarCacheFile), "Expected artifact saved in build cache.");
203     }
204 
205     private void deployWithCache(Verifier verifier) throws VerificationException {
206         // Deploy with clean build, with a higher goal should restore cached mojo executions and apply increments
207         cleanBuild(verifier);
208 
209         verifier.setLogFileName("../log-deploy.txt");
210         verifier.executeGoal("deploy");
211         verifier.verifyErrorFreeLog();
212         verifier.verifyTextInLog(
213                 "Project org.apache.maven.caching.test:mbuildcache-incremental restored partially. Highest cached goal: install, requested: deploy");
214         verifySkippedPluginExecutions(verifier);
215         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_INTEGRATION_TEST);
216         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_VERIFY);
217         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_INSTALL_INSTALL);
218         verifyNoCachedPluginExecutions(verifier);
219         verifyNoTextInLog(verifier, INSTALL_DEFAULT_INSTALL_MBUILDCACHE_INCREMENTAL);
220         verifier.verifyTextInLog(DEPLOY_DEFAULT_DEPLOY_MBUILDCACHE_INCREMENTAL);
221         verifier.verifyTextInLog("Using alternate deployment repository local::file:./target/staging");
222         verifier.verifyTextInLog(SAVED_BUILD_TO_LOCAL_FILE);
223         verifier.verifyFilePresent(GENERATED_JAR);
224         verifier.verifyFilePresent(GENERATED_SOURCES_JAR);
225         verifier.verifyFilePresent(GENERATED_JAVADOC_JAR);
226         verifyCachedExtraOutputs(verifier);
227         Assertions.assertTrue(Files.exists(jarCacheFile), "Expected artifact saved in build cache.");
228         Assertions.assertTrue(Files.exists(jarSourcesCacheFile), "Expected sources artifact saved in build cache.");
229         Assertions.assertTrue(Files.exists(jarJavadocCacheFile), "Expected javadoc artifact saved in build cache.");
230     }
231 
232     private void replayInstallWithCache(Verifier verifier) throws VerificationException {
233         // Replay install with clean build, with a lower goal should only restore cached mojo executions
234         verifier.setLogFileName("../log-install-replay.txt");
235         verifier.executeGoal("install");
236         verifier.verifyErrorFreeLog();
237         verifier.verifyTextInLog(
238                 FOUND_CACHED_BUILD_RESTORING_ORG_APACHE_MAVEN_CACHING_TEST_MBUILDCACHE_INCREMENTAL_FROM_CACHE_BY_CHECKSUM);
239         verifySkippedPluginExecutions(verifier);
240         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_INTEGRATION_TEST);
241         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_FAILSAFE_VERIFY);
242         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_INSTALL_INSTALL);
243         verifyNoTextInLog(verifier, DEPLOY_DEFAULT_DEPLOY_MBUILDCACHE_INCREMENTAL);
244         verifyNoCachedPluginExecutions(verifier);
245         verifyNoTextInLog(verifier, SAVED_BUILD_TO_LOCAL_FILE, "Expected successful build cache restore.");
246         verifier.verifyFilePresent(GENERATED_JAR);
247         verifier.verifyFilePresent(GENERATED_SOURCES_JAR);
248         verifier.verifyFilePresent(GENERATED_JAVADOC_JAR);
249         verifyCachedExtraOutputs(verifier);
250         Assertions.assertTrue(Files.exists(jarCacheFile), "Expected artifact saved in build cache.");
251         Assertions.assertTrue(Files.exists(jarSourcesCacheFile), "Expected sources artifact saved in build cache.");
252         Assertions.assertTrue(Files.exists(jarJavadocCacheFile), "Expected javadoc artifact saved in build cache.");
253     }
254 
255     private void cleanBuild(Verifier verifier) throws VerificationException {
256         verifier.setMavenDebug(false);
257         verifier.setLogFileName("../log-clean.txt");
258         verifier.executeGoal("clean");
259         verifier.verifyFileNotPresent(GENERATED_JAR);
260     }
261 
262     private void verifyAllExtraOutputsPresent(Verifier verifier) throws VerificationException {
263         verifier.verifyFilePresent(EXTRA_OUTPUT_1);
264         verifier.verifyFilePresent(EXTRA_OUTPUT_2);
265         verifier.verifyFilePresent(EXTRA_OUTPUT_3);
266         verifier.verifyFilePresent(EXTRA_OUTPUT_4);
267         verifier.verifyFilePresent(EXTRA_OUTPUT_5);
268         verifier.verifyFilePresent(EXTRA_OUTPUT_6);
269     }
270 
271     private void verifyCachedExtraOutputs(Verifier verifier) throws VerificationException {
272         verifier.verifyFilePresent(EXTRA_OUTPUT_1);
273         verifier.verifyFilePresent(EXTRA_OUTPUT_2);
274         verifier.verifyFileNotPresent(EXTRA_OUTPUT_3);
275         verifier.verifyFileNotPresent(EXTRA_OUTPUT_4);
276         verifier.verifyFileNotPresent(EXTRA_OUTPUT_5);
277         verifier.verifyFilePresent(EXTRA_OUTPUT_6);
278     }
279 
280     private void verifySkippedPluginExecutions(Verifier verifier) throws VerificationException {
281         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_RESOURCES_RESOURCES);
282         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_COMPILER_COMPILE);
283         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_RESOURCES_TEST_RESOURCES);
284         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_COMPILER_TEST_COMPILE);
285         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_SUREFIRE_TEST);
286         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_JAR_JAR);
287         verifier.verifyTextInLog(SKIPPING_PLUGIN_EXECUTION_CACHED_RESOURCES_COPY);
288     }
289 
290     private void verifyNoCachedPluginExecutions(Verifier verifier) throws VerificationException {
291         verifyNoTextInLog(verifier, RESOURCES_DEFAULT_RESOURCES_MBUILDCACHE_INCREMENTAL);
292         verifyNoTextInLog(verifier, COMPILE_DEFAULT_COMPILE_MBUILDCACHE_INCREMENTAL);
293         verifyNoTextInLog(verifier, TEST_RESOURCES_DEFAULT_TEST_RESOURCES_MBUILDCACHE_INCREMENTAL);
294         verifyNoTextInLog(verifier, TEST_COMPILE_DEFAULT_TEST_COMPILE_MBUILDCACHE_INCREMENTAL);
295         verifyNoTextInLog(verifier, TEST_DEFAULT_TEST_MBUILDCACHE_INCREMENTAL);
296         verifyNoTextInLog(verifier, JAR_DEFAULT_JAR_MBUILDCACHE_INCREMENTAL);
297         verifyNoTextInLog(verifier, INTEGRATION_TEST_DEFAULT_MBUILDCACHE_INCREMENTAL);
298         verifyNoTextInLog(verifier, VERIFY_DEFAULT_MBUILDCACHE_INCREMENTAL);
299     }
300 
301     private static void verifyNoTextInLog(Verifier verifier, String text, String message) throws VerificationException {
302         Assertions.assertNull(findFirstLineContainingTextsInLogs(verifier, text), message);
303     }
304 
305     private static void verifyNoTextInLog(Verifier verifier, String text) throws VerificationException {
306         Assertions.assertNull(findFirstLineContainingTextsInLogs(verifier, text));
307     }
308 
309     private static Path getSavedBuildInfoPath(Verifier verifier) throws VerificationException {
310         String savedPathLogLine = findFirstLineContainingTextsInLogs(verifier, SAVED_BUILD_TO_LOCAL_FILE);
311         String[] array = savedPathLogLine.split(SAVED_BUILD_TO_LOCAL_FILE);
312         return Paths.get(array[array.length - 1]);
313     }
314 }