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

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

Source:MountableFile.java Github

copy

Full Screen

...37 private static final int BASE_DIR_MODE = 0040000;38 private final String path;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 this...

Full Screen

Full Screen

resolvePath

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import com.github.dockerjava.api.command.InspectContainerResponse;3import org.apache.commons.lang3.RandomStringUtils;4import org.junit.Before;5import org.junit.Test;6import org.testcontainers.containers.wait.strategy.Wait;7import org.testcontainers.utility.MountableFile;8import java.io.File;9import java.io.IOException;10import java.nio.file.Files;11import java.nio.file.Path;12import java.nio.file.Paths;13import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;14public class MountableFileTest {15 private static final String TEST_STRING = "Hello world!";16 private static final String FILE_NAME = "test.txt";17 private static final String FILE_CONTENT = "This is a test file";18 private static final String CONTAINER_FILE_PATH = "/test.txt";19 private static final String CONTAINER_FILE_CONTENT = "This is a test file";20 private static final String CONTAINER_DIR_PATH = "/testDir";21 private static final String CONTAINER_DIR_CONTENT = "This is a test directory";22 private static final String CONTAINER_DIR_FILE_PATH = "/testDir/test.txt";23 private static final String CONTAINER_DIR_FILE_CONTENT = "This is a test file in a test directory";24 private static final String CONTAINER_DIR_FILE_NAME = "test.txt";25 private static final String CONTAINER_DIR_FILE_NAME_2 = "test2.txt";26 private static final String CONTAINER_DIR_FILE_CONTENT_2 = "This is another test file in a test directory";27 private static final String CONTAINER_DIR_FILE_PATH_2 = "/testDir/test2.txt";28 private static final String CONTAINER_DIR_FILE_CONTENT_3 = "This is a third test file in a test directory";29 private static final String CONTAINER_DIR_FILE_PATH_3 = "/testDir/test3.txt";30 private static final String CONTAINER_DIR_FILE_NAME_3 = "test3.txt";31 private static final String CONTAINER_DIR_FILE_CONTENT_4 = "This is a fourth test file in a test directory";32 private static final String CONTAINER_DIR_FILE_PATH_4 = "/testDir/test4.txt";33 private static final String CONTAINER_DIR_FILE_NAME_4 = "test4.txt";34 private static final String CONTAINER_DIR_FILE_CONTENT_5 = "This is a fifth test file in a test directory";35 private static final String CONTAINER_DIR_FILE_PATH_5 = "/testDir/test5.txt";

Full Screen

Full Screen

resolvePath

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer()3 .withInitScript("test.sql");4postgreSQLContainer.start();5String jdbcUrl = postgreSQLContainer.getJdbcUrl();6String username = postgreSQLContainer.getUsername();7String password = postgreSQLContainer.getPassword();8postgreSQLContainer.stop();

Full Screen

Full Screen

resolvePath

Using AI Code Generation

copy

Full Screen

1def mountableFile = MountableFile.forClasspathResource("test.txt");2def mountableFile = MountableFile.forClasspathResource("test.txt");3def mountableFile = MountableFile.forClasspathResource("test.txt");4def mountableFile = MountableFile.forClasspathResource("test.txt");5def mountableFile = MountableFile.forClasspathResource("test.txt");6def mountableFile = MountableFile.forClasspathResource("test.txt");7def mountableFile = MountableFile.forClasspathResource("test.txt");8def mountableFile = MountableFile.forClasspathResource("test.txt");9def mountableFile = MountableFile.forClasspathResource("test.txt");10def mountableFile = MountableFile.forClasspathResource("test.txt");11def mountableFile = MountableFile.forClasspathResource("test.txt

Full Screen

Full Screen

resolvePath

Using AI Code Generation

copy

Full Screen

1MountableFile mountableFile = MountableFile.forClasspathResource("test.txt");2String path = mountableFile.getResolvedPath();3System.out.println("Path of file to be mounted is: " + path);4container.withFileSystemBind(path, "/test.txt", BindMode.READ_ONLY);5String[] cmd = {"cat", "/test.txt"};6container.execInContainer(cmd);7String output = container.getLogs(OutputFrame.OutputType.STDOUT, OutputFrame.OutputType.STDERR);8System.out.println("Output of command is: " + output);

Full Screen

Full Screen

resolvePath

Using AI Code Generation

copy

Full Screen

1MountableFile mountableFile = MountableFile.forHostPath(Paths.get("test.txt").toAbsolutePath().toString());2String mountableFileAbsolutePath = mountableFile.getResolvedPath();3System.out.println(mountableFileAbsolutePath);4java.lang.IllegalStateException: MountableFile must be resolved before using getResolvedPath()5MountableFile mountableFile = MountableFile.forHostPath(Paths.get("test.txt").toAbsolutePath().toString());6String mountableFileAbsolutePath = mountableFile.resolvePath();7System.out.println(mountableFileAbsolutePath);8java.lang.IllegalStateException: MountableFile must be resolved before using getResolvedPath()9MountableFile mountableFile = MountableFile.forHostPath(Paths.get("test.txt").toAbsolutePath().toString());10String mountableFileAbsolutePath = mountableFile.resolve();11System.out.println(mountableFileAbsolutePath);12java.lang.IllegalStateException: MountableFile must be resolved before using getResolvedPath()

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