Skip to content

Commit 943e47d

Browse files
committed
[MBUILDCACHE-67] IT test to check if a restoration error is handled properly
1 parent 9bf19f4 commit 943e47d

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<cache xmlns="http://maven.apache.org/CACHE-CONFIG/1.0.0">
23+
24+
</cache>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!--
2+
3+
Copyright 2021 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
-->
18+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20+
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>org.apache.maven.caching.test.mbuildcache-67</groupId>
23+
<artifactId>mbuildcache-67</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
<packaging>jar</packaging>
26+
27+
<properties>
28+
<maven.compiler.source>1.8</maven.compiler.source>
29+
<maven.compiler.target>1.8</maven.compiler.target>
30+
</properties>
31+
32+
<build>
33+
<extensions>
34+
<extension>
35+
<groupId>org.apache.maven.extensions</groupId>
36+
<artifactId>maven-build-cache-extension</artifactId>
37+
<version>${projectVersion}</version>
38+
</extension>
39+
</extensions>
40+
</build>
41+
42+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;
20+
21+
class Test
22+
{
23+
24+
}

0 commit comments

Comments
 (0)