From 99b91b89b6ee3f8f0e9545e86d9f0744b301db30 Mon Sep 17 00:00:00 2001 From: Kevin Wittek Date: Thu, 19 Aug 2021 17:03:28 +0200 Subject: [PATCH] Update `OracleContainer` and re-activate on CI (#4298) Will now use "gvenzl/oracle-xe:18.4.0-slim" by default. Co-authored-by: Piotr Co-authored-by: Sergei Egorov --- .../utility/TestcontainersConfiguration.java | 5 +- .../containers/OracleContainer.java | 48 +++++++++++-------- .../containers/OracleContainerProvider.java | 9 ++-- .../junit/oracle/SimpleOracleTest.java | 13 +++-- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java b/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java index fd189a0ad59..cbbe92dd76d 100644 --- a/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java +++ b/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java @@ -24,7 +24,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; @@ -61,6 +60,7 @@ public class TestcontainersConfiguration { private static final String PULSAR_IMAGE = "apachepulsar/pulsar"; private static final String LOCALSTACK_IMAGE = "localstack/localstack"; private static final String SSHD_IMAGE = "testcontainers/sshd"; + private static final String ORACLE_IMAGE = "gvenzl/oracle-xe"; private static final ImmutableMap CONTAINER_MAPPING = ImmutableMap.builder() .put(DockerImageName.parse(AMBASSADOR_IMAGE), "ambassador.container.image") @@ -73,6 +73,7 @@ public class TestcontainersConfiguration { .put(DockerImageName.parse(PULSAR_IMAGE), "pulsar.container.image") .put(DockerImageName.parse(LOCALSTACK_IMAGE), "localstack.container.image") .put(DockerImageName.parse(SSHD_IMAGE), "sshd.container.image") + .put(DockerImageName.parse(ORACLE_IMAGE), "oracle.container.image") .build(); @Getter(lazy = true) @@ -146,7 +147,7 @@ public String getKafkaImage() { @Deprecated public String getOracleImage() { - return getEnvVarOrProperty("oracle.container.image", null); + return getImage(ORACLE_IMAGE).asCanonicalNameString(); } @Deprecated diff --git a/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainer.java b/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainer.java index 2573ef722e3..ccd705f5af9 100644 --- a/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainer.java +++ b/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainer.java @@ -1,45 +1,39 @@ package org.testcontainers.containers; import com.google.common.collect.Sets; +import org.apache.commons.lang.StringUtils; import org.testcontainers.utility.DockerImageName; -import org.testcontainers.utility.TestcontainersConfiguration; +import java.util.Arrays; +import java.util.List; import java.util.Set; import java.util.concurrent.Future; -/** - * @author gusohal - */ public class OracleContainer extends JdbcDatabaseContainer { public static final String NAME = "oracle"; + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("gvenzl/oracle-xe"); + + + static final String DEFAULT_TAG = "18.4.0-slim"; + static final String IMAGE = DEFAULT_IMAGE_NAME.getUnversionedPart(); private static final int ORACLE_PORT = 1521; private static final int APEX_HTTP_PORT = 8080; private static final int DEFAULT_STARTUP_TIMEOUT_SECONDS = 240; private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 120; + private static final List ORACLE_SYSTEM_USERS = Arrays.asList("system", "sys"); - private String username = "system"; - private String password = "oracle"; - - private static String resolveImageName() { - String image = TestcontainersConfiguration.getInstance().getOracleImage(); - - if (image == null) { - throw new IllegalStateException("An image to use for Oracle containers must be configured. " + - "To do this, please place a file on the classpath named `testcontainers.properties`, " + - "containing `oracle.container.image=IMAGE`, where IMAGE is a suitable image name and tag."); - } - return image; - } + private String username = "test"; + private String password = "test"; /** * @deprecated use {@link OracleContainer(DockerImageName)} instead */ @Deprecated public OracleContainer() { - this(resolveImageName()); + this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); } public OracleContainer(String dockerImageName) { @@ -74,7 +68,7 @@ public String getDriverClassName() { @Override public String getJdbcUrl() { - return "jdbc:oracle:thin:" + getUsername() + "/" + getPassword() + "@" + getHost() + ":" + getOraclePort() + ":" + getSid(); + return "jdbc:oracle:thin:" + getUsername() + "/" + getPassword() + "@" + getHost() + ":" + getOraclePort() + "/xepdb1"; } @Override @@ -89,12 +83,21 @@ public String getPassword() { @Override public OracleContainer withUsername(String username) { + if (StringUtils.isEmpty(username)) { + throw new IllegalArgumentException("Username cannot be null or empty"); + } + if (ORACLE_SYSTEM_USERS.contains(username.toLowerCase())) { + throw new IllegalArgumentException("Username cannot be one of " + ORACLE_SYSTEM_USERS); + } this.username = username; return self(); } @Override public OracleContainer withPassword(String password) { + if (StringUtils.isEmpty(password)) { + throw new IllegalArgumentException("Password cannot be null or empty"); + } this.password = password; return self(); } @@ -122,4 +125,11 @@ public Integer getWebPort() { public String getTestQueryString() { return "SELECT 1 FROM DUAL"; } + + @Override + protected void configure() { + withEnv("ORACLE_PASSWORD", password); + withEnv("APP_USER", username); + withEnv("APP_USER_PASSWORD", password); + } } diff --git a/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainerProvider.java b/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainerProvider.java index 853ffdaf17a..a298ae23359 100644 --- a/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainerProvider.java +++ b/modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainerProvider.java @@ -1,5 +1,7 @@ package org.testcontainers.containers; +import org.testcontainers.utility.DockerImageName; + /** * Factory for Oracle containers. */ @@ -11,15 +13,14 @@ public boolean supports(String databaseType) { @Override public JdbcDatabaseContainer newInstance() { - return new OracleContainer(); + return newInstance(OracleContainer.DEFAULT_TAG); } @Override public JdbcDatabaseContainer newInstance(String tag) { if (tag != null) { - throw new UnsupportedOperationException("Oracle database tag should be set in the configured image name"); + return new OracleContainer(DockerImageName.parse(OracleContainer.IMAGE).withTag(tag)); } - - return new OracleContainer(); + return newInstance(); } } diff --git a/modules/oracle-xe/src/test/java/org/testcontainers/junit/oracle/SimpleOracleTest.java b/modules/oracle-xe/src/test/java/org/testcontainers/junit/oracle/SimpleOracleTest.java index 2415fed11f4..7d026924442 100644 --- a/modules/oracle-xe/src/test/java/org/testcontainers/junit/oracle/SimpleOracleTest.java +++ b/modules/oracle-xe/src/test/java/org/testcontainers/junit/oracle/SimpleOracleTest.java @@ -1,21 +1,28 @@ package org.testcontainers.junit.oracle; -import org.junit.Ignore; import org.junit.Test; import org.testcontainers.containers.OracleContainer; import org.testcontainers.db.AbstractContainerDatabaseTest; +import org.testcontainers.utility.DockerImageName; import java.sql.ResultSet; import java.sql.SQLException; import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; -@Ignore public class SimpleOracleTest extends AbstractContainerDatabaseTest { + public static final DockerImageName ORACLE_DOCKER_IMAGE_NAME = DockerImageName.parse("gvenzl/oracle-xe:18.4.0-slim"); + @Test public void testSimple() throws SQLException { - try (OracleContainer oracle = new OracleContainer()) { + + try ( + OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME) + .withUsername("baz") + .withPassword("bar") + ) { + oracle.start(); ResultSet resultSet = performQuery(oracle, "SELECT 1 FROM dual"); int resultSetInt = resultSet.getInt(1);