From 81e33dc801988068cef204a61b4c8e7d51f22fb7 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 25 Oct 2017 13:57:00 +0200 Subject: [PATCH] Fix NPE in url is null and no embedded database is available Closes gh-10626 --- .../jdbc/EmbeddedDatabaseConnection.java | 2 +- .../DataSourceAutoConfigurationTests.java | 24 ---------- .../jdbc/DataSourcePropertiesTests.java | 18 ++++++- .../jdbc/HidePackagesClassLoader.java | 48 +++++++++++++++++++ 4 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HidePackagesClassLoader.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDatabaseConnection.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDatabaseConnection.java index b3cb57e24f..924d78d00a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDatabaseConnection.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDatabaseConnection.java @@ -106,7 +106,7 @@ public enum EmbeddedDatabaseConnection { */ public String getUrl(String databaseName) { Assert.hasText(databaseName, "DatabaseName must not be null."); - return String.format(this.url, databaseName); + return this.url != null ? String.format(this.url, databaseName) : null; } /** diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java index a51a9118d3..2f558d2469 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.jdbc; -import java.net.URL; -import java.net.URLClassLoader; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverPropertyInfo; @@ -323,26 +321,4 @@ public class DataSourceAutoConfigurationTests { } - private static final class HidePackagesClassLoader extends URLClassLoader { - - private final String[] hiddenPackages; - - private HidePackagesClassLoader(String... hiddenPackages) { - super(new URL[0], DataSourceAutoConfigurationTests.class.getClassLoader()); - this.hiddenPackages = hiddenPackages; - } - - @Override - protected Class loadClass(String name, boolean resolve) - throws ClassNotFoundException { - for (String hiddenPackage : this.hiddenPackages) { - if (name.startsWith(hiddenPackage)) { - throw new ClassNotFoundException(); - } - } - return super.loadClass(name, resolve); - } - - } - } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java index b5c7eaaafb..bc6109787d 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -16,7 +16,9 @@ package org.springframework.boot.autoconfigure.jdbc; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; @@ -29,6 +31,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class DataSourcePropertiesTests { + @Rule + public final ExpectedException thrown = ExpectedException.none(); + @Test public void determineDriver() { DataSourceProperties properties = new DataSourceProperties(); @@ -57,6 +62,17 @@ public class DataSourcePropertiesTests { .isEqualTo(EmbeddedDatabaseConnection.H2.getUrl()); } + @Test + public void determineUrlWithNoEmbeddedSupport() throws Exception { + DataSourceProperties properties = new DataSourceProperties(); + properties.setBeanClassLoader(new HidePackagesClassLoader("org.h2", + "org.apache.derby", "org.hsqldb")); + properties.afterPropertiesSet(); + this.thrown.expect(DataSourceProperties.DataSourceBeanCreationException.class); + this.thrown.expectMessage("Cannot determine embedded database url"); + properties.determineUrl(); + } + @Test public void determineUrlWithExplicitConfig() throws Exception { DataSourceProperties properties = new DataSourceProperties(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HidePackagesClassLoader.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HidePackagesClassLoader.java new file mode 100644 index 0000000000..9b0351f948 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HidePackagesClassLoader.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jdbc; + +import java.net.URL; +import java.net.URLClassLoader; + +/** + * Test {@link URLClassLoader} that hides configurable packages. No class in one of those + * packages or sub-packages are visible. + * + * @author Stephane Nicoll + */ +final class HidePackagesClassLoader extends URLClassLoader { + + private final String[] hiddenPackages; + + HidePackagesClassLoader(String... hiddenPackages) { + super(new URL[0], DataSourceAutoConfigurationTests.class.getClassLoader()); + this.hiddenPackages = hiddenPackages; + } + + @Override + protected Class loadClass(String name, boolean resolve) + throws ClassNotFoundException { + for (String hiddenPackage : this.hiddenPackages) { + if (name.startsWith(hiddenPackage)) { + throw new ClassNotFoundException(); + } + } + return super.loadClass(name, resolve); + } + +}