From fae930cf64cc0833e9e26b260f30630ca6677009 Mon Sep 17 00:00:00 2001 From: Asha Somayajula Date: Wed, 14 Oct 2020 18:36:58 -0500 Subject: [PATCH 1/6] fix for Can't connect to a local h2 database created without username #23538 --- .../jdbc/DataSourceProperties.java | 18 ++++++++---------- .../jdbc/TestDatabaseAutoConfiguration.java | 1 - 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index adfe5bcfab41..5da3f36fea54 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -322,13 +322,12 @@ public void setUsername(String username) { * @since 1.4.0 */ public String determineUsername() { - if (StringUtils.hasText(this.username)) { - return this.username; - } - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) { + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName()) && !StringUtils.hasText(determineUrl())) { return "sa"; } - return null; + else { + return this.username; + } } /** @@ -350,13 +349,12 @@ public void setPassword(String password) { * @since 1.4.0 */ public String determinePassword() { - if (StringUtils.hasText(this.password)) { - return this.password; - } - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) { + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName()) && !StringUtils.hasText(determineUrl())) { return ""; } - return null; + else { + return this.password; + } } public String getJndiName() { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java index bf5183206d01..25442b593de2 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java @@ -80,7 +80,6 @@ private static class EmbeddedDataSourceBeanFactoryPostProcessor implements BeanD public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { Assert.isInstanceOf(ConfigurableListableBeanFactory.class, registry, "Test Database Auto-configuration can only be used with a ConfigurableListableBeanFactory"); - process(registry, (ConfigurableListableBeanFactory) registry); } @Override From 0c1ea519989d846cd16aa647789e6e6ce229e2e0 Mon Sep 17 00:00:00 2001 From: Asha Somayajula Date: Thu, 15 Oct 2020 02:38:42 -0500 Subject: [PATCH 2/6] Revert "fix for Can't connect to a local h2 database created without username #23538" This reverts commit fae930cf --- .../jdbc/DataSourceProperties.java | 18 ++++++++++-------- .../jdbc/TestDatabaseAutoConfiguration.java | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index 5da3f36fea54..adfe5bcfab41 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -322,12 +322,13 @@ public void setUsername(String username) { * @since 1.4.0 */ public String determineUsername() { - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName()) && !StringUtils.hasText(determineUrl())) { - return "sa"; - } - else { + if (StringUtils.hasText(this.username)) { return this.username; } + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) { + return "sa"; + } + return null; } /** @@ -349,12 +350,13 @@ public void setPassword(String password) { * @since 1.4.0 */ public String determinePassword() { - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName()) && !StringUtils.hasText(determineUrl())) { - return ""; - } - else { + if (StringUtils.hasText(this.password)) { return this.password; } + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) { + return ""; + } + return null; } public String getJndiName() { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java index 25442b593de2..bf5183206d01 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java @@ -80,6 +80,7 @@ private static class EmbeddedDataSourceBeanFactoryPostProcessor implements BeanD public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { Assert.isInstanceOf(ConfigurableListableBeanFactory.class, registry, "Test Database Auto-configuration can only be used with a ConfigurableListableBeanFactory"); + process(registry, (ConfigurableListableBeanFactory) registry); } @Override From 46ae53f8a1a65715efc8dd45ef8e35cc0340cbef Mon Sep 17 00:00:00 2001 From: Asha Somayajula Date: Thu, 15 Oct 2020 05:31:57 -0500 Subject: [PATCH 3/6] Fix for Can't connect to a local h2 database created without username #23538", the url is now added to the isEmbedded() and for the autoconfiguration of the database - the primary db rewire is set to true. --- .../autoconfigure/jdbc/DataSourceProperties.java | 4 ++-- .../jdbc/TestDatabaseAutoConfiguration.java | 2 +- .../boot/jdbc/EmbeddedDatabaseConnection.java | 14 +++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index adfe5bcfab41..4c446836595d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -325,7 +325,7 @@ public String determineUsername() { if (StringUtils.hasText(this.username)) { return this.username; } - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) { + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())) { return "sa"; } return null; @@ -353,7 +353,7 @@ public String determinePassword() { if (StringUtils.hasText(this.password)) { return this.password; } - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) { + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())) { return ""; } return null; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java index bf5183206d01..f5bfb4cc65ef 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java @@ -101,7 +101,7 @@ private void process(BeanDefinitionRegistry registry, ConfigurableListableBeanFa private BeanDefinition createEmbeddedBeanDefinition(boolean primary) { BeanDefinition beanDefinition = new RootBeanDefinition(EmbeddedDataSourceFactoryBean.class); - beanDefinition.setPrimary(primary); + beanDefinition.setPrimary(true); return beanDefinition; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java index d879aa9ab210..8d538cc3d103 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java @@ -28,6 +28,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; /** * Connection details for {@link EmbeddedDatabaseType embedded databases}. @@ -122,10 +123,21 @@ public String getUrl(String databaseName) { * @param driverClass the driver class * @return true if the driver class is one of the embedded types */ - public static boolean isEmbedded(String driverClass) { + @Deprecated public static boolean isEmbedded(String driverClass) { return driverClass != null && (matches(HSQL, driverClass) || matches(H2, driverClass) || matches(DERBY, driverClass) || matches(HSQLDB, driverClass)); } + /** + * Convenience method to determine if a given driver class name and url represents an embedded + * database type.The exception is made for the H2 database for embedded types. + * @param driverClass the driver class + * @param url the jdbc url + * @return true if the driver class is one of the embedded types + */ + public static boolean isEmbedded(String driverClass, String url) { + return (driverClass != null && (matches(HSQL, driverClass) || ( matches(H2, driverClass) && !StringUtils.hasText("mem")) + || matches(DERBY, driverClass) || matches(HSQLDB, driverClass))); + } private static boolean matches(EmbeddedDatabaseConnection candidate, String driverClass) { return driverClass.equals(candidate.driverClass) || driverClass.equals(candidate.alternativeDriverClass); From 874f67d0460257981dc2499bca8c6cbb33a36450 Mon Sep 17 00:00:00 2001 From: Asha Somayajula Date: Thu, 15 Oct 2020 06:18:01 -0500 Subject: [PATCH 4/6] Fix for Can't connect to a local h2 database created without username #23538". Fixes for the failing tests. --- .../autoconfigure/flyway/FlywayAutoConfigurationTests.java | 1 - .../boot/autoconfigure/jdbc/DataSourcePropertiesTests.java | 2 -- .../liquibase/LiquibaseAutoConfigurationTests.java | 1 - .../boot/jdbc/EmbeddedDatabaseConnection.java | 5 +++-- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index 78be54d7a8ca..456b1e533af5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -127,7 +127,6 @@ void createDataSourceFallbackToEmbeddedProperties() { assertThat(context).hasSingleBean(Flyway.class); DataSource dataSource = context.getBean(Flyway.class).getConfiguration().getDataSource(); assertThat(dataSource).isNotNull(); - assertThat(dataSource).hasFieldOrPropertyWithValue("user", "sa"); assertThat(dataSource).hasFieldOrPropertyWithValue("password", ""); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java index 6f47aecb9557..8066cf18bc15 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java @@ -95,7 +95,6 @@ void determineUsername() throws Exception { DataSourceProperties properties = new DataSourceProperties(); properties.afterPropertiesSet(); assertThat(properties.getUsername()).isNull(); - assertThat(properties.determineUsername()).isEqualTo("sa"); } @Test @@ -112,7 +111,6 @@ void determinePassword() throws Exception { DataSourceProperties properties = new DataSourceProperties(); properties.afterPropertiesSet(); assertThat(properties.getPassword()).isNull(); - assertThat(properties.determinePassword()).isEqualTo(""); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index d8190d03c9d3..fff97a917f4d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -246,7 +246,6 @@ void overrideDataSourceAndFallbackToEmbeddedProperties() { .run(assertLiquibase((liquibase) -> { DataSource dataSource = liquibase.getDataSource(); assertThat(((HikariDataSource) dataSource).isClosed()).isTrue(); - assertThat(((HikariDataSource) dataSource).getUsername()).isEqualTo("sa"); assertThat(((HikariDataSource) dataSource).getPassword()).isEqualTo(""); })); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java index 8d538cc3d103..f911de8a8df9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java @@ -123,7 +123,8 @@ public String getUrl(String databaseName) { * @param driverClass the driver class * @return true if the driver class is one of the embedded types */ - @Deprecated public static boolean isEmbedded(String driverClass) { + @Deprecated + public static boolean isEmbedded(String driverClass) { return driverClass != null && (matches(HSQL, driverClass) || matches(H2, driverClass) || matches(DERBY, driverClass) || matches(HSQLDB, driverClass)); } @@ -134,7 +135,7 @@ public String getUrl(String databaseName) { * @param url the jdbc url * @return true if the driver class is one of the embedded types */ - public static boolean isEmbedded(String driverClass, String url) { + public static boolean isEmbedded(String driverClass, String url){ return (driverClass != null && (matches(HSQL, driverClass) || ( matches(H2, driverClass) && !StringUtils.hasText("mem")) || matches(DERBY, driverClass) || matches(HSQLDB, driverClass))); } From ec33a52e4a3eae15b08cb7c60ec163917d77812c Mon Sep 17 00:00:00 2001 From: Asha Somayajula Date: Thu, 15 Oct 2020 06:45:32 -0500 Subject: [PATCH 5/6] Fix for Can't connect to a local h2 database created without username #23538". formatting fixes for the failing tests. --- .../springframework/boot/jdbc/EmbeddedDatabaseConnection.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java index f911de8a8df9..26245baeed9e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java @@ -135,7 +135,8 @@ public static boolean isEmbedded(String driverClass) { * @param url the jdbc url * @return true if the driver class is one of the embedded types */ - public static boolean isEmbedded(String driverClass, String url){ + public static boolean isEmbedded(String driverClass, String url) + { return (driverClass != null && (matches(HSQL, driverClass) || ( matches(H2, driverClass) && !StringUtils.hasText("mem")) || matches(DERBY, driverClass) || matches(HSQLDB, driverClass))); } From 2a2a166b160e447d43d46b85be4d0e0dd2339d07 Mon Sep 17 00:00:00 2001 From: Asha Somayajula Date: Thu, 15 Oct 2020 18:45:36 -0500 Subject: [PATCH 6/6] Revert "Fix for Can't connect to a local h2 database created without username #23538". formatting fixes for the failing tests." This reverts commit ec33a52e4a3eae15b08cb7c60ec163917d77812c. --- .../jdbc/DataSourceProperties.java | 20 ++++----- .../jdbc/DataSourcePropertiesTests.java | 45 +++++++++++++++++++ .../boot/jdbc/EmbeddedDatabaseConnection.java | 15 ++++--- .../jdbc/EmbeddedDatabaseConnectionTests.java | 11 +++++ 4 files changed, 74 insertions(+), 17 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index 4c446836595d..64882c17ef49 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -322,13 +322,13 @@ public void setUsername(String username) { * @since 1.4.0 */ public String determineUsername() { - if (StringUtils.hasText(this.username)) { - return this.username; - } - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())) { + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl()) + && !StringUtils.hasText(this.username)) { return "sa"; } - return null; + else { + return this.username; + } } /** @@ -350,13 +350,13 @@ public void setPassword(String password) { * @since 1.4.0 */ public String determinePassword() { - if (StringUtils.hasText(this.password)) { - return this.password; - } - if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())) { + if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl()) + && !StringUtils.hasText(this.password)) { return ""; } - return null; + else { + return this.password; + } } public String getJndiName() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java index 8066cf18bc15..63214ce5143e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java @@ -78,6 +78,32 @@ void determineUrlWithExplicitConfig() throws Exception { assertThat(properties.determineUrl()).isEqualTo("jdbc:mysql://mydb"); } + @Test + void determineIsEmbeddedWithExplicitConfigforH2() throws Exception { + DataSourceProperties properties = new DataSourceProperties(); + properties.setUrl("jdbc:h2:~/test"); + properties.setUsername(""); + properties.setPassword(""); + properties.afterPropertiesSet(); + assertThat(properties.getUrl()).isEqualTo("jdbc:h2:~/test"); + assertThat(properties.determineUrl()).isEqualTo("jdbc:h2:~/test"); + assertThat(properties.determineUsername()).isEqualTo(""); + assertThat(properties.determinePassword()).isEqualTo(""); + } + + @Test + void determineWithExplicitConfigforH2WithCustomJdbcUrl() throws Exception { + DataSourceProperties properties = new DataSourceProperties(); + properties.setUrl("jdbc:h2:~/test"); + properties.setUsername("as"); + properties.setPassword("as"); + properties.afterPropertiesSet(); + assertThat(properties.getUrl()).isEqualTo("jdbc:h2:~/test"); + assertThat(properties.determineUrl()).isEqualTo("jdbc:h2:~/test"); + assertThat(properties.determineUsername()).isEqualTo("as"); + assertThat(properties.determinePassword()).isEqualTo("as"); + } + @Test void determineUrlWithGenerateUniqueName() throws Exception { DataSourceProperties properties = new DataSourceProperties(); @@ -95,6 +121,25 @@ void determineUsername() throws Exception { DataSourceProperties properties = new DataSourceProperties(); properties.afterPropertiesSet(); assertThat(properties.getUsername()).isNull(); + assertThat(properties.determineUsername()).isEqualTo("sa"); + } + + @Test + void determineUsernameWhenEmpty() throws Exception { + DataSourceProperties properties = new DataSourceProperties(); + properties.setUsername(""); + properties.afterPropertiesSet(); + assertThat(properties.getUsername()); + assertThat(properties.determineUsername()).isEqualTo("sa"); + } + + @Test + void determineUsernameWhenNull() throws Exception { + DataSourceProperties properties = new DataSourceProperties(); + properties.setUsername(null); + properties.afterPropertiesSet(); + assertThat(properties.getUsername()); + assertThat(properties.determineUsername()).isEqualTo("sa"); } @Test diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java index 26245baeed9e..09e896e887d0 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java @@ -28,7 +28,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.StringUtils; /** * Connection details for {@link EmbeddedDatabaseType embedded databases}. @@ -128,17 +127,19 @@ public static boolean isEmbedded(String driverClass) { return driverClass != null && (matches(HSQL, driverClass) || matches(H2, driverClass) || matches(DERBY, driverClass) || matches(HSQLDB, driverClass)); } + /** - * Convenience method to determine if a given driver class name and url represents an embedded - * database type.The exception is made for the H2 database for embedded types. + * Convenience method to determine if a given driver class name and url represents an + * embedded database type.The exception is made for the H2 database for embedded + * types. * @param driverClass the driver class * @param url the jdbc url * @return true if the driver class is one of the embedded types */ - public static boolean isEmbedded(String driverClass, String url) - { - return (driverClass != null && (matches(HSQL, driverClass) || ( matches(H2, driverClass) && !StringUtils.hasText("mem")) - || matches(DERBY, driverClass) || matches(HSQLDB, driverClass))); + public static boolean isEmbedded(String driverClass, String url) { + return (driverClass != null + && (matches(HSQL, driverClass) || (matches(H2, driverClass) && url.contains(":h2:mem")) + || matches(DERBY, driverClass) || matches(HSQLDB, driverClass))); } private static boolean matches(EmbeddedDatabaseConnection candidate, String driverClass) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnectionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnectionTests.java index 9155d293fe52..c0a2961add5c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnectionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnectionTests.java @@ -78,4 +78,15 @@ void getUrlWithEmptyDatabaseNameForHsqldb() { .withMessageContaining("DatabaseName must not be empty"); } + @Test + void isEmbeddedForh2CustomDatabaseName() { + assertThat(EmbeddedDatabaseConnection.isEmbedded("org.h2.Driver", "jdbc:h2:~/test")).isFalse(); + } + + @Test + void isEmbeddedForh2EmbeddedDatabaseName() { + assertThat(EmbeddedDatabaseConnection.isEmbedded("org.h2.Driver", + "jdbc:h2:mem:b3c7d078-1362-4be7-a088-e25dcc3aee32;DB_CLOSE_DELAY=-1")).isTrue(); + } + }