Skip to content

Commit

Permalink
Clean up following review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rnorth committed Mar 12, 2017
1 parent 19905c0 commit 4caddcc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public void addEnv(String key, String value) {
public void addFileSystemBind(String hostPath, String containerPath, BindMode mode) {

final MountableFile mountableFile = MountableFile.forHostPath(hostPath);
binds.add(new Bind(mountableFile.getMountablePath(), new Volume(containerPath), mode.accessMode));
binds.add(new Bind(mountableFile.getResolvedPath(), new Volume(containerPath), mode.accessMode));
}

/**
Expand Down Expand Up @@ -618,7 +618,7 @@ public SELF withNetworkMode(String networkMode) {
public SELF withClasspathResourceMapping(String resourcePath, String containerPath, BindMode mode) {
final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath);

this.addFileSystemBind(mountableFile.getMountablePath(), containerPath, mode);
this.addFileSystemBind(mountableFile.getResolvedPath(), containerPath, mode);

return self();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface ClasspathTrait<SELF extends ClasspathTrait<SELF> & BuildContext
default SELF withFileFromClasspath(String path, String resourcePath) {
final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath);

return ((SELF) this).withFileFromPath(path, Paths.get(mountableFile.getMountablePath()));
return ((SELF) this).withFileFromPath(path, Paths.get(mountableFile.getResolvedPath()));
}
}
36 changes: 17 additions & 19 deletions core/src/main/java/org/testcontainers/utility/MountableFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.testcontainers.utility;

import com.google.common.base.Charsets;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.SystemUtils;
import org.jetbrains.annotations.NotNull;
Expand All @@ -21,21 +23,21 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static lombok.AccessLevel.PRIVATE;
import static org.testcontainers.utility.PathUtils.recursiveDeleteDir;

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

private final String path;
private String resolvedPath = null;

private MountableFile(final String path) {
this.path = path;
}
@Getter(lazy = true)
private final String resolvedPath = resolvePath();

/**
* Obtains a {@link MountableFile} corresponding to a resource on the classpath (including resources in JAR files)
Expand Down Expand Up @@ -64,35 +66,31 @@ public static MountableFile forHostPath(@NotNull final String path) {
*
* @return a volume-mountable path.
*/
public String getMountablePath() {

// Don't recompute if already resolved
if (resolvedPath != null) {
return resolvedPath;
}

private String resolvePath() {
String result;
if (path.contains(".jar!")) {
resolvedPath = extractClassPathResourceToTempLocation(this.path);
result = extractClassPathResourceToTempLocation(this.path);
} else {
resolvedPath = unencodeResourceURIToFilePath(path);
result = unencodeResourceURIToFilePath(path);
}

if (SystemUtils.IS_OS_WINDOWS) {
resolvedPath = PathUtils.createMinGWPath(resolvedPath);
result = PathUtils.createMinGWPath(result);
}

return resolvedPath;
return result;
}

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

final Set<ClassLoader> classLoadersToSearch = new HashSet<>(classLoaders);
// try context and system classloaders as well
classLoaders.add(Thread.currentThread().getContextClassLoader());
classLoaders.add(ClassLoader.getSystemClassLoader());
classLoaders.add(MountableFile.class.getClassLoader());
classLoadersToSearch.add(Thread.currentThread().getContextClassLoader());
classLoadersToSearch.add(ClassLoader.getSystemClassLoader());
classLoadersToSearch.add(MountableFile.class.getClassLoader());

for (final ClassLoader classLoader : classLoaders) {
for (final ClassLoader classLoader : classLoadersToSearch) {
URL resource = classLoader.getResource(resourcePath);
if (resource != null) {
return resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void forHostPathWithSpaces() throws Exception {

performChecks(mountableFile);

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

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

private void performChecks(final MountableFile mountableFile) {
final String mountablePath = mountableFile.getMountablePath();
final String mountablePath = mountableFile.getResolvedPath();
assertTrue("The resolved path can be found", new File(mountablePath).exists());
assertFalse("The resolved path does not contain any URL escaping", mountablePath.contains("%20"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected void optionallyMapResourceParameterAsVolume(@NotNull String paramName,

if (resourceName != null) {
final MountableFile mountableFile = MountableFile.forClasspathResource(resourceName);
addFileSystemBind(mountableFile.getMountablePath(), pathNameInContainer, BindMode.READ_ONLY);
addFileSystemBind(mountableFile.getResolvedPath(), pathNameInContainer, BindMode.READ_ONLY);
}
}

Expand Down

0 comments on commit 4caddcc

Please sign in to comment.