diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraHealthContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraHealthContributorAutoConfigurationTests.java index a76c786630f..96b67e78358 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraHealthContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraHealthContributorAutoConfigurationTests.java @@ -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. @@ -54,6 +54,7 @@ class CassandraHealthContributorAutoConfigurationTests { } @Test + @SuppressWarnings("resource") void runWithCqlSessionAndSpringDataAbsentShouldCreateDriverIndicator() { this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class)) .withClassLoader(new FilteredClassLoader("org.springframework.data")) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraReactiveHealthContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraReactiveHealthContributorAutoConfigurationTests.java index 0f85369b4b3..1a2ce0d8752 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraReactiveHealthContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraReactiveHealthContributorAutoConfigurationTests.java @@ -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. @@ -67,6 +67,7 @@ class CassandraReactiveHealthContributorAutoConfigurationTests { } @Test + @SuppressWarnings("resource") void runWithCqlSessionAndSpringDataAbsentShouldCreateDriverIndicator() { this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class)) .withClassLoader(new FilteredClassLoader("org.springframework.data")) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurationsSenderConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurationsSenderConfigurationTests.java index 7988fd31083..d5c2ddee738 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurationsSenderConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurationsSenderConfigurationTests.java @@ -198,6 +198,7 @@ class ZipkinConfigurationsSenderConfigurationTests { } @Test + @SuppressWarnings("resource") void shouldUseCustomHttpEndpointSupplierFactoryWhenReactive() { this.reactiveContextRunner.withUserConfiguration(WebClientConfiguration.class) .withClassLoader(new FilteredClassLoader(URLConnectionSender.class)) @@ -207,6 +208,7 @@ class ZipkinConfigurationsSenderConfigurationTests { } @Test + @SuppressWarnings("resource") void shouldUseCustomHttpEndpointSupplierFactoryWhenRestTemplate() { this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class) .withClassLoader(new FilteredClassLoader(URLConnectionSender.class, WebClient.class)) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java index 8b03635db22..1b1d9ff5d34 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java @@ -102,6 +102,8 @@ class PulsarContainerFactoryCustomizersTests { /** * Test customizer that will match all {@link PulsarListenerContainerFactory}. + * + * @param the container factory type */ static class TestCustomizer> implements PulsarContainerFactoryCustomizer { diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/AbstractDevToolsDataSourceAutoConfigurationTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/AbstractDevToolsDataSourceAutoConfigurationTests.java index 76987fe76e6..a85d959247d 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/AbstractDevToolsDataSourceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/AbstractDevToolsDataSourceAutoConfigurationTests.java @@ -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 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 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 { diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java index 5acc1ecd7aa..a5cb76cd5de 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java @@ -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(); } } diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java index d06223ce749..d5891df333f 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java @@ -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 = context.getBeansOfType(ConnectionFactory.class).values(); - context.close(); - assertThat(shutdowns).doesNotContainAnyElementsOf(connectionFactory); + try (ConfigurableApplicationContext context = getContext( + () -> createContext(MultipleConnectionFactoriesConfiguration.class))) { + Collection connectionFactory = context.getBeansOfType(ConnectionFactory.class) + .values(); + context.close(); + assertThat(shutdowns).doesNotContainAnyElementsOf(connectionFactory); + } } @Test diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java index e6599e2f4ef..5bb815e9a48 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java @@ -403,7 +403,7 @@ public abstract class AbstractApplicationContextRunner assertType = (Class) resolvableType.resolveGeneric(1); diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java index 3ff56807dd4..cb3e25bc04a 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java @@ -33,6 +33,7 @@ import static org.mockito.Mockito.withSettings; class AssertableApplicationContextTests { @Test + @SuppressWarnings("resource") void getShouldReturnProxy() { AssertableApplicationContext context = AssertableApplicationContext .get(() -> mock(ConfigurableApplicationContext.class)); @@ -41,12 +42,13 @@ class AssertableApplicationContextTests { @Test void getWhenHasAdditionalInterfaceShouldReturnProxy() { - AssertableApplicationContext context = AssertableApplicationContext.get( + try (AssertableApplicationContext context = AssertableApplicationContext.get( () -> mock(ConfigurableApplicationContext.class, withSettings().extraInterfaces(AdditionalContextInterface.class)), - AdditionalContextInterface.class); - assertThat(context).isInstanceOf(ConfigurableApplicationContext.class) - .isInstanceOf(AdditionalContextInterface.class); + AdditionalContextInterface.class)) { + assertThat(context).isInstanceOf(ConfigurableApplicationContext.class) + .isInstanceOf(AdditionalContextInterface.class); + } } } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java index 0947f9b80ea..4aa46f2de35 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java @@ -33,6 +33,7 @@ import static org.mockito.Mockito.withSettings; class AssertableReactiveWebApplicationContextTests { @Test + @SuppressWarnings("resource") void getShouldReturnProxy() { AssertableReactiveWebApplicationContext context = AssertableReactiveWebApplicationContext .get(() -> mock(ConfigurableReactiveWebApplicationContext.class)); @@ -41,12 +42,13 @@ class AssertableReactiveWebApplicationContextTests { @Test void getWhenHasAdditionalInterfaceShouldReturnProxy() { - AssertableReactiveWebApplicationContext context = AssertableReactiveWebApplicationContext.get( + try (AssertableReactiveWebApplicationContext context = AssertableReactiveWebApplicationContext.get( () -> mock(ConfigurableReactiveWebApplicationContext.class, withSettings().extraInterfaces(AdditionalContextInterface.class)), - AdditionalContextInterface.class); - assertThat(context).isInstanceOf(ConfigurableReactiveWebApplicationContext.class) - .isInstanceOf(AdditionalContextInterface.class); + AdditionalContextInterface.class)) { + assertThat(context).isInstanceOf(ConfigurableReactiveWebApplicationContext.class) + .isInstanceOf(AdditionalContextInterface.class); + } } } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java index 1a2268dbc0a..31268d147dc 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java @@ -33,6 +33,7 @@ import static org.mockito.Mockito.withSettings; class AssertableWebApplicationContextTests { @Test + @SuppressWarnings("resource") void getShouldReturnProxy() { AssertableWebApplicationContext context = AssertableWebApplicationContext .get(() -> mock(ConfigurableWebApplicationContext.class)); @@ -41,12 +42,13 @@ class AssertableWebApplicationContextTests { @Test void getWhenHasAdditionalInterfaceShouldReturnProxy() { - ConfigurableWebApplicationContext context = AssertableWebApplicationContext.get( + try (ConfigurableWebApplicationContext context = AssertableWebApplicationContext.get( () -> mock(ConfigurableWebApplicationContext.class, withSettings().extraInterfaces(AdditionalContextInterface.class)), - AdditionalContextInterface.class); - assertThat(context).isInstanceOf(ConfigurableWebApplicationContext.class) - .isInstanceOf(AdditionalContextInterface.class); + AdditionalContextInterface.class)) { + assertThat(context).isInstanceOf(ConfigurableWebApplicationContext.class) + .isInstanceOf(AdditionalContextInterface.class); + } } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/DockerApiTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/DockerApiTests.java index e60f04278a1..b12bf7f2f64 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/DockerApiTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/DockerApiTests.java @@ -600,7 +600,7 @@ class DockerApiTests { } @Test - void createWithPlatformAndInsufficientApiVersionThrowsException() throws Exception { + void createWithPlatformAndInsufficientApiVersionThrowsException() { ImageReference imageReference = ImageReference.of("ubuntu:bionic"); ContainerConfig config = ContainerConfig.of(imageReference, (update) -> update.withCommand("/bin/bash")); ImagePlatform platform = ImagePlatform.of("linux/arm64/v1"); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/AbstractJsonTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/AbstractJsonTests.java index edc27b55541..3d86534cf91 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/AbstractJsonTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/AbstractJsonTests.java @@ -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. @@ -17,8 +17,10 @@ package org.springframework.boot.buildpack.platform.json; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; @@ -45,8 +47,13 @@ public abstract class AbstractJsonTests { } protected final String getContentAsString(String name) { - return new BufferedReader(new InputStreamReader(getContent(name), StandardCharsets.UTF_8)).lines() - .collect(Collectors.joining("\n")); + try (InputStream in = getContent(name)) { + return new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines() + .collect(Collectors.joining("\n")); + } + catch (IOException ex) { + throw new UncheckedIOException(ex); + } } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java index fdde482b7e2..a4870eeb31b 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java @@ -348,8 +348,11 @@ class BootZipCopyAction implements CopyAction { private void writeJarModeLibrary(String location, JarModeLibrary library) throws IOException { String name = location + library.getName(); - writeEntry(name, ZipEntryContentWriter.fromInputStream(library.openStream()), false, - (entry) -> prepareStoredEntry(library.openStream(), entry)); + writeEntry(name, ZipEntryContentWriter.fromInputStream(library.openStream()), false, (entry) -> { + try (InputStream in = library.openStream()) { + prepareStoredEntry(library.openStream(), entry); + } + }); if (BootZipCopyAction.this.layerResolver != null) { Layer layer = BootZipCopyAction.this.layerResolver.getLayer(library); this.layerIndex.add(layer, name); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests.java index cf9d67951c2..3e3f0b17354 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests.java @@ -122,9 +122,10 @@ class NativeImagePluginActionIntegrationTests { BuildResult result = this.gradleBuild.build("bootJar"); assertThat(result.task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); File buildLibs = new File(this.gradleBuild.getProjectDir(), "build/libs"); - JarFile jarFile = new JarFile(new File(buildLibs, this.gradleBuild.getProjectDir().getName() + ".jar")); - Manifest manifest = jarFile.getManifest(); - assertThat(manifest.getMainAttributes().getValue("Spring-Boot-Native-Processed")).isEqualTo("true"); + try (JarFile jarFile = new JarFile(new File(buildLibs, this.gradleBuild.getProjectDir().getName() + ".jar"))) { + Manifest manifest = jarFile.getManifest(); + assertThat(manifest.getMainAttributes().getValue("Spring-Boot-Native-Processed")).isEqualTo("true"); + } } private String projectPath(String path) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedFileSystemProvider.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedFileSystemProvider.java index fce7e8c5658..133b86ff29d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedFileSystemProvider.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedFileSystemProvider.java @@ -84,6 +84,7 @@ public class NestedFileSystemProvider extends FileSystemProvider { } @Override + @SuppressWarnings("resource") public Path getPath(URI uri) { NestedLocation location = NestedLocation.fromUri(uri); synchronized (this.fileSystems) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/dockerTest/java/org/springframework/boot/maven/BuildImageTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/dockerTest/java/org/springframework/boot/maven/BuildImageTests.java index ec9d5037f67..38abfece174 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/dockerTest/java/org/springframework/boot/maven/BuildImageTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/dockerTest/java/org/springframework/boot/maven/BuildImageTests.java @@ -475,7 +475,6 @@ class BuildImageTests extends AbstractArchiveIntegrationTests { @TestTemplate void whenBuildImageIsInvokedWithCreatedDate(MavenBuild mavenBuild) { - String testBuildId = randomString(); mavenBuild.project("dockerTest", "build-image-created-date") .goals("package") .systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT") diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java index 1b01bd89882..a81a793bdda 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java @@ -88,6 +88,7 @@ public enum TestImage { * {@link org.testcontainers.containers.CassandraContainer}. * @deprecated since 3.4.0 for removal in 3.6.0 in favor of {@link #CASSANDRA} */ + @SuppressWarnings("deprecation") @Deprecated(since = "3.4.0", forRemoval = true) CASSANDRA_DEPRECATED("cassandra", "3.11.10", () -> org.testcontainers.containers.CassandraContainer.class, (container) -> ((org.testcontainers.containers.CassandraContainer) container) @@ -139,6 +140,7 @@ public enum TestImage { * deprecated {@link org.testcontainers.containers.KafkaContainer}. * @deprecated since 3.4.0 for removal in 3.6.0 in favor of {@link #CONFLUENT_KAFKA} */ + @SuppressWarnings("deprecation") @Deprecated(since = "3.4.0", forRemoval = true) CONFLUENT_KAFKA_DEPRECATED("confluentinc/cp-kafka", "7.4.0", () -> org.testcontainers.containers.KafkaContainer.class), diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java index 9f804efceac..ae0d28e425e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java @@ -99,6 +99,7 @@ final class ModifiedClassPathClassLoader extends URLClassLoader { return super.loadClass(name); } + @SuppressWarnings("resource") static ModifiedClassPathClassLoader get(Class testClass, Method testMethod, List arguments) { Set candidates = new LinkedHashSet<>(); candidates.add(testClass); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/AbstractClientHttpRequestFactoriesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/AbstractClientHttpRequestFactoriesTests.java index 5b81f4c3d97..0c913c31353 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/AbstractClientHttpRequestFactoriesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/AbstractClientHttpRequestFactoriesTests.java @@ -67,6 +67,7 @@ abstract class AbstractClientHttpRequestFactoriesTests