Merge branch '3.3.x'

Closes gh-43270
This commit is contained in:
Andy Wilkinson
2024-11-22 15:40:51 +00:00
28 changed files with 184 additions and 129 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@@ -51,32 +51,35 @@ abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
@Test
void singleManuallyConfiguredDataSourceIsNotClosed() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext(SingleDataSourceConfiguration.class));
DataSource dataSource = context.getBean(DataSource.class);
Statement statement = configureDataSourceBehavior(dataSource);
then(statement).should(never()).execute("SHUTDOWN");
}
@Test
void multipleDataSourcesAreIgnored() throws Exception {
ConfigurableApplicationContext context = getContext(
() -> createContext(MultipleDataSourcesConfiguration.class));
Collection<DataSource> dataSources = context.getBeansOfType(DataSource.class).values();
for (DataSource dataSource : dataSources) {
try (ConfigurableApplicationContext context = getContext(
() -> createContext(SingleDataSourceConfiguration.class))) {
DataSource dataSource = context.getBean(DataSource.class);
Statement statement = configureDataSourceBehavior(dataSource);
then(statement).should(never()).execute("SHUTDOWN");
}
}
@Test
void multipleDataSourcesAreIgnored() throws Exception {
try (ConfigurableApplicationContext context = getContext(
() -> createContext(MultipleDataSourcesConfiguration.class))) {
Collection<DataSource> dataSources = context.getBeansOfType(DataSource.class).values();
for (DataSource dataSource : dataSources) {
Statement statement = configureDataSourceBehavior(dataSource);
then(statement).should(never()).execute("SHUTDOWN");
}
}
}
@Test
void emptyFactoryMethodMetadataIgnored() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
DataSource dataSource = mock(DataSource.class);
AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(dataSource.getClass());
context.registerBeanDefinition("dataSource", beanDefinition);
context.register(DevToolsDataSourceAutoConfiguration.class);
context.refresh();
context.close();
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
DataSource dataSource = mock(DataSource.class);
AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(dataSource.getClass());
context.registerBeanDefinition("dataSource", beanDefinition);
context.register(DevToolsDataSourceAutoConfiguration.class);
context.refresh();
}
}
protected final Statement configureDataSourceBehavior(DataSource dataSource) throws SQLException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@@ -61,88 +61,99 @@ class DevToolsPooledDataSourceAutoConfigurationTests extends AbstractDevToolsDat
@Test
void autoConfiguredInMemoryDataSourceIsShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(
() -> createContext(DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should().execute("SHUTDOWN");
try (ConfigurableApplicationContext context = getContext(
() -> createContext(DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should().execute("SHUTDOWN");
}
}
@Test
void autoConfiguredExternalDataSourceIsNotShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext("org.postgresql.Driver",
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.postgresql.Driver",
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
}
}
@Test
void h2ServerIsNotShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver",
"jdbc:h2:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver",
"jdbc:h2:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
}
}
@Test
void inMemoryH2IsShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver", "jdbc:h2:mem:test",
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should().execute("SHUTDOWN");
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver",
"jdbc:h2:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should().execute("SHUTDOWN");
}
}
@Test
void hsqlServerIsNotShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
"jdbc:hsqldb:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
"jdbc:hsqldb:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
}
}
@Test
void inMemoryHsqlIsShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
"jdbc:hsqldb:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should().execute("SHUTDOWN");
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
"jdbc:hsqldb:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should().execute("SHUTDOWN");
}
}
@Test
void derbyClientIsNotShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext("org.apache.derby.jdbc.ClientDriver",
"jdbc:derby://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
try (ConfigurableApplicationContext context = getContext(
() -> createContext("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost",
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
context.close();
then(statement).should(never()).execute("SHUTDOWN");
}
}
@Test
void inMemoryDerbyIsShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(
try (ConfigurableApplicationContext context = getContext(
() -> createContext("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:test;create=true",
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
HikariDataSource dataSource = context.getBean(HikariDataSource.class);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1");
HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
// Prevent a race between Hikari's initialization and Derby shutdown
Awaitility.await()
.atMost(Duration.ofSeconds(30))
.until(pool::getIdleConnections, (idle) -> idle == dataSource.getMinimumIdle());
context.close();
// Connect should fail as DB no longer exists
assertThatExceptionOfType(SQLException.class)
.isThrownBy(() -> new EmbeddedDriver().connect("jdbc:derby:memory:test", new Properties()))
.satisfies((ex) -> assertThat(ex.getSQLState()).isEqualTo("XJ004"));
// Shut Derby down fully so that it closes its log file
assertThatExceptionOfType(SQLException.class)
.isThrownBy(() -> new EmbeddedDriver().connect("jdbc:derby:;shutdown=true", new Properties()));
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
HikariDataSource dataSource = context.getBean(HikariDataSource.class);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1");
HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
// Prevent a race between Hikari's initialization and Derby shutdown
Awaitility.await()
.atMost(Duration.ofSeconds(30))
.until(pool::getIdleConnections, (idle) -> idle == dataSource.getMinimumIdle());
context.close();
// Connect should fail as DB no longer exists
assertThatExceptionOfType(SQLException.class).isThrownBy(() -> connect("jdbc:derby:memory:test"))
.satisfies((ex) -> assertThat(ex.getSQLState()).isEqualTo("XJ004"));
// Shut Derby down fully so that it closes its log file
assertThatExceptionOfType(SQLException.class).isThrownBy(() -> connect("jdbc:derby:;shutdown=true"));
}
}
private void connect(String jdbcUrl) throws SQLException {
new EmbeddedDriver().connect(jdbcUrl, new Properties()).close();
}
}

View File

@@ -71,28 +71,32 @@ class DevToolsR2dbcAutoConfigurationTests {
@Test
void nonEmbeddedConnectionFactoryIsNotShutdown() throws Exception {
ConfigurableApplicationContext context = getContext(() -> createContext("r2dbc:h2:file:///testdb"));
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
context.close();
assertThat(shutdowns).doesNotContain(connectionFactory);
try (ConfigurableApplicationContext context = getContext(() -> createContext("r2dbc:h2:file:///testdb"))) {
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
context.close();
assertThat(shutdowns).doesNotContain(connectionFactory);
}
}
@Test
void singleManuallyConfiguredConnectionFactoryIsNotClosed() throws Exception {
ConfigurableApplicationContext context = getContext(
() -> createContext(SingleConnectionFactoryConfiguration.class));
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
context.close();
assertThat(shutdowns).doesNotContain(connectionFactory);
try (ConfigurableApplicationContext context = getContext(
() -> createContext(SingleConnectionFactoryConfiguration.class))) {
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
context.close();
assertThat(shutdowns).doesNotContain(connectionFactory);
}
}
@Test
void multipleConnectionFactoriesAreIgnored() throws Exception {
ConfigurableApplicationContext context = getContext(
() -> createContext(MultipleConnectionFactoriesConfiguration.class));
Collection<ConnectionFactory> connectionFactory = context.getBeansOfType(ConnectionFactory.class).values();
context.close();
assertThat(shutdowns).doesNotContainAnyElementsOf(connectionFactory);
try (ConfigurableApplicationContext context = getContext(
() -> createContext(MultipleConnectionFactoriesConfiguration.class))) {
Collection<ConnectionFactory> connectionFactory = context.getBeansOfType(ConnectionFactory.class)
.values();
context.close();
assertThat(shutdowns).doesNotContainAnyElementsOf(connectionFactory);
}
}
@Test