Skip to content

Commit 81e33dc

Browse files
committed
Fix NPE in url is null and no embedded database is available
Closes gh-10626
1 parent 33ff240 commit 81e33dc

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public String getUrl() {
106106
*/
107107
public String getUrl(String databaseName) {
108108
Assert.hasText(databaseName, "DatabaseName must not be null.");
109-
return String.format(this.url, databaseName);
109+
return this.url != null ? String.format(this.url, databaseName) : null;
110110
}
111111

112112
/**

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.jdbc;
1818

19-
import java.net.URL;
20-
import java.net.URLClassLoader;
2119
import java.sql.Connection;
2220
import java.sql.Driver;
2321
import java.sql.DriverPropertyInfo;
@@ -323,26 +321,4 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
323321

324322
}
325323

326-
private static final class HidePackagesClassLoader extends URLClassLoader {
327-
328-
private final String[] hiddenPackages;
329-
330-
private HidePackagesClassLoader(String... hiddenPackages) {
331-
super(new URL[0], DataSourceAutoConfigurationTests.class.getClassLoader());
332-
this.hiddenPackages = hiddenPackages;
333-
}
334-
335-
@Override
336-
protected Class<?> loadClass(String name, boolean resolve)
337-
throws ClassNotFoundException {
338-
for (String hiddenPackage : this.hiddenPackages) {
339-
if (name.startsWith(hiddenPackage)) {
340-
throw new ClassNotFoundException();
341-
}
342-
}
343-
return super.loadClass(name, resolve);
344-
}
345-
346-
}
347-
348324
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.boot.autoconfigure.jdbc;
1818

19+
import org.junit.Rule;
1920
import org.junit.Test;
21+
import org.junit.rules.ExpectedException;
2022

2123
import static org.assertj.core.api.Assertions.assertThat;
2224

@@ -29,6 +31,9 @@
2931
*/
3032
public class DataSourcePropertiesTests {
3133

34+
@Rule
35+
public final ExpectedException thrown = ExpectedException.none();
36+
3237
@Test
3338
public void determineDriver() {
3439
DataSourceProperties properties = new DataSourceProperties();
@@ -57,6 +62,17 @@ public void determineUrl() throws Exception {
5762
.isEqualTo(EmbeddedDatabaseConnection.H2.getUrl());
5863
}
5964

65+
@Test
66+
public void determineUrlWithNoEmbeddedSupport() throws Exception {
67+
DataSourceProperties properties = new DataSourceProperties();
68+
properties.setBeanClassLoader(new HidePackagesClassLoader("org.h2",
69+
"org.apache.derby", "org.hsqldb"));
70+
properties.afterPropertiesSet();
71+
this.thrown.expect(DataSourceProperties.DataSourceBeanCreationException.class);
72+
this.thrown.expectMessage("Cannot determine embedded database url");
73+
properties.determineUrl();
74+
}
75+
6076
@Test
6177
public void determineUrlWithExplicitConfig() throws Exception {
6278
DataSourceProperties properties = new DataSourceProperties();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jdbc;
18+
19+
import java.net.URL;
20+
import java.net.URLClassLoader;
21+
22+
/**
23+
* Test {@link URLClassLoader} that hides configurable packages. No class in one of those
24+
* packages or sub-packages are visible.
25+
*
26+
* @author Stephane Nicoll
27+
*/
28+
final class HidePackagesClassLoader extends URLClassLoader {
29+
30+
private final String[] hiddenPackages;
31+
32+
HidePackagesClassLoader(String... hiddenPackages) {
33+
super(new URL[0], DataSourceAutoConfigurationTests.class.getClassLoader());
34+
this.hiddenPackages = hiddenPackages;
35+
}
36+
37+
@Override
38+
protected Class<?> loadClass(String name, boolean resolve)
39+
throws ClassNotFoundException {
40+
for (String hiddenPackage : this.hiddenPackages) {
41+
if (name.startsWith(hiddenPackage)) {
42+
throw new ClassNotFoundException();
43+
}
44+
}
45+
return super.loadClass(name, resolve);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)