Skip to content

Commit a800f7d

Browse files
committed
Merge branch '2.6.x' into 2.7.x
Closes gh-32406
2 parents 7827e81 + 61e11cd commit a800f7d

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,22 @@ public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties propert
6565
ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping);
6666
configureH2ConsoleSettings(registration, properties.getSettings());
6767
if (logger.isInfoEnabled()) {
68-
logDataSources(dataSource, path);
68+
withThreadContextClassLoader(getClass().getClassLoader(), () -> logDataSources(dataSource, path));
6969
}
7070
return registration;
7171
}
7272

73+
private void withThreadContextClassLoader(ClassLoader classLoader, Runnable action) {
74+
ClassLoader previous = Thread.currentThread().getContextClassLoader();
75+
try {
76+
Thread.currentThread().setContextClassLoader(classLoader);
77+
action.run();
78+
}
79+
finally {
80+
Thread.currentThread().setContextClassLoader(previous);
81+
}
82+
}
83+
7384
private void logDataSources(ObjectProvider<DataSource> dataSource, String path) {
7485
List<String> urls = dataSource.orderedStream().map((available) -> {
7586
try (Connection connection = available.getConnection()) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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,6 +16,8 @@
1616

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

19+
import java.net.URL;
20+
import java.net.URLClassLoader;
1921
import java.sql.Connection;
2022
import java.sql.DatabaseMetaData;
2123
import java.sql.SQLException;
@@ -24,6 +26,8 @@
2426

2527
import org.junit.jupiter.api.Test;
2628
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.mockito.invocation.InvocationOnMock;
30+
import org.mockito.stubbing.Answer;
2731

2832
import org.springframework.beans.factory.BeanCreationException;
2933
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -137,7 +141,8 @@ void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) {
137141
@Test
138142
@ExtendWith(OutputCaptureExtension.class)
139143
void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) {
140-
this.contextRunner
144+
ClassLoader webAppClassLoader = new URLClassLoader(new URL[0]);
145+
this.contextRunner.withClassLoader(webAppClassLoader)
141146
.withUserConfiguration(FailingDataSourceConfiguration.class, MultiDataSourceConfiguration.class)
142147
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> assertThat(output).contains(
143148
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
@@ -179,9 +184,20 @@ DataSource someDataSource() throws SQLException {
179184

180185
private DataSource mockDataSource(String url) throws SQLException {
181186
DataSource dataSource = mock(DataSource.class);
182-
given(dataSource.getConnection()).willReturn(mock(Connection.class));
183-
given(dataSource.getConnection().getMetaData()).willReturn(mock(DatabaseMetaData.class));
184-
given(dataSource.getConnection().getMetaData().getURL()).willReturn(url);
187+
given(dataSource.getConnection()).will(new Answer<Connection>() {
188+
189+
@Override
190+
public Connection answer(InvocationOnMock invocation) throws Throwable {
191+
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(getClass().getClassLoader());
192+
Connection connection = mock(Connection.class);
193+
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
194+
given(connection.getMetaData()).willReturn(metadata);
195+
given(metadata.getURL()).willReturn(url);
196+
return connection;
197+
}
198+
199+
});
200+
185201
return dataSource;
186202
}
187203

0 commit comments

Comments
 (0)