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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user