Skip to content
Permalink
Browse files
Use try-with-resource.
  • Loading branch information
garydgregory committed Nov 21, 2020
1 parent aeaac81 commit 9633f81fdd49b464b4c2206e16405b6045ac24fb
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 68 deletions.
@@ -44,7 +44,8 @@ public InputStream getInputStream(final URL url) throws ConfigurationException
}

@Override
public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions) throws ConfigurationException
public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions)
throws ConfigurationException
{
// throw an exception if the target URL is a directory
final File file = FileLocatorUtils.fileFromURL(url);
@@ -228,17 +229,10 @@ public URL locateFromURL(final String basePath, final String fileName)
url = new URL(baseURL, fileName);

// check if the file exists
InputStream in = null;
try
try (InputStream in = url.openStream())
{
in = url.openStream();
}
finally
{
if (in != null)
{
in.close();
}
// nothing

}
return url;
}
@@ -197,15 +197,10 @@ private static String saveToString(final INIConfiguration config)
private File writeTestFile(final String content) throws IOException
{
final File file = folder.newFile();
final PrintWriter out = new PrintWriter(new FileWriter(file));
try
try (PrintWriter out = new PrintWriter(new FileWriter(file)))
{
out.println(content);
}
finally
{
out.close();
}
return file;
}

@@ -905,9 +905,8 @@ public void testKeepSeparators() throws ConfigurationException, IOException
"test.separator.no.space=foo"
};
final Set<String> foundLines = new HashSet<>();
final BufferedReader in = new BufferedReader(new FileReader(
testSavePropertiesFile));
try
try (BufferedReader in = new BufferedReader(new FileReader(
testSavePropertiesFile)))
{
String s;
while ((s = in.readLine()) != null)
@@ -920,10 +919,6 @@ public void testKeepSeparators() throws ConfigurationException, IOException
}
}
}
finally
{
in.close();
}
assertEquals("No all separators were found: " + foundLines,
separatorTests.length, foundLines.size());
}
@@ -1349,8 +1344,7 @@ public void testPropertyLoadedIncludeNotAllowed() throws ConfigurationException
public void testReadCalledDirectly() throws IOException
{
conf = new PropertiesConfiguration();
final Reader in = new FileReader(ConfigurationAssert.getTestFile("test.properties"));
try
try (Reader in = new FileReader(ConfigurationAssert.getTestFile("test.properties")))
{
conf.read(in);
fail("No exception thrown!");
@@ -1359,10 +1353,6 @@ public void testReadCalledDirectly() throws IOException
{
assertThat(e.getMessage(), containsString("FileHandler"));
}
finally
{
in.close();
}
}

/**
@@ -1351,19 +1351,10 @@ public void testSaveToStreamWithEncoding() throws ConfigurationException, IOExce
{
final FileHandler handler = new FileHandler(conf);
handler.setEncoding("UTF8");
FileOutputStream out = null;
try
try (FileOutputStream out = new FileOutputStream(testSaveConf))
{
out = new FileOutputStream(testSaveConf);
handler.save(out);
}
finally
{
if(out != null)
{
out.close();
}
}

checkSavedConfig(testSaveConf);
}
@@ -107,19 +107,10 @@ private static void addReloadSource(final Configuration config, final String fil
*/
private static void writeFile(final File file, final String content) throws IOException
{
PrintWriter out = null;
try
try (PrintWriter out = new PrintWriter(new FileWriter(file)))
{
out = new PrintWriter(new FileWriter(file));
out.print(content);
}
finally
{
if (out != null)
{
out.close();
}
}
}

/**
@@ -606,15 +606,10 @@ public void testLoadFromStream() throws Exception
final File file = createTestFile();
final FileBasedTestImpl content = new FileBasedTestImpl();
final FileHandler handler = new FileHandler(content);
final FileInputStream in = new FileInputStream(file);
try
try (FileInputStream in = new FileInputStream(file))
{
handler.load(in);
}
finally
{
in.close();
}
assertEquals("Wrong content", CONTENT, content.getContent());
}

@@ -627,15 +622,10 @@ public void testLoadFromReader() throws Exception
final File file = createTestFile();
final FileBasedTestImpl content = new FileBasedTestImpl();
final FileHandler handler = new FileHandler(content);
final Reader in = new FileReader(file);
try
try (Reader in = new FileReader(file))
{
handler.load(in);
}
finally
{
in.close();
}
assertEquals("Wrong content", CONTENT, content.getContent());
}

@@ -552,8 +552,7 @@ public void testWriteCalledDirectly() throws IOException
config = new XMLPropertyListConfiguration();
config.addProperty("foo", "bar");

final Writer out = new FileWriter(folder.newFile());
try
try (Writer out = new FileWriter(folder.newFile()))
{
config.write(out);
fail("No exception thrown!");
@@ -562,9 +561,5 @@ public void testWriteCalledDirectly() throws IOException
{
assertThat(e.getMessage(), containsString("FileHandler"));
}
finally
{
out.close();
}
}
}

0 comments on commit 9633f81

Please sign in to comment.