How to use resolveFilesystemPath method of org.testcontainers.utility.MountableFile class

Best Testcontainers-java code snippet using org.testcontainers.utility.MountableFile.resolveFilesystemPath

Source:MountableFile.java Github

copy

Full Screen

...39 private final Integer forcedFileMode;40 @Getter(lazy = true)41 private final String resolvedPath = resolvePath();42 @Getter(lazy = true)43 private final String filesystemPath = resolveFilesystemPath();44 private String resourcePath;45 /**46 * Obtains a {@link MountableFile} corresponding to a resource on the classpath (including resources in JAR files)47 *48 * @param resourceName the classpath path to the resource49 * @return a {@link MountableFile} that may be used to obtain a mountable path50 */51 public static MountableFile forClasspathResource(@NotNull final String resourceName) {52 return forClasspathResource(resourceName, null);53 }54 /**55 * Obtains a {@link MountableFile} corresponding to a file on the docker host filesystem.56 *57 * @param path the path to the resource58 * @return a {@link MountableFile} that may be used to obtain a mountable path59 */60 public static MountableFile forHostPath(@NotNull final String path) {61 return forHostPath(path, null);62 }63 /**64 * Obtains a {@link MountableFile} corresponding to a file on the docker host filesystem.65 *66 * @param path the path to the resource67 * @return a {@link MountableFile} that may be used to obtain a mountable path68 */69 public static MountableFile forHostPath(final Path path) {70 return forHostPath(path, null);71 }72 /**73 * Obtains a {@link MountableFile} corresponding to a resource on the classpath (including resources in JAR files)74 *75 * @param resourceName the classpath path to the resource76 * @param mode octal value of posix file mode (000..777)77 * @return a {@link MountableFile} that may be used to obtain a mountable path78 */79 public static MountableFile forClasspathResource(@NotNull final String resourceName, Integer mode) {80 return new MountableFile(getClasspathResource(resourceName, new HashSet<>()).toString(), mode);81 }82 /**83 * Obtains a {@link MountableFile} corresponding to a file on the docker host filesystem.84 *85 * @param path the path to the resource86 * @param mode octal value of posix file mode (000..777)87 * @return a {@link MountableFile} that may be used to obtain a mountable path88 */89 public static MountableFile forHostPath(@NotNull final String path, Integer mode) {90 return forHostPath(Paths.get(path), mode);91 }92 /**93 * Obtains a {@link MountableFile} corresponding to a file on the docker host filesystem.94 *95 * @param path the path to the resource96 * @param mode octal value of posix file mode (000..777)97 * @return a {@link MountableFile} that may be used to obtain a mountable path98 */99 public static MountableFile forHostPath(final Path path, Integer mode) {100 return new MountableFile(path.toAbsolutePath().toString(), mode);101 }102 @NotNull103 private static URL getClasspathResource(@NotNull final String resourcePath, @NotNull final Set<ClassLoader> classLoaders) {104 final Set<ClassLoader> classLoadersToSearch = new HashSet<>(classLoaders);105 // try context and system classloaders as well106 classLoadersToSearch.add(Thread.currentThread().getContextClassLoader());107 classLoadersToSearch.add(ClassLoader.getSystemClassLoader());108 classLoadersToSearch.add(MountableFile.class.getClassLoader());109 for (final ClassLoader classLoader : classLoadersToSearch) {110 if (classLoader == null) {111 continue;112 }113 URL resource = classLoader.getResource(resourcePath);114 if (resource != null) {115 return resource;116 }117 // Be lenient if an absolute path was given118 if (resourcePath.startsWith("/")) {119 resource = classLoader.getResource(resourcePath.replaceFirst("/", ""));120 if (resource != null) {121 return resource;122 }123 }124 }125 throw new IllegalArgumentException("Resource with path " + resourcePath + " could not be found on any of these classloaders: " + classLoadersToSearch);126 }127 private static String unencodeResourceURIToFilePath(@NotNull final String resource) {128 try {129 // Convert any url-encoded characters (e.g. spaces) back into unencoded form130 return URLDecoder.decode(resource.replaceAll("\\+", "%2B"), Charsets.UTF_8.name())131 .replaceFirst("jar:", "")132 .replaceFirst("file:", "")133 .replaceAll("!.*", "");134 } catch (UnsupportedEncodingException e) {135 throw new IllegalStateException(e);136 }137 }138 /**139 * Obtain a path that the Docker daemon should be able to use to volume mount a file/resource140 * into a container. If this is a classpath resource residing in a JAR, it will be extracted to141 * a temporary location so that the Docker daemon is able to access it.142 *143 * @return a volume-mountable path.144 */145 private String resolvePath() {146 String result = getResourcePath();147 // Special case for Windows148 if (SystemUtils.IS_OS_WINDOWS && result.startsWith("/")) {149 // Remove leading /150 result = result.substring(1);151 }152 return result;153 }154 /**155 * Obtain a path in local filesystem that the Docker daemon should be able to use to volume mount a file/resource156 * into a container. If this is a classpath resource residing in a JAR, it will be extracted to157 * a temporary location so that the Docker daemon is able to access it.158 *159 * TODO: rename method accordingly and check if really needed like this160 *161 * @return162 */163 private String resolveFilesystemPath() {164 String result = getResourcePath();165 if (SystemUtils.IS_OS_WINDOWS && result.startsWith("/")) {166 result = PathUtils.createMinGWPath(result).substring(1);167 }168 return result;169 }170 private String getResourcePath() {171 if (path.contains(".jar!")) {172 resourcePath = extractClassPathResourceToTempLocation(this.path);173 } else {174 resourcePath = unencodeResourceURIToFilePath(path);175 }176 return resourcePath;177 }...

Full Screen

Full Screen

resolveFilesystemPath

Using AI Code Generation

copy

Full Screen

1MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");2String path = mountableFile.resolveFilesystemPath();3System.out.println("path: " + path);4MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");5String path = mountableFile.resolvePath();6System.out.println("path: " + path);7MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");8String path = mountableFile.resolvePath();9System.out.println("path: " + path);10MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");11String path = mountableFile.resolvePath();12System.out.println("path: " + path);13MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");14String path = mountableFile.resolvePath();15System.out.println("path: " + path);16MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");17String path = mountableFile.resolvePath();18System.out.println("path: " + path);19MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");20String path = mountableFile.resolvePath();21System.out.println("path: " + path);

