diff --git a/cassandra/README.md b/cassandra/README.md index 4d0717d..90f1e20 100644 --- a/cassandra/README.md +++ b/cassandra/README.md @@ -18,7 +18,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.6.1" ``` **Maven** @@ -26,7 +26,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.6.0" io.goodforgod testcontainers-extensions-cassandra - 0.6.0 + 0.6.1 test ``` @@ -63,7 +63,7 @@ testImplementation "com.datastax.oss:java-driver-core:4.17.0" `@TestcontainersCassandra` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/CassandraMetadata.java b/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/CassandraMetadata.java index c786643..e5a8eef 100644 --- a/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/CassandraMetadata.java +++ b/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/CassandraMetadata.java @@ -16,7 +16,7 @@ final class CassandraMetadata extends AbstractContainerMetadata { } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "cassandra-" + System.currentTimeMillis(); } diff --git a/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/TestcontainersCassandraExtension.java b/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/TestcontainersCassandraExtension.java index 8780d9d..6e0dc8a 100644 --- a/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/TestcontainersCassandraExtension.java +++ b/cassandra/src/main/java/io/goodforgod/testcontainers/extensions/cassandra/TestcontainersCassandraExtension.java @@ -46,8 +46,8 @@ protected CassandraContainer getContainerDefault(CassandraMetadata metadata) var container = new CassandraContainer<>(dockerImage) .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(CassandraContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .waitingFor(Wait.forListeningPort()) .withStartupTimeout(Duration.ofMinutes(5)); @@ -85,11 +85,10 @@ protected Optional findMetadata(@NotNull ExtensionContext con @NotNull protected CassandraConnection getConnectionForContainer(CassandraMetadata metadata, @NotNull CassandraContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) - .filter(a -> !a.isBlank()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final String alias = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .orElse(null); return CassandraConnectionImpl.forContainer(container.getHost(), diff --git a/cockroachdb/README.md b/cockroachdb/README.md index eae6c29..be6d24c 100644 --- a/cockroachdb/README.md +++ b/cockroachdb/README.md @@ -18,7 +18,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-cockroachdb:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-cockroachdb:0.6.1" ``` **Maven** @@ -26,7 +26,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-cockroachdb:0.6.0" io.goodforgod testcontainers-extensions-cockroachdb - 0.6.0 + 0.6.1 test ``` @@ -62,7 +62,7 @@ testRuntimeOnly "org.postgresql:postgresql:42.6.0" `@TestcontainersCockroachdb` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/CockroachMetadata.java b/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/CockroachMetadata.java index 023ba21..8eb1dfe 100644 --- a/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/CockroachMetadata.java +++ b/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/CockroachMetadata.java @@ -12,7 +12,7 @@ final class CockroachMetadata extends JdbcMetadata { } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "cockroachdb-" + System.currentTimeMillis(); } } diff --git a/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersCockroachdbExtension.java b/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersCockroachdbExtension.java index 15b8722..425dd24 100644 --- a/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersCockroachdbExtension.java +++ b/cockroachdb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersCockroachdbExtension.java @@ -50,8 +50,8 @@ protected CockroachContainer getContainerDefault(CockroachMetadata metadata) { var container = new CockroachContainer(dockerImage) .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(CockroachContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .withStartupTimeout(Duration.ofMinutes(5)); if (metadata.networkShared()) { @@ -74,11 +74,10 @@ protected Optional findMetadata(@NotNull ExtensionContext con @NotNull protected JdbcConnection getConnectionForContainer(CockroachMetadata metadata, @NotNull CockroachContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) - .filter(a -> !a.isBlank()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final String alias = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .orElse(null); return JdbcConnectionImpl.forJDBC(container.getJdbcUrl(), diff --git a/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractContainerMetadata.java b/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractContainerMetadata.java index 8fcd150..9eeb61c 100644 --- a/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractContainerMetadata.java +++ b/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractContainerMetadata.java @@ -10,15 +10,21 @@ public abstract class AbstractContainerMetadata implements ContainerMetadata { private final boolean network; private final String alias; + private final String aliasOrDefault; private final String image; private final ContainerMode runMode; protected AbstractContainerMetadata(boolean network, String alias, String image, ContainerMode runMode) { this.network = network; this.runMode = runMode; - this.alias = Optional.ofNullable(getEnvValue("Alias", alias)).orElse(networkAliasDefault()); - this.image = Optional.ofNullable(getEnvValue("Image", image)).orElseThrow( - () -> new IllegalArgumentException(getClass() + " expected image from '" + image + "' but received null")); + this.alias = Optional.ofNullable(getEnvValue("Alias", alias)) + .filter(a -> !a.isBlank()) + .orElse(null); + this.aliasOrDefault = Optional.ofNullable(this.alias).orElse(networkAliasDefault()); + this.image = Optional.ofNullable(getEnvValue("Image", image)) + .filter(a -> !a.isBlank()) + .orElseThrow(() -> new IllegalArgumentException( + getClass() + " expected image from '" + image + "' but received null")); } private static boolean isEnvironmentValue(String value) { @@ -50,9 +56,6 @@ private static String getEnvValue(String name, String envOrValue) { } } - @NotNull - protected abstract String networkAliasDefault(); - @Override public boolean networkShared() { return network; @@ -63,6 +66,11 @@ public boolean networkShared() { return alias; } + @Override + public @NotNull String networkAliasOrDefault() { + return aliasOrDefault; + } + @Override public @NotNull String image() { return image; diff --git a/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractTestcontainersExtension.java b/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractTestcontainersExtension.java index 4aa7f98..cc3b708 100644 --- a/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractTestcontainersExtension.java +++ b/common/src/main/java/io/goodforgod/testcontainers/extensions/AbstractTestcontainersExtension.java @@ -57,6 +57,11 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(image, network, alias); } + + @Override + public String toString() { + return "[image=" + image + ", alias=" + alias + ']'; + } } static final Map>> CLASS_TO_SHARED_CONTAINERS = new ConcurrentHashMap<>(); @@ -263,10 +268,10 @@ private void setupBeforeAll(ExtensionContext context) { .orElse(a.get(0))) .orElse(metadata.networkAlias()); + var sharedKey = new SharedKey(imageShared, networkShared, networkAlias); var sharedContainerMap = CLASS_TO_SHARED_CONTAINERS.computeIfAbsent(getClass().getCanonicalName(), k -> new ConcurrentHashMap<>()); - var sharedKey = new SharedKey(imageShared, networkShared, networkAlias); var extensionContainer = sharedContainerMap.computeIfAbsent(sharedKey, k -> { var container = containerFromField.orElseGet(() -> { logger.debug("Getting default container for image: {}", metadata.image()); diff --git a/common/src/main/java/io/goodforgod/testcontainers/extensions/ContainerMetadata.java b/common/src/main/java/io/goodforgod/testcontainers/extensions/ContainerMetadata.java index 76cb112..4b1200d 100644 --- a/common/src/main/java/io/goodforgod/testcontainers/extensions/ContainerMetadata.java +++ b/common/src/main/java/io/goodforgod/testcontainers/extensions/ContainerMetadata.java @@ -18,6 +18,15 @@ public interface ContainerMetadata { @Nullable String networkAlias(); + @NotNull + String networkAliasDefault(); + + /** + * @see Network + */ + @NotNull + String networkAliasOrDefault(); + @NotNull String image(); diff --git a/common/src/test/java/io/goodforgod/testcontainers/extensions/ContainerPerClassAbstractTests.java b/common/src/test/java/io/goodforgod/testcontainers/extensions/ContainerPerClassAbstractTests.java index aeb8f6e..65afcfd 100644 --- a/common/src/test/java/io/goodforgod/testcontainers/extensions/ContainerPerClassAbstractTests.java +++ b/common/src/test/java/io/goodforgod/testcontainers/extensions/ContainerPerClassAbstractTests.java @@ -34,6 +34,10 @@ void firstConnection(@ContainerRedisConnection RedisConnection connection) { assertNull(firstConnection); assertNotNull(connection); assertNotNull(connection.params().uri()); + assertTrue(connection.paramsInNetwork().isPresent()); + assertNotNull(connection.paramsInNetwork().get().uri()); + assertNotNull(connection.paramsInNetwork().get().host()); + assertFalse(connection.paramsInNetwork().get().host().isBlank()); assertNotNull(sameConnectionChild); assertEquals(sameConnectionChild, connection); assertEquals(sameConnectionChild, sameConnectionParent); @@ -45,6 +49,10 @@ void firstConnection(@ContainerRedisConnection RedisConnection connection) { void secondConnection(@ContainerRedisConnection RedisConnection connection) { assertNotNull(connection); assertNotNull(connection.params().uri()); + assertTrue(connection.paramsInNetwork().isPresent()); + assertNotNull(connection.paramsInNetwork().get().uri()); + assertNotNull(connection.paramsInNetwork().get().host()); + assertFalse(connection.paramsInNetwork().get().host().isBlank()); assertNotNull(firstConnection); assertNotNull(sameConnectionChild); assertEquals(sameConnectionChild, connection); diff --git a/common/src/test/java/io/goodforgod/testcontainers/extensions/example/RedisMetadata.java b/common/src/test/java/io/goodforgod/testcontainers/extensions/example/RedisMetadata.java index 9feb67d..b95c99d 100644 --- a/common/src/test/java/io/goodforgod/testcontainers/extensions/example/RedisMetadata.java +++ b/common/src/test/java/io/goodforgod/testcontainers/extensions/example/RedisMetadata.java @@ -11,7 +11,7 @@ final class RedisMetadata extends AbstractContainerMetadata { } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "redis-" + System.currentTimeMillis(); } } diff --git a/common/src/test/java/io/goodforgod/testcontainers/extensions/example/TestcontainersRedisExtension.java b/common/src/test/java/io/goodforgod/testcontainers/extensions/example/TestcontainersRedisExtension.java index 4cb0022..a30cbfa 100644 --- a/common/src/test/java/io/goodforgod/testcontainers/extensions/example/TestcontainersRedisExtension.java +++ b/common/src/test/java/io/goodforgod/testcontainers/extensions/example/TestcontainersRedisExtension.java @@ -42,8 +42,8 @@ protected RedisContainer getContainerDefault(RedisMetadata metadata) { var container = new RedisContainer(dockerImage) .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(RedisContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .waitingFor(Wait.forListeningPort()) .withStartupTimeout(Duration.ofMinutes(5)); @@ -67,7 +67,7 @@ protected Optional findMetadata(@NotNull ExtensionContext context @NotNull protected RedisConnection getConnectionForContainer(@NotNull RedisMetadata metadata, @NotNull RedisContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) + final String alias = Optional.ofNullable(metadata.networkAliasOrDefault()) .filter(a -> !a.isBlank()) .or(() -> container.getNetworkAliases().stream() .filter(a -> a.startsWith("redis")) diff --git a/gradle.properties b/gradle.properties index 14f3478..76d8327 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ groupId=io.goodforgod artifactRootId=testcontainers-extensions -artifactVersion=0.6.0-SNAPSHOT +artifactVersion=0.6.1-SNAPSHOT ##### GRADLE ##### diff --git a/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/ExampleTestcontainersJdbcExtension.java b/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/ExampleTestcontainersJdbcExtension.java index d0cf512..d82cc38 100644 --- a/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/ExampleTestcontainersJdbcExtension.java +++ b/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/ExampleTestcontainersJdbcExtension.java @@ -50,8 +50,8 @@ protected PostgreSQLContainer getContainerDefault(PostgresJdbcMetadata metada .withPassword("postgres") .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(PostgreSQLContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()); + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()); } @NotNull diff --git a/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresJdbcMetadata.java b/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresJdbcMetadata.java index 2f27eac..07597c7 100644 --- a/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresJdbcMetadata.java +++ b/jdbc/src/test/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresJdbcMetadata.java @@ -10,7 +10,7 @@ final class PostgresJdbcMetadata extends JdbcMetadata { } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "postgres-" + System.currentTimeMillis(); } } diff --git a/kafka/README.md b/kafka/README.md index 91fc63f..0596019 100644 --- a/kafka/README.md +++ b/kafka/README.md @@ -18,7 +18,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-kafka:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-kafka:0.6.1" ``` **Maven** @@ -26,7 +26,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-kafka:0.6.0" io.goodforgod testcontainers-extensions-kafka - 0.6.0 + 0.6.1 test ``` @@ -65,7 +65,7 @@ testRuntimeOnly "org.apache.kafka:kafka-clients:3.5.1" `@TestcontainersKafka` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/KafkaMetadata.java b/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/KafkaMetadata.java index 4f28472..182514a 100644 --- a/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/KafkaMetadata.java +++ b/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/KafkaMetadata.java @@ -19,7 +19,7 @@ final class KafkaMetadata extends AbstractContainerMetadata { } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "kafka-" + System.currentTimeMillis(); } diff --git a/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/TestcontainersKafkaExtension.java b/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/TestcontainersKafkaExtension.java index b421a4e..990cc5a 100644 --- a/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/TestcontainersKafkaExtension.java +++ b/kafka/src/main/java/io/goodforgod/testcontainers/extensions/kafka/TestcontainersKafkaExtension.java @@ -90,11 +90,11 @@ protected KafkaContainer getContainerDefault(KafkaMetadata metadata) { var container = new KafkaContainer(dockerImage) .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(KafkaContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) + .withMdc("alias", metadata.networkAliasOrDefault())) .withEnv("KAFKA_CONFLUENT_SUPPORT_METRICS_ENABLE", "false") .withEnv("AUTO_CREATE_TOPICS", "true") .withEmbeddedZookeeper() - .withNetworkAliases(metadata.networkAlias()) + .withNetworkAliases(metadata.networkAliasOrDefault()) .waitingFor(Wait.forListeningPort()) .withStartupTimeout(Duration.ofMinutes(5)); @@ -110,11 +110,10 @@ protected KafkaConnection getConnectionForContainer(KafkaMetadata metadata, Kafk final Properties properties = new Properties(); properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, container.getBootstrapServers()); - final Properties networkProperties = Optional.ofNullable(metadata.networkAlias()) - .filter(a -> !a.isBlank()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final Properties networkProperties = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .map(alias -> { final Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, diff --git a/mariadb/README.md b/mariadb/README.md index 02c8d32..d7eff54 100644 --- a/mariadb/README.md +++ b/mariadb/README.md @@ -18,7 +18,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-mariadb:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-mariadb:0.6.1" ``` **Maven** @@ -26,7 +26,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-mariadb:0.6.0" io.goodforgod testcontainers-extensions-mariadb - 0.6.0 + 0.6.1 test ``` @@ -62,7 +62,7 @@ testRuntimeOnly "org.mariadb.jdbc:mariadb-java-client:3.1.4" `@TestcontainersMariadb` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MariadbMetadata.java b/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MariadbMetadata.java index 9c663f5..7ffae48 100644 --- a/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MariadbMetadata.java +++ b/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MariadbMetadata.java @@ -12,7 +12,7 @@ public MariadbMetadata(boolean network, String alias, String image, ContainerMod } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "mariadb-" + System.currentTimeMillis(); } } diff --git a/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMariadbExtension.java b/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMariadbExtension.java index 79f3e6a..006deae 100644 --- a/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMariadbExtension.java +++ b/mariadb/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMariadbExtension.java @@ -55,8 +55,8 @@ protected MariaDBContainer getContainerDefault(MariadbMetadata metadata) { .withPassword("mariadb") .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(MariaDBContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .waitingFor(Wait.forListeningPort()) .withStartupTimeout(Duration.ofMinutes(5)); @@ -80,10 +80,10 @@ protected Optional findMetadata(@NotNull ExtensionContext conte @NotNull protected JdbcConnection getConnectionForContainer(MariadbMetadata metadata, @NotNull MariaDBContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final String alias = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .orElse(null); return JdbcConnectionImpl.forJDBC(container.getJdbcUrl(), diff --git a/mysql/README.md b/mysql/README.md index 3a0dfbd..68fa9ed 100644 --- a/mysql/README.md +++ b/mysql/README.md @@ -18,7 +18,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-mysql:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-mysql:0.6.1" ``` **Maven** @@ -26,7 +26,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-mysql:0.6.0" io.goodforgod testcontainers-extensions-mysql - 0.6.0 + 0.6.1 test ``` @@ -62,7 +62,7 @@ testRuntimeOnly "mysql:mysql-connector-java:8.0.33" `@TestcontainersMysql` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MysqlMetadata.java b/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MysqlMetadata.java index 620753f..f067f33 100644 --- a/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MysqlMetadata.java +++ b/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/MysqlMetadata.java @@ -12,7 +12,7 @@ public MysqlMetadata(boolean network, String alias, String image, ContainerMode } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "mysql-" + System.currentTimeMillis(); } } diff --git a/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMysqlExtension.java b/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMysqlExtension.java index a5d70cb..ef94c5c 100644 --- a/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMysqlExtension.java +++ b/mysql/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersMysqlExtension.java @@ -54,8 +54,8 @@ protected MySQLContainer getContainerDefault(MysqlMetadata metadata) { .withPassword("mysql") .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(MySQLContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .waitingFor(Wait.forListeningPort()) .withStartupTimeout(Duration.ofMinutes(5)); @@ -79,10 +79,10 @@ protected Optional findMetadata(@NotNull ExtensionContext context @NotNull protected JdbcConnection getConnectionForContainer(MysqlMetadata metadata, @NotNull MySQLContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final String alias = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .orElse(null); return JdbcConnectionImpl.forJDBC(container.getJdbcUrl(), diff --git a/oracle/README.md b/oracle/README.md index ea16618..884ebd6 100644 --- a/oracle/README.md +++ b/oracle/README.md @@ -18,7 +18,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-oracle:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-oracle:0.6.1" ``` **Maven** @@ -26,7 +26,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-oracle:0.6.0" io.goodforgod testcontainers-extensions-oracle - 0.6.0 + 0.6.1 test ``` @@ -65,7 +65,7 @@ Extension tested against image `gvenzl/oracle-xe:18.4.0-faststart` and driver `c `@TestcontainersOracle` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/OracleMetadata.java b/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/OracleMetadata.java index f6fc75c..865f59d 100644 --- a/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/OracleMetadata.java +++ b/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/OracleMetadata.java @@ -12,7 +12,7 @@ public OracleMetadata(boolean network, String alias, String image, ContainerMode } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "oracle-" + System.currentTimeMillis(); } } diff --git a/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersOracleExtension.java b/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersOracleExtension.java index ea74ad5..c641f51 100644 --- a/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersOracleExtension.java +++ b/oracle/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersOracleExtension.java @@ -51,8 +51,8 @@ protected OracleContainer getContainerDefault(OracleMetadata metadata) { .withDatabaseName("oracle") .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(OracleContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .withStartupTimeout(Duration.ofMinutes(5)); if (metadata.networkShared()) { @@ -75,10 +75,10 @@ protected Optional findMetadata(@NotNull ExtensionContext contex @NotNull protected JdbcConnection getConnectionForContainer(OracleMetadata metadata, @NotNull OracleContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final String alias = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .orElse(null); return JdbcConnectionImpl.forJDBC(container.getJdbcUrl(), diff --git a/postgres/README.md b/postgres/README.md index d8dc331..a923761 100644 --- a/postgres/README.md +++ b/postgres/README.md @@ -18,7 +18,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.6.1" ``` **Maven** @@ -26,7 +26,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.6.0" io.goodforgod testcontainers-extensions-postgres - 0.6.0 + 0.6.1 test ``` @@ -62,7 +62,7 @@ testRuntimeOnly "org.postgresql:postgresql:42.6.0" `@TestcontainersPostgres` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresMetadata.java b/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresMetadata.java index 67f22ee..0368979 100644 --- a/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresMetadata.java +++ b/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/PostgresMetadata.java @@ -12,7 +12,7 @@ public PostgresMetadata(boolean network, String alias, String image, ContainerMo } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "postgres-" + System.currentTimeMillis(); } } diff --git a/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersPostgresExtension.java b/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersPostgresExtension.java index ecdf868..acae847 100644 --- a/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersPostgresExtension.java +++ b/postgres/src/main/java/io/goodforgod/testcontainers/extensions/jdbc/TestcontainersPostgresExtension.java @@ -53,8 +53,8 @@ protected PostgreSQLContainer getContainerDefault(PostgresMetadata metadata) .withPassword("postgres") .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(PostgreSQLContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .withStartupTimeout(Duration.ofMinutes(5)); if (metadata.networkShared()) { @@ -77,10 +77,10 @@ protected Optional findMetadata(@NotNull ExtensionContext cont @NotNull protected JdbcConnection getConnectionForContainer(PostgresMetadata metadata, @NotNull PostgreSQLContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final String alias = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .orElse(null); return JdbcConnectionImpl.forJDBC(container.getJdbcUrl(), diff --git a/redis/README.md b/redis/README.md index 5fe69cd..92a83b1 100644 --- a/redis/README.md +++ b/redis/README.md @@ -17,7 +17,7 @@ Features: **Gradle** ```groovy -testImplementation "io.goodforgod:testcontainers-extensions-redis:0.6.0" +testImplementation "io.goodforgod:testcontainers-extensions-redis:0.6.1" ``` **Maven** @@ -25,7 +25,7 @@ testImplementation "io.goodforgod:testcontainers-extensions-redis:0.6.0" io.goodforgod testcontainers-extensions-redis - 0.6.0 + 0.6.1 test ``` @@ -61,7 +61,7 @@ testImplementation "redis.clients:jedis:4.4.3" `@TestcontainersRedis` - allow **automatically start container** with specified image in different modes without the need to configure it. Available containers modes: -- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes) +- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes) - `PER_CLASS` - start new container each *test class*. - `PER_METHOD` - start new container each *test method*. diff --git a/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/RedisMetadata.java b/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/RedisMetadata.java index 90d41c2..28d3475 100644 --- a/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/RedisMetadata.java +++ b/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/RedisMetadata.java @@ -13,7 +13,7 @@ final class RedisMetadata extends AbstractContainerMetadata { } @Override - protected @NotNull String networkAliasDefault() { + public @NotNull String networkAliasDefault() { return "redis-" + System.currentTimeMillis(); } } diff --git a/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/TestcontainersRedisExtension.java b/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/TestcontainersRedisExtension.java index a63724c..20ebc03 100644 --- a/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/TestcontainersRedisExtension.java +++ b/redis/src/main/java/io/goodforgod/testcontainers/extensions/redis/TestcontainersRedisExtension.java @@ -50,8 +50,8 @@ protected RedisContainer getContainerDefault(RedisMetadata metadata) { var container = new RedisContainer(dockerImage) .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(RedisContainer.class)) .withMdc("image", metadata.image()) - .withMdc("alias", metadata.networkAlias())) - .withNetworkAliases(metadata.networkAlias()) + .withMdc("alias", metadata.networkAliasOrDefault())) + .withNetworkAliases(metadata.networkAliasOrDefault()) .waitingFor(Wait.forListeningPort()) .withStartupTimeout(Duration.ofMinutes(5)); @@ -75,10 +75,10 @@ protected Optional findMetadata(@NotNull ExtensionContext context @NotNull protected RedisConnection getConnectionForContainer(RedisMetadata metadata, @NotNull RedisContainer container) { - final String alias = Optional.ofNullable(metadata.networkAlias()) - .or(() -> (container.getNetworkAliases().isEmpty()) - ? Optional.empty() - : Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1))) + final String alias = container.getNetworkAliases().stream() + .filter(a -> a.equals(metadata.networkAliasOrDefault())) + .findFirst() + .or(() -> container.getNetworkAliases().stream().findFirst()) .orElse(null); return RedisConnectionImpl.forContainer(container.getHost(),