Skip to content

Commit 96f078e

Browse files
author
tinexw
committed
Fix NPE if no database url is given
Fixes gh-8690
1 parent f860281 commit 96f078e

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
import javax.sql.DataSource;
2626

27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
29+
2730
import org.springframework.beans.factory.BeanClassLoaderAware;
2831
import org.springframework.beans.factory.BeanCreationException;
2932
import org.springframework.beans.factory.InitializingBean;
@@ -46,11 +49,14 @@
4649
* @author Stephane Nicoll
4750
* @author Benedikt Ritter
4851
* @author Eddú Meléndez
52+
* @author Kristine Jetzke
4953
* @since 1.1.0
5054
*/
5155
@ConfigurationProperties(prefix = "spring.datasource")
5256
public class DataSourceProperties
53-
implements BeanClassLoaderAware, EnvironmentAware, InitializingBean {
57+
implements BeanClassLoaderAware, EnvironmentAware, InitializingBean {
58+
59+
private static final Log logger = LogFactory.getLog(DataSourceProperties.class);
5460

5561
private ClassLoader classLoader;
5662

@@ -288,6 +294,8 @@ public String determineUrl() {
288294
if (StringUtils.hasText(this.url)) {
289295
return this.url;
290296
}
297+
298+
logger.debug("No url defined, looking for embedded database.");
291299
String url = this.embeddedDatabaseConnection.getUrl(determineDatabaseName());
292300
if (!StringUtils.hasText(url)) {
293301
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2425

2526
/**
2627
* Tests for {@link DataSourceProperties}.
2728
*
2829
* @author Maciej Walkowiak
2930
* @author Stephane Nicoll
3031
* @author Eddú Meléndez
32+
* @author Kristine Jetzke
3133
*/
3234
public class DataSourcePropertiesTests {
3335

@@ -59,6 +61,14 @@ public void determineUrl() throws Exception {
5961
.isEqualTo(EmbeddedDatabaseConnection.H2.getUrl());
6062
}
6163

64+
@Test
65+
public void determineUrlUrlNull() throws Exception {
66+
DataSourceProperties properties = new DataSourceProperties();
67+
assertThatThrownBy(() -> properties.determineUrl())
68+
.isInstanceOf(DataSourceProperties.DataSourceBeanCreationException.class)
69+
.hasMessageContaining("Cannot determine embedded database url");
70+
}
71+
6272
@Test
6373
public void determineUrlWithExplicitConfig() throws Exception {
6474
DataSourceProperties properties = new DataSourceProperties();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @author Phillip Webb
3535
* @author Dave Syer
3636
* @author Stephane Nicoll
37+
* @author Kristine Jetzke
3738
* @see #get(ClassLoader)
3839
*/
3940
public enum EmbeddedDatabaseConnection {
@@ -106,7 +107,7 @@ public String getUrl() {
106107
*/
107108
public String getUrl(String databaseName) {
108109
Assert.hasText(databaseName, "DatabaseName must not be null.");
109-
return String.format(this.url, databaseName);
110+
return this.url != null ? String.format(this.url, databaseName) : null;
110111
}
111112

112113
/**

0 commit comments

Comments
 (0)