Use try-with-resources language construct where feasible

Closes gh-2063

Co-authored-by: igor-suhorukov <igor.suhorukov@gmail.com>
This commit is contained in:
Sam Brannen
2020-06-16 22:57:45 +02:00
parent 456d2c46e3
commit 8099fc8178
23 changed files with 179 additions and 394 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -146,13 +146,9 @@ public class ConfigurableMimeFileTypeMap extends FileTypeMap implements Initiali
protected FileTypeMap createFileTypeMap(@Nullable Resource mappingLocation, @Nullable String[] mappings) throws IOException {
MimetypesFileTypeMap fileTypeMap = null;
if (mappingLocation != null) {
InputStream is = mappingLocation.getInputStream();
try {
try (InputStream is = mappingLocation.getInputStream()) {
fileTypeMap = new MimetypesFileTypeMap(is);
}
finally {
is.close();
}
}
else {
fileTypeMap = new MimetypesFileTypeMap();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -101,10 +101,7 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
@Test
public void exceptionCacheResolverLazilyRequired() {
ConfigurableApplicationContext context =
new AnnotationConfigApplicationContext(NoExceptionCacheResolverConfig.class);
try {
try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(NoExceptionCacheResolverConfig.class)) {
DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
assertThat(cos.getCacheResolver()).isSameAs(context.getBean("cacheResolver"));
@@ -115,9 +112,6 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
assertThatIllegalStateException().isThrownBy(() ->
service.cacheWithException("test", false));
}
finally {
context.close();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -295,40 +295,31 @@ public class QuartzSupportTests {
@Test // SPR-772
public void multipleSchedulers() throws Exception {
ClassPathXmlApplicationContext ctx = context("multipleSchedulers.xml");
try {
try (ClassPathXmlApplicationContext ctx = context("multipleSchedulers.xml")) {
Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1");
Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2");
assertThat(scheduler2).isNotSameAs(scheduler1);
assertThat(scheduler1.getSchedulerName()).isEqualTo("quartz1");
assertThat(scheduler2.getSchedulerName()).isEqualTo("quartz2");
}
finally {
ctx.close();
}
}
@Test // SPR-16884
public void multipleSchedulersWithQuartzProperties() throws Exception {
ClassPathXmlApplicationContext ctx = context("multipleSchedulersWithQuartzProperties.xml");
try {
try (ClassPathXmlApplicationContext ctx = context("multipleSchedulersWithQuartzProperties.xml")) {
Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1");
Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2");
assertThat(scheduler2).isNotSameAs(scheduler1);
assertThat(scheduler1.getSchedulerName()).isEqualTo("quartz1");
assertThat(scheduler2.getSchedulerName()).isEqualTo("quartz2");
}
finally {
ctx.close();
}
}
@Test
@EnabledForTestGroups(PERFORMANCE)
public void twoAnonymousMethodInvokingJobDetailFactoryBeans() throws Exception {
ClassPathXmlApplicationContext ctx = context("multipleAnonymousMethodInvokingJobDetailFB.xml");
Thread.sleep(3000);
try {
try (ClassPathXmlApplicationContext ctx = context("multipleAnonymousMethodInvokingJobDetailFB.xml")) {
QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
QuartzTestBean importService = (QuartzTestBean) ctx.getBean("importService");
@@ -337,17 +328,13 @@ public class QuartzSupportTests {
assertThat(importService.getImportCount()).as("doImport not called on importService").isEqualTo(2);
assertThat(importService.getExportCount()).as("doExport called on importService").isEqualTo(0);
}
finally {
ctx.close();
}
}
@Test
@EnabledForTestGroups(PERFORMANCE)
public void schedulerAccessorBean() throws Exception {
ClassPathXmlApplicationContext ctx = context("schedulerAccessorBean.xml");
Thread.sleep(3000);
try {
try (ClassPathXmlApplicationContext ctx = context("schedulerAccessorBean.xml")) {
QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
QuartzTestBean importService = (QuartzTestBean) ctx.getBean("importService");
@@ -356,9 +343,6 @@ public class QuartzSupportTests {
assertThat(importService.getImportCount()).as("doImport not called on importService").isEqualTo(2);
assertThat(importService.getExportCount()).as("doExport called on importService").isEqualTo(0);
}
finally {
ctx.close();
}
}
@Test
@@ -387,9 +371,9 @@ public class QuartzSupportTests {
@Test
public void schedulerRepositoryExposure() throws Exception {
ClassPathXmlApplicationContext ctx = context("schedulerRepositoryExposure.xml");
assertThat(ctx.getBean("scheduler")).isSameAs(SchedulerRepository.getInstance().lookup("myScheduler"));
ctx.close();
try (ClassPathXmlApplicationContext ctx = context("schedulerRepositoryExposure.xml")) {
assertThat(ctx.getBean("scheduler")).isSameAs(SchedulerRepository.getInstance().lookup("myScheduler"));
}
}
/**
@@ -401,19 +385,15 @@ public class QuartzSupportTests {
DummyJob.param = 0;
DummyJob.count = 0;
ClassPathXmlApplicationContext ctx = context("databasePersistence.xml");
JdbcTemplate jdbcTemplate = new JdbcTemplate(ctx.getBean(DataSource.class));
assertThat(jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").isEmpty()).as("No triggers were persisted").isFalse();
try (ClassPathXmlApplicationContext ctx = context("databasePersistence.xml")) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(ctx.getBean(DataSource.class));
assertThat(jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").isEmpty()).as("No triggers were persisted").isFalse();
/*
Thread.sleep(3000);
try {
assertTrue("DummyJob should have been executed at least once.", DummyJob.count > 0);
/*
Thread.sleep(3000);
assertTrue("DummyJob should have been executed at least once.", DummyJob.count > 0);
*/
}
finally {
ctx.close();
}
*/
}
private ClassPathXmlApplicationContext context(String path) {