Move JDBC Docker Compose support into spring-boot-jdbc

See gh-46085
This commit is contained in:
Phillip Webb 2025-05-19 19:25:25 -07:00 committed by Andy Wilkinson
parent fef1087e66
commit 720e503f7e
49 changed files with 1357 additions and 38 deletions

View File

@ -1,29 +1,22 @@
# Connection Details Factories
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
org.springframework.boot.docker.compose.service.connection.clickhouse.ClickHouseJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.clickhouse.ClickHouseR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.flyway.JdbcAdaptingFlywayConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.hazelcast.HazelcastDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.ldap.LLdapDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.ldap.OpenLdapDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.liquibase.JdbcAdaptingLiquibaseConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mongo.MongoDockerComposeConnectionDetailsFactory,\
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.OracleFreeJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryLoggingDockerComposeConnectionDetailsFactory,\
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,\
org.springframework.boot.docker.compose.service.connection.postgres.PostgresR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.pulsar.PulsarDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.redis.RedisDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.sqlserver.SqlServerJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.sqlserver.SqlServerR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.zipkin.ZipkinDockerComposeConnectionDetailsFactory

View File

@ -37,6 +37,7 @@ dependencies {
optional(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
optional(project(":spring-boot-project:spring-boot-autoconfigure"))
optional(project(":spring-boot-project:spring-boot-docker-compose"))
optional(project(":spring-boot-project:spring-boot-testcontainers"))
optional("com.h2database:h2")
optional("com.mchange:c3p0")
@ -54,9 +55,15 @@ dependencies {
dockerTestImplementation(project(":spring-boot-project:spring-boot-test"))
dockerTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support-docker"))
dockerTestImplementation(testFixtures(project(":spring-boot-project:spring-boot-docker-compose")))
dockerTestImplementation("org.testcontainers:junit-jupiter")
dockerTestImplementation("org.testcontainers:postgresql")
dockerTestRuntimeOnly("com.clickhouse:clickhouse-jdbc")
dockerTestRuntimeOnly("com.microsoft.sqlserver:mssql-jdbc")
dockerTestRuntimeOnly("org.lz4:lz4-java:1.8.0")
dockerTestRuntimeOnly("org.postgresql:postgresql")
testImplementation(project(":spring-boot-project:spring-boot-test"))
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
testImplementation(testFixtures(project(":spring-boot-project:spring-boot-autoconfigure")))

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.mariadb;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.mysql;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;

View File

@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '8123'
environment:
- 'CLICKHOUSE_USER=myuser'
- 'CLICKHOUSE_PASSWORD=secret'
- 'CLICKHOUSE_DB=mydatabase'

View File

@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '8123'
environment:
- 'CLICKHOUSE_USER=myuser'
- 'CLICKHOUSE_PASSWORD=secret'
- 'CLICKHOUSE_DB=mydatabase'

View File

@ -0,0 +1,10 @@
services:
database:
image: '{imageName}'
ports:
- '3306'
environment:
- 'MARIADB_ROOT_PASSWORD=verysecret'
- 'MARIADB_USER=myuser'
- 'MARIADB_PASSWORD=secret'
- 'MARIADB_DATABASE=mydatabase'

View File

@ -0,0 +1,11 @@
services:
database:
image: '{imageName}'
ports:
- '3306'
environment:
- 'MARIADB_ROOT_PASSWORD=verysecret'
- 'MARIADB_USER=myuser'
- 'MARIADB_PASSWORD=secret'
- 'MARIADB_DATABASE=mydatabase'

View File

@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '1433'
environment:
- 'MSSQL_PID=express'
- 'MSSQL_SA_PASSWORD=verYs3cret'
- 'ACCEPT_EULA=yes'

View File

@ -0,0 +1,11 @@
services:
database:
image: '{imageName}'
ports:
- '1433'
environment:
- 'MSSQL_PID=express'
- 'MSSQL_SA_PASSWORD=verYs3cret'
- 'ACCEPT_EULA=yes'
labels:
org.springframework.boot.jdbc.parameters: sendStringParametersAsUnicode=false

View File

@ -0,0 +1,10 @@
services:
database:
image: '{imageName}'
ports:
- '3306'
environment:
- 'MYSQL_ROOT_PASSWORD=verysecret'
- 'MYSQL_USER=myuser'
- 'MYSQL_PASSWORD=secret'
- 'MYSQL_DATABASE=mydatabase'

View File

@ -0,0 +1,10 @@
services:
database:
image: '{imageName}'
ports:
- '3306'
environment:
- 'MYSQL_ROOT_PASSWORD=verysecret'
- 'MYSQL_USER=myuser'
- 'MYSQL_PASSWORD=secret'
- 'MYSQL_DATABASE=mydatabase'

View File

@ -0,0 +1,15 @@
services:
database:
image: '{imageName}'
ports:
- '1521'
environment:
- 'APP_USER=app_user'
- 'APP_USER_PASSWORD=app_user_secret'
- 'ORACLE_PASSWORD=secret'
healthcheck:
test: ["CMD-SHELL", "healthcheck.sh"]
interval: 10s
timeout: 5s
retries: 10

View File

@ -0,0 +1,6 @@
services:
otlp:
image: '{imageName}'
ports:
- '4317'
- '4318'

View File

@ -0,0 +1,12 @@
services:
database:
image: '{imageName}'
ports:
- '5432'
environment:
- 'POSTGRES_USER=myuser'
- 'POSTGRES_DB=mydatabase'
- 'POSTGRES_PASSWORD=secret'
labels:
org.springframework.boot.jdbc.parameters: 'ApplicationName=spring+boot'
org.springframework.boot.r2dbc.parameters: 'applicationName=spring boot'

View File

@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '5432'
environment:
- 'POSTGRESQL_USERNAME=myuser'
- 'POSTGRESQL_DATABASE=mydatabase'
- 'POSTGRESQL_PASSWORD=secret'

View File

@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '5432'
environment:
- 'POSTGRES_USER=myuser'
- 'POSTGRES_DB=mydatabase'
- 'POSTGRES_PASSWORD=secret'

View File

@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '5432'
environment:
- 'POSTGRES_USER=myuser'
- 'POSTGRES_DB=mydatabase'
- 'POSTGRES_HOST_AUTH_METHOD=trust'

View File

@ -0,0 +1,62 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* ClickHouse environment details.
*
* @author Stephane Nicoll
*/
class ClickHouseEnvironment {
private final String username;
private final String password;
private final String database;
ClickHouseEnvironment(Map<String, String> env) {
this.username = env.getOrDefault("CLICKHOUSE_USER", "default");
this.password = extractPassword(env);
this.database = env.getOrDefault("CLICKHOUSE_DB", "default");
}
private String extractPassword(Map<String, String> env) {
boolean allowEmpty = env.containsKey("ALLOW_EMPTY_PASSWORD");
String password = env.get("CLICKHOUSE_PASSWORD");
Assert.state(StringUtils.hasLength(password) || allowEmpty, "No ClickHouse password found");
return (password != null) ? password : "";
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}
String getDatabase() {
return this.database;
}
}

View File

@ -14,12 +14,11 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.clickhouse;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;
/**

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.jdbc;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.util.Assert;
@ -26,9 +26,8 @@ import org.springframework.util.StringUtils;
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @since 3.1.0
*/
public class JdbcUrlBuilder {
class JdbcUrlBuilder {
private static final String PARAMETERS_LABEL = "org.springframework.boot.jdbc.parameters";
@ -41,7 +40,7 @@ public class JdbcUrlBuilder {
* @param driverProtocol the driver protocol
* @param containerPort the source container port
*/
public JdbcUrlBuilder(String driverProtocol, int containerPort) {
JdbcUrlBuilder(String driverProtocol, int containerPort) {
Assert.notNull(driverProtocol, "'driverProtocol' must not be null");
this.driverProtocol = driverProtocol;
this.containerPort = containerPort;
@ -52,7 +51,7 @@ public class JdbcUrlBuilder {
* @param service the running service
* @return a new JDBC URL
*/
public String build(RunningService service) {
String build(RunningService service) {
return build(service, null);
}
@ -62,7 +61,7 @@ public class JdbcUrlBuilder {
* @param database the database to connect to
* @return a new JDBC URL
*/
public String build(RunningService service, String database) {
String build(RunningService service, String database) {
return urlFor(service, database);
}

View File

@ -0,0 +1,84 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* MariaDB environment details.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MariaDbEnvironment {
private final String username;
private final String password;
private final String database;
MariaDbEnvironment(Map<String, String> env) {
this.username = extractUsername(env);
this.password = extractPassword(env);
this.database = extractDatabase(env);
}
private String extractUsername(Map<String, String> env) {
String user = env.get("MARIADB_USER");
return (user != null) ? user : env.getOrDefault("MYSQL_USER", "root");
}
private String extractPassword(Map<String, String> env) {
Assert.state(!env.containsKey("MARIADB_RANDOM_ROOT_PASSWORD"), "MARIADB_RANDOM_ROOT_PASSWORD is not supported");
Assert.state(!env.containsKey("MYSQL_RANDOM_ROOT_PASSWORD"), "MYSQL_RANDOM_ROOT_PASSWORD is not supported");
Assert.state(!env.containsKey("MARIADB_ROOT_PASSWORD_HASH"), "MARIADB_ROOT_PASSWORD_HASH is not supported");
boolean allowEmpty = env.containsKey("MARIADB_ALLOW_EMPTY_PASSWORD")
|| env.containsKey("MYSQL_ALLOW_EMPTY_PASSWORD") || env.containsKey("ALLOW_EMPTY_PASSWORD");
String password = env.get("MARIADB_PASSWORD");
password = (password != null) ? password : env.get("MYSQL_PASSWORD");
password = (password != null) ? password : env.get("MARIADB_ROOT_PASSWORD");
password = (password != null) ? password : env.get("MYSQL_ROOT_PASSWORD");
Assert.state(StringUtils.hasLength(password) || allowEmpty, "No MariaDB password found");
return (password != null) ? password : "";
}
private String extractDatabase(Map<String, String> env) {
String database = env.get("MARIADB_DATABASE");
database = (database != null) ? database : env.get("MYSQL_DATABASE");
Assert.state(database != null, "No MARIADB_DATABASE defined");
return database;
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}
String getDatabase() {
return this.database;
}
}

View File

@ -14,12 +14,11 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.mariadb;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;
/**

View File

@ -0,0 +1,73 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* MySQL environment details.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MySqlEnvironment {
private final String username;
private final String password;
private final String database;
MySqlEnvironment(Map<String, String> env) {
this.username = env.getOrDefault("MYSQL_USER", "root");
this.password = extractPassword(env);
this.database = extractDatabase(env);
}
private String extractPassword(Map<String, String> env) {
Assert.state(!env.containsKey("MYSQL_RANDOM_ROOT_PASSWORD"), "MYSQL_RANDOM_ROOT_PASSWORD is not supported");
boolean allowEmpty = env.containsKey("MYSQL_ALLOW_EMPTY_PASSWORD") || env.containsKey("ALLOW_EMPTY_PASSWORD");
String password = env.get("MYSQL_PASSWORD");
password = (password != null) ? password : env.get("MYSQL_ROOT_PASSWORD");
Assert.state(StringUtils.hasLength(password) || allowEmpty, "No MySQL password found");
return (password != null) ? password : "";
}
private String extractDatabase(Map<String, String> env) {
String database = env.get("MYSQL_DATABASE");
Assert.state(database != null, "No MYSQL_DATABASE defined");
return database;
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}
String getDatabase() {
return this.database;
}
}

View File

@ -14,12 +14,11 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.mysql;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;
/**

View File

@ -0,0 +1,47 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
/**
* 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

@ -0,0 +1,68 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Oracle Database environment details.
*
* @author Andy Wilkinson
*/
class OracleEnvironment {
private final String username;
private final String password;
private final String database;
OracleEnvironment(Map<String, String> env, String defaultDatabase) {
this.username = env.getOrDefault("APP_USER", "system");
this.password = extractPassword(env);
this.database = env.getOrDefault("ORACLE_DATABASE", defaultDatabase);
}
private String extractPassword(Map<String, String> env) {
if (env.containsKey("APP_USER")) {
String password = env.get("APP_USER_PASSWORD");
Assert.state(StringUtils.hasLength(password), "No Oracle app password found");
return password;
}
Assert.state(!env.containsKey("ORACLE_RANDOM_PASSWORD"),
"ORACLE_RANDOM_PASSWORD is not supported without APP_USER and APP_USER_PASSWORD");
String password = env.get("ORACLE_PASSWORD");
Assert.state(StringUtils.hasLength(password), "No Oracle password found");
return password;
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}
String getDatabase() {
return this.database;
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;

View File

@ -0,0 +1,92 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Postgres environment details.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Sidmar Theodoro
* @author He Zean
*/
class PostgresEnvironment {
private static final String[] USERNAME_KEYS = new String[] { "POSTGRES_USER", "POSTGRESQL_USER",
"POSTGRESQL_USERNAME" };
private static final String DEFAULT_USERNAME = "postgres";
private static final String[] DATABASE_KEYS = new String[] { "POSTGRES_DB", "POSTGRESQL_DB",
"POSTGRESQL_DATABASE" };
private final String username;
private final String password;
private final String database;
PostgresEnvironment(Map<String, String> env) {
this.username = extract(env, USERNAME_KEYS, DEFAULT_USERNAME);
this.password = extractPassword(env);
this.database = extract(env, DATABASE_KEYS, this.username);
}
private String extract(Map<String, String> env, String[] keys, String defaultValue) {
for (String key : keys) {
if (env.containsKey(key)) {
return env.get(key);
}
}
return defaultValue;
}
private String extractPassword(Map<String, String> env) {
if (isUsingTrustHostAuthMethod(env)) {
return null;
}
String password = env.getOrDefault("POSTGRES_PASSWORD", env.get("POSTGRESQL_PASSWORD"));
boolean allowEmpty = env.containsKey("ALLOW_EMPTY_PASSWORD");
Assert.state(allowEmpty || StringUtils.hasLength(password), "No PostgreSQL password found");
return (password != null) ? password : "";
}
private boolean isUsingTrustHostAuthMethod(Map<String, String> env) {
String hostAuthMethod = env.get("POSTGRES_HOST_AUTH_METHOD");
return "trust".equals(hostAuthMethod);
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}
String getDatabase() {
return this.database;
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.postgres;
package org.springframework.boot.jdbc.docker.compose;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

View File

@ -0,0 +1,54 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* MS SQL Server environment details.
*
* @author Andy Wilkinson
*/
class SqlServerEnvironment {
private final String username = "SA";
private final String password;
SqlServerEnvironment(Map<String, String> env) {
this.password = extractPassword(env);
}
private String extractPassword(Map<String, String> env) {
String password = env.get("MSSQL_SA_PASSWORD");
password = (password != null) ? password : env.get("SA_PASSWORD");
Assert.state(StringUtils.hasLength(password), "No MSSQL password found");
return password;
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}
}

View File

@ -14,12 +14,11 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.sqlserver;
package org.springframework.boot.jdbc.docker.compose;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder;
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;
/**

View File

@ -15,7 +15,6 @@
*/
/**
* Utilities to help when creating
* {@link org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails}.
* Support for Docker Compose JDBC service connections.
*/
package org.springframework.boot.docker.compose.service.connection.jdbc;
package org.springframework.boot.jdbc.docker.compose;

View File

@ -5,6 +5,13 @@ org.springframework.boot.jdbc.autoconfigure.HikariDriverConfigurationFailureAnal
# Connection Details Factories
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
org.springframework.boot.jdbc.docker.compose.ClickHouseJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.jdbc.docker.compose.MariaDbJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.jdbc.docker.compose.MySqlJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.jdbc.docker.compose.OracleFreeJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.jdbc.docker.compose.OracleXeJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.jdbc.docker.compose.PostgresJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.jdbc.docker.compose.SqlServerJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.jdbc.testcontainers.JdbcContainerConnectionDetailsFactory
# Database Initializer Detectors

View File

@ -0,0 +1,84 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link ClickHouseEnvironment}.
*
* @author Stephane Nicoll
*/
class ClickHouseEnvironmentTests {
@Test
void createWhenNoPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new ClickHouseEnvironment(Collections.emptyMap()))
.withMessage("No ClickHouse password found");
}
@Test
void getPasswordWhenHasPassword() {
ClickHouseEnvironment environment = new ClickHouseEnvironment(Map.of("CLICKHOUSE_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
ClickHouseEnvironment environment = new ClickHouseEnvironment(Map.of("ALLOW_EMPTY_PASSWORD", "true"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPasswordIsYes() {
ClickHouseEnvironment environment = new ClickHouseEnvironment(Map.of("ALLOW_EMPTY_PASSWORD", "yes"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getUsernameWhenNoUser() {
ClickHouseEnvironment environment = new ClickHouseEnvironment(Map.of("CLICKHOUSE_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("default");
}
@Test
void getUsernameWhenHasUser() {
ClickHouseEnvironment environment = new ClickHouseEnvironment(
Map.of("CLICKHOUSE_USER", "me", "CLICKHOUSE_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}
@Test
void getDatabaseWhenNoDatabase() {
ClickHouseEnvironment environment = new ClickHouseEnvironment(Map.of("CLICKHOUSE_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("default");
}
@Test
void getDatabaseWhenHasDatabase() {
ClickHouseEnvironment environment = new ClickHouseEnvironment(
Map.of("CLICKHOUSE_DB", "db", "CLICKHOUSE_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.jdbc;
package org.springframework.boot.jdbc.docker.compose;
import java.util.Collections;
import java.util.Map;

View File

@ -0,0 +1,176 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link MariaDbEnvironment}.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Jinseong Hwang
* @author Scott Frederick
*/
class MariaDbEnvironmentTests {
@Test
void createWhenHasMariadbRandomRootPasswordThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new MariaDbEnvironment(Map.of("MARIADB_RANDOM_ROOT_PASSWORD", "true")))
.withMessage("MARIADB_RANDOM_ROOT_PASSWORD is not supported");
}
@Test
void createWhenHasMysqlRandomRootPasswordThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new MariaDbEnvironment(Map.of("MYSQL_RANDOM_ROOT_PASSWORD", "true")))
.withMessage("MYSQL_RANDOM_ROOT_PASSWORD is not supported");
}
@Test
void createWhenHasMariadbRootPasswordHashThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new MariaDbEnvironment(Map.of("MARIADB_ROOT_PASSWORD_HASH", "0FF")))
.withMessage("MARIADB_ROOT_PASSWORD_HASH is not supported");
}
@Test
void createWhenHasNoPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new MariaDbEnvironment(Collections.emptyMap()))
.withMessage("No MariaDB password found");
}
@Test
void createWhenHasNoDatabaseThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new MariaDbEnvironment(Map.of("MARIADB_PASSWORD", "secret")))
.withMessage("No MARIADB_DATABASE defined");
}
@Test
void getUsernameWhenHasMariadbUser() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_USER", "myself", "MARIADB_PASSWORD", "secret", "MARIADB_DATABASE", "db"));
assertThat(environment.getUsername()).isEqualTo("myself");
}
@Test
void getUsernameWhenHasMysqlUser() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MYSQL_USER", "myself", "MARIADB_PASSWORD", "secret", "MARIADB_DATABASE", "db"));
assertThat(environment.getUsername()).isEqualTo("myself");
}
@Test
void getUsernameWhenHasMariadbUserAndMysqlUser() {
MariaDbEnvironment environment = new MariaDbEnvironment(Map.of("MARIADB_USER", "myself", "MYSQL_USER", "me",
"MARIADB_PASSWORD", "secret", "MARIADB_DATABASE", "db"));
assertThat(environment.getUsername()).isEqualTo("myself");
}
@Test
void getUsernameWhenHasNoMariadbUserOrMysqlUser() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_PASSWORD", "secret", "MARIADB_DATABASE", "db"));
assertThat(environment.getUsername()).isEqualTo("root");
}
@Test
void getPasswordWhenHasMariadbPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_PASSWORD", "secret", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasMysqlPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MYSQL_PASSWORD", "secret", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasMysqlRootPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MYSQL_ROOT_PASSWORD", "secret", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasMariadbPasswordAndMysqlPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_PASSWORD", "secret", "MYSQL_PASSWORD", "donttell", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasMariadbPasswordAndMysqlRootPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_PASSWORD", "secret", "MYSQL_ROOT_PASSWORD", "donttell", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("ALLOW_EMPTY_PASSWORD", "true", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getPasswordWhenHasNoPasswordAndMariadbAllowEmptyPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_ALLOW_EMPTY_PASSWORD", "true", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getPasswordWhenHasNoPasswordAndMysqlAllowEmptyPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MYSQL_ALLOW_EMPTY_PASSWORD", "true", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getDatabaseWhenHasMariadbDatabase() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_ALLOW_EMPTY_PASSWORD", "true", "MARIADB_DATABASE", "db"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
@Test
void getDatabaseWhenHasMysqlDatabase() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_ALLOW_EMPTY_PASSWORD", "true", "MYSQL_DATABASE", "db"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
@Test
void getDatabaseWhenHasMariadbAndMysqlDatabase() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("MARIADB_ALLOW_EMPTY_PASSWORD", "true", "MARIADB_DATABASE", "db", "MYSQL_DATABASE", "otherdb"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
}

View File

@ -0,0 +1,104 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link MySqlEnvironment}.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Jinseong Hwang
* @author Scott Frederick
*/
class MySqlEnvironmentTests {
@Test
void createWhenHasMysqlRandomRootPasswordThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new MySqlEnvironment(Map.of("MYSQL_RANDOM_ROOT_PASSWORD", "true")))
.withMessage("MYSQL_RANDOM_ROOT_PASSWORD is not supported");
}
@Test
void createWhenHasNoPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new MySqlEnvironment(Collections.emptyMap()))
.withMessage("No MySQL password found");
}
@Test
void createWhenHasNoDatabaseThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new MySqlEnvironment(Map.of("MYSQL_PASSWORD", "secret")))
.withMessage("No MYSQL_DATABASE defined");
}
@Test
void getUsernameWhenHasMysqlUser() {
MySqlEnvironment environment = new MySqlEnvironment(
Map.of("MYSQL_USER", "myself", "MYSQL_PASSWORD", "secret", "MYSQL_DATABASE", "db"));
assertThat(environment.getUsername()).isEqualTo("myself");
}
@Test
void getUsernameWhenHasNoMysqlUser() {
MySqlEnvironment environment = new MySqlEnvironment(Map.of("MYSQL_PASSWORD", "secret", "MYSQL_DATABASE", "db"));
assertThat(environment.getUsername()).isEqualTo("root");
}
@Test
void getPasswordWhenHasMysqlPassword() {
MySqlEnvironment environment = new MySqlEnvironment(Map.of("MYSQL_PASSWORD", "secret", "MYSQL_DATABASE", "db"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasMysqlRootPassword() {
MySqlEnvironment environment = new MySqlEnvironment(
Map.of("MYSQL_ROOT_PASSWORD", "secret", "MYSQL_DATABASE", "db"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasNoPasswordAndMysqlAllowEmptyPassword() {
MySqlEnvironment environment = new MySqlEnvironment(
Map.of("MYSQL_ALLOW_EMPTY_PASSWORD", "true", "MYSQL_DATABASE", "db"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
MySqlEnvironment environment = new MySqlEnvironment(
Map.of("ALLOW_EMPTY_PASSWORD", "true", "MYSQL_DATABASE", "db"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getDatabaseWhenHasMysqlDatabase() {
MySqlEnvironment environment = new MySqlEnvironment(
Map.of("MYSQL_ALLOW_EMPTY_PASSWORD", "true", "MYSQL_DATABASE", "db"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
}

View File

@ -0,0 +1,115 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for {@link OracleEnvironment}.
*
* @author Andy Wilkinson
*/
class OracleEnvironmentTests {
@Test
void getUsernameWhenHasAppUser() {
OracleEnvironment environment = new OracleEnvironment(
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"), "defaultDb");
assertThat(environment.getUsername()).isEqualTo("system");
}
@Test
void getPasswordWhenHasAppPassword() {
OracleEnvironment environment = new OracleEnvironment(
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"), "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"),
"defaultDb"));
}
@Test
void createWhenRandomPasswordThrowsException() {
assertThatIllegalStateException()
.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"), "defaultDb"))
.withMessage("No Oracle app password found");
}
@Test
void createWhenAppUserAndEmptyAppPasswordThrowsException() {
assertThatIllegalStateException()
.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(), "defaultDb"))
.withMessage("No Oracle password found");
}
@Test
void createWhenHasEmptyPasswordThrowsException() {
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"), "defaultDb");
assertThat(environment.getDatabase()).isEqualTo("defaultDb");
}
@Test
void getDatabaseWhenHasOracleDatabase() {
OracleEnvironment environment = new OracleEnvironment(
Map.of("ORACLE_PASSWORD", "secret", "ORACLE_DATABASE", "db"), "defaultDb");
assertThat(environment.getDatabase()).isEqualTo("db");
}
}

View File

@ -0,0 +1,156 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link PostgresEnvironment}.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Sidmar Theodoro
* @author He Zean
*/
class PostgresEnvironmentTests {
@Test
void createWhenNoPostgresPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new PostgresEnvironment(Collections.emptyMap()))
.withMessage("No PostgreSQL password found");
}
@Test
void getUsernameWhenNoPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("postgres");
}
@Test
void getUsernameWhenNoPostgresqlUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("postgres");
}
@Test
void getUsernameWhenHasPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_USER", "me", "POSTGRES_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}
@Test
void getUsernameWhenHasPostgresqlUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USER", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}
@Test
void getUsernameWhenHasPostgresqlUsername() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}
@Test
void getPasswordWhenHasPostgresPassword() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasPostgresqlPassword() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasTrustHostAuthMethod() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_HOST_AUTH_METHOD", "trust"));
assertThat(environment.getPassword()).isNull();
}
@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("ALLOW_EMPTY_PASSWORD", "yes"));
assertThat(environment.getPassword()).isEmpty();
}
@Test
void getDatabaseWhenNoPostgresDbOrPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("postgres");
}
@Test
void getDatabaseWhenNoPostgresqlDbOrPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("postgres");
}
@Test
void getDatabaseWhenNoPostgresDbAndPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_USER", "me", "POSTGRES_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("me");
}
@Test
void getDatabaseWhenNoPostgresqlDbAndPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USER", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("me");
}
@Test
void getDatabaseWhenNoPostgresqlDatabaseAndPostgresqlUsername() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("me");
}
@Test
void getDatabaseWhenHasPostgresDb() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_DB", "db", "POSTGRES_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
@Test
void getDatabaseWhenHasPostgresqlDb() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_DB", "db", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
@Test
void getDatabaseWhenHasPostgresqlDatabase() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2012-present 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.jdbc.docker.compose;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link SqlServerEnvironment}.
*
* @author Andy Wilkinson
*/
class SqlServerEnvironmentTests {
@Test
void createWhenHasNoPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new SqlServerEnvironment(Collections.emptyMap()))
.withMessage("No MSSQL password found");
}
@Test
void getUsernameWhenHasNoMsSqlUser() {
SqlServerEnvironment environment = new SqlServerEnvironment(Map.of("MSSQL_SA_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("SA");
}
@Test
void getPasswordWhenHasMsSqlSaPassword() {
SqlServerEnvironment environment = new SqlServerEnvironment(Map.of("MSSQL_SA_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasSaPassword() {
SqlServerEnvironment environment = new SqlServerEnvironment(Map.of("SA_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasMsSqlSaPasswordAndSaPasswordPrefersMsSqlSaPassword() {
SqlServerEnvironment environment = new SqlServerEnvironment(
Map.of("MSSQL_SA_PASSWORD", "secret", "SA_PASSWORD", "not used"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
}