H2ConsoleAutoConfigurationTests.java (spring-boot-2.7.3) | : | H2ConsoleAutoConfigurationTests.java (spring-boot-2.7.4) | ||
---|---|---|---|---|
/* | /* | |||
* Copyright 2012-2021 the original author or authors. | * Copyright 2012-2022 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. | |||
* You may obtain a copy of the License at | * You may obtain a copy of the License at | |||
* | * | |||
* https://www.apache.org/licenses/LICENSE-2.0 | * https://www.apache.org/licenses/LICENSE-2.0 | |||
* | * | |||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | |||
* limitations under the License. | * limitations under the License. | |||
*/ | */ | |||
package org.springframework.boot.autoconfigure.h2; | package org.springframework.boot.autoconfigure.h2; | |||
import java.net.URL; | ||||
import java.net.URLClassLoader; | ||||
import java.sql.Connection; | import java.sql.Connection; | |||
import java.sql.DatabaseMetaData; | import java.sql.DatabaseMetaData; | |||
import java.sql.SQLException; | import java.sql.SQLException; | |||
import javax.sql.DataSource; | import javax.sql.DataSource; | |||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | |||
import org.junit.jupiter.api.extension.ExtendWith; | import org.junit.jupiter.api.extension.ExtendWith; | |||
import org.mockito.invocation.InvocationOnMock; | ||||
import org.mockito.stubbing.Answer; | ||||
import org.springframework.beans.factory.BeanCreationException; | import org.springframework.beans.factory.BeanCreationException; | |||
import org.springframework.boot.autoconfigure.AutoConfigurations; | import org.springframework.boot.autoconfigure.AutoConfigurations; | |||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | |||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; | import org.springframework.boot.test.context.runner.WebApplicationContextRunner; | |||
import org.springframework.boot.test.system.CapturedOutput; | import org.springframework.boot.test.system.CapturedOutput; | |||
import org.springframework.boot.test.system.OutputCaptureExtension; | import org.springframework.boot.test.system.OutputCaptureExtension; | |||
import org.springframework.boot.web.servlet.ServletRegistrationBean; | import org.springframework.boot.web.servlet.ServletRegistrationBean; | |||
import org.springframework.context.annotation.Bean; | import org.springframework.context.annotation.Bean; | |||
import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | |||
skipping to change at line 140 | skipping to change at line 144 | |||
@ExtendWith(OutputCaptureExtension.class) | @ExtendWith(OutputCaptureExtension.class) | |||
void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) { | void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) { | |||
this.contextRunner.withUserConfiguration(FailingDataSourceConfigu ration.class) | this.contextRunner.withUserConfiguration(FailingDataSourceConfigu ration.class) | |||
.withPropertyValues("spring.h2.console.enabled=tr ue") | .withPropertyValues("spring.h2.console.enabled=tr ue") | |||
.run((context) -> assertThat(output).doesNotConta in("H2 console available")); | .run((context) -> assertThat(output).doesNotConta in("H2 console available")); | |||
} | } | |||
@Test | @Test | |||
@ExtendWith(OutputCaptureExtension.class) | @ExtendWith(OutputCaptureExtension.class) | |||
void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput outpu t) { | void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput outpu t) { | |||
this.contextRunner | ClassLoader webAppClassLoader = new URLClassLoader(new URL[0]); | |||
this.contextRunner.withClassLoader(webAppClassLoader) | ||||
.withUserConfiguration(FailingDataSourceConfigura tion.class, MultiDataSourceConfiguration.class) | .withUserConfiguration(FailingDataSourceConfigura tion.class, MultiDataSourceConfiguration.class) | |||
.withPropertyValues("spring.h2.console.enabled=tr ue").run((context) -> assertThat(output).contains( | .withPropertyValues("spring.h2.console.enabled=tr ue").run((context) -> assertThat(output).contains( | |||
"H2 console available at '/h2-con sole'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'")); | "H2 console available at '/h2-con sole'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'")); | |||
} | } | |||
@Test | @Test | |||
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() { | void h2ConsoleShouldNotFailIfDatabaseConnectionFails() { | |||
this.contextRunner.withUserConfiguration(FailingDataSourceConfigu ration.class) | this.contextRunner.withUserConfiguration(FailingDataSourceConfigu ration.class) | |||
.withPropertyValues("spring.h2.console.enabled=tr ue") | .withPropertyValues("spring.h2.console.enabled=tr ue") | |||
.run((context) -> assertThat(context.isRunning()) .isTrue()); | .run((context) -> assertThat(context.isRunning()) .isTrue()); | |||
skipping to change at line 182 | skipping to change at line 187 | |||
} | } | |||
@Bean | @Bean | |||
@Order(0) | @Order(0) | |||
DataSource someDataSource() throws SQLException { | DataSource someDataSource() throws SQLException { | |||
return mockDataSource("someJdbcUrl"); | return mockDataSource("someJdbcUrl"); | |||
} | } | |||
private DataSource mockDataSource(String url) throws SQLException { | private DataSource mockDataSource(String url) throws SQLException { | |||
DataSource dataSource = mock(DataSource.class); | DataSource dataSource = mock(DataSource.class); | |||
given(dataSource.getConnection()).willReturn(mock(Connect | given(dataSource.getConnection()).will(new Answer<Connect | |||
ion.class)); | ion>() { | |||
given(dataSource.getConnection().getMetaData()).willRetur | ||||
n(mock(DatabaseMetaData.class)); | @Override | |||
given(dataSource.getConnection().getMetaData().getURL()). | public Connection answer(InvocationOnMock invocat | |||
willReturn(url); | ion) throws Throwable { | |||
assertThat(Thread.currentThread().getCont | ||||
extClassLoader()).isEqualTo(getClass().getClassLoader()); | ||||
Connection connection = mock(Connection.c | ||||
lass); | ||||
DatabaseMetaData metadata = mock(Database | ||||
MetaData.class); | ||||
given(connection.getMetaData()).willRetur | ||||
n(metadata); | ||||
given(metadata.getURL()).willReturn(url); | ||||
return connection; | ||||
} | ||||
}); | ||||
return dataSource; | return dataSource; | |||
} | } | |||
} | } | |||
} | } | |||
End of changes. 5 change blocks. | ||||
8 lines changed or deleted | 27 lines changed or added |