Skip to content

Commit 6f7464e

Browse files
committed
Refactor test
1 parent 1612670 commit 6f7464e

File tree

1 file changed

+76
-80
lines changed

1 file changed

+76
-80
lines changed

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

+76-80
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
*/
1616
package org.springframework.sbm;
1717

18-
import net.sf.saxon.trans.SymbolicName;
1918
import org.apache.commons.io.FileUtils;
2019
import org.apache.maven.shared.invoker.*;
2120
import org.junit.jupiter.api.*;
2221
import org.junit.jupiter.api.io.TempDir;
23-
import org.openrewrite.SourceFile;
24-
import org.openrewrite.java.JavaParser;
25-
import org.openrewrite.java.internal.JavaTypeCache;
2622
import org.openrewrite.java.marker.JavaSourceSet;
2723
import org.openrewrite.java.tree.J;
2824
import org.openrewrite.java.tree.JavaType;
@@ -44,11 +40,8 @@
4440
import java.net.URL;
4541
import java.nio.file.Files;
4642
import java.nio.file.Path;
47-
import java.nio.file.attribute.PosixFilePermission;
48-
import java.nio.file.attribute.PosixFilePermissions;
4943
import java.util.Arrays;
5044
import java.util.List;
51-
import java.util.Set;
5245
import java.util.regex.Matcher;
5346
import java.util.regex.Pattern;
5447
import java.util.zip.ZipEntry;
@@ -95,79 +88,7 @@ static void beforeAll(@TempDir Path tempDir) {
9588
originalUserHome = System.getProperty("user.home");
9689
newUserHome = Path.of(".").resolve(TESTCODE_DIR + "/user.home").toAbsolutePath().normalize().toString();
9790
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;
91+
installMavenForTestIfNotExists(tempDir);
17192
}
17293

17394
@AfterAll
@@ -222,6 +143,7 @@ void mavenSettingsShouldBeRead() throws IOException, MavenInvocationException, I
222143

223144
// @Test
224145
// @Order(2)
146+
225147
@DisplayName("verify dependencies from private repo were resolved")
226148
void verifyDependenciesFromPrivateRepoWereResolved() {
227149
// verify dependency does not exist in local Maven repo
@@ -255,6 +177,80 @@ void verifyDependenciesFromPrivateRepoWereResolved() {
255177
assertThat(type.getFullyQualifiedName()).isEqualTo(DEPENDENCY_CLASS_FQNAME);
256178
}
257179

180+
private static void installMavenForTestIfNotExists(Path tempDir) {
181+
if (!Path.of("./testcode/reposilite-test/user.home/apache-maven-3.9.5/bin/mvn").toFile().exists()) {
182+
String mavenDownloadUrl = "https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.zip";
183+
try {
184+
Path mavenInstallDir = Path.of(TESTCODE_DIR + "/user.home");
185+
File downloadedMavenZipFile = tempDir.resolve("apache-maven-3.9.5-bin.zip").toFile();
186+
FileUtils.copyURLToFile(
187+
new URL(mavenDownloadUrl),
188+
downloadedMavenZipFile,
189+
10000,
190+
30000);
191+
unzip(downloadedMavenZipFile, mavenInstallDir);
192+
File file = mavenInstallDir.resolve("apache-maven-3.9.5/bin/mvn").toFile();
193+
file.setExecutable(true, false);
194+
assertThat(file.canExecute()).isTrue();
195+
} catch (IOException e) {
196+
throw new RuntimeException(e);
197+
}
198+
}
199+
}
200+
201+
private static void unzip(File downloadedMavenZipFile, Path mavenInstallDir) {
202+
try {
203+
byte[] buffer = new byte[1024];
204+
ZipInputStream zis = null;
205+
206+
zis = new ZipInputStream(new FileInputStream(downloadedMavenZipFile));
207+
208+
ZipEntry zipEntry = zis.getNextEntry();
209+
while (zipEntry != null) {
210+
File newFile = newFile(mavenInstallDir.toFile(), zipEntry);
211+
if (zipEntry.isDirectory()) {
212+
if (!newFile.isDirectory() && !newFile.mkdirs()) {
213+
throw new IOException("Failed to create directory " + newFile);
214+
}
215+
} else {
216+
// fix for Windows-created archives
217+
File parent = newFile.getParentFile();
218+
if (!parent.isDirectory() && !parent.mkdirs()) {
219+
throw new IOException("Failed to create directory " + parent);
220+
}
221+
222+
// write file content
223+
FileOutputStream fos = new FileOutputStream(newFile);
224+
int len;
225+
while ((len = zis.read(buffer)) > 0) {
226+
fos.write(buffer, 0, len);
227+
}
228+
fos.close();
229+
}
230+
zipEntry = zis.getNextEntry();
231+
}
232+
zis.closeEntry();
233+
zis.close();
234+
} catch (FileNotFoundException e) {
235+
throw new RuntimeException(e);
236+
} catch (IOException e) {
237+
throw new RuntimeException(e);
238+
}
239+
}
240+
241+
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
242+
File destFile = new File(destinationDir, zipEntry.getName());
243+
244+
String destDirPath = destinationDir.getCanonicalPath();
245+
String destFilePath = destFile.getCanonicalPath();
246+
247+
if (!destFilePath.startsWith(destDirPath + File.separator)) {
248+
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
249+
}
250+
251+
return destFile;
252+
}
253+
258254

259255
private Path renderSettingsXml(String testcodeDir, Path settingsXmlTmplPath) throws IOException {
260256
String settingsXmlContent = Files.readString(settingsXmlTmplPath);

0 commit comments

Comments
 (0)