Fix NPE in url is null and no embedded database is available
Closes gh-10626
This commit is contained in:
parent
33ff2407e4
commit
81e33dc801
|
|
@ -106,7 +106,7 @@ public enum EmbeddedDatabaseConnection {
|
||||||
*/
|
*/
|
||||||
public String getUrl(String databaseName) {
|
public String getUrl(String databaseName) {
|
||||||
Assert.hasText(databaseName, "DatabaseName must not be null.");
|
Assert.hasText(databaseName, "DatabaseName must not be null.");
|
||||||
return String.format(this.url, databaseName);
|
return this.url != null ? String.format(this.url, databaseName) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.jdbc;
|
package org.springframework.boot.autoconfigure.jdbc;
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLClassLoader;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.Driver;
|
import java.sql.Driver;
|
||||||
import java.sql.DriverPropertyInfo;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,7 +16,9 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.jdbc;
|
package org.springframework.boot.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
|
@ -29,6 +31,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
public class DataSourcePropertiesTests {
|
public class DataSourcePropertiesTests {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void determineDriver() {
|
public void determineDriver() {
|
||||||
DataSourceProperties properties = new DataSourceProperties();
|
DataSourceProperties properties = new DataSourceProperties();
|
||||||
|
|
@ -57,6 +62,17 @@ public class DataSourcePropertiesTests {
|
||||||
.isEqualTo(EmbeddedDatabaseConnection.H2.getUrl());
|
.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
|
@Test
|
||||||
public void determineUrlWithExplicitConfig() throws Exception {
|
public void determineUrlWithExplicitConfig() throws Exception {
|
||||||
DataSourceProperties properties = new DataSourceProperties();
|
DataSourceProperties properties = new DataSourceProperties();
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue