Skip to content

Commit bb3651b

Browse files
committed
Reduce warnings reported by Eclipse
Closes gh-43269
1 parent cf1dadf commit bb3651b

File tree

17 files changed

+146
-115
lines changed

17 files changed

+146
-115
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraHealthContributorAutoConfigurationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@ void runWithCqlSessionOnlyShouldCreateDriverIndicator() {
5454
}
5555

5656
@Test
57+
@SuppressWarnings("resource")
5758
void runWithCqlSessionAndSpringDataAbsentShouldCreateDriverIndicator() {
5859
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
5960
.withClassLoader(new FilteredClassLoader("org.springframework.data"))

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraReactiveHealthContributorAutoConfigurationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,6 +67,7 @@ void runWithCqlSessionAndReactiveCassandraOperationsShouldCreateDriverIndicator(
6767
}
6868

6969
@Test
70+
@SuppressWarnings("resource")
7071
void runWithCqlSessionAndSpringDataAbsentShouldCreateDriverIndicator() {
7172
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
7273
.withClassLoader(new FilteredClassLoader("org.springframework.data"))

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurationsSenderConfigurationTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ void shouldUseCustomHttpEndpointSupplierFactory() {
198198
}
199199

200200
@Test
201+
@SuppressWarnings("resource")
201202
void shouldUseCustomHttpEndpointSupplierFactoryWhenReactive() {
202203
this.reactiveContextRunner.withUserConfiguration(WebClientConfiguration.class)
203204
.withClassLoader(new FilteredClassLoader(URLConnectionSender.class))
@@ -207,6 +208,7 @@ void shouldUseCustomHttpEndpointSupplierFactoryWhenReactive() {
207208
}
208209

209210
@Test
211+
@SuppressWarnings("resource")
210212
void shouldUseCustomHttpEndpointSupplierFactoryWhenRestTemplate() {
211213
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class)
212214
.withClassLoader(new FilteredClassLoader(URLConnectionSender.class, WebClient.class))

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/AbstractDevToolsDataSourceAutoConfigurationTests.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,32 +51,35 @@ abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
5151

5252
@Test
5353
void singleManuallyConfiguredDataSourceIsNotClosed() throws Exception {
54-
ConfigurableApplicationContext context = getContext(() -> createContext(SingleDataSourceConfiguration.class));
55-
DataSource dataSource = context.getBean(DataSource.class);
56-
Statement statement = configureDataSourceBehavior(dataSource);
57-
then(statement).should(never()).execute("SHUTDOWN");
54+
try (ConfigurableApplicationContext context = getContext(
55+
() -> createContext(SingleDataSourceConfiguration.class))) {
56+
DataSource dataSource = context.getBean(DataSource.class);
57+
Statement statement = configureDataSourceBehavior(dataSource);
58+
then(statement).should(never()).execute("SHUTDOWN");
59+
}
5860
}
5961

6062
@Test
6163
void multipleDataSourcesAreIgnored() throws Exception {
62-
ConfigurableApplicationContext context = getContext(
63-
() -> createContext(MultipleDataSourcesConfiguration.class));
64-
Collection<DataSource> dataSources = context.getBeansOfType(DataSource.class).values();
65-
for (DataSource dataSource : dataSources) {
66-
Statement statement = configureDataSourceBehavior(dataSource);
67-
then(statement).should(never()).execute("SHUTDOWN");
64+
try (ConfigurableApplicationContext context = getContext(
65+
() -> createContext(MultipleDataSourcesConfiguration.class))) {
66+
Collection<DataSource> dataSources = context.getBeansOfType(DataSource.class).values();
67+
for (DataSource dataSource : dataSources) {
68+
Statement statement = configureDataSourceBehavior(dataSource);
69+
then(statement).should(never()).execute("SHUTDOWN");
70+
}
6871
}
6972
}
7073

7174
@Test
7275
void emptyFactoryMethodMetadataIgnored() {
73-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
74-
DataSource dataSource = mock(DataSource.class);
75-
AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(dataSource.getClass());
76-
context.registerBeanDefinition("dataSource", beanDefinition);
77-
context.register(DevToolsDataSourceAutoConfiguration.class);
78-
context.refresh();
79-
context.close();
76+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
77+
DataSource dataSource = mock(DataSource.class);
78+
AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(dataSource.getClass());
79+
context.registerBeanDefinition("dataSource", beanDefinition);
80+
context.register(DevToolsDataSourceAutoConfiguration.class);
81+
context.refresh();
82+
}
8083
}
8184

8285
protected final Statement configureDataSourceBehavior(DataSource dataSource) throws SQLException {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,88 +61,99 @@ void after() {
6161

6262
@Test
6363
void autoConfiguredInMemoryDataSourceIsShutdown() throws Exception {
64-
ConfigurableApplicationContext context = getContext(
65-
() -> createContext(DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
66-
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
67-
context.close();
68-
then(statement).should().execute("SHUTDOWN");
64+
try (ConfigurableApplicationContext context = getContext(
65+
() -> createContext(DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
66+
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
67+
context.close();
68+
then(statement).should().execute("SHUTDOWN");
69+
}
6970
}
7071

7172
@Test
7273
void autoConfiguredExternalDataSourceIsNotShutdown() throws Exception {
73-
ConfigurableApplicationContext context = getContext(() -> createContext("org.postgresql.Driver",
74-
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
75-
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
76-
context.close();
77-
then(statement).should(never()).execute("SHUTDOWN");
74+
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.postgresql.Driver",
75+
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
76+
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
77+
context.close();
78+
then(statement).should(never()).execute("SHUTDOWN");
79+
}
7880
}
7981

8082
@Test
8183
void h2ServerIsNotShutdown() throws Exception {
82-
ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver",
83-
"jdbc:h2:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
84-
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
85-
context.close();
86-
then(statement).should(never()).execute("SHUTDOWN");
84+
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver",
85+
"jdbc:h2:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
86+
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
87+
context.close();
88+
then(statement).should(never()).execute("SHUTDOWN");
89+
}
8790
}
8891

8992
@Test
9093
void inMemoryH2IsShutdown() throws Exception {
91-
ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver", "jdbc:h2:mem:test",
92-
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
93-
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
94-
context.close();
95-
then(statement).should().execute("SHUTDOWN");
94+
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver",
95+
"jdbc:h2:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
96+
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
97+
context.close();
98+
then(statement).should().execute("SHUTDOWN");
99+
}
96100
}
97101

98102
@Test
99103
void hsqlServerIsNotShutdown() throws Exception {
100-
ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
101-
"jdbc:hsqldb:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
102-
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
103-
context.close();
104-
then(statement).should(never()).execute("SHUTDOWN");
104+
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
105+
"jdbc:hsqldb:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
106+
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
107+
context.close();
108+
then(statement).should(never()).execute("SHUTDOWN");
109+
}
105110
}
106111

107112
@Test
108113
void inMemoryHsqlIsShutdown() throws Exception {
109-
ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
110-
"jdbc:hsqldb:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
111-
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
112-
context.close();
113-
then(statement).should().execute("SHUTDOWN");
114+
try (ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver",
115+
"jdbc:hsqldb:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
116+
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
117+
context.close();
118+
then(statement).should().execute("SHUTDOWN");
119+
}
114120
}
115121

116122
@Test
117123
void derbyClientIsNotShutdown() throws Exception {
118-
ConfigurableApplicationContext context = getContext(() -> createContext("org.apache.derby.jdbc.ClientDriver",
119-
"jdbc:derby://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
120-
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
121-
context.close();
122-
then(statement).should(never()).execute("SHUTDOWN");
124+
try (ConfigurableApplicationContext context = getContext(
125+
() -> createContext("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost",
126+
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
127+
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
128+
context.close();
129+
then(statement).should(never()).execute("SHUTDOWN");
130+
}
123131
}
124132

125133
@Test
126134
void inMemoryDerbyIsShutdown() throws Exception {
127-
ConfigurableApplicationContext context = getContext(
135+
try (ConfigurableApplicationContext context = getContext(
128136
() -> createContext("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:test;create=true",
129-
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
130-
HikariDataSource dataSource = context.getBean(HikariDataSource.class);
131-
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
132-
jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1");
133-
HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
134-
// Prevent a race between Hikari's initialization and Derby shutdown
135-
Awaitility.await()
136-
.atMost(Duration.ofSeconds(30))
137-
.until(pool::getIdleConnections, (idle) -> idle == dataSource.getMinimumIdle());
138-
context.close();
139-
// Connect should fail as DB no longer exists
140-
assertThatExceptionOfType(SQLException.class)
141-
.isThrownBy(() -> new EmbeddedDriver().connect("jdbc:derby:memory:test", new Properties()))
142-
.satisfies((ex) -> assertThat(ex.getSQLState()).isEqualTo("XJ004"));
143-
// Shut Derby down fully so that it closes its log file
144-
assertThatExceptionOfType(SQLException.class)
145-
.isThrownBy(() -> new EmbeddedDriver().connect("jdbc:derby:;shutdown=true", new Properties()));
137+
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) {
138+
HikariDataSource dataSource = context.getBean(HikariDataSource.class);
139+
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
140+
jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1");
141+
HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
142+
// Prevent a race between Hikari's initialization and Derby shutdown
143+
Awaitility.await()
144+
.atMost(Duration.ofSeconds(30))
145+
.until(pool::getIdleConnections, (idle) -> idle == dataSource.getMinimumIdle());
146+
context.close();
147+
// Connect should fail as DB no longer exists
148+
assertThatExceptionOfType(SQLException.class).isThrownBy(() -> connect("jdbc:derby:memory:test"))
149+
.satisfies((ex) -> assertThat(ex.getSQLState()).isEqualTo("XJ004"));
150+
// Shut Derby down fully so that it closes its log file
151+
assertThatExceptionOfType(SQLException.class).isThrownBy(() -> connect("jdbc:derby:;shutdown=true"));
152+
}
153+
}
154+
155+
private void connect(String jdbcUrl) throws SQLException {
156+
new EmbeddedDriver().connect(jdbcUrl, new Properties()).close();
146157
}
147158

148159
}

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,32 @@ void autoConfiguredInMemoryConnectionFactoryIsShutdown() throws Exception {
7171

7272
@Test
7373
void nonEmbeddedConnectionFactoryIsNotShutdown() throws Exception {
74-
ConfigurableApplicationContext context = getContext(() -> createContext("r2dbc:h2:file:///testdb"));
75-
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
76-
context.close();
77-
assertThat(shutdowns).doesNotContain(connectionFactory);
74+
try (ConfigurableApplicationContext context = getContext(() -> createContext("r2dbc:h2:file:///testdb"))) {
75+
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
76+
context.close();
77+
assertThat(shutdowns).doesNotContain(connectionFactory);
78+
}
7879
}
7980

8081
@Test
8182
void singleManuallyConfiguredConnectionFactoryIsNotClosed() throws Exception {
82-
ConfigurableApplicationContext context = getContext(
83-
() -> createContext(SingleConnectionFactoryConfiguration.class));
84-
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
85-
context.close();
86-
assertThat(shutdowns).doesNotContain(connectionFactory);
83+
try (ConfigurableApplicationContext context = getContext(
84+
() -> createContext(SingleConnectionFactoryConfiguration.class))) {
85+
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
86+
context.close();
87+
assertThat(shutdowns).doesNotContain(connectionFactory);
88+
}
8789
}
8890

8991
@Test
9092
void multipleConnectionFactoriesAreIgnored() throws Exception {
91-
ConfigurableApplicationContext context = getContext(
92-
() -> createContext(MultipleConnectionFactoriesConfiguration.class));
93-
Collection<ConnectionFactory> connectionFactory = context.getBeansOfType(ConnectionFactory.class).values();
94-
context.close();
95-
assertThat(shutdowns).doesNotContainAnyElementsOf(connectionFactory);
93+
try (ConfigurableApplicationContext context = getContext(
94+
() -> createContext(MultipleConnectionFactoriesConfiguration.class))) {
95+
Collection<ConnectionFactory> connectionFactory = context.getBeansOfType(ConnectionFactory.class)
96+
.values();
97+
context.close();
98+
assertThat(shutdowns).doesNotContainAnyElementsOf(connectionFactory);
99+
}
96100
}
97101

98102
@Test

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -381,7 +381,7 @@ private void withContextClassLoader(ClassLoader classLoader, Runnable action) {
381381
}
382382
}
383383

384-
@SuppressWarnings("unchecked")
384+
@SuppressWarnings({ "unchecked", "resource" })
385385
private A createAssertableContext(boolean refresh) {
386386
ResolvableType resolvableType = ResolvableType.forClass(AbstractApplicationContextRunner.class, getClass());
387387
Class<A> assertType = (Class<A>) resolvableType.resolveGeneric(1);

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
class AssertableApplicationContextTests {
3333

3434
@Test
35+
@SuppressWarnings("resource")
3536
void getShouldReturnProxy() {
3637
AssertableApplicationContext context = AssertableApplicationContext
3738
.get(() -> mock(ConfigurableApplicationContext.class));

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
class AssertableReactiveWebApplicationContextTests {
3333

3434
@Test
35+
@SuppressWarnings("resource")
3536
void getShouldReturnProxy() {
3637
AssertableReactiveWebApplicationContext context = AssertableReactiveWebApplicationContext
3738
.get(() -> mock(ConfigurableReactiveWebApplicationContext.class));

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
class AssertableWebApplicationContextTests {
3333

3434
@Test
35+
@SuppressWarnings("resource")
3536
void getShouldReturnProxy() {
3637
AssertableWebApplicationContext context = AssertableWebApplicationContext
3738
.get(() -> mock(ConfigurableWebApplicationContext.class));

0 commit comments

Comments
 (0)