diff --git a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java index f731c7e990..fa2d96b66a 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java @@ -39,8 +39,9 @@ import static org.assertj.core.api.Assertions.assertThat; class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests { @DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL) - void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) { + void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException { assertConnectionDetails(connectionDetails); + checkDatabaseAccess(connectionDetails); } @DockerComposeTest(composeFile = "postgres-with-trust-host-auth-method-compose.yaml", image = TestImage.POSTGRESQL) @@ -53,8 +54,10 @@ class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests { } @DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL) - void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) { + void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) + throws ClassNotFoundException { assertConnectionDetails(connectionDetails); + checkDatabaseAccess(connectionDetails); } @DockerComposeTest(composeFile = "postgres-application-name-compose.yaml", image = TestImage.POSTGRESQL) diff --git a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java index 0fe577a1bb..b6e0e2e604 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java @@ -43,6 +43,7 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests { @DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL) void runCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) { assertConnectionDetails(connectionDetails); + checkDatabaseAccess(connectionDetails); } @DockerComposeTest(composeFile = "postgres-with-trust-host-auth-method-compose.yaml", image = TestImage.POSTGRESQL) @@ -59,6 +60,7 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests { @DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL) void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) { assertConnectionDetails(connectionDetails); + checkDatabaseAccess(connectionDetails); } @DockerComposeTest(composeFile = "postgres-application-name-compose.yaml", image = TestImage.POSTGRESQL) diff --git a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml index cb34245ec1..97eca6b3c8 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml +++ b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml @@ -4,6 +4,6 @@ services: ports: - '5432' environment: - - 'POSTGRESQL_USER=myuser' - - 'POSTGRESQL_DB=mydatabase' + - 'POSTGRESQL_USERNAME=myuser' + - 'POSTGRESQL_DATABASE=mydatabase' - 'POSTGRESQL_PASSWORD=secret' diff --git a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java index 0f0055b68d..220dbbf3e5 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java +++ b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -29,9 +29,18 @@ import org.springframework.util.StringUtils; * @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; @@ -39,9 +48,18 @@ class PostgresEnvironment { private final String database; PostgresEnvironment(Map env) { - this.username = env.getOrDefault("POSTGRES_USER", env.getOrDefault("POSTGRESQL_USER", "postgres")); + this.username = extract(env, USERNAME_KEYS, DEFAULT_USERNAME); this.password = extractPassword(env); - this.database = env.getOrDefault("POSTGRES_DB", env.getOrDefault("POSTGRESQL_DB", this.username)); + this.database = extract(env, DATABASE_KEYS, this.username); + } + + private String extract(Map env, String[] keys, String defaultValue) { + for (String key : keys) { + if (env.containsKey(key)) { + return env.get(key); + } + } + return defaultValue; } private String extractPassword(Map env) { diff --git a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java index 3a5cb93357..24ce50bc11 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException; * @author Phillip Webb * @author Scott Frederick * @author Sidmar Theodoro + * @author He Zean */ class PostgresEnvironmentTests { @@ -67,6 +68,13 @@ class PostgresEnvironmentTests { 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")); @@ -111,6 +119,13 @@ class PostgresEnvironmentTests { 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( @@ -125,4 +140,11 @@ class PostgresEnvironmentTests { 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"); + } + }