From d61ba241b5fc3488973a77e26749f409f59333f1 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 24 Jan 2018 19:50:06 -0800 Subject: [PATCH] Move "testdb" naming to DataSourceProperties Move the "testdb" naming logic to `DataSourceProperties` and expose the `deduceDatabaseName` method so they can be used in auto-configuration. See gh-11719 --- .../jdbc/DataSourceHealthIndicatorTests.java | 7 +++--- .../jdbc/DataSourceProperties.java | 22 ++++++++++++++----- .../jdbc/EmbeddedDataSourceConfiguration.java | 10 +++------ .../jdbc/DataSourceJmxConfigurationTests.java | 9 ++++---- .../jdbc/DataSourcePropertiesTests.java | 5 ++--- .../boot/jdbc/EmbeddedDatabaseConnection.java | 15 ++++--------- .../jdbc/EmbeddedDatabaseConnectionTests.java | 12 +++++----- 7 files changed, 40 insertions(+), 40 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/jdbc/DataSourceHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/jdbc/DataSourceHealthIndicatorTests.java index 6fa0033b40b..22378d8b48b 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/jdbc/DataSourceHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/jdbc/DataSourceHealthIndicatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -50,9 +50,8 @@ public class DataSourceHealthIndicatorTests { @Before public void init() { EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQL; - this.dataSource = new SingleConnectionDataSource(db.getUrl( - EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME) + ";shutdown=true", - "sa", "", false); + this.dataSource = new SingleConnectionDataSource( + db.getUrl("testdb") + ";shutdown=true", "sa", "", false); this.dataSource.setDriverClassName(db.getDriverClassName()); } 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 b4d53ba31ff..63ff671b9fa 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 @@ -237,15 +237,12 @@ public class DataSourceProperties return this.driverClassName; } String driverClassName = null; - if (StringUtils.hasText(this.url)) { driverClassName = DatabaseDriver.fromJdbcUrl(this.url).getDriverClassName(); } - if (!StringUtils.hasText(driverClassName)) { driverClassName = this.embeddedDatabaseConnection.getDriverClassName(); } - if (!StringUtils.hasText(driverClassName)) { throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection, this.environment, "driver class"); @@ -289,7 +286,9 @@ public class DataSourceProperties if (StringUtils.hasText(this.url)) { return this.url; } - String url = this.embeddedDatabaseConnection.getUrl(determineDatabaseName()); + String databaseName = determineDatabaseName(); + String url = (databaseName == null ? null + : this.embeddedDatabaseConnection.getUrl(databaseName)); if (!StringUtils.hasText(url)) { throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection, this.environment, "url"); @@ -297,14 +296,25 @@ public class DataSourceProperties return url; } - private String determineDatabaseName() { + /** + * Determine the name to used based on this configuration. + * @return the database name to use or {@code null} + * @since 2.0.0 + */ + public String determineDatabaseName() { if (this.generateUniqueName) { if (this.uniqueName == null) { this.uniqueName = UUID.randomUUID().toString(); } return this.uniqueName; } - return this.name; + if (StringUtils.hasLength(this.name)) { + return this.name; + } + if (this.embeddedDatabaseConnection != EmbeddedDatabaseConnection.NONE) { + return "testdb"; + } + return null; } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java index 28da8949ca8..0914581a1b7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java @@ -25,7 +25,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; -import org.springframework.util.StringUtils; /** * Configuration for embedded data sources. @@ -56,12 +55,9 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware { @Bean public EmbeddedDatabase dataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() - .setType(EmbeddedDatabaseConnection.get(this.classLoader).getType()); - String name = (StringUtils.hasText(this.properties.getName()) - ? this.properties.getName() - : EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME); - this.database = builder.setName(name) - .generateUniqueName(this.properties.isGenerateUniqueName()).build(); + .setType(EmbeddedDatabaseConnection.get(this.classLoader).getType()) + .setName(this.properties.determineDatabaseName()); + this.database = builder.build(); return this.database; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java index e32ede73e81..6f74afab138 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java @@ -74,8 +74,8 @@ public class DataSourceJmxConfigurationTests { public void hikariAutoConfiguredWithoutDataSourceName() throws MalformedObjectNameException { MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); - Set existingInstances = mBeanServer.queryMBeans( - new ObjectName("com.zaxxer.hikari:type=*"), null); + Set existingInstances = mBeanServer + .queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null); load("spring.datasource.type=" + HikariDataSource.class.getName(), "spring.datasource.hikari.register-mbeans=true"); assertThat(this.context.getBeansOfType(HikariDataSource.class)).hasSize(1); @@ -83,8 +83,9 @@ public class DataSourceJmxConfigurationTests { .isTrue(); // We can rely on the number of MBeans so we're checking that the pool and pool // config mBeans were registered - assertThat(mBeanServer.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), - null).size()).isEqualTo(existingInstances.size() + 2); + assertThat(mBeanServer + .queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null).size()) + .isEqualTo(existingInstances.size() + 2); } @Test 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 44f11e4a520..f0d8dcbb9ce 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -62,8 +62,7 @@ public class DataSourcePropertiesTests { properties.afterPropertiesSet(); assertThat(properties.getUrl()).isNull(); assertThat(properties.determineUrl()) - .isEqualTo(EmbeddedDatabaseConnection.H2.getUrl( - EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME)); + .isEqualTo(EmbeddedDatabaseConnection.H2.getUrl("testdb")); } @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 d04451cb842..6232eec5f7f 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 @@ -25,8 +25,8 @@ import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcTemplate; 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}. @@ -60,11 +60,6 @@ public enum EmbeddedDatabaseConnection { */ HSQL(EmbeddedDatabaseType.HSQL, "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:%s"); - /** - * Default database name. - */ - public static final String DEFAULT_DATABASE_NAME = "testdb"; - private final EmbeddedDatabaseType type; private final String driverClass; @@ -95,15 +90,13 @@ public enum EmbeddedDatabaseConnection { } /** - * Returns the URL for the connection using the specified {@code databaseName} or - * {@value DEFAULT_DATABASE_NAME} if {@code databaseName} is empty or {@code null}. + * Returns the URL for the connection using the specified {@code databaseName}. * @param databaseName the name of the database * @return the connection URL */ public String getUrl(String databaseName) { - String name = (StringUtils.hasText(databaseName) - ? databaseName : DEFAULT_DATABASE_NAME); - return (this.url != null ? String.format(this.url, name) : null); + Assert.hasText(databaseName, "DatabaseName must not be empty"); + return (this.url != null ? String.format(this.url, databaseName) : null); } /** 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 741effdc984..73c9996c6bd 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -52,14 +52,16 @@ public class EmbeddedDatabaseConnectionTests { @Test public void getUrlWithNullDatabaseName() { - assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(null)) - .isEqualTo("jdbc:hsqldb:mem:testdb"); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("DatabaseName must not be empty"); + EmbeddedDatabaseConnection.HSQL.getUrl(null); } @Test public void getUrlWithEmptyDatabaseName() { - assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(" ")) - .isEqualTo("jdbc:hsqldb:mem:testdb"); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("DatabaseName must not be empty"); + EmbeddedDatabaseConnection.HSQL.getUrl(" "); } }