Best Testcontainers-java code snippet using org.testcontainers.utility.PathUtils
Source:VaultTestUtil.java
...14import java.util.logging.Logger;15import org.testcontainers.containers.Network;16import org.testcontainers.containers.wait.strategy.Wait;17import org.testcontainers.shaded.org.apache.commons.lang.SystemUtils;18import org.testcontainers.utility.PathUtils;19import org.testcontainers.utility.TestEnvironment;20import org.testcontainers.vault.VaultContainer;21import static com.github.dockerjava.api.model.Capability.IPC_LOCK;22import static org.apache.commons.io.FileUtils.writeStringToFile;23import static org.testcontainers.utility.MountableFile.forHostPath;24@SuppressWarnings("WeakerAccess")25public class VaultTestUtil implements TestConstants {26 private final static Logger LOGGER = Logger.getLogger(VaultTestUtil.class.getName());27 private static boolean configured = false;28 private static Network network = Network.newNetwork();29 public static void runCommand(VaultContainer container, final String... command)30 throws IOException, InterruptedException {31 LOGGER.log(Level.FINE, String.join(" ", command));32 container.execInContainer(command);33 }34 public static boolean hasDockerDaemon() {35 try {36 return TestEnvironment.dockerApiAtLeast("1.10");37 } catch (IllegalStateException e) {38 return false;39 }40 }41 public static VaultContainer createVaultContainer() {42 if (!hasDockerDaemon()) {43 return null;44 }45 String path = getTestPath("vaultTest_adminPolicy.hcl");46 return new VaultContainer<>(VaultTestUtil.VAULT_DOCKER_IMAGE)47 .withVaultToken(VaultTestUtil.VAULT_ROOT_TOKEN)48 .withNetwork(network)49 .withNetworkAliases("vault")50 .withCopyFileToContainer(forHostPath(51 path),52 "/admin.hcl")53 .withExposedPorts(8200)54 .waitingFor(Wait.forHttp("/v1/sys/seal-status").forStatusCode(200));55 }56 public static String getTestPath(String resource) {57 URI uri = null;58 try {59 uri = VaultTestUtil.class.getResource(resource).toURI();60 } catch (URISyntaxException e) {61 // ignored62 }63 if (uri == null) return "FileNotFound"; // Should never happen.64 String result = Paths.get(uri).toFile().getAbsoluteFile().toString();65 if (SystemUtils.IS_OS_WINDOWS && result.startsWith("/")) {66 result = PathUtils.createMinGWPath(result).substring(1);67 }68 return result;69 }70 public static VaultContainer createVaultAgentContainer(71 Path roleIDPath,72 Path secretIDPath) {73 return new VaultContainer<>("vault:1.2.1")74 .withNetwork(network)75 .withCreateContainerCmdModifier(cmd -> cmd.withCapAdd(IPC_LOCK))76 .withCopyFileToContainer(forHostPath(77 getTestPath("vaultTest_agent.hcl")),78 "/agent.hcl")79 .withCopyFileToContainer(forHostPath(roleIDPath),80 "/home/vault/role_id")...
Source:TarantoolContainerClientHelper.java
...10import java.util.Arrays;11import java.util.List;12import java.util.concurrent.CompletableFuture;13import java.util.concurrent.atomic.AtomicReference;14import static org.testcontainers.containers.PathUtils.normalizePath;15/**16 * Provides a wrapper around a Tarantool client with helper methods17 *18 * @author Alexey Kuzin19 */20public final class TarantoolContainerClientHelper {21 private static final String TMP_DIR = "/tmp";22 private final TarantoolContainerOperations<? extends Container<?>> container;23 private final AtomicReference<TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>>> clientHolder =24 new AtomicReference<>();25 private final TarantoolClientBuilder clientBuilder;26 TarantoolContainerClientHelper(TarantoolContainerOperations<? extends Container<?>> container) {27 this.container = container;28 this.clientBuilder = TarantoolClientFactory.createClient()...
Source:EmbeddedKafkaWithBindingTest.java
...9import org.springframework.beans.factory.annotation.Qualifier;10import org.springframework.test.context.TestPropertySource;11import org.testcontainers.DockerClientFactory;12import org.testcontainers.containers.GenericContainer;13import org.testcontainers.utility.PathUtils;14import java.nio.file.Path;15import static com.playtika.test.kafka.properties.KafkaConfigurationProperties.KAFKA_BEAN_NAME;16import static org.assertj.core.api.Assertions.assertThat;17@Order(6)18@TestPropertySource(properties = {19 "embedded.kafka.waitTimeoutInSeconds=120",20 "embedded.zookeeper.fileSystemBind.enabled=true",21 "embedded.kafka.fileSystemBind.enabled=true",22 "embedded.kafka.fileSystemBind.dataFolder=${java.io.tmpdir}/embedded-kafka-data", // /tmp is more permissible than target23 "embedded.zookeeper.fileSystemBind.dataFolder=${java.io.tmpdir}/embedded-zk-data",24 "embedded.zookeeper.fileSystemBind.txnLogsFolder=${java.io.tmpdir}/embedded-zk-txn-logs",25 "embedded.kafka.dockerUser=root", // Needed to create mounted directories, or Zookeeper fails: "Unable to create data directory /var/lib/zookeeper/log/version-2"26})27@TestInstance(TestInstance.Lifecycle.PER_CLASS)28@DisplayName("Test that embedded-kafka with filesystem binding")29public class EmbeddedKafkaWithBindingTest extends EmbeddedKafkaTest {30 @AfterAll31 public static void afterAll(@Autowired KafkaConfigurationProperties kafkaProperties, @Autowired ZookeeperConfigurationProperties zookeeperProperties, @Qualifier(KAFKA_BEAN_NAME) GenericContainer kafka) throws Exception {32 Path projectDir = projectDir();33 Path zookeeperDataFolder = projectDir.resolve(zookeeperProperties.getFileSystemBind().getDataFolder());34 Path zookeeperTxnLogsFolder = projectDir.resolve(zookeeperProperties.getFileSystemBind().getTxnLogsFolder());35 Path kafkaDataFolder = projectDir.resolve(kafkaProperties.getFileSystemBind().getDataFolder());36 assertThat(zookeeperDataFolder.toFile()).isNotEmptyDirectory();37 assertThat(zookeeperTxnLogsFolder.toFile()).isNotEmptyDirectory();38 assertThat(kafkaDataFolder.toFile()).isNotEmptyDirectory();39 // Delete created files now40 kafka.execInContainer("rm", "-rf", "/var/lib/zookeeper/log", "/var/lib/zookeeper/data", "/var/lib/kafka/data");41 // Delete mounted directories after test run42 Runtime.getRuntime().addShutdownHook(new Thread(DockerClientFactory.TESTCONTAINERS_THREAD_GROUP, () -> {43 PathUtils.recursiveDeleteDir(kafkaDataFolder);44 PathUtils.recursiveDeleteDir(zookeeperDataFolder);45 PathUtils.recursiveDeleteDir(zookeeperTxnLogsFolder);46 }));47 }48}...
PathUtils
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.PostgreSQLContainer;3import org.testcontainers.containers.output.OutputFrame;4import org.testcontainers.containers.output.Slf4jLogConsumer;5import org.testcontainers.containers.output.WaitingConsumer;6import org.testcontainers.containers.wait.strategy.Wait;7import org.testcontainers.utility.PathUtils;8import org.junit.Test;9import java.nio.file.Path;10import java.util.concurrent.TimeUnit;11import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;12public class PathUtilsTest {13 public void testPathUtils() {14 Path path = PathUtils.get("src/test/resources/test.sql");15 assertTrue(path.toString().endsWith("src/test/resources/test.sql"));16 }17}18import org.testcontainers.containers.GenericContainer;19import org.testcontainers.containers.PostgreSQLContainer;20import org.testcontainers.containers.output.OutputFrame;21import org.testcontainers.containers.output.Slf4jLogConsumer;22import org.testcontainers.containers.output.WaitingConsumer;23import org.testcontainers.containers.wait.strategy.Wait;24import org.testcontainers.utility.PathUtils;25import org.junit.Test;26import java.nio.file.Path;27import java.util.concurrent.TimeUnit;28import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;29public class PathUtilsTest {30 public void testPathUtils() {31 Path path = PathUtils.get("src/test/resources/test.sql");32 assertTrue(path.toString().endsWith("src/test/resources/test.sql"));33 }34}35import org.testcontainers.containers.GenericContainer;36import org.testcontainers.containers.PostgreSQLContainer;37import org.testcontainers.containers.output.OutputFrame;38import org.testcontainers.containers.output.Slf4jLogConsumer;39import org.testcontainers.containers.output.WaitingConsumer;40import org.testcontainers.containers.wait.strategy.Wait;41import org.testcontainers.utility.PathUtils;42import org.junit.Test;43import java.nio.file.Path;44import java.util.concurrent.TimeUnit;45import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;46public class PathUtilsTest {47 public void testPathUtils() {48 Path path = PathUtils.get("src/test/resources/test.sql");49 assertTrue(path.toString().endsWith("src/test/resources/test.sql"));50 }51}
PathUtils
Using AI Code Generation
1import org.testcontainers.containers.PostgreSQLContainer;2import org.testcontainers.utility.PathUtils;3public class PathTest {4 public static void main(String[] args) {5 PostgreSQLContainer container = new PostgreSQLContainer();6 container.start();7 String path = PathUtils.getTestResourcePath("test.sql");8 System.out.println(path);9 container.stop();10 }11}
PathUtils
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.utility.PathUtils;3public class PathTest {4 public static void main(String[] args) {5 GenericContainer container = new GenericContainer("alpine:3.8");6 container.start();7 String path = PathUtils.getTestResourcePath("1.java");8 container.copyFileToContainer(path, "/1.java");9 System.out.println("File copied");10 container.stop();11 }12}
PathUtils
Using AI Code Generation
1import org.testcontainers.utility.PathUtils;2public class PathTest {3 public static void main(String[] args) {4 System.out.println(PathUtils.getTestResourceAbsolutePath("test.txt"));5 }6}7PathUtils.getTestResourceAbsolutePath()8PathUtils.getTestResourcePath()9PathUtils.getTestResourcesPath()
PathUtils
Using AI Code Generation
1public class TestContainers {2 public static void main(String[] args) throws IOException {3 String path = PathUtils.getTestResourcesPath(TestContainers.class);4 System.out.println(path);5 }6}74. PathUtils.getTestResourcesPath(Class<?> testClass, String... additionalPathSegments)8public class TestContainers {9 public static void main(String[] args) throws IOException {10 String path = PathUtils.getTestResourcesPath(TestContainers.class, "test");11 System.out.println(path);12 }13}145. PathUtils.getTestResourcesPath(String testClassName)15public class TestContainers {16 public static void main(String[] args) throws IOException {17 String path = PathUtils.getTestResourcesPath("TestContainers");18 System.out.println(path);19 }20}216. PathUtils.getTestResourcesPath(String testClassName, String... additionalPathSegments)22public class TestContainers {23 public static void main(String[] args) throws IOException {24 String path = PathUtils.getTestResourcesPath("TestContainers", "test");25 System.out.println(path);26 }27}
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!!