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
This commit is contained in:
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");
|
||||
* 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());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -74,8 +74,8 @@ public class DataSourceJmxConfigurationTests {
|
|||
public void hikariAutoConfiguredWithoutDataSourceName()
|
||||
throws MalformedObjectNameException {
|
||||
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
Set<ObjectInstance> existingInstances = mBeanServer.queryMBeans(
|
||||
new ObjectName("com.zaxxer.hikari:type=*"), null);
|
||||
Set<ObjectInstance> 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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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(" ");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue