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
pull/11773/head
Phillip Webb 7 years ago
parent b67903a04a
commit d61ba241b5

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,9 +50,8 @@ public class DataSourceHealthIndicatorTests {
@Before @Before
public void init() { public void init() {
EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQL; EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQL;
this.dataSource = new SingleConnectionDataSource(db.getUrl( this.dataSource = new SingleConnectionDataSource(
EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME) + ";shutdown=true", db.getUrl("testdb") + ";shutdown=true", "sa", "", false);
"sa", "", false);
this.dataSource.setDriverClassName(db.getDriverClassName()); this.dataSource.setDriverClassName(db.getDriverClassName());
} }

@ -237,15 +237,12 @@ public class DataSourceProperties
return this.driverClassName; return this.driverClassName;
} }
String driverClassName = null; String driverClassName = null;
if (StringUtils.hasText(this.url)) { if (StringUtils.hasText(this.url)) {
driverClassName = DatabaseDriver.fromJdbcUrl(this.url).getDriverClassName(); driverClassName = DatabaseDriver.fromJdbcUrl(this.url).getDriverClassName();
} }
if (!StringUtils.hasText(driverClassName)) { if (!StringUtils.hasText(driverClassName)) {
driverClassName = this.embeddedDatabaseConnection.getDriverClassName(); driverClassName = this.embeddedDatabaseConnection.getDriverClassName();
} }
if (!StringUtils.hasText(driverClassName)) { if (!StringUtils.hasText(driverClassName)) {
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection, throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,
this.environment, "driver class"); this.environment, "driver class");
@ -289,7 +286,9 @@ public class DataSourceProperties
if (StringUtils.hasText(this.url)) { if (StringUtils.hasText(this.url)) {
return 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)) { if (!StringUtils.hasText(url)) {
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection, throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,
this.environment, "url"); this.environment, "url");
@ -297,15 +296,26 @@ public class DataSourceProperties
return url; 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.generateUniqueName) {
if (this.uniqueName == null) { if (this.uniqueName == null) {
this.uniqueName = UUID.randomUUID().toString(); this.uniqueName = UUID.randomUUID().toString();
} }
return this.uniqueName; return this.uniqueName;
} }
if (StringUtils.hasLength(this.name)) {
return this.name; return this.name;
} }
if (this.embeddedDatabaseConnection != EmbeddedDatabaseConnection.NONE) {
return "testdb";
}
return null;
}
/** /**
* Return the configured username or {@code null} if none was configured. * Return the configured username or {@code null} if none was configured.

@ -25,7 +25,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.util.StringUtils;
/** /**
* Configuration for embedded data sources. * Configuration for embedded data sources.
@ -56,12 +55,9 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
@Bean @Bean
public EmbeddedDatabase dataSource() { public EmbeddedDatabase dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType()); .setType(EmbeddedDatabaseConnection.get(this.classLoader).getType())
String name = (StringUtils.hasText(this.properties.getName()) .setName(this.properties.determineDatabaseName());
? this.properties.getName() this.database = builder.build();
: EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME);
this.database = builder.setName(name)
.generateUniqueName(this.properties.isGenerateUniqueName()).build();
return this.database; return this.database;
} }

@ -74,8 +74,8 @@ public class DataSourceJmxConfigurationTests {
public void hikariAutoConfiguredWithoutDataSourceName() public void hikariAutoConfiguredWithoutDataSourceName()
throws MalformedObjectNameException { throws MalformedObjectNameException {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectInstance> existingInstances = mBeanServer.queryMBeans( Set<ObjectInstance> existingInstances = mBeanServer
new ObjectName("com.zaxxer.hikari:type=*"), null); .queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null);
load("spring.datasource.type=" + HikariDataSource.class.getName(), load("spring.datasource.type=" + HikariDataSource.class.getName(),
"spring.datasource.hikari.register-mbeans=true"); "spring.datasource.hikari.register-mbeans=true");
assertThat(this.context.getBeansOfType(HikariDataSource.class)).hasSize(1); assertThat(this.context.getBeansOfType(HikariDataSource.class)).hasSize(1);
@ -83,8 +83,9 @@ public class DataSourceJmxConfigurationTests {
.isTrue(); .isTrue();
// We can rely on the number of MBeans so we're checking that the pool and pool // We can rely on the number of MBeans so we're checking that the pool and pool
// config mBeans were registered // config mBeans were registered
assertThat(mBeanServer.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), assertThat(mBeanServer
null).size()).isEqualTo(existingInstances.size() + 2); .queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null).size())
.isEqualTo(existingInstances.size() + 2);
} }
@Test @Test

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -62,8 +62,7 @@ public class DataSourcePropertiesTests {
properties.afterPropertiesSet(); properties.afterPropertiesSet();
assertThat(properties.getUrl()).isNull(); assertThat(properties.getUrl()).isNull();
assertThat(properties.determineUrl()) assertThat(properties.determineUrl())
.isEqualTo(EmbeddedDatabaseConnection.H2.getUrl( .isEqualTo(EmbeddedDatabaseConnection.H2.getUrl("testdb"));
EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME));
} }
@Test @Test

@ -25,8 +25,8 @@ import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/** /**
* Connection details for {@link EmbeddedDatabaseType embedded databases}. * Connection details for {@link EmbeddedDatabaseType embedded databases}.
@ -60,11 +60,6 @@ public enum EmbeddedDatabaseConnection {
*/ */
HSQL(EmbeddedDatabaseType.HSQL, "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:%s"); 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 EmbeddedDatabaseType type;
private final String driverClass; private final String driverClass;
@ -95,15 +90,13 @@ public enum EmbeddedDatabaseConnection {
} }
/** /**
* Returns the URL for the connection using the specified {@code databaseName} or * Returns the URL for the connection using the specified {@code databaseName}.
* {@value DEFAULT_DATABASE_NAME} if {@code databaseName} is empty or {@code null}.
* @param databaseName the name of the database * @param databaseName the name of the database
* @return the connection URL * @return the connection URL
*/ */
public String getUrl(String databaseName) { public String getUrl(String databaseName) {
String name = (StringUtils.hasText(databaseName) Assert.hasText(databaseName, "DatabaseName must not be empty");
? databaseName : DEFAULT_DATABASE_NAME); return (this.url != null ? String.format(this.url, databaseName) : null);
return (this.url != null ? String.format(this.url, name) : null);
} }
/** /**

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,14 +52,16 @@ public class EmbeddedDatabaseConnectionTests {
@Test @Test
public void getUrlWithNullDatabaseName() { public void getUrlWithNullDatabaseName() {
assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(null)) this.thrown.expect(IllegalArgumentException.class);
.isEqualTo("jdbc:hsqldb:mem:testdb"); this.thrown.expectMessage("DatabaseName must not be empty");
EmbeddedDatabaseConnection.HSQL.getUrl(null);
} }
@Test @Test
public void getUrlWithEmptyDatabaseName() { public void getUrlWithEmptyDatabaseName() {
assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(" ")) this.thrown.expect(IllegalArgumentException.class);
.isEqualTo("jdbc:hsqldb:mem:testdb"); this.thrown.expectMessage("DatabaseName must not be empty");
EmbeddedDatabaseConnection.HSQL.getUrl(" ");
} }
} }

Loading…
Cancel
Save