|
15 | 15 | */
|
16 | 16 | package org.springframework.sbm;
|
17 | 17 |
|
| 18 | +import net.sf.saxon.trans.SymbolicName; |
| 19 | +import org.apache.commons.io.FileUtils; |
18 | 20 | import org.apache.maven.shared.invoker.*;
|
19 | 21 | import org.junit.jupiter.api.*;
|
| 22 | +import org.junit.jupiter.api.io.TempDir; |
20 | 23 | import org.openrewrite.SourceFile;
|
21 | 24 | import org.openrewrite.java.JavaParser;
|
22 | 25 | import org.openrewrite.java.internal.JavaTypeCache;
|
|
37 | 40 | import org.testcontainers.utility.DockerImageName;
|
38 | 41 | import org.testcontainers.utility.MountableFile;
|
39 | 42 |
|
40 |
| -import java.io.File; |
41 |
| -import java.io.IOException; |
| 43 | +import java.io.*; |
| 44 | +import java.net.URL; |
42 | 45 | import java.nio.file.Files;
|
43 | 46 | import java.nio.file.Path;
|
| 47 | +import java.nio.file.attribute.PosixFilePermission; |
| 48 | +import java.nio.file.attribute.PosixFilePermissions; |
44 | 49 | import java.util.Arrays;
|
45 | 50 | import java.util.List;
|
| 51 | +import java.util.Set; |
46 | 52 | import java.util.regex.Matcher;
|
47 | 53 | import java.util.regex.Pattern;
|
| 54 | +import java.util.zip.ZipEntry; |
| 55 | +import java.util.zip.ZipInputStream; |
48 | 56 |
|
49 | 57 | import static org.assertj.core.api.Assertions.assertThat;
|
50 | 58 | import static org.assertj.core.api.Fail.fail;
|
@@ -83,10 +91,83 @@ public class PrivateArtifactRepositoryTest {
|
83 | 91 | private Path dependencyPathInLocalMavenRepo = Path.of(TESTCODE_DIR + "/user.home/.m2/repository/com/example/dependency/dependency-project");
|
84 | 92 |
|
85 | 93 | @BeforeAll
|
86 |
| - static void beforeAll() { |
| 94 | + static void beforeAll(@TempDir Path tempDir) { |
87 | 95 | originalUserHome = System.getProperty("user.home");
|
88 | 96 | newUserHome = Path.of(".").resolve(TESTCODE_DIR + "/user.home").toAbsolutePath().normalize().toString();
|
89 | 97 | 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; |
90 | 171 | }
|
91 | 172 |
|
92 | 173 | @AfterAll
|
|
0 commit comments