1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
61 Assertions.assertTrue(
62 Files.deleteIfExists(Paths.get(jarCachePath)), "mbuildcache-67.jar was expected in the local cache");
63
64
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
83
84
85
86
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 }