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:
Phillip Webb
2018-01-24 19:50:06 -08:00
parent b67903a04a
commit d61ba241b5
7 changed files with 40 additions and 40 deletions

View File

@@ -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);
}
/**

View File

@@ -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(" ");
}
}