Full Screen

Full Screen

resolveFilesystemPath

Using AI Code Generation

copy

Full Screen

1package com.example.testcontainers;2import java.io.File;3import java.io.IOException;4import org.testcontainers.containers.GenericContainer;5import org.testcontainers.containers.output.Slf4jLogConsumer;6import org.testcontainers.containers.wait.strategy.Wait;7import org.testcontainers.utility.MountableFile;8import lombok.extern.slf4j.Slf4j;9public class TestContainerExample {10 public static void main(String[] args) throws IOException, InterruptedException {11 GenericContainer container = new GenericContainer("ubuntu:18.04")12 .withExposedPorts(22)13 .waitingFor(Wait.forListeningPort())14 .withCommand("tail", "-f", "/dev/null")15 .withFileSystemBind("/Users/username/Downloads", "/tmp")16 .withLogConsumer(new Slf4jLogConsumer(log));17 container.start();18 String containerId = container.getContainerId();19 log.info("Container ID: {}", containerId);20 String absoluteFilePath = MountableFile.forHostPath("/Users/username/Downloads/README.md").resolveFilesystemPath();21 log.info("absoluteFilePath: {}", absoluteFilePath);22 String absoluteFilePath2 = new File("/Users/username/Downloads/README.md").getAbsolutePath();23 log.info("absoluteFilePath2: {}", absoluteFilePath2);24 container.stop();25 }26}

Full Screen

Full Screen

resolveFilesystemPath

Using AI Code Generation

copy

Full Screen

1def mountableFile = MountableFile.forClasspathResource("test.sql")2def file = mountableFile.resolveFilesystemPath()3def sqlFile = new File(file)4def mountableFile = MountableFile.forClasspathResource("test.sql")5def file = mountableFile.resolveFilesystemPath()6def sqlFile = new File(file)7def mountableFile = MountableFile.forClasspathResource("test.sql")8def file = mountableFile.resolveFilesystemPath()9def sqlFile = new File(file)10def mountableFile = MountableFile.forClasspathResource("test.sql")11def file = mountableFile.resolveFilesystemPath()12def sqlFile = new File(file)13def mountableFile = MountableFile.forClasspathResource("test.sql")14def file = mountableFile.resolveFilesystemPath()15def sqlFile = new File(file)16def mountableFile = MountableFile.forClasspathResource("test.sql")17def file = mountableFile.resolveFilesystemPath()18def sqlFile = new File(file)19def mountableFile = MountableFile.forClasspathResource("test.sql")20def file = mountableFile.resolveFilesystemPath()21def sqlFile = new File(file)22def mountableFile = MountableFile.forClasspathResource("test.sql")

Full Screen

Full Screen

resolveFilesystemPath

Using AI Code Generation

copy

Full Screen

1public class TestContainerDemo {2 public void testContainerDemo() throws Exception {3 String path = "/home/raj/Downloads/test.txt";4 MountableFile mountableFile = MountableFile.forClasspathResource(path);5 mountableFile.resolveFilesystemPath();6 }7}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful