|
17 | 17 | package org.springframework.boot.maven; |
18 | 18 |
|
19 | 19 | import java.io.File; |
20 | | -import java.io.FileReader; |
21 | 20 | import java.io.IOException; |
22 | 21 | import java.util.ArrayList; |
23 | 22 | import java.util.List; |
24 | 23 | import java.util.Map; |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
25 | 25 | import java.util.jar.JarFile; |
| 26 | +import java.util.stream.Collectors; |
26 | 27 |
|
27 | 28 | import org.junit.jupiter.api.TestTemplate; |
28 | 29 | import org.junit.jupiter.api.extension.ExtendWith; |
29 | 30 |
|
| 31 | +import org.springframework.boot.loader.tools.FileUtils; |
30 | 32 | import org.springframework.boot.loader.tools.JarModeLibrary; |
31 | | -import org.springframework.util.FileCopyUtils; |
| 33 | +import org.springframework.util.FileSystemUtils; |
32 | 34 |
|
33 | 35 | import static org.assertj.core.api.Assertions.assertThat; |
34 | 36 |
|
@@ -78,17 +80,34 @@ void whenRequiresUnpackConfigurationIsProvidedItIsReflectedInTheRepackagedWar(Ma |
78 | 80 | } |
79 | 81 |
|
80 | 82 | @TestTemplate |
81 | | - void whenWarIsRepackagedWithOutputTimestampTheBuildFailsAsItIsNotSupported(MavenBuild mavenBuild) |
| 83 | + void whenWarIsRepackagedWithOutputTimestampConfiguredThenWarIsReproducible(MavenBuild mavenBuild) |
82 | 84 | throws InterruptedException { |
83 | | - mavenBuild.project("war-output-timestamp").executeAndFail((project) -> { |
84 | | - try { |
85 | | - String log = FileCopyUtils.copyToString(new FileReader(new File(project, "target/build.log"))); |
86 | | - assertThat(log).contains("Reproducible repackaging is not supported with war packaging"); |
| 85 | + String firstHash = buildWarWithOutputTimestamp(mavenBuild); |
| 86 | + Thread.sleep(1500); |
| 87 | + String secondHash = buildWarWithOutputTimestamp(mavenBuild); |
| 88 | + assertThat(firstHash).isEqualTo(secondHash); |
| 89 | + } |
| 90 | + |
| 91 | + private String buildWarWithOutputTimestamp(MavenBuild mavenBuild) { |
| 92 | + AtomicReference<String> warHash = new AtomicReference<>(); |
| 93 | + mavenBuild.project("war-output-timestamp").execute((project) -> { |
| 94 | + File repackaged = new File(project, "target/war-output-timestamp-0.0.1.BUILD-SNAPSHOT.war"); |
| 95 | + assertThat(repackaged).isFile(); |
| 96 | + assertThat(repackaged.lastModified()).isEqualTo(1584352800000L); |
| 97 | + try (JarFile jar = new JarFile(repackaged)) { |
| 98 | + List<String> unreproducibleEntries = jar.stream() |
| 99 | + .filter((entry) -> entry.getLastModifiedTime().toMillis() != 1584352800000L) |
| 100 | + .map((entry) -> entry.getName() + ": " + entry.getLastModifiedTime()) |
| 101 | + .collect(Collectors.toList()); |
| 102 | + assertThat(unreproducibleEntries).isEmpty(); |
| 103 | + warHash.set(FileUtils.sha1Hash(repackaged)); |
| 104 | + FileSystemUtils.deleteRecursively(project); |
87 | 105 | } |
88 | | - catch (Exception ex) { |
| 106 | + catch (IOException ex) { |
89 | 107 | throw new RuntimeException(ex); |
90 | 108 | } |
91 | 109 | }); |
| 110 | + return warHash.get(); |
92 | 111 | } |
93 | 112 |
|
94 | 113 | @TestTemplate |
|
0 commit comments