Use different default database names for Oracle XE and Oracle Free

Closes gh-38476
This commit is contained in:
Andy Wilkinson 2023-11-22 10:48:35 +00:00
parent 2323c7a838
commit a7571cf667
14 changed files with 234 additions and 42 deletions

View File

@ -0,0 +1,47 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
/**
* Enumeration of supported Oracle containers.
*
* @author Andy Wilkinson
*/
enum OracleContainer {
FREE("gvenzl/oracle-free", "freepdb1"),
XE("gvenzl/oracle-xe", "xepdb1");
private final String imageName;
private final String defaultDatabase;
OracleContainer(String imageName, String defaultDatabase) {
this.imageName = imageName;
this.defaultDatabase = defaultDatabase;
}
String getImageName() {
return this.imageName;
}
String getDefaultDatabase() {
return this.defaultDatabase;
}
}

View File

@ -28,18 +28,16 @@ import org.springframework.util.StringUtils;
*/
class OracleEnvironment {
static final String[] CONTAINER_NAMES = { "gvenzl/oracle-xe", "gvenzl/oracle-free" };
private final String username;
private final String password;
private final String database;
OracleEnvironment(Map<String, String> env) {
OracleEnvironment(Map<String, String> env, String defaultDatabase) {
this.username = env.getOrDefault("APP_USER", "system");
this.password = extractPassword(env);
this.database = env.getOrDefault("ORACLE_DATABASE", "xepdb1");
this.database = env.getOrDefault("ORACLE_DATABASE", defaultDatabase);
}
private String extractPassword(Map<String, String> env) {

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link JdbcConnectionDetails}
* for an {@link OracleContainer#FREE} service.
*
* @author Andy Wilkinson
*/
class OracleFreeJdbcDockerComposeConnectionDetailsFactory extends OracleJdbcDockerComposeConnectionDetailsFactory {
protected OracleFreeJdbcDockerComposeConnectionDetailsFactory() {
super(OracleContainer.FREE);
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link R2dbcConnectionDetails}
* for an {@link OracleContainer#FREE} service.
*
* @author Andy Wilkinson
*/
class OracleFreeR2dbcDockerComposeConnectionDetailsFactory extends OracleR2dbcDockerComposeConnectionDetailsFactory {
protected OracleFreeR2dbcDockerComposeConnectionDetailsFactory() {
super(OracleContainer.FREE);
}
}

View File

@ -23,27 +23,30 @@ import org.springframework.boot.docker.compose.service.connection.DockerComposeC
import org.springframework.util.StringUtils;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link JdbcConnectionDetails}
* for an {@code oracle-xe} service.
* Base class for a {@link DockerComposeConnectionDetailsFactory} to create
* {@link JdbcConnectionDetails} for an {@code oracle-free} or {@code oracle-xe} service.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
class OracleJdbcDockerComposeConnectionDetailsFactory
abstract class OracleJdbcDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<JdbcConnectionDetails> {
protected OracleJdbcDockerComposeConnectionDetailsFactory() {
super(OracleEnvironment.CONTAINER_NAMES);
private final String defaultDatabase;
protected OracleJdbcDockerComposeConnectionDetailsFactory(OracleContainer container) {
super(container.getImageName());
this.defaultDatabase = container.getDefaultDatabase();
}
@Override
protected JdbcConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new OracleJdbcDockerComposeConnectionDetails(source.getRunningService());
return new OracleJdbcDockerComposeConnectionDetails(source.getRunningService(), this.defaultDatabase);
}
/**
* {@link JdbcConnectionDetails} backed by an {@code oracle-xe}
* {@link JdbcConnectionDetails} backed by an {@code oracle-xe} or {@code oracle-free}
* {@link RunningService}.
*/
static class OracleJdbcDockerComposeConnectionDetails extends DockerComposeConnectionDetails
@ -55,9 +58,9 @@ class OracleJdbcDockerComposeConnectionDetailsFactory
private final String jdbcUrl;
OracleJdbcDockerComposeConnectionDetails(RunningService service) {
OracleJdbcDockerComposeConnectionDetails(RunningService service, String defaultDatabase) {
super(service);
this.environment = new OracleEnvironment(service.env());
this.environment = new OracleEnvironment(service.env(), defaultDatabase);
this.jdbcUrl = "jdbc:oracle:thin:@" + service.host() + ":" + service.ports().get(1521) + "/"
+ this.environment.getDatabase() + getParameters(service);
}

View File

@ -25,23 +25,26 @@ import org.springframework.boot.docker.compose.service.connection.DockerComposeC
import org.springframework.boot.docker.compose.service.connection.r2dbc.ConnectionFactoryOptionsBuilder;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link R2dbcConnectionDetails}
* for an {@code oracle-xe} service.
* Base class for a {@link DockerComposeConnectionDetailsFactory} to create
* {@link R2dbcConnectionDetails} for an {@code oracle-free} or {@code oracle-xe} service.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
class OracleR2dbcDockerComposeConnectionDetailsFactory
abstract class OracleR2dbcDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<R2dbcConnectionDetails> {
OracleR2dbcDockerComposeConnectionDetailsFactory() {
super(OracleEnvironment.CONTAINER_NAMES, "io.r2dbc.spi.ConnectionFactoryOptions");
private final String defaultDatabase;
OracleR2dbcDockerComposeConnectionDetailsFactory(OracleContainer container) {
super(container.getImageName(), "io.r2dbc.spi.ConnectionFactoryOptions");
this.defaultDatabase = container.getDefaultDatabase();
}
@Override
protected R2dbcConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new OracleDbR2dbcDockerComposeConnectionDetails(source.getRunningService());
return new OracleDbR2dbcDockerComposeConnectionDetails(source.getRunningService(), this.defaultDatabase);
}
/**
@ -56,9 +59,9 @@ class OracleR2dbcDockerComposeConnectionDetailsFactory
private final ConnectionFactoryOptions connectionFactoryOptions;
OracleDbR2dbcDockerComposeConnectionDetails(RunningService service) {
OracleDbR2dbcDockerComposeConnectionDetails(RunningService service, String defaultDatabase) {
super(service);
OracleEnvironment environment = new OracleEnvironment(service.env());
OracleEnvironment environment = new OracleEnvironment(service.env(), defaultDatabase);
this.connectionFactoryOptions = connectionFactoryOptionsBuilder.build(service, environment.getDatabase(),
environment.getUsername(), environment.getPassword());
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link JdbcConnectionDetails}
* for an {@link OracleContainer#XE} service.
*
* @author Andy Wilkinson
*/
class OracleXeJdbcDockerComposeConnectionDetailsFactory extends OracleJdbcDockerComposeConnectionDetailsFactory {
protected OracleXeJdbcDockerComposeConnectionDetailsFactory() {
super(OracleContainer.XE);
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link R2dbcConnectionDetails}
* for an {@link OracleContainer#XE} service.
*
* @author Andy Wilkinson
*/
class OracleXeR2dbcDockerComposeConnectionDetailsFactory extends OracleR2dbcDockerComposeConnectionDetailsFactory {
protected OracleXeR2dbcDockerComposeConnectionDetailsFactory() {
super(OracleContainer.XE);
}
}

View File

@ -16,8 +16,10 @@ org.springframework.boot.docker.compose.service.connection.mongo.MongoDockerComp
org.springframework.boot.docker.compose.service.connection.mysql.MySqlJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mysql.MySqlR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.neo4j.Neo4jDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryMetricsDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryTracingDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.postgres.PostgresJdbcDockerComposeConnectionDetailsFactory,\

View File

@ -35,77 +35,80 @@ class OracleEnvironmentTests {
@Test
void getUsernameWhenHasAppUser() {
OracleEnvironment environment = new OracleEnvironment(
Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret"));
Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret"), "defaultDb");
assertThat(environment.getUsername()).isEqualTo("alice");
}
@Test
void getUsernameWhenHasNoAppUser() {
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"));
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"), "defaultDb");
assertThat(environment.getUsername()).isEqualTo("system");
}
@Test
void getPasswordWhenHasAppPassword() {
OracleEnvironment environment = new OracleEnvironment(
Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret"));
Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret"), "defaultDb");
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasOraclePassword() {
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"));
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"), "defaultDb");
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void createWhenRandomPasswordAndAppPasswordDoesNotThrow() {
assertThatNoException().isThrownBy(() -> new OracleEnvironment(
Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret", "ORACLE_RANDOM_PASSWORD", "true")));
Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret", "ORACLE_RANDOM_PASSWORD", "true"),
"defaultDb"));
}
@Test
void createWhenRandomPasswordThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new OracleEnvironment(Map.of("ORACLE_RANDOM_PASSWORD", "true")))
.isThrownBy(() -> new OracleEnvironment(Map.of("ORACLE_RANDOM_PASSWORD", "true"), "defaultDb"))
.withMessage("ORACLE_RANDOM_PASSWORD is not supported without APP_USER and APP_USER_PASSWORD");
}
@Test
void createWhenAppUserAndNoAppPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice")))
assertThatIllegalStateException()
.isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice"), "defaultDb"))
.withMessage("No Oracle app password found");
}
@Test
void createWhenAppUserAndEmptyAppPasswordThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "")))
.isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice", "APP_USER_PASSWORD", ""), "defaultDb"))
.withMessage("No Oracle app password found");
}
@Test
void createWhenHasNoPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new OracleEnvironment(Collections.emptyMap()))
assertThatIllegalStateException().isThrownBy(() -> new OracleEnvironment(Collections.emptyMap(), "defaultDb"))
.withMessage("No Oracle password found");
}
@Test
void createWhenHasEmptyPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new OracleEnvironment(Map.of("ORACLE_PASSWORD", "")))
assertThatIllegalStateException()
.isThrownBy(() -> new OracleEnvironment(Map.of("ORACLE_PASSWORD", ""), "defaultDb"))
.withMessage("No Oracle password found");
}
@Test
void getDatabaseWhenHasNoOracleDatabase() {
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("xepdb1");
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"), "defaultDb");
assertThat(environment.getDatabase()).isEqualTo("defaultDb");
}
@Test
void getDatabaseWhenHasOracleDatabase() {
OracleEnvironment environment = new OracleEnvironment(
Map.of("ORACLE_PASSWORD", "secret", "ORACLE_DATABASE", "db"));
Map.of("ORACLE_PASSWORD", "secret", "ORACLE_DATABASE", "db"), "defaultDb");
assertThat(environment.getDatabase()).isEqualTo("db");
}

View File

@ -35,7 +35,7 @@ import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link OracleJdbcDockerComposeConnectionDetailsFactory}
* Integration tests for {@link OracleFreeJdbcDockerComposeConnectionDetailsFactory}
*
* @author Andy Wilkinson
*/
@ -54,7 +54,7 @@ class OracleFreeJdbcDockerComposeConnectionDetailsFactoryIntegrationTests
JdbcConnectionDetails connectionDetails = run(JdbcConnectionDetails.class);
assertThat(connectionDetails.getUsername()).isEqualTo("app_user");
assertThat(connectionDetails.getPassword()).isEqualTo("app_user_secret");
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:oracle:thin:@").endsWith("/xepdb1");
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:oracle:thin:@").endsWith("/freepdb1");
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setUrl(connectionDetails.getJdbcUrl());
dataSource.setUsername(connectionDetails.getUsername());

