Refactor logic from DataSourceProperties
Introduce dedicate method to determine the actual value to use based on the configuration and the environment. Closes gh-1436
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -117,9 +117,9 @@ public class DataSourceAutoConfiguration {
|
||||
public DataSource dataSource() {
|
||||
DataSourceBuilder factory = DataSourceBuilder
|
||||
.create(this.properties.getClassLoader())
|
||||
.driverClassName(this.properties.getDriverClassName())
|
||||
.url(this.properties.getUrl()).username(this.properties.getUsername())
|
||||
.password(this.properties.getPassword());
|
||||
.driverClassName(this.properties.determineDriverClassName())
|
||||
.url(this.properties.determineUrl()).username(this.properties.determineUsername())
|
||||
.password(this.properties.determinePassword());
|
||||
if (this.properties.getType() != null) {
|
||||
factory.type(this.properties.getType());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -160,7 +160,25 @@ public class DataSourceProperties
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured driver or {@code null} if none was configured.
|
||||
* @return the configured driver
|
||||
* @see #determineDriverClassName()
|
||||
*/
|
||||
public String getDriverClassName() {
|
||||
return this.driverClassName;
|
||||
}
|
||||
|
||||
public void setDriverClassName(String driverClassName) {
|
||||
this.driverClassName = driverClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the driver to use based on this configuration and the environment.
|
||||
* @return the driver to use
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public String determineDriverClassName() {
|
||||
if (StringUtils.hasText(this.driverClassName)) {
|
||||
Assert.state(driverClassIsLoadable(),
|
||||
"Cannot load driver class: " + this.driverClassName);
|
||||
@@ -197,11 +215,25 @@ public class DataSourceProperties
|
||||
}
|
||||
}
|
||||
|
||||
public void setDriverClassName(String driverClassName) {
|
||||
this.driverClassName = driverClassName;
|
||||
/**
|
||||
* Return the configured url or {@code null} if none was configured.
|
||||
* @return the configured url
|
||||
* @see #determineUrl()
|
||||
*/
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the url to use based on this configuration and the environment.
|
||||
* @return the url to use
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public String determineUrl() {
|
||||
if (StringUtils.hasText(this.url)) {
|
||||
return this.url;
|
||||
}
|
||||
@@ -213,38 +245,63 @@ public class DataSourceProperties
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured username or {@code null} if none was configured.
|
||||
* @return the configured username
|
||||
* @see #determineUsername()
|
||||
*/
|
||||
public String getUsername() {
|
||||
if (StringUtils.hasText(this.username)) {
|
||||
return this.username;
|
||||
}
|
||||
if (EmbeddedDatabaseConnection.isEmbedded(getDriverClassName())) {
|
||||
return "sa";
|
||||
}
|
||||
return null;
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
if (StringUtils.hasText(this.password)) {
|
||||
return this.password;
|
||||
/**
|
||||
* Determine the username to use based on this configuration and the environment.
|
||||
* @return the username to use
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public String determineUsername() {
|
||||
if (StringUtils.hasText(this.username)) {
|
||||
return this.username;
|
||||
}
|
||||
if (EmbeddedDatabaseConnection.isEmbedded(getDriverClassName())) {
|
||||
return "";
|
||||
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
|
||||
return "sa";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured password or {@code null} if none was configured.
|
||||
* @return the configured password
|
||||
* @see #determinePassword()
|
||||
*/
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine the password to use based on this configuration and the environment.
|
||||
* @return the password to use
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public String determinePassword() {
|
||||
if (StringUtils.hasText(this.password)) {
|
||||
return this.password;
|
||||
}
|
||||
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
|
||||
return "";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getJndiName() {
|
||||
return this.jndiName;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -83,7 +83,7 @@ public class XADataSourceAutoConfiguration implements BeanClassLoaderAware {
|
||||
private XADataSource createXaDataSource() {
|
||||
String className = this.properties.getXa().getDataSourceClassName();
|
||||
if (!StringUtils.hasLength(className)) {
|
||||
className = DatabaseDriver.fromJdbcUrl(this.properties.getUrl())
|
||||
className = DatabaseDriver.fromJdbcUrl(this.properties.determineUrl())
|
||||
.getXaDataSourceClassName();
|
||||
}
|
||||
Assert.state(StringUtils.hasLength(className),
|
||||
@@ -108,9 +108,9 @@ public class XADataSourceAutoConfiguration implements BeanClassLoaderAware {
|
||||
|
||||
private void bindXaProperties(XADataSource target, DataSourceProperties properties) {
|
||||
MutablePropertyValues values = new MutablePropertyValues();
|
||||
values.add("user", this.properties.getUsername());
|
||||
values.add("password", this.properties.getPassword());
|
||||
values.add("url", this.properties.getUrl());
|
||||
values.add("user", this.properties.determineUsername());
|
||||
values.add("password", this.properties.determinePassword());
|
||||
values.add("url", this.properties.determineUrl());
|
||||
values.addPropertyValues(properties.getXa().getProperties());
|
||||
new RelaxedDataBinder(target).withAlias("user", "username").bind(values);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -19,29 +19,82 @@ package org.springframework.boot.autoconfigure.jdbc;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataSourceProperties}.
|
||||
*
|
||||
* @author Maciej Walkowiak
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DataSourcePropertiesTests {
|
||||
|
||||
@Test
|
||||
public void correctDriverClassNameFromJdbcUrlWhenDriverClassNameNotDefined() {
|
||||
DataSourceProperties configuration = new DataSourceProperties();
|
||||
configuration.setUrl("jdbc:mysql://mydb");
|
||||
String driverClassName = configuration.getDriverClassName();
|
||||
assertEquals(driverClassName, "com.mysql.jdbc.Driver");
|
||||
public void determineDriver() {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.setUrl("jdbc:mysql://mydb");
|
||||
assertNull(properties.getDriverClassName());
|
||||
assertEquals("com.mysql.jdbc.Driver", properties.determineDriverClassName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void driverClassNameFromDriverClassNamePropertyWhenDefined() {
|
||||
DataSourceProperties configuration = new DataSourceProperties();
|
||||
configuration.setUrl("jdbc:mysql://mydb");
|
||||
configuration.setDriverClassName("org.hsqldb.jdbcDriver");
|
||||
String driverClassName = configuration.getDriverClassName();
|
||||
assertEquals(driverClassName, "org.hsqldb.jdbcDriver");
|
||||
public void determineDriverWithExplicitConfig() {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.setUrl("jdbc:mysql://mydb");
|
||||
properties.setDriverClassName("org.hsqldb.jdbcDriver");
|
||||
assertEquals("org.hsqldb.jdbcDriver", properties.getDriverClassName());
|
||||
assertEquals("org.hsqldb.jdbcDriver", properties.determineDriverClassName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void determineUrl() throws Exception {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.afterPropertiesSet();
|
||||
assertNull(properties.getUrl());
|
||||
assertEquals(EmbeddedDatabaseConnection.H2.getUrl(), properties.determineUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void determineUrlWithExplicitConfig() throws Exception {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.setUrl("jdbc:mysql://mydb");
|
||||
properties.afterPropertiesSet();
|
||||
assertEquals("jdbc:mysql://mydb", properties.getUrl());
|
||||
assertEquals("jdbc:mysql://mydb", properties.determineUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void determineUsername() throws Exception {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.afterPropertiesSet();
|
||||
assertNull(properties.getUsername());
|
||||
assertEquals("sa", properties.determineUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void determineUsernameWithExplicitConfig() throws Exception {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.setUsername("foo");
|
||||
properties.afterPropertiesSet();
|
||||
assertEquals("foo", properties.getUsername());
|
||||
assertEquals("foo", properties.determineUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void determinePassword() throws Exception {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.afterPropertiesSet();
|
||||
assertNull(properties.getPassword());
|
||||
assertEquals("", properties.determinePassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void determinePasswordWithExplicitConfig() throws Exception {
|
||||
DataSourceProperties properties = new DataSourceProperties();
|
||||
properties.setPassword("bar");
|
||||
properties.afterPropertiesSet();
|
||||
assertEquals("bar", properties.getPassword());
|
||||
assertEquals("bar", properties.determinePassword());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -146,9 +146,10 @@ public class TomcatDataSourceConfigurationTests {
|
||||
public DataSource dataSource() {
|
||||
DataSourceBuilder factory = DataSourceBuilder
|
||||
.create(this.properties.getClassLoader())
|
||||
.driverClassName(this.properties.getDriverClassName())
|
||||
.url(this.properties.getUrl()).username(this.properties.getUsername())
|
||||
.password(this.properties.getPassword())
|
||||
.driverClassName(this.properties.determineDriverClassName())
|
||||
.url(this.properties.determineUrl())
|
||||
.username(this.properties.determineUsername())
|
||||
.password(this.properties.determinePassword())
|
||||
.type(org.apache.tomcat.jdbc.pool.DataSource.class);
|
||||
return factory.build();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user