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.nio.file.Paths;
25  import java.util.List;
26  import java.util.stream.Collectors;
27  
28  import org.apache.maven.buildcache.its.junit.IntegrationTest;
29  import org.apache.maven.buildcache.util.LogFileUtils;
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  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * Check if cached builds are cleaned up correctly also for projects
39   * which don't contain a package phase.
40   */
41  @IntegrationTest("src/test/projects/mbuildcache-74-clean-cache-any-artifact")
42  public class Issue74Test {
43  
44      private static final Logger logger = LoggerFactory.getLogger(Issue74Test.class);
45  
46      @Test
47      void simple(Verifier verifier) throws VerificationException, IOException {
48          verifier.setAutoclean(false);
49          verifier.setMavenDebug(true);
50  
51          // first run - uncached
52          verifier.setLogFileName("../log-1.txt");
53          verifier.setSystemProperty("passed.by.test", "123");
54  
55          verifier.executeGoal("verify");
56  
57          verifier.verifyErrorFreeLog();
58          verifier.verifyTextInLog("Local build was not found");
59          verifyBuildCacheEntries(verifier, 1);
60  
61          // second run - modified
62          verifier.setLogFileName("../log-2.txt");
63          verifier.setSystemProperty("passed.by.test", "456");
64  
65          verifier.executeGoal("verify");
66  
67          verifier.verifyErrorFreeLog();
68          verifier.verifyTextInLog("Local build was not found");
69          verifyBuildCacheEntries(verifier, 1);
70      }
71  
72      private static void verifyBuildCacheEntries(final Verifier verifier, long expectedBuilds)
73              throws VerificationException, IOException {
74          String buildInfoXmlLog =
75                  LogFileUtils.findFirstLineContainingTextsInLogs(verifier, "Saved Build to local file: ");
76          String buildInfoXmlLocation = buildInfoXmlLog.split(":\\s")[1];
77  
78          Path buildInfoXmlPath = Paths.get(buildInfoXmlLocation);
79          // buildinfo.xml -> local -> hash -> project
80          Path projectPathInCache = buildInfoXmlPath.getParent().getParent().getParent();
81  
82          logger.info("Checking '{}' for cached builds ...", projectPathInCache);
83  
84          if (!Files.exists(projectPathInCache)) {
85              throw new VerificationException(
86                      String.format("Project directory in build cache doesn't exist: '%s'", projectPathInCache));
87          }
88  
89          List<Path> entries =
90                  Files.list(projectPathInCache).filter(p -> Files.isDirectory(p)).collect(Collectors.toList());
91  
92          Assertions.assertEquals(
93                  expectedBuilds,
94                  entries.size(),
95                  "Expected amount of cached builds not satisfied. Found: "
96                          + entries.stream().map(p -> p.getFileName().toString()).collect(Collectors.joining(",")));
97      }
98  }