View File

@ -34,7 +34,7 @@ import org.springframework.r2dbc.core.DatabaseClient;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link OracleR2dbcDockerComposeConnectionDetailsFactory}
* Integration tests for {@link OracleFreeR2dbcDockerComposeConnectionDetailsFactory}
*
* @author Andy Wilkinson
*/
@ -51,7 +51,7 @@ class OracleFreeR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests
void runCreatesConnectionDetailsThatCanBeUsedToAccessDatabase() {
R2dbcConnectionDetails connectionDetails = run(R2dbcConnectionDetails.class);
ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
assertThat(connectionFactoryOptions.toString()).contains("database=xepdb1", "driver=oracle",
assertThat(connectionFactoryOptions.toString()).contains("database=freepdb1", "driver=oracle",
"password=REDACTED", "user=app_user");
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD))
.isEqualTo("app_user_secret");

View File

@ -35,7 +35,7 @@ import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link OracleJdbcDockerComposeConnectionDetailsFactory}
* Integration tests for {@link OracleXeJdbcDockerComposeConnectionDetailsFactory}
*
* @author Andy Wilkinson
*/

View File

@ -34,7 +34,7 @@ import org.springframework.r2dbc.core.DatabaseClient;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link OracleR2dbcDockerComposeConnectionDetailsFactory}
* Integration tests for {@link OracleXeR2dbcDockerComposeConnectionDetailsFactory}
*
* @author Andy Wilkinson
*/