Skip to content

Commit a1bdd79

Browse files
committed
New Maven installation for test
1 parent 4501579 commit a1bdd79

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

sbm-support-rewrite/.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
testcode/reposilite-test/user.home/**
2+
!testcode/reposilite-test/user.home/.m2/settings.xml.template
3+
!testcode/reposilite-test/user.home/.m2/settings-security.xml
14
testcode/reposilite-test/reposilite-data/**
2-
!testcode/reposilite-test/reposilite-data/shared.configuration.json
5+
!testcode/reposilite-test/reposilite-data/shared.configuration.json
6+
/testcode/reposilite-test/user.home/apache-maven-3.9.5/

sbm-support-rewrite/src/test/java/org/springframework/sbm/PrivateArtifactRepositoryTest.java

+84-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.springframework.sbm;
1717

18+
import net.sf.saxon.trans.SymbolicName;
19+
import org.apache.commons.io.FileUtils;
1820
import org.apache.maven.shared.invoker.*;
1921
import org.junit.jupiter.api.*;
22+
import org.junit.jupiter.api.io.TempDir;
2023
import org.openrewrite.SourceFile;
2124
import org.openrewrite.java.JavaParser;
2225
import org.openrewrite.java.internal.JavaTypeCache;
@@ -37,14 +40,19 @@
3740
import org.testcontainers.utility.DockerImageName;
3841
import org.testcontainers.utility.MountableFile;
3942

40-
import java.io.File;
41-
import java.io.IOException;
43+
import java.io.*;
44+
import java.net.URL;
4245
import java.nio.file.Files;
4346
import java.nio.file.Path;
47+
import java.nio.file.attribute.PosixFilePermission;
48+
import java.nio.file.attribute.PosixFilePermissions;
4449
import java.util.Arrays;
4550
import java.util.List;
51+
import java.util.Set;
4652
import java.util.regex.Matcher;
4753
import java.util.regex.Pattern;
54+
import java.util.zip.ZipEntry;
55+
import java.util.zip.ZipInputStream;
4856

4957
import static org.assertj.core.api.Assertions.assertThat;
5058
import static org.assertj.core.api.Fail.fail;
@@ -83,10 +91,83 @@ public class PrivateArtifactRepositoryTest {
8391
private Path dependencyPathInLocalMavenRepo = Path.of(TESTCODE_DIR + "/user.home/.m2/repository/com/example/dependency/dependency-project");
8492

8593
@BeforeAll
86-
static void beforeAll() {
94+
static void beforeAll(@TempDir Path tempDir) {
8795
originalUserHome = System.getProperty("user.home");
8896
newUserHome = Path.of(".").resolve(TESTCODE_DIR + "/user.home").toAbsolutePath().normalize().toString();
8997
System.setProperty("user.home", newUserHome);
98+
99+
// download Maven
100+
if (!Path.of("./testcode/reposilite-test/user.home/apache-maven-3.9.5/bin/mvn").toFile().exists()) {
101+
String mavenDownloadUrl = "https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.zip";
102+
try {
103+
Path mavenInstallDir = Path.of(TESTCODE_DIR + "/user.home");
104+
File downloadedMavenZipFile = tempDir.resolve("apache-maven-3.9.5-bin.zip").toFile();
105+
FileUtils.copyURLToFile(
106+
new URL(mavenDownloadUrl),
107+
downloadedMavenZipFile,
108+
10000,
109+
30000);
110+
unzip(downloadedMavenZipFile, mavenInstallDir);
111+
File file = mavenInstallDir.resolve("apache-maven-3.9.5/bin/mvn").toFile();
112+
file.setExecutable(true, false);
113+
assertThat(file.canExecute()).isTrue();
114+
} catch (IOException e) {
115+
throw new RuntimeException(e);
116+
}
117+
}
118+
}
119+
120+
private static void unzip(File downloadedMavenZipFile, Path mavenInstallDir) {
121+
try {
122+
byte[] buffer = new byte[1024];
123+
ZipInputStream zis = null;
124+
125+
zis = new ZipInputStream(new FileInputStream(downloadedMavenZipFile));
126+
127+
ZipEntry zipEntry = zis.getNextEntry();
128+
while (zipEntry != null) {
129+
File newFile = newFile(mavenInstallDir.toFile(), zipEntry);
130+
if (zipEntry.isDirectory()) {
131+
if (!newFile.isDirectory() && !newFile.mkdirs()) {
132+
throw new IOException("Failed to create directory " + newFile);
133+
}
134+
} else {
135+
// fix for Windows-created archives
136+
File parent = newFile.getParentFile();
137+
if (!parent.isDirectory() && !parent.mkdirs()) {
138+
throw new IOException("Failed to create directory " + parent);
139+
}
140+
141+
// write file content
142+
FileOutputStream fos = new FileOutputStream(newFile);
143+
int len;
144+
while ((len = zis.read(buffer)) > 0) {
145+
fos.write(buffer, 0, len);
146+
}
147+
fos.close();
148+
}
149+
zipEntry = zis.getNextEntry();
150+
}
151+
zis.closeEntry();
152+
zis.close();
153+
} catch (FileNotFoundException e) {
154+
throw new RuntimeException(e);
155+
} catch (IOException e) {
156+
throw new RuntimeException(e);
157+
}
158+
}
159+
160+
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
161+
File destFile = new File(destinationDir, zipEntry.getName());
162+
163+
String destDirPath = destinationDir.getCanonicalPath();
164+
String destFilePath = destFile.getCanonicalPath();
165+
166+
if (!destFilePath.startsWith(destDirPath + File.separator)) {
167+
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
168+
}
169+
170+
return destFile;
90171
}
91172

92173
@AfterAll
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[filesystem "Azul Systems, Inc.|17.0.2|/dev/disk3s5"]
2+
timestampResolution = 4000 nanoseconds
3+
minRacyThreshold = 0 nanoseconds

0 commit comments

Comments
 (0)