|
| 1 | +package org.testcontainers.oracle; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | +import org.testcontainers.containers.JdbcDatabaseContainer; |
| 6 | +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; |
| 7 | +import org.testcontainers.utility.DockerImageName; |
| 8 | + |
| 9 | +import java.time.Duration; |
| 10 | +import java.time.temporal.ChronoUnit; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +/** |
| 17 | + * Testcontainers implementation for Oracle Database Free. |
| 18 | + * <p> |
| 19 | + * Supported image: {@code gvenzl/oracle-free} |
| 20 | + * <p> |
| 21 | + * Exposed ports: 1521 |
| 22 | + */ |
| 23 | +public class OracleContainer extends JdbcDatabaseContainer<OracleContainer> { |
| 24 | + |
| 25 | + static final String NAME = "oracle"; |
| 26 | + |
| 27 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("gvenzl/oracle-free"); |
| 28 | + |
| 29 | + static final String DEFAULT_TAG = "slim"; |
| 30 | + |
| 31 | + static final String IMAGE = DEFAULT_IMAGE_NAME.getUnversionedPart(); |
| 32 | + |
| 33 | + static final int ORACLE_PORT = 1521; |
| 34 | + |
| 35 | + private static final int DEFAULT_STARTUP_TIMEOUT_SECONDS = 60; |
| 36 | + |
| 37 | + private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 60; |
| 38 | + |
| 39 | + // Container defaults |
| 40 | + static final String DEFAULT_DATABASE_NAME = "freepdb1"; |
| 41 | + |
| 42 | + static final String DEFAULT_SID = "free"; |
| 43 | + |
| 44 | + static final String DEFAULT_SYSTEM_USER = "system"; |
| 45 | + |
| 46 | + static final String DEFAULT_SYS_USER = "sys"; |
| 47 | + |
| 48 | + // Test container defaults |
| 49 | + static final String APP_USER = "test"; |
| 50 | + |
| 51 | + static final String APP_USER_PASSWORD = "test"; |
| 52 | + |
| 53 | + // Restricted user and database names |
| 54 | + private static final List<String> ORACLE_SYSTEM_USERS = Arrays.asList(DEFAULT_SYSTEM_USER, DEFAULT_SYS_USER); |
| 55 | + |
| 56 | + private String databaseName = DEFAULT_DATABASE_NAME; |
| 57 | + |
| 58 | + private String username = APP_USER; |
| 59 | + |
| 60 | + private String password = APP_USER_PASSWORD; |
| 61 | + |
| 62 | + private boolean usingSid = false; |
| 63 | + |
| 64 | + public OracleContainer(String dockerImageName) { |
| 65 | + this(DockerImageName.parse(dockerImageName)); |
| 66 | + } |
| 67 | + |
| 68 | + public OracleContainer(final DockerImageName dockerImageName) { |
| 69 | + super(dockerImageName); |
| 70 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 71 | + this.waitStrategy = |
| 72 | + new LogMessageWaitStrategy() |
| 73 | + .withRegEx(".*DATABASE IS READY TO USE!.*\\s") |
| 74 | + .withTimes(1) |
| 75 | + .withStartupTimeout(Duration.of(DEFAULT_STARTUP_TIMEOUT_SECONDS, ChronoUnit.SECONDS)); |
| 76 | + withConnectTimeoutSeconds(DEFAULT_CONNECT_TIMEOUT_SECONDS); |
| 77 | + addExposedPorts(ORACLE_PORT); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void waitUntilContainerStarted() { |
| 82 | + getWaitStrategy().waitUntilReady(this); |
| 83 | + } |
| 84 | + |
| 85 | + @NotNull |
| 86 | + @Override |
| 87 | + public Set<Integer> getLivenessCheckPortNumbers() { |
| 88 | + return Collections.singleton(getMappedPort(ORACLE_PORT)); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String getDriverClassName() { |
| 93 | + return "oracle.jdbc.driver.OracleDriver"; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String getJdbcUrl() { |
| 98 | + return isUsingSid() |
| 99 | + ? "jdbc:oracle:thin:" + "@" + getHost() + ":" + getOraclePort() + ":" + getSid() |
| 100 | + : "jdbc:oracle:thin:" + "@" + getHost() + ":" + getOraclePort() + "/" + getDatabaseName(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String getUsername() { |
| 105 | + // An application user is tied to the database, and therefore not authenticated to connect to SID. |
| 106 | + return isUsingSid() ? DEFAULT_SYSTEM_USER : username; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String getPassword() { |
| 111 | + return password; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String getDatabaseName() { |
| 116 | + return databaseName; |
| 117 | + } |
| 118 | + |
| 119 | + protected boolean isUsingSid() { |
| 120 | + return usingSid; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public OracleContainer withUsername(String username) { |
| 125 | + if (StringUtils.isEmpty(username)) { |
| 126 | + throw new IllegalArgumentException("Username cannot be null or empty"); |
| 127 | + } |
| 128 | + if (ORACLE_SYSTEM_USERS.contains(username.toLowerCase())) { |
| 129 | + throw new IllegalArgumentException("Username cannot be one of " + ORACLE_SYSTEM_USERS); |
| 130 | + } |
| 131 | + this.username = username; |
| 132 | + return self(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public OracleContainer withPassword(String password) { |
| 137 | + if (StringUtils.isEmpty(password)) { |
| 138 | + throw new IllegalArgumentException("Password cannot be null or empty"); |
| 139 | + } |
| 140 | + this.password = password; |
| 141 | + return self(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public OracleContainer withDatabaseName(String databaseName) { |
| 146 | + if (StringUtils.isEmpty(databaseName)) { |
| 147 | + throw new IllegalArgumentException("Database name cannot be null or empty"); |
| 148 | + } |
| 149 | + |
| 150 | + if (DEFAULT_DATABASE_NAME.equals(databaseName.toLowerCase())) { |
| 151 | + throw new IllegalArgumentException("Database name cannot be set to " + DEFAULT_DATABASE_NAME); |
| 152 | + } |
| 153 | + |
| 154 | + this.databaseName = databaseName; |
| 155 | + return self(); |
| 156 | + } |
| 157 | + |
| 158 | + public OracleContainer usingSid() { |
| 159 | + this.usingSid = true; |
| 160 | + return self(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public OracleContainer withUrlParam(String paramName, String paramValue) { |
| 165 | + throw new UnsupportedOperationException("The Oracle Database driver does not support this"); |
| 166 | + } |
| 167 | + |
| 168 | + @SuppressWarnings("SameReturnValue") |
| 169 | + public String getSid() { |
| 170 | + return DEFAULT_SID; |
| 171 | + } |
| 172 | + |
| 173 | + public Integer getOraclePort() { |
| 174 | + return getMappedPort(ORACLE_PORT); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public String getTestQueryString() { |
| 179 | + return "SELECT 1 FROM DUAL"; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + protected void configure() { |
| 184 | + withEnv("ORACLE_PASSWORD", password); |
| 185 | + |
| 186 | + // Only set ORACLE_DATABASE if different than the default. |
| 187 | + if (databaseName != DEFAULT_DATABASE_NAME) { |
| 188 | + withEnv("ORACLE_DATABASE", databaseName); |
| 189 | + } |
| 190 | + |
| 191 | + withEnv("APP_USER", username); |
| 192 | + withEnv("APP_USER_PASSWORD", password); |
| 193 | + } |
| 194 | +} |
0 commit comments