Reduce warnings reported by Eclipse

Closes gh-43269
This commit is contained in:
Andy Wilkinson 2024-11-22 14:42:48 +00:00
parent cf1dadf2e7
commit bb3651b7d1
17 changed files with 148 additions and 117 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.
@ -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"))

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.
@ -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"))

View File

@ -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))

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

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.
@ -381,7 +381,7 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
}
}
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "resource" })
private A createAssertableContext(boolean refresh) {
ResolvableType resolvableType = ResolvableType.forClass(AbstractApplicationContextRunner.class, getClass());
Class<A> assertType = (Class<A>) resolvableType.resolveGeneric(1);

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.
@ -32,6 +32,7 @@ import static org.mockito.Mockito.mock;
class AssertableApplicationContextTests {
@Test
@SuppressWarnings("resource")
void getShouldReturnProxy() {
AssertableApplicationContext context = AssertableApplicationContext
.get(() -> mock(ConfigurableApplicationContext.class));

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.
@ -32,6 +32,7 @@ import static org.mockito.Mockito.mock;
class AssertableReactiveWebApplicationContextTests {
@Test
@SuppressWarnings("resource")
void getShouldReturnProxy() {
AssertableReactiveWebApplicationContext context = AssertableReactiveWebApplicationContext
.get(() -> mock(ConfigurableReactiveWebApplicationContext.class));

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.
@ -32,6 +32,7 @@ import static org.mockito.Mockito.mock;
class AssertableWebApplicationContextTests {
@Test
@SuppressWarnings("resource")
void getShouldReturnProxy() {
AssertableWebApplicationContext context = AssertableWebApplicationContext
.get(() -> mock(ConfigurableWebApplicationContext.class));

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.
@ -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);
}
}
}

View File

@ -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);

View File

@ -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) {

View File

@ -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) {

View File

@ -99,6 +99,7 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
return super.loadClass(name);
}
@SuppressWarnings("resource")
static ModifiedClassPathClassLoader get(Class<?> testClass, Method testMethod, List<Object> arguments) {
Set<AnnotatedElement> candidates = new LinkedHashSet<>();
candidates.add(testClass);

View File

@ -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

View File

@ -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