Skip to content

Commit aaec507

Browse files
committed
Clean up following review comments
1 parent abfe039 commit aaec507

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public void addEnv(String key, String value) {
481481
public void addFileSystemBind(String hostPath, String containerPath, BindMode mode) {
482482

483483
final MountableFile mountableFile = MountableFile.forHostPath(hostPath);
484-
binds.add(new Bind(mountableFile.getMountablePath(), new Volume(containerPath), mode.accessMode));
484+
binds.add(new Bind(mountableFile.getResolvedPath(), new Volume(containerPath), mode.accessMode));
485485
}
486486

487487
/**
@@ -615,7 +615,7 @@ public SELF withNetworkMode(String networkMode) {
615615
public SELF withClasspathResourceMapping(String resourcePath, String containerPath, BindMode mode) {
616616
final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath);
617617

618-
this.addFileSystemBind(mountableFile.getMountablePath(), containerPath, mode);
618+
this.addFileSystemBind(mountableFile.getResolvedPath(), containerPath, mode);
619619

620620
return self();
621621
}

core/src/main/java/org/testcontainers/images/builder/traits/ClasspathTrait.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public interface ClasspathTrait<SELF extends ClasspathTrait<SELF> & BuildContext
1313
default SELF withFileFromClasspath(String path, String resourcePath) {
1414
final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath);
1515

16-
return ((SELF) this).withFileFromPath(path, Paths.get(mountableFile.getMountablePath()));
16+
return ((SELF) this).withFileFromPath(path, Paths.get(mountableFile.getResolvedPath()));
1717
}
1818
}

core/src/main/java/org/testcontainers/utility/MountableFile.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.testcontainers.utility;
22

33
import com.google.common.base.Charsets;
4+
import lombok.Getter;
5+
import lombok.RequiredArgsConstructor;
46
import lombok.extern.slf4j.Slf4j;
57
import org.apache.commons.lang.SystemUtils;
68
import org.jetbrains.annotations.NotNull;
@@ -21,21 +23,21 @@
2123
import java.util.jar.JarEntry;
2224
import java.util.jar.JarFile;
2325

26+
import static lombok.AccessLevel.PRIVATE;
2427
import static org.testcontainers.utility.PathUtils.recursiveDeleteDir;
2528

2629
/**
2730
* An abstraction over files and classpath resources aimed at encapsulating all the complexity of generating
2831
* a path that the Docker daemon is about to create a volume mount for.
2932
*/
33+
@RequiredArgsConstructor(access = PRIVATE)
3034
@Slf4j
3135
public class MountableFile {
3236

3337
private final String path;
34-
private String resolvedPath = null;
3538

36-
private MountableFile(final String path) {
37-
this.path = path;
38-
}
39+
@Getter(lazy = true)
40+
private final String resolvedPath = resolvePath();
3941

4042
/**
4143
* Obtains a {@link MountableFile} corresponding to a resource on the classpath (including resources in JAR files)
@@ -64,35 +66,31 @@ public static MountableFile forHostPath(@NotNull final String path) {
6466
*
6567
* @return a volume-mountable path.
6668
*/
67-
public String getMountablePath() {
68-
69-
// Don't recompute if already resolved
70-
if (resolvedPath != null) {
71-
return resolvedPath;
72-
}
73-
69+
private String resolvePath() {
70+
String result;
7471
if (path.contains(".jar!")) {
75-
resolvedPath = extractClassPathResourceToTempLocation(this.path);
72+
result = extractClassPathResourceToTempLocation(this.path);
7673
} else {
77-
resolvedPath = unencodeResourceURIToFilePath(path);
74+
result = unencodeResourceURIToFilePath(path);
7875
}
7976

8077
if (SystemUtils.IS_OS_WINDOWS) {
81-
resolvedPath = PathUtils.createMinGWPath(resolvedPath);
78+
result = PathUtils.createMinGWPath(result);
8279
}
8380

84-
return resolvedPath;
81+
return result;
8582
}
8683

8784
@NotNull
8885
private static URL getClasspathResource(@NotNull final String resourcePath, @NotNull final Set<ClassLoader> classLoaders) {
8986

87+
final Set<ClassLoader> classLoadersToSearch = new HashSet<>(classLoaders);
9088
// try context and system classloaders as well
91-
classLoaders.add(Thread.currentThread().getContextClassLoader());
92-
classLoaders.add(ClassLoader.getSystemClassLoader());
93-
classLoaders.add(MountableFile.class.getClassLoader());
89+
classLoadersToSearch.add(Thread.currentThread().getContextClassLoader());
90+
classLoadersToSearch.add(ClassLoader.getSystemClassLoader());
91+
classLoadersToSearch.add(MountableFile.class.getClassLoader());
9492

95-
for (final ClassLoader classLoader : classLoaders) {
93+
for (final ClassLoader classLoader : classLoadersToSearch) {
9694
URL resource = classLoader.getResource(resourcePath);
9795
if (resource != null) {
9896
return resource;

core/src/test/java/org/testcontainers/utility/MountableFileTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void forHostPathWithSpaces() throws Exception {
4949

5050
performChecks(mountableFile);
5151

52-
assertTrue("The resolved path contains the original space", mountableFile.getMountablePath().contains(" "));assertFalse("The resolved path does not contain an escaped space", mountableFile.getMountablePath().contains("\\ "));
52+
assertTrue("The resolved path contains the original space", mountableFile.getResolvedPath().contains(" "));assertFalse("The resolved path does not contain an escaped space", mountableFile.getResolvedPath().contains("\\ "));
5353
}
5454

5555
/*
@@ -69,7 +69,7 @@ private Path createTempFile(final String name) throws IOException {
6969
}
7070

7171
private void performChecks(final MountableFile mountableFile) {
72-
final String mountablePath = mountableFile.getMountablePath();
72+
final String mountablePath = mountableFile.getResolvedPath();
7373
assertTrue("The resolved path can be found", new File(mountablePath).exists());
7474
assertFalse("The resolved path does not contain any URL escaping", mountablePath.contains("%20"));
7575
}

modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected void optionallyMapResourceParameterAsVolume(@NotNull String paramName,
131131

132132
if (resourceName != null) {
133133
final MountableFile mountableFile = MountableFile.forClasspathResource(resourceName);
134-
addFileSystemBind(mountableFile.getMountablePath(), pathNameInContainer, BindMode.READ_ONLY);
134+
addFileSystemBind(mountableFile.getResolvedPath(), pathNameInContainer, BindMode.READ_ONLY);
135135
}
136136
}
137137

0 commit comments

Comments
 (0)