Best Testcontainers-java code snippet using org.testcontainers.utility.MountableFile.unencodeResourceURIToFilePath
Source:MountableFile.java
...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 }178 /**179 * Extract a file or directory tree from a JAR file to a temporary location.180 * This allows Docker to mount classpath resources as files.181 *182 * @param hostPath the path on the host, expected to be of the format 'file:/path/to/some.jar!/classpath/path/to/resource'183 * @return the path of the temporary file/directory184 */185 private String extractClassPathResourceToTempLocation(final String hostPath) {186 File tmpLocation = createTempDirectory();187 //noinspection ResultOfMethodCallIgnored188 tmpLocation.delete();189 String urldecodedJarPath = unencodeResourceURIToFilePath(hostPath);190 String internalPath = hostPath.replaceAll("[^!]*!/", "");191 try (JarFile jarFile = new JarFile(urldecodedJarPath)) {192 Enumeration<JarEntry> entries = jarFile.entries();193 while (entries.hasMoreElements()) {194 JarEntry entry = entries.nextElement();195 final String name = entry.getName();196 if (name.startsWith(internalPath)) {197 log.debug("Copying classpath resource(s) from {} to {} to permit Docker to bind",198 hostPath,199 tmpLocation);200 copyFromJarToLocation(jarFile, entry, internalPath, tmpLocation);201 }202 }203 } catch (IOException e) {...
unencodeResourceURIToFilePath
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.utility.MountableFile;3import java.io.File;4public class TestContainerDemo {5 public static void main(String[] args) {6 GenericContainer container = new GenericContainer("nginx:latest");7 container.withCopyFileToContainer(8 MountableFile.forHostPath(unencodeResourceURIToFilePath("src/main/resources/nginx.conf")),9 "/etc/nginx/nginx.conf");10 container.start();11 System.out.println("Started container: " + container.getContainerId());12 }13}14public static String unencodeResourceURIToFilePath(String resourcePath) {15 return new File(TestContainerDemo.class.getClassLoader().getResource(resourcePath).getFile()).getAbsolutePath();16 }17container.withCopyFileToContainer(18 "/etc/nginx/nginx.conf");19container.withCopyFileToContainer(20 "/Dockerfile");
unencodeResourceURIToFilePath
Using AI Code Generation
1 public void testUnencodedResourceURIToFilePath() throws URISyntaxException {2 String path = MountableFile.forClasspathResource("test.txt").getResolvedPath();3 log.info("path: " + path);4 log.info("unencodedPath: " + unencodedPath);5 }6}
unencodeResourceURIToFilePath
Using AI Code Generation
1public static String unencodeResourceURIToFilePath(String resourcePath) {2 String[] parts = resourcePath.split("/", 2);3 String[] jarPath = parts[0].split(":", 2);4 String jar = jarPath[0];5 String path = jarPath[1];6 return jar + "!" + path;7}8String path = unencodeResourceURIToFilePath("file:/path/to/file.txt");
unencodeResourceURIToFilePath
Using AI Code Generation
1 System.out.println(filePath);2 MountableFile mountableFile = MountableFile.forHostPath(filePath);3 System.out.println(mountableFile.getResolvedPath());4 System.out.println(mountableFile.getCanonicalPath());5 System.out.println(mountableFile.getMountablePath());6 System.out.println(mountableFile.getMountablePath().getAbsolutePath());7 System.out.println(mountableFile.getMountablePath().getCanonicalPath());8 System.out.println(mountableFile.getMountablePath().getName());9 System.out.println(mountableFile.getMountablePath().getParent());10 System.out.println(mountableFile.getMountablePath().getPath());11 System.out.println(mountableFile.getMountablePath().getTotalSpace());12 System.out.println(mountableFile.getMountablePath().getUsableSpace());13 System.out.println(mountableFile.getMountablePath().getFreeSpace());14 System.out.println(mountableFile.getMountablePath().getAbsoluteFile());15 System.out.println(mountableFile.getMountablePath().getCanonicalFile());16 System.out.println(mountableFile.getMountablePath().toURI());17 System.out.println(mountableFile.getMountablePath().toURI().toURL());18 System.out.println(mountableFile.getMountablePath().toURI().toURL().getAuthority());19 System.out.println(mountableFile.getMountablePath().toURI().toURL().getContent());20 System.out.println(mountableFile.getMountablePath().toURI().toURL().getDefaultPort());21 System.out.println(mountableFile.getMountablePath().toURI().toURL().getFile());22 System.out.println(mountableFile.getMountablePath().toURI().toURL().getHost());23 System.out.println(mountableFile.getMountablePath().toURI().toURL().getPath());24 System.out.println(mountableFile.getMountablePath().toURI().toURL().getPort());25 System.out.println(mountableFile.getMountablePath().toURI().toURL().getProtocol());26 System.out.println(mountableFile.getMountablePath().toURI().toURL().getQuery());27 System.out.println(mountableFile.getMountablePath().toURI().toURL
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!