Last active
February 10, 2024 12:55
-
-
Save LvargaDS/da3562c57abffb6edaa4c32ccefa0350 to your computer and use it in GitHub Desktop.
Small util class for testcontainer to copy whole directory recursively instead of signle file only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.testcontainers.containers.GenericContainer; | |
| import org.testcontainers.utility.MountableFile; | |
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| import java.util.stream.Stream; | |
| public class CopyDirectoryToTestContainer { | |
| /** | |
| * Method will copy all files from localPathToDirectory (source directory) to the container, into the directory | |
| * passed as containerPathToDirectory parameter. | |
| * | |
| * @param container container instance on which {@link GenericContainer#withCopyFileToContainer(MountableFile, String)} method will be called | |
| * @param localPathToDirectory local directory (full path), to copy all files recursively from | |
| * @param containerPathToDirectory directory path in container, where to put all content from source dir | |
| * @param <X> type of your container | |
| * @return your container after copying filet into it | |
| */ | |
| public static <X extends GenericContainer<X>> GenericContainer<X> copyDir( | |
| GenericContainer<X> container, | |
| String localPathToDirectory, | |
| String containerPathToDirectory) { | |
| if (!containerPathToDirectory.endsWith("/")) { | |
| throw new RuntimeException("containerPathToDirectory parameter has to end with / character (denoting, target is a directory)."); | |
| } | |
| Path start = Paths.get(localPathToDirectory); | |
| try (Stream<Path> stream = Files.walk(start)) { | |
| stream.filter(Files::isRegularFile) | |
| .forEach(x -> { | |
| final Path relativePathOfCopyiedFile = start.relativize(x); | |
| final String containerPathWithFilename = containerPathToDirectory + relativePathOfCopyiedFile; | |
| // System.out.println(x.toFile().getAbsolutePath() + "\t-> " + containerPathWithFilename); | |
| container.withCopyFileToContainer(MountableFile.forHostPath(x), containerPathWithFilename); | |
| }); | |
| } catch (IOException ex) { | |
| throw new RuntimeException( | |
| "Error while trying to walk through given directory path: \"" + localPathToDirectory + "\"! ex=" + ex.getMessage(), | |
| ex); | |
| } | |
| return container; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface SampleUsage { | |
| GenericContainer<?> liberty = | |
| copyDir(new GenericContainer<>(libertyImage) | |
| .withLabel("purpose", "testContainer"), | |
| "target/libraries", | |
| "/libraries/") // end of copyDir call... | |
| .withExposedPorts(9080, 9443) | |
| .withCopyFileToContainer( | |
| MountableFile.forClasspathResource("server.xml", 511), // 511 == 0777 (octal, rwxrwxrwx) | |
| "/config/config.xml") | |
| .withCopyFileToContainer(MountableFile.forHostPath( | |
| "target/mypp.ear"), | |
| "/config/dropins/myapp.ear") | |
| .waitingFor(Wait.forLogMessage(".*CWWKF0011I: The defaultServer server is ready to run a smarter planet.*", 1) | |
| .withStartupTimeout(Duration.of(1000 * LIBERTY_STARTUP_TIMEOUT_SECONDS, ChronoUnit.SECONDS))) | |
| .withLogConsumer(new Slf4jLogConsumer(LOG_LIBERTY)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment