This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
The following commit(s) were added to refs/heads/master by this push: new 6adf321 CONFIGURATION-805 - Use try with resource (#114) 6adf321 is described below commit 6adf321ba1eeacef853501880b0f033ebbd2face Author: Arturo Bernal <arturobern...@gmail.com> AuthorDate: Sat Jun 26 17:26:45 2021 +0200 CONFIGURATION-805 - Use try with resource (#114) --- .../configuration2/TestXMLConfiguration.java | 12 +------- .../commons/configuration2/io/TestFileHandler.java | 33 +++------------------- 2 files changed, 5 insertions(+), 40 deletions(-) diff --git a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java index 60c4bb5..1b6555e 100644 --- a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java +++ b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java @@ -1325,20 +1325,10 @@ public class TestXMLConfiguration @Test public void testSaveToStream() throws ConfigurationException, IOException { - FileOutputStream out = null; final FileHandler handler = new FileHandler(conf); - try - { - out = new FileOutputStream(testSaveConf); + try (FileOutputStream out = new FileOutputStream(testSaveConf)) { handler.save(out, "UTF8"); } - finally - { - if(out != null) - { - out.close(); - } - } checkSavedConfig(testSaveConf); } diff --git a/src/test/java/org/apache/commons/configuration2/io/TestFileHandler.java b/src/test/java/org/apache/commons/configuration2/io/TestFileHandler.java index c9a2769..3366f11 100644 --- a/src/test/java/org/apache/commons/configuration2/io/TestFileHandler.java +++ b/src/test/java/org/apache/commons/configuration2/io/TestFileHandler.java @@ -148,31 +148,12 @@ public class TestFileHandler */ private static String readFile(final File f) { - Reader in = null; - try - { - in = new FileReader(f); + try (Reader in = new FileReader(f)) { return readReader(in); - } - catch (final IOException ioex) - { + } catch (final IOException ioex) { fail("Could not read file: " + ioex); return null; // cannot happen } - finally - { - if (in != null) - { - try - { - in.close(); - } - catch (final IOException ioex) - { - // ignore - } - } - } } /** @@ -726,16 +707,10 @@ public class TestFileHandler public void testSaveToStream() throws ConfigurationException, IOException { final File file = folder.newFile(); - final FileOutputStream out = new FileOutputStream(file); - final FileHandler handler = new FileHandler(new FileBasedTestImpl()); - try - { + try (FileOutputStream out = new FileOutputStream(file)) { + final FileHandler handler = new FileHandler(new FileBasedTestImpl()); handler.save(out); } - finally - { - out.close(); - } assertEquals("Wrong content", CONTENT, readFile(file)); }