Polish "Use try-with-resources to close resources automatically"

- Apply code formatting
- Use try-with-resources in many other places that were missed in the
  pull request

Closes gh-8045
This commit is contained in:
Andy Wilkinson
2017-05-23 17:24:01 +01:00
parent 3e797c326a
commit d5438c299c
78 changed files with 284 additions and 703 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.util.CollectionUtils;
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Raja Kolli
* @since 1.3.0
*/
@Configuration

View File

@@ -57,6 +57,7 @@ import org.springframework.util.StringUtils;
*
* @author Eddú Meléndez
* @author Mathieu Ouellet
* @author Raja Kolli
* @since 1.5.0
*/
@Configuration

View File

@@ -127,8 +127,8 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
assertThat(this.context).isInstanceOf(ServletWebServerApplicationContext.class);
assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
"EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
int expected = ((ServletWebServerApplicationContext) this.context)
.getWebServer().getPort();
int expected = ((ServletWebServerApplicationContext) this.context).getWebServer()
.getPort();
String actual = getProperty(createDefaultObjectName(), "local.server.port");
assertThat(actual).isEqualTo(String.valueOf(expected));
}
@@ -142,26 +142,17 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
.child(JmxAutoConfiguration.class,
SpringApplicationAdminJmxAutoConfiguration.class)
.web(WebApplicationType.NONE);
ConfigurableApplicationContext parent = null;
ConfigurableApplicationContext child = null;
try {
parent = parentBuilder.run("--" + ENABLE_ADMIN_PROP);
child = childBuilder.run("--" + ENABLE_ADMIN_PROP);
try (ConfigurableApplicationContext parent = parentBuilder
.run("--" + ENABLE_ADMIN_PROP);
ConfigurableApplicationContext child = childBuilder
.run("--" + ENABLE_ADMIN_PROP)) {
BeanFactoryUtils.beanOfType(parent.getBeanFactory(),
SpringApplicationAdminMXBeanRegistrar.class);
this.thrown.expect(NoSuchBeanDefinitionException.class);
BeanFactoryUtils.beanOfType(child.getBeanFactory(),
SpringApplicationAdminMXBeanRegistrar.class);
}
finally {
if (parent != null) {
parent.close();
}
if (child != null) {
child.close();
}
}
}
private ObjectName createDefaultObjectName() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -145,17 +145,13 @@ public class ConditionalOnSingleCandidateTests {
@Test
public void singleCandidateMultipleCandidatesInContextHierarchy() {
load(FooPrimaryConfiguration.class, BarConfiguration.class);
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.setParent(this.context);
child.register(OnBeanSingleCandidateConfiguration.class);
try {
try (AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext()) {
child.setParent(this.context);
child.register(OnBeanSingleCandidateConfiguration.class);
child.refresh();
assertThat(child.containsBean("baz")).isTrue();
assertThat(child.getBean("baz")).isEqualTo("foo");
}
finally {
child.close();
}
}
private void load(Class<?>... classes) {

View File

@@ -143,9 +143,8 @@ public class MessageSourceAutoConfigurationTests {
@Test
public void existingMessageSourceInParentIsIgnored() {
ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext();
parent.refresh();
try {
try (ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext()) {
parent.refresh();
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
EnvironmentTestUtils.addEnvironment(this.context,
@@ -156,9 +155,6 @@ public class MessageSourceAutoConfigurationTests {
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
.isEqualTo("bar");
}
finally {
parent.close();
}
}
private void load(String... environment) {

View File

@@ -83,14 +83,10 @@ public class CassandraDataAutoConfigurationIntegrationTests {
}
private void createTestKeyspaceIfNotExists() {
Session session = this.cassandra.getCluster().connect();
try {
try (Session session = this.cassandra.getCluster().connect()) {
session.execute("CREATE KEYSPACE IF NOT EXISTS boot_test"
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
}
finally {
session.close();
}
}
}

View File

@@ -235,20 +235,16 @@ public class NoSuchBeanDefinitionFailureAnalyzerTests {
}
private FatalBeanException createFailure(Class<?> config, String... environment) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
this.analyzer.setBeanFactory(context.getBeanFactory());
EnvironmentTestUtils.addEnvironment(context, environment);
context.register(config);
try {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
this.analyzer.setBeanFactory(context.getBeanFactory());
EnvironmentTestUtils.addEnvironment(context, environment);
context.register(config);
context.refresh();
return null;
}
catch (FatalBeanException ex) {
return ex;
}
finally {
context.close();
}
}
private FailureAnalysis analyzeFailure(Exception failure) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -177,18 +177,14 @@ public class FreeMarkerAutoConfigurationTests {
@Test
public void renderNonWebAppTemplate() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
FreeMarkerAutoConfiguration.class);
try {
freemarker.template.Configuration freemarker = context
try (AnnotationConfigApplicationContext customContext = new AnnotationConfigApplicationContext(
FreeMarkerAutoConfiguration.class)) {
freemarker.template.Configuration freemarker = customContext
.getBean(freemarker.template.Configuration.class);
StringWriter writer = new StringWriter();
freemarker.getTemplate("message.ftl").process(this, writer);
assertThat(writer.toString()).contains("Hello World");
}
finally {
context.close();
}
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -56,22 +56,17 @@ public class EmbeddedDataSourceConfigurationTests {
@Test
public void generateUniqueName() throws Exception {
this.context = load("spring.datasource.generate-unique-name=true");
AnnotationConfigApplicationContext context2 = load(
"spring.datasource.generate-unique-name=true");
try {
try (AnnotationConfigApplicationContext context2 = load(
"spring.datasource.generate-unique-name=true")) {
DataSource dataSource = this.context.getBean(DataSource.class);
DataSource dataSource2 = context2.getBean(DataSource.class);
assertThat(getDatabaseName(dataSource))
.isNotEqualTo(getDatabaseName(dataSource2));
}
finally {
context2.close();
}
}
private String getDatabaseName(DataSource dataSource) throws SQLException {
Connection connection = dataSource.getConnection();
try {
try (Connection connection = dataSource.getConnection()) {
ResultSet catalogs = connection.getMetaData().getCatalogs();
if (catalogs.next()) {
return catalogs.getString(1);
@@ -80,9 +75,6 @@ public class EmbeddedDataSourceConfigurationTests {
throw new IllegalStateException("Unable to get database name");
}
}
finally {
connection.close();
}
}
private AnnotationConfigApplicationContext load(String... environment) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -234,9 +234,8 @@ public class ArtemisAutoConfigurationTests {
@Test
public void severalEmbeddedBrokers() {
load(EmptyConfiguration.class, "spring.artemis.embedded.queues=Queue1");
AnnotationConfigApplicationContext anotherContext = doLoad(
EmptyConfiguration.class, "spring.artemis.embedded.queues=Queue2");
try {
try (AnnotationConfigApplicationContext anotherContext = doLoad(
EmptyConfiguration.class, "spring.artemis.embedded.queues=Queue2")) {
ArtemisProperties properties = this.context.getBean(ArtemisProperties.class);
ArtemisProperties anotherProperties = anotherContext
.getBean(ArtemisProperties.class);
@@ -249,29 +248,23 @@ public class ArtemisAutoConfigurationTests {
anotherChecker.checkQueue("Queue2", true);
anotherChecker.checkQueue("Queue1", true);
}
finally {
anotherContext.close();
}
}
@Test
public void connectToASpecificEmbeddedBroker() {
load(EmptyConfiguration.class, "spring.artemis.embedded.serverId=93",
"spring.artemis.embedded.queues=Queue1");
AnnotationConfigApplicationContext anotherContext = doLoad(
try (AnnotationConfigApplicationContext anotherContext = doLoad(
EmptyConfiguration.class, "spring.artemis.mode=embedded",
"spring.artemis.embedded.serverId=93", // Connect to the "main" broker
"spring.artemis.embedded.enabled=false"); // do not start a specific one
try {
"spring.artemis.embedded.serverId=93", /* Connect to the "main" broker */
"spring.artemis.embedded.enabled=false" /* do not start a specific one */)) {
DestinationChecker checker = new DestinationChecker(this.context);
checker.checkQueue("Queue1", true);
DestinationChecker anotherChecker = new DestinationChecker(anotherContext);
anotherChecker.checkQueue("Queue1", true);
}
finally {
anotherContext.close();
}
}
private TransportConfiguration assertInVmConnectionFactory(

View File

@@ -108,9 +108,8 @@ public class EmbeddedMongoAutoConfigurationTests {
@Test
public void portIsAvailableInParentContext() {
ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext();
parent.refresh();
try {
try (ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext()) {
parent.refresh();
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
this.context.register(EmbeddedMongoAutoConfiguration.class,
@@ -119,9 +118,6 @@ public class EmbeddedMongoAutoConfigurationTests {
assertThat(parent.getEnvironment().getProperty("local.mongo.port"))
.isNotNull();
}
finally {
parent.close();
}
}
@Test

View File

@@ -108,16 +108,12 @@ public class SecurityAutoConfigurationTests {
@Test
public void testFilterIsNotRegisteredInNonWeb() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SecurityAutoConfiguration.class,
SecurityFilterAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
try {
context.refresh();
assertThat(context.containsBean("securityFilterChainRegistration")).isFalse();
}
finally {
context.close();
try (AnnotationConfigApplicationContext customContext = new AnnotationConfigApplicationContext()) {
customContext.register(SecurityAutoConfiguration.class,
SecurityFilterAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
customContext.refresh();
assertThat(customContext.containsBean("securityFilterChainRegistration")).isFalse();
}
}

View File

@@ -58,8 +58,7 @@ public class SecurityFilterAutoConfigurationEarlyInitializationTests {
@Test
public void testSecurityFilterDoesNotCauseEarlyInitialization() throws Exception {
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext();
try {
try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext()) {
EnvironmentTestUtils.addEnvironment(context, "server.port:0",
"security.user.password:password");
context.register(Config.class);
@@ -70,10 +69,6 @@ public class SecurityFilterAutoConfigurationEarlyInitializationTests {
// If early initialization occurred a ConverterNotFoundException is thrown
}
finally {
context.close();
}
}
@Configuration

View File

@@ -44,16 +44,11 @@ public class SecurityFilterAutoConfigurationTests {
@Test
public void filterAutoConfigurationWorksWithoutSecurityAutoConfiguration()
throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
try {
try (AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext()) {
context.setServletContext(new MockServletContext());
context.register(Config.class);
context.refresh();
}
finally {
context.close();
}
}
@Configuration

View File

@@ -173,20 +173,17 @@ public class ThymeleafServletAutoConfigurationTests {
@Test
public void renderNonWebAppTemplate() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
try (AnnotationConfigApplicationContext customContext = new AnnotationConfigApplicationContext(
ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
assertThat(context.getBeanNamesForType(ViewResolver.class).length).isEqualTo(0);
try {
TemplateEngine engine = context.getBean(TemplateEngine.class);
PropertyPlaceholderAutoConfiguration.class)) {
assertThat(customContext.getBeanNamesForType(ViewResolver.class).length)
.isEqualTo(0);
TemplateEngine engine = customContext.getBean(TemplateEngine.class);
Context attrs = new Context(Locale.UK,
Collections.singletonMap("greeting", "Hello World"));
String result = engine.process("message", attrs);
assertThat(result).contains("Hello World");
}
finally {
context.close();
}
}
@Test