From bb3651b7d1c4334d5c350a9b127729aac2bfbf10 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 22 Nov 2024 14:42:48 +0000 Subject: [PATCH] Reduce warnings reported by Eclipse Closes gh-43269 --- ...althContributorAutoConfigurationTests.java | 3 +- ...althContributorAutoConfigurationTests.java | 3 +- ...onfigurationsSenderConfigurationTests.java | 2 + ...ToolsDataSourceAutoConfigurationTests.java | 43 ++++--- ...ooledDataSourceAutoConfigurationTests.java | 119 ++++++++++-------- .../DevToolsR2dbcAutoConfigurationTests.java | 32 ++--- .../AbstractApplicationContextRunner.java | 4 +- .../AssertableApplicationContextTests.java | 3 +- ...bleReactiveWebApplicationContextTests.java | 3 +- .../AssertableWebApplicationContextTests.java | 3 +- .../platform/json/AbstractJsonTests.java | 13 +- .../tasks/bundling/BootZipCopyAction.java | 7 +- ...tiveImagePluginActionIntegrationTests.java | 7 +- .../nio/file/NestedFileSystemProvider.java | 1 + .../ModifiedClassPathClassLoader.java | 1 + ...AbstractReactiveWebServerFactoryTests.java | 11 +- .../AbstractServletWebServerFactoryTests.java | 10 +- 17 files changed, 148 insertions(+), 117 deletions(-) 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-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 635222f8c74..8ea584c79ca 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 @@ -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. @@ -381,7 +381,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 91189ce2a99..5a1a81b2d64 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 @@ -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. @@ -32,6 +32,7 @@ import static org.mockito.Mockito.mock; class AssertableApplicationContextTests { @Test + @SuppressWarnings("resource") void getShouldReturnProxy() { AssertableApplicationContext context = AssertableApplicationContext .get(() -> mock(ConfigurableApplicationContext.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 b84b776bbd3..faa827c69c7 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 @@ -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. @@ -32,6 +32,7 @@ import static org.mockito.Mockito.mock; class AssertableReactiveWebApplicationContextTests { @Test + @SuppressWarnings("resource") void getShouldReturnProxy() { AssertableReactiveWebApplicationContext context = AssertableReactiveWebApplicationContext .get(() -> mock(ConfigurableReactiveWebApplicationContext.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 53873f9c47a..980012c4a07 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 @@ -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. @@ -32,6 +32,7 @@ import static org.mockito.Mockito.mock; class AssertableWebApplicationContextTests { @Test + @SuppressWarnings("resource") void getShouldReturnProxy() { AssertableWebApplicationContext context = AssertableWebApplicationContext .get(() -> mock(ConfigurableWebApplicationContext.class)); 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 c711b77e58e..43ff5357c59 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 @@ -130,9 +130,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-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 8b014b92853..07065ba859c 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/reactive/server/AbstractReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java index 116e21e01f7..c1d20a9be10 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java @@ -569,19 +569,16 @@ public abstract class AbstractReactiveWebServerFactoryTests { factory.setHttp2(http2); this.webServer = factory.getWebServer(new EchoHandler()); this.webServer.start(); - org.eclipse.jetty.client.HttpClient client = new org.eclipse.jetty.client.HttpClient( - new HttpClientTransportOverHTTP2(new HTTP2Client())); - client.start(); - try { + + try (org.eclipse.jetty.client.HttpClient client = new org.eclipse.jetty.client.HttpClient( + new HttpClientTransportOverHTTP2(new HTTP2Client()))) { + client.start(); ContentResponse response = client.POST("http://localhost:" + this.webServer.getPort()) .body(new StringRequestContent("text/plain", "Hello World")) .send(); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); assertThat(response.getContentAsString()).isEqualTo("Hello World"); } - finally { - client.stop(); - } } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index 9219de35c0f..bafcd060dc8 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -1291,17 +1291,13 @@ public abstract class AbstractServletWebServerFactoryTests { factory.setHttp2(http2); this.webServer = factory.getWebServer(exampleServletRegistration()); this.webServer.start(); - org.eclipse.jetty.client.HttpClient client = new org.eclipse.jetty.client.HttpClient( - new HttpClientTransportOverHTTP2(new HTTP2Client())); - client.start(); - try { + try (org.eclipse.jetty.client.HttpClient client = new org.eclipse.jetty.client.HttpClient( + new HttpClientTransportOverHTTP2(new HTTP2Client()))) { + client.start(); ContentResponse response = client.GET("http://localhost:" + this.webServer.getPort() + "/hello"); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); assertThat(response.getContentAsString()).isEqualTo("Hello World"); } - finally { - client.stop(); - } } @Test