Skip to content

Commit cf526be

Browse files
committed
Extract repository data from zip file
The long path was problematic under Windows without explicitly setting Git config. To prevent this the repository data is copied to its location from a zip file.
1 parent 4ca2776 commit cf526be

File tree

41 files changed

+72
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+72
-568
lines changed

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@
163163
<scope>provided</scope>
164164
</dependency>
165165

166+
<dependency>
167+
<groupId>net.lingala.zip4j</groupId>
168+
<artifactId>zip4j</artifactId>
169+
<version>2.6.1</version>
170+
<scope>test</scope>
171+
</dependency>
172+
166173
</dependencies>
167174

168175
</project>

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/src/test/java/org/springframework/sbm/PrivateArtifactRepositoryTest.java

Lines changed: 59 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.sbm;
1717

18-
import org.apache.commons.io.FileUtils;
18+
import net.lingala.zip4j.ZipFile;
1919
import org.apache.maven.shared.invoker.*;
2020
import org.junit.jupiter.api.*;
2121
import org.junit.jupiter.api.io.TempDir;
@@ -42,14 +42,11 @@
4242
import org.testcontainers.utility.MountableFile;
4343

4444
import java.io.*;
45-
import java.net.URL;
4645
import java.nio.file.Files;
4746
import java.nio.file.Path;
4847
import java.nio.file.Paths;
4948
import java.util.Arrays;
5049
import java.util.List;
51-
import java.util.zip.ZipEntry;
52-
import java.util.zip.ZipInputStream;
5350

5451
import static org.assertj.core.api.Assertions.assertThat;
5552
import static org.assertj.core.api.Fail.fail;
@@ -97,29 +94,51 @@ public class PrivateArtifactRepositoryTest {
9794
// All test resources live here
9895
public static final String TESTCODE_DIR = "testcode/maven-projects/private-repository";
9996

100-
// The private Artifact repository (reposilite) provides the dependency.
101-
@Container
102-
static GenericContainer reposilite = new GenericContainer(DockerImageName.parse("dzikoysk/reposilite:3.4.10"))
103-
.withExposedPorts(8080)
104-
// copy required config files and cached dependency to repository
105-
.withCopyFileToContainer(MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-data"), "/app/data")
106-
// Create temp user 'user' with password 'secret'
107-
.withEnv("REPOSILITE_OPTS", "--token user:secret --shared-config shared.configuration.json");
97+
public static final String REPOSITORIES_ZIP = Path
98+
.of("./testcode/maven-projects/private-repository/reposilite-data.zip")
99+
.toAbsolutePath()
100+
.toString();
108101

109102
public static final String DEPENDENCY_CLASS_FQNAME = "com.example.dependency.DependencyClass";
110103

111104
private static final String NEW_USER_HOME = Path.of(".")
112-
.resolve(TESTCODE_DIR + "/user.home")
113-
.toAbsolutePath()
114-
.normalize()
115-
.toString();
105+
.resolve(TESTCODE_DIR + "/user.home")
106+
.toAbsolutePath()
107+
.normalize()
108+
.toString();
109+
private static final File LOCAL_MAVEN_REPOSITORY = Path.of(NEW_USER_HOME + "/.m2/repository").toFile();
116110

117111
private static final Path DEPENDENCY_PATH_IN_LOCAL_MAVEN_REPO = Path
118-
.of(NEW_USER_HOME + "/.m2/repository/com/example/dependency/dependency-project")
119-
.toAbsolutePath()
120-
.normalize();
112+
.of(NEW_USER_HOME + "/.m2/repository/com/example/dependency/dependency-project")
113+
.toAbsolutePath()
114+
.normalize();
115+
116+
// File path was too long under Windows when Maven repository was under TESTCODE_DIR +
117+
// "/reposilite-data"
118+
// To fix this the repo with jars for reposilite is extracted from a zip to its target
119+
// dir.
120+
private static final Path TEMP_DIR;
121+
static {
122+
try {
123+
TEMP_DIR = Files.createTempDirectory("reposilite-data");
124+
}
125+
catch (IOException e) {
126+
throw new RuntimeException(e);
127+
}
128+
if (!Files.exists(TEMP_DIR.resolve("reposilite-data"))) {
129+
TestHelper.unzip(TEMP_DIR);
130+
}
131+
}
132+
private static final Path REPOSILITE_DATA_DIR = TEMP_DIR.resolve("reposilite-data");
121133

122-
private static final File LOCAL_MAVEN_REPOSITORY = Path.of(NEW_USER_HOME + "/.m2/repository").toFile();
134+
// The private Artifact repository (reposilite) provides the dependency.
135+
@Container
136+
static GenericContainer reposilite = new GenericContainer(DockerImageName.parse("dzikoysk/reposilite:3.4.10"))
137+
.withExposedPorts(8080)
138+
// copy required config files and cached dependency to repository
139+
.withCopyFileToContainer(MountableFile.forHostPath(REPOSILITE_DATA_DIR), "/app/data")
140+
// Create temp user 'user' with password 'secret'
141+
.withEnv("REPOSILITE_OPTS", "--token user:secret --shared-config shared.configuration.json");
123142

124143
private static MavenRepository originalMavenRepository;
125144

@@ -146,14 +165,6 @@ static void beforeAll(@TempDir Path tempDir) {
146165
Whitebox.setInternalState(MavenRepository.class, "MAVEN_LOCAL_DEFAULT", mavenRepository);
147166
}
148167

149-
@AfterAll
150-
static void afterAll() {
151-
// set back to initial values
152-
System.setProperty("user.home", originalUserHome);
153-
Whitebox.setInternalState(MavenRepository.class, "MAVEN_LOCAL_DEFAULT", originalMavenRepository);
154-
FileSystemUtils.deleteRecursively(LOCAL_MAVEN_REPOSITORY);
155-
}
156-
157168
@BeforeEach
158169
void beforeEach() throws IOException {
159170
Integer port = reposilite.getMappedPort(8080);
@@ -162,6 +173,14 @@ void beforeEach() throws IOException {
162173
TestHelper.clearDependencyFromLocalMavenRepo();
163174
}
164175

176+
@AfterAll
177+
static void afterAll() {
178+
// set back to initial values
179+
System.setProperty("user.home", originalUserHome);
180+
Whitebox.setInternalState(MavenRepository.class, "MAVEN_LOCAL_DEFAULT", originalMavenRepository);
181+
FileSystemUtils.deleteRecursively(LOCAL_MAVEN_REPOSITORY);
182+
}
183+
165184
@Test
166185
@DisplayName("Maven settings should be read from secured private repo")
167186
void mavenSettingsShouldBeReadFromSecuredPrivateRepo() {
@@ -275,15 +294,6 @@ static void clearDependencyFromLocalMavenRepo() {
275294
}
276295
}
277296

278-
}
279-
280-
/*
281-
* Currently not used as the dependency is provided to the container (cached). But
282-
* kept in case deployment of the dependency or building the dependent project is
283-
* needed.
284-
*/
285-
class DeploymentHelper {
286-
287297
void deployDependency(Path pomXmlPath) throws MavenInvocationException {
288298
InvocationRequest request = new DefaultInvocationRequest();
289299
request.setPomFile(pomXmlPath.toFile());
@@ -328,86 +338,24 @@ private void buildProject(Path dependentPomPath) throws MavenInvocationException
328338
}
329339
}
330340

331-
static void installMavenForTestIfNotExists(Path tempDir) {
332-
if (!Path.of("./testcode/maven-projects/private-repository/user.home/apache-maven-3.9.5/bin/mvn")
333-
.toFile()
334-
.exists()) {
335-
String mavenDownloadUrl = "https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.zip";
336-
try {
337-
Path mavenInstallDir = Path.of(TESTCODE_DIR + "/user.home");
338-
File downloadedMavenZipFile = tempDir.resolve("apache-maven-3.9.5-bin.zip").toFile();
339-
FileUtils.copyURLToFile(new URL(mavenDownloadUrl), downloadedMavenZipFile, 10000, 30000);
340-
Unzipper.unzip(downloadedMavenZipFile, mavenInstallDir);
341-
File file = mavenInstallDir.resolve("apache-maven-3.9.5/bin/mvn").toFile();
342-
file.setExecutable(true, false);
343-
assertThat(file.canExecute()).isTrue();
344-
}
345-
catch (IOException e) {
346-
throw new RuntimeException(e);
347-
}
341+
static void unzip(Path repoDir) {
342+
try {
343+
;
344+
ZipFile zipFile = new ZipFile(REPOSITORIES_ZIP);
345+
zipFile.extractAll(repoDir.toString());
348346
}
349-
}
350-
351-
class Unzipper {
352-
353-
private static void unzip(File downloadedMavenZipFile, Path mavenInstallDir) {
354-
try {
355-
byte[] buffer = new byte[1024];
356-
ZipInputStream zis = null;
357-
358-
zis = new ZipInputStream(new FileInputStream(downloadedMavenZipFile));
359-
360-
ZipEntry zipEntry = zis.getNextEntry();
361-
while (zipEntry != null) {
362-
File newFile = newFile(mavenInstallDir.toFile(), zipEntry);
363-
if (zipEntry.isDirectory()) {
364-
if (!newFile.isDirectory() && !newFile.mkdirs()) {
365-
throw new IOException("Failed to create directory " + newFile);
366-
}
367-
}
368-
else {
369-
// fix for Windows-created archives
370-
File parent = newFile.getParentFile();
371-
if (!parent.isDirectory() && !parent.mkdirs()) {
372-
throw new IOException("Failed to create directory " + parent);
373-
}
374-
375-
// write file content
376-
FileOutputStream fos = new FileOutputStream(newFile);
377-
int len;
378-
while ((len = zis.read(buffer)) > 0) {
379-
fos.write(buffer, 0, len);
380-
}
381-
fos.close();
382-
}
383-
zipEntry = zis.getNextEntry();
384-
}
385-
zis.closeEntry();
386-
zis.close();
387-
}
388-
catch (FileNotFoundException e) {
389-
throw new RuntimeException(e);
390-
}
391-
catch (IOException e) {
392-
throw new RuntimeException(e);
393-
}
347+
catch (IOException e) {
348+
throw new RuntimeException(e);
394349
}
395350

396-
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
397-
File destFile = new File(destinationDir, zipEntry.getName());
398-
399-
String destDirPath = destinationDir.getCanonicalPath();
400-
String destFilePath = destFile.getCanonicalPath();
401-
402-
if (!destFilePath.startsWith(destDirPath + java.io.File.separator)) {
403-
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
404-
}
351+
}
405352

406-
return destFile;
353+
static void mkdir(Path target) {
354+
boolean mkdir = target.toFile().mkdir();
355+
if (!mkdir) {
356+
throw new RuntimeException("Could not create target dir");
407357
}
408-
409358
}
410-
411359
}
412360

413361
}

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/.gitignore

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
/user.home/.m2/settings.xml
99
!/user.home/.m2/settings.xml.template
1010
!/user.home/.m2/settings-security.xml
11-
/reposilite-data/static
12-
/reposilite-data/.local
13-
/reposilite-data/plugins
14-
/reposilite-data/reposilite.db
15-
/reposilite-data/configuration.cdn
11+
#/reposilite-data/static
12+
#/reposilite-data/.local
13+
#/reposilite-data/plugins
14+
#/reposilite-data/repositories
15+
#/reposilite-data/reposilite.db
16+
#/reposilite-data--/configuration.cdn

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar.md5

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar.sha1

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom

Lines changed: 0 additions & 25 deletions
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom.md5

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom.sha1

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/maven-metadata.xml

Lines changed: 0 additions & 25 deletions
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/maven-metadata.xml.md5

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/maven-metadata.xml.sha1

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml

Lines changed: 0 additions & 11 deletions
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml.md5

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-rewrite-commons-functional-tests/private-artifact-repository-tests/testcode/maven-projects/private-repository/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml.sha1

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)