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.Paths;
24  import java.util.Arrays;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.maven.buildcache.its.junit.IntegrationTest;
29  import org.apache.maven.it.VerificationException;
30  import org.apache.maven.it.Verifier;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Check if a restoration error is handled properly = the build should be executed "normally", like if there is no cache.
36   */
37  @IntegrationTest("src/test/projects/mbuildcache-67")
38  public class Issue67Test {
39  
40      public static final String SAVED_BUILD_TO_LOCAL_FILE = "Saved Build to local file: ";
41      public static final String GENERATED_JAR = "target/mbuildcache-67-0.0.1-SNAPSHOT.jar";
42  
43      @Test
44      void simple(Verifier verifier) throws VerificationException, IOException {
45          verifier.setAutoclean(false);
46          verifier.setMavenDebug(true);
47  
48          // First build, nothing in cache
49          verifier.setLogFileName("../log.txt");
50          verifier.executeGoal("verify");
51          verifier.verifyErrorFreeLog();
52  
53          verifier.verifyFilePresent(GENERATED_JAR);
54  
55          String savedPathLogLine = findFirstLineContainingTextsInLogs(verifier, SAVED_BUILD_TO_LOCAL_FILE);
56          Assertions.assertNotNull(savedPathLogLine, "We expect a debug log line with the path to the saved cache file");
57          String[] array = savedPathLogLine.split(SAVED_BUILD_TO_LOCAL_FILE);
58          String jarCachePath = array[array.length - 1].replace("buildinfo.xml", "mbuildcache-67.jar");
59  
60          // We remove from the local cache repository the jar artefact. In order to launch a restoration error.
61          Assertions.assertTrue(
62                  Files.deleteIfExists(Paths.get(jarCachePath)), "mbuildcache-67.jar was expected in the local cache");
63  
64          // Second build, with a corrupted cache
65          verifier.setMavenDebug(false);
66          verifier.setLogFileName("../log-2.txt");
67          verifier.executeGoal("clean");
68          verifier.verifyFileNotPresent(GENERATED_JAR);
69  
70          verifier.setLogFileName("../log-3.txt");
71          verifier.executeGoal("verify");
72  
73          verifier.verifyTextInLog(
74                  "Found cached build, restoring org.apache.maven.caching.test.mbuildcache-67:mbuildcache-67 from cache by checksum");
75          verifier.verifyTextInLog("Cannot restore project artifacts, continuing with non cached build");
76          verifier.verifyErrorFreeLog();
77  
78          verifier.verifyFilePresent(GENERATED_JAR);
79      }
80  
81      /**
82       * TODO : remove this function and use the one in LogFileUtils instead (not merged yet)
83       * @param verifier
84       * @param texts
85       * @return
86       * @throws VerificationException
87       */
88      private static String findFirstLineContainingTextsInLogs(final Verifier verifier, final String... texts)
89              throws VerificationException {
90          List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
91          Iterator it = lines.iterator();
92  
93          while (it.hasNext()) {
94              String line = verifier.stripAnsi((String) it.next());
95              boolean matches = true;
96              Iterator<String> toMatchIterator = Arrays.stream(texts).iterator();
97              while (matches && toMatchIterator.hasNext()) {
98                  matches = line.contains(toMatchIterator.next());
99              }
100             if (matches) {
101                 return line;
102             }
103         }
104 
105         return null;
106     }
107 }