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.Path;
23  import java.nio.file.Paths;
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.XmlService;
30  import org.apache.maven.buildcache.xml.build.ProjectsInputInfo;
31  import org.apache.maven.it.VerificationException;
32  import org.apache.maven.it.Verifier;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  
37  @IntegrationTest("src/test/projects/mbuildcache-87")
38  class Issue87Test {
39  
40      private static final String MODULE1_PROJECT_ARTIFACT = "org.apache.maven.caching.test.mbuildcache-87:module1:jar";
41      private static final String MODULE2_PROJECT_NAME = "org.apache.maven.caching.test.mbuildcache-87:module2";
42      private static final String FOUND_CACHED_RESTORING_MODULE2_MESSAGE =
43              "Found cached build, restoring " + MODULE2_PROJECT_NAME + " from cache";
44  
45      @Test
46      void simple(Verifier verifier) throws VerificationException, IOException {
47          verifier.setLogFileName("../log-0.txt");
48          verifier.executeGoals(Arrays.asList("-f", "external", "install"));
49          verifier.verifyErrorFreeLog();
50  
51          verifier.setAutoclean(false);
52  
53          verifier.setLogFileName("../log-1.txt");
54          verifier.executeGoals(Arrays.asList("-f", "top", "verify"));
55          verifier.verifyErrorFreeLog();
56  
57          verifier.setLogFileName("../log-2.txt");
58          verifier.executeGoals(Arrays.asList("-f", "top", "verify"));
59          verifier.verifyErrorFreeLog();
60          verifier.verifyTextInLog(FOUND_CACHED_RESTORING_MODULE2_MESSAGE);
61  
62          // START: Modifying maven plugin reactor dependency makes the cache stale
63          verifier.writeFile("top/module1/src/main/resources/org/apache/maven/buildcache/test.properties", "foo=bar");
64          verifier.setLogFileName("../log-3.txt");
65          verifier.executeGoals(Arrays.asList("-f", "top", "verify"));
66          verifier.verifyErrorFreeLog();
67          verifyTextNotInLog(verifier, FOUND_CACHED_RESTORING_MODULE2_MESSAGE);
68          // END: Modifying maven plugin reactor dependency makes the cache stale
69  
70          String buildInfoXmlLog =
71                  LogFileUtils.findLinesContainingTextsInLogs(verifier, "Saved Build to local file: ").stream()
72                          .filter(line -> line.contains("module2"))
73                          .findFirst()
74                          .orElseThrow(
75                                  () -> new VerificationException("Could not find module2 build info file location"));
76          Path buildInfoXmlLocation = Paths.get(buildInfoXmlLog.split(":\\s")[1]);
77  
78          ProjectsInputInfo projectsInputInfo =
79                  new XmlService().loadBuild(buildInfoXmlLocation.toFile()).getProjectsInputInfo();
80  
81          assertEquals(
82                  1,
83                  projectsInputInfo.getItems().stream()
84                          .filter(item -> "dependency".equals(item.getType()))
85                          .filter(item -> MODULE1_PROJECT_ARTIFACT.equals(item.getValue()))
86                          .count(),
87                  "Expected artifact acting as plugin dependency and project dependency to be considered twice during checksum computation");
88          assertEquals(
89                  1,
90                  projectsInputInfo.getItems().stream()
91                          .filter(item -> "pluginDependency".equals(item.getType()))
92                          .filter(item -> ("org.apache.maven.plugins:maven-dependency-plugin:maven-plugin|0|"
93                                          + MODULE1_PROJECT_ARTIFACT)
94                                  .equals(item.getValue()))
95                          .count(),
96                  "Expected artifact acting as plugin dependency and project dependency to be considered twice during checksum computation");
97  
98          assertEquals(
99                  1,
100                 projectsInputInfo.getItems().stream()
101                         .filter(item -> "pluginDependency".equals(item.getType()))
102                         .filter(item ->
103                                 "org.apache.maven.plugins:maven-dependency-plugin:maven-plugin|0|org.apache.maven.caching.test.mbuildcache-87:external:jar"
104                                         .equals(item.getValue()))
105                         .count(),
106                 "Expected external snapshot plugin dependency to be included in the checksum computation");
107 
108         assertEquals(
109                 0,
110                 projectsInputInfo.getItems().stream()
111                         .filter(item -> "pluginDependency".equals(item.getType()))
112                         .filter(item -> item.getValue()
113                                 .startsWith("org.apache.maven.plugins:maven-compiler-plugin:maven-plugin|"))
114                         .count(),
115                 "Expected plugins having excludeDependencies=true to have their dependencies excluded");
116     }
117 
118     private void verifyTextNotInLog(Verifier verifier, String text) throws VerificationException {
119 
120         List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
121 
122         boolean result = true;
123         for (String line : lines) {
124             if (Verifier.stripAnsi(line).contains(text)) {
125                 result = false;
126                 break;
127             }
128         }
129         if (!result) {
130             throw new VerificationException("Text found in log: " + text);
131         }
132     }
133 }