You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by gi...@apache.org on 2018/04/02 22:28:26 UTC
[7/8] ant git commit: Use try with resources
Use try with resources
Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/6319e498
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/6319e498
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/6319e498
Branch: refs/heads/master
Commit: 6319e4988b24f7830cf7048534c1caacb38ce550
Parents: 22b69b9
Author: Gintas Grigelionis <gi...@apache.org>
Authored: Mon Apr 2 22:43:43 2018 +0200
Committer: Gintas Grigelionis <gi...@apache.org>
Committed: Mon Apr 2 22:43:43 2018 +0200
----------------------------------------------------------------------
.../ant/taskdefs/optional/ssh/ScpToMessage.java | 4 +-
.../org/apache/tools/ant/FileUtilities.java | 11 +----
.../org/apache/tools/ant/taskdefs/CopyTest.java | 10 +----
.../ant/taskdefs/ExecStreamRedirectorTest.java | 5 +--
.../apache/tools/ant/taskdefs/FixCrLfTest.java | 14 +-----
.../tools/ant/taskdefs/InitializeClassTest.java | 15 +++----
.../org/apache/tools/ant/taskdefs/JarTest.java | 23 +++-------
.../org/apache/tools/ant/taskdefs/JavaTest.java | 13 +-----
.../apache/tools/ant/taskdefs/ManifestTest.java | 10 +----
.../apache/tools/ant/taskdefs/ReplaceTest.java | 15 +------
.../org/apache/tools/ant/taskdefs/ZipTest.java | 44 +++---------------
.../taskdefs/optional/EchoPropertiesTest.java | 11 +----
.../junit/XMLFormatterWithCDATAOnSystemOut.java | 10 +----
.../optional/junit/XMLResultAggregatorTest.java | 5 +--
.../org/apache/tools/zip/UTF8ZipFilesTest.java | 47 +++++++-------------
15 files changed, 52 insertions(+), 185 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
index 7c176ec..81ed07f 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
@@ -347,7 +347,6 @@ public class ScpToMessage extends AbstractSshMessage {
waitForAck(in);
// send a content of lfile
- final InputStream fis = Files.newInputStream(localFile.toPath());
final byte[] buf = new byte[BUFFER_SIZE];
final long startTime = System.currentTimeMillis();
long totalLength = 0;
@@ -359,7 +358,7 @@ public class ScpToMessage extends AbstractSshMessage {
final long initFilesize = filesize;
int percentTransmitted = 0;
- try {
+ try (InputStream fis = Files.newInputStream(localFile.toPath())) {
if (this.getVerbose()) {
log("Sending: " + localFile.getName() + " : " + localFile.length());
}
@@ -385,7 +384,6 @@ public class ScpToMessage extends AbstractSshMessage {
final long endTime = System.currentTimeMillis();
logStats(startTime, endTime, totalLength);
}
- fis.close();
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/FileUtilities.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/FileUtilities.java b/src/tests/junit/org/apache/tools/ant/FileUtilities.java
index 321d8d4..0b7e0ce 100644
--- a/src/tests/junit/org/apache/tools/ant/FileUtilities.java
+++ b/src/tests/junit/org/apache/tools/ant/FileUtilities.java
@@ -46,15 +46,8 @@ public class FileUtilities {
* @throws IOException on error reading the file (not existing, not readable etc)
*/
public static String getFileContents(File file) throws IOException {
- FileReader rdr = null;
- try {
- rdr = new FileReader(file);
- return FileUtils.readFully(rdr);
- }
- finally {
- if (rdr != null) {
- rdr.close();
- }
+ try (FileReader rdr = new FileReader(file)) {
+ return FileUtils.readFully(rdr);
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java
index 961a473..5b36d05 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java
@@ -298,11 +298,8 @@ public class CopyTest {
assertTrue("Source file " + srcFile + " was expected to be a file", srcFile.isFile());
final long originalFileSize = srcFile.length();
final String originalContent;
- final BufferedReader reader = new BufferedReader(new FileReader(srcFile));
- try {
+ try (BufferedReader reader = new BufferedReader(new FileReader(srcFile))) {
originalContent = FileUtils.readFully(reader);
- } finally {
- reader.close();
}
assertTrue("Content missing in file " + srcFile, originalContent != null && originalContent.length() > 0);
@@ -324,12 +321,9 @@ public class CopyTest {
private void assertSizeAndContent(final File file, final long expectedSize, final String expectedContent) throws IOException {
assertTrue(file + " was expected to be a file", file.isFile());
assertEquals("Unexpected size of file " + file, expectedSize, file.length());
- final BufferedReader reader = new BufferedReader(new FileReader(file));
final String content;
- try {
+ try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
content = FileUtils.readFully(reader);
- } finally {
- reader.close();
}
assertEquals("Unexpected content in file " + file, expectedContent, content);
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java
index 7a51946..99cb018 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java
@@ -75,16 +75,13 @@ public class ExecStreamRedirectorTest {
}
private static byte[] readAllBytes(final File file) throws IOException {
- final FileInputStream fis = new FileInputStream(file);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try {
+ try (FileInputStream fis = new FileInputStream(file)) {
final byte[] dataChunk = new byte[1024];
int numRead = -1;
while ((numRead = fis.read(dataChunk)) > 0) {
bos.write(dataChunk, 0, numRead);
}
- } finally {
- fis.close();
}
return bos.toByteArray();
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java
index 451a951..7ba5556 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java
@@ -237,11 +237,8 @@ public class FixCrLfTest {
fail("Expected file " + result + " doesn\'t exist");
}
- InputStream inExpect = null;
- InputStream inResult = null;
- try {
- inExpect = new BufferedInputStream(new FileInputStream(expect));
- inResult = new BufferedInputStream(new FileInputStream(result));
+ try (InputStream inExpect = new BufferedInputStream(new FileInputStream(expect));
+ InputStream inResult = new BufferedInputStream(new FileInputStream(result))) {
int expectedByte = inExpect.read();
while (expectedByte != -1) {
@@ -249,13 +246,6 @@ public class FixCrLfTest {
expectedByte = inExpect.read();
}
assertEquals("End of file", -1, inResult.read());
- } finally {
- if (inResult != null) {
- inResult.close();
- }
- if (inExpect != null) {
- inExpect.close();
- }
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java
index a44303f..04aa153 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java
@@ -56,15 +56,12 @@ public class InitializeClassTest {
buildRule.executeTarget("forked");
synchronized (System.out) {
PrintStream ps = System.out;
- PrintStream newps = new PrintStream(new FileOutputStream(f2));
- try {
- System.setOut(newps);
- buildRule.getProject().executeTarget("unforked");
- } finally {
- System.setOut(ps);
-
- newps.close();
- }
+ try (PrintStream newps = new PrintStream(new FileOutputStream(f2))) {
+ System.setOut(newps);
+ buildRule.getProject().executeTarget("unforked");
+ } finally {
+ System.setOut(ps);
+ }
}
assertEquals(FileUtilities.getFileContents(f1), FileUtilities.getFileContents(f2));
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
index a9ab254..0c46172 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
@@ -261,10 +261,8 @@ public class JarTest {
// bugzilla report 10262
@Test
public void testNoDuplicateIndex() throws IOException {
- ZipFile archive = null;
- try {
- buildRule.executeTarget("testIndexTests");
- archive = new ZipFile(new File(getOutputDir(), tempJar));
+ buildRule.executeTarget("testIndexTests");
+ try (ZipFile archive = new ZipFile(new File(getOutputDir(), tempJar))) {
Enumeration<? extends ZipEntry> e = archive.entries();
int numberOfIndexLists = 0;
while (e.hasMoreElements()) {
@@ -274,24 +272,17 @@ public class JarTest {
}
}
assertEquals(1, numberOfIndexLists);
- } finally {
- if (archive != null) {
- archive.close();
- }
}
}
// bugzilla report 16972
@Test
public void testRootFilesInIndex() throws IOException {
- ZipFile archive = null;
- try {
- buildRule.executeTarget("testIndexTests");
- archive = new ZipFile(new File(getOutputDir(), tempJar));
+ buildRule.executeTarget("testIndexTests");
+ try (ZipFile archive = new ZipFile(new File(getOutputDir(), tempJar))) {
ZipEntry ze = archive.getEntry("META-INF/INDEX.LIST");
InputStream is = archive.getInputStream(ze);
- BufferedReader r = new BufferedReader(new InputStreamReader(is,
- "UTF8"));
+ BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF8"));
boolean foundSub = false;
boolean foundSubFoo = false;
boolean foundFoo = false;
@@ -311,10 +302,6 @@ public class JarTest {
assertTrue(foundSub);
assertTrue(!foundSubFoo);
assertTrue(foundFoo);
- } finally {
- if (archive != null) {
- archive.close();
- }
}
}
@Test
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
index bb66305..59d3cf6 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
@@ -506,22 +506,13 @@ public class JavaTest {
if (argv.length >= 2) {
logFile = argv[1];
}
- OutputStreamWriter out = null;
Thread.sleep(sleepTime * 1000);
- try {
- File dest = new File(logFile);
- FileOutputStream fos = new FileOutputStream(dest);
- out = new OutputStreamWriter(fos);
+ File dest = new File(logFile);
+ try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(dest))) {
out.write("bye bye\n");
} catch (Exception ex) {
- } finally {
- try {
- out.close();
- } catch (IOException ioe) {
- }
}
-
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java
index 690d27e..f7749bc 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java
@@ -287,8 +287,7 @@ public class ManifestTest {
assertEquals("NOT_LONG_NAME_VALUE_MISMATCH", VALUE, value);
Set<String> set = new HashSet<>();
- FileReader fin = new FileReader(expandedManifest);
- try {
+ try (FileReader fin = new FileReader(expandedManifest)) {
BufferedReader in = new BufferedReader(fin);
String read = in.readLine();
@@ -297,8 +296,6 @@ public class ManifestTest {
read = in.readLine();
}
in.close();
- } finally {
- fin.close();
}
assertTrue("Manifest file should have contained string ",
@@ -465,11 +462,8 @@ public class ManifestTest {
* Reads mftest.mf.
*/
private Manifest getManifest(File file) throws IOException, ManifestException {
- FileReader r = new FileReader(file);
- try {
+ try (FileReader r = new FileReader(file)) {
return new Manifest(r);
- } finally {
- r.close();
}
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
index 7dabd3d..35190e7 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
@@ -157,25 +157,14 @@ public class ReplaceTest {
fail("Expected file " + result + " doesn\'t exist");
}
- InputStream inExpect = null;
- InputStream inResult = null;
- try {
- inExpect = new BufferedInputStream(new FileInputStream(expect));
- inResult = new BufferedInputStream(new FileInputStream(result));
-
+ try (InputStream inExpect = new BufferedInputStream(new FileInputStream(expect));
+ InputStream inResult = new BufferedInputStream(new FileInputStream(result))) {
int expectedByte = inExpect.read();
while (expectedByte != -1) {
assertEquals(expectedByte, inResult.read());
expectedByte = inExpect.read();
}
assertEquals("End of file", -1, inResult.read());
- } finally {
- if (inResult != null) {
- inResult.close();
- }
- if (inExpect != null) {
- inExpect.close();
- }
}
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java
index 9f93706..75b3e1d 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java
@@ -102,7 +102,6 @@ public class ZipTest {
if (zfPrefixAddsDir != null) {
zfPrefixAddsDir.close();
}
-
} catch (IOException e) {
//ignored
}
@@ -217,15 +216,8 @@ public class ZipTest {
@Test
public void testDefaultExcludesAndUpdate() throws IOException {
buildRule.executeTarget("testDefaultExcludesAndUpdate");
- ZipFile f = null;
- try {
- f = new ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
- assertNotNull("ziptest~ should be included",
- f.getEntry("ziptest~"));
- } finally {
- if (f != null) {
- f.close();
- }
+ try (ZipFile f = new ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) {
+ assertNotNull("ziptest~ should be included", f.getEntry("ziptest~"));
}
}
@@ -242,60 +234,36 @@ public class ZipTest {
@Test
public void testTarFileSet() throws IOException {
buildRule.executeTarget("testTarFileSet");
- org.apache.tools.zip.ZipFile zf = null;
- try {
- zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
+ try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) {
org.apache.tools.zip.ZipEntry ze = zf.getEntry("asf-logo.gif");
assertEquals(UnixStat.FILE_FLAG | 0446, ze.getUnixMode());
- } finally {
- if (zf != null) {
- zf.close();
- }
}
}
@Test
public void testRewriteZeroPermissions() throws IOException {
buildRule.executeTarget("rewriteZeroPermissions");
- org.apache.tools.zip.ZipFile zf = null;
- try {
- zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
+ try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) {
org.apache.tools.zip.ZipEntry ze = zf.getEntry("testdir/test.txt");
assertEquals(UnixStat.FILE_FLAG | 0644, ze.getUnixMode());
- } finally {
- if (zf != null) {
- zf.close();
- }
}
}
@Test
public void testAcceptZeroPermissions() throws IOException {
buildRule.executeTarget("acceptZeroPermissions");
- org.apache.tools.zip.ZipFile zf = null;
- try {
- zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
+ try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) {
org.apache.tools.zip.ZipEntry ze = zf.getEntry("testdir/test.txt");
assertEquals(0000, ze.getUnixMode());
- } finally {
- if (zf != null) {
- zf.close();
- }
}
}
@Test
public void testForBugzilla34764() throws IOException {
buildRule.executeTarget("testForBugzilla34764");
- org.apache.tools.zip.ZipFile zf = null;
- try {
- zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
+ try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) {
org.apache.tools.zip.ZipEntry ze = zf.getEntry("file1");
assertEquals(UnixStat.FILE_FLAG | 0644, ze.getUnixMode());
- } finally {
- if (zf != null) {
- zf.close();
- }
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java
index 1c2d445..07c6537 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java
@@ -221,17 +221,8 @@ public class EchoPropertiesTest {
throws IOException {
File f = createRelativeFile(relativeFilename);
Properties props = new Properties();
- InputStream in = null;
- try {
- in = new BufferedInputStream(new FileInputStream(f));
+ try (InputStream in = new BufferedInputStream(new FileInputStream(f))) {
props.load(in);
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- }
- }
}
return props;
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java
index 1221ba4..8d03a72 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java
@@ -65,16 +65,10 @@ public class XMLFormatterWithCDATAOnSystemOut {
// avoid endless loop
buildRule.executeTarget("run-junit");
File f = buildRule.getProject().resolveFile(REPORT);
- FileReader reader = null;
- try {
- reader = new FileReader(f);
+ try (FileReader reader = new FileReader(f)) {
String content = FileUtils.readFully(reader);
- assertTrue(content.indexOf("</RESPONSE>]]>"
- + "</ERROR>") > 0);
+ assertTrue(content.contains("</RESPONSE>]]></ERROR>"));
} finally {
- if (reader != null) {
- reader.close();
- }
f.delete();
}
}
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java
index b4d2728..2c21e1a 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java
@@ -50,14 +50,11 @@ public class XMLResultAggregatorTest {
}
assertTrue(d.getAbsolutePath(), d.mkdir());
File xml = new File(d, "x.xml");
- PrintWriter pw = new PrintWriter(new FileOutputStream(xml));
- try {
+ try (PrintWriter pw = new PrintWriter(new FileOutputStream(xml))) {
pw.println("<testsuite errors='0' failures='0' name='my.UnitTest' tests='1'>");
pw.println(" <testcase classname='my.UnitTest' name='testSomething'/>");
pw.println("</testsuite>");
pw.flush();
- } finally {
- pw.close();
}
XMLResultAggregator task = new XMLResultAggregator();
task.setTodir(d);
http://git-wip-us.apache.org/repos/asf/ant/blob/6319e498/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java b/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java
index f2a875a..39828a5 100644
--- a/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java
+++ b/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java
@@ -126,25 +126,23 @@ public class UTF8ZipFilesTest {
ZipEncoding zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
- ZipOutputStream zos = null;
- try {
- zos = new ZipOutputStream(file);
+ try (ZipOutputStream zos = new ZipOutputStream(file)) {
zos.setEncoding(encoding);
zos.setUseLanguageEncodingFlag(withEFS);
zos.setCreateUnicodeExtraFields(withExplicitUnicodeExtra
- ? ZipOutputStream.UnicodeExtraFieldPolicy.NEVER
- : ZipOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
+ ? ZipOutputStream.UnicodeExtraFieldPolicy.NEVER
+ : ZipOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
ZipEntry ze = new ZipEntry(OIL_BARREL_TXT);
if (withExplicitUnicodeExtra
- && !zipEncoding.canEncode(ze.getName())) {
+ && !zipEncoding.canEncode(ze.getName())) {
ByteBuffer en = zipEncoding.encode(ze.getName());
ze.addExtraField(new UnicodePathExtraField(ze.getName(),
- en.array(),
- en.arrayOffset(),
- en.limit()));
+ en.array(),
+ en.arrayOffset(),
+ en.limit()));
}
zos.putNextEntry(ze);
@@ -153,14 +151,14 @@ public class UTF8ZipFilesTest {
ze = new ZipEntry(EURO_FOR_DOLLAR_TXT);
if (withExplicitUnicodeExtra
- && !zipEncoding.canEncode(ze.getName())) {
+ && !zipEncoding.canEncode(ze.getName())) {
ByteBuffer en = zipEncoding.encode(ze.getName());
ze.addExtraField(new UnicodePathExtraField(ze.getName(),
- en.array(),
- en.arrayOffset(),
- en.limit()));
+ en.array(),
+ en.arrayOffset(),
+ en.limit()));
}
zos.putNextEntry(ze);
@@ -170,27 +168,19 @@ public class UTF8ZipFilesTest {
ze = new ZipEntry(ASCII_TXT);
if (withExplicitUnicodeExtra
- && !zipEncoding.canEncode(ze.getName())) {
+ && !zipEncoding.canEncode(ze.getName())) {
ByteBuffer en = zipEncoding.encode(ze.getName());
ze.addExtraField(new UnicodePathExtraField(ze.getName(),
- en.array(),
- en.arrayOffset(),
- en.limit()));
+ en.array(),
+ en.arrayOffset(),
+ en.limit()));
}
zos.putNextEntry(ze);
zos.write("ascii".getBytes("US-ASCII"));
zos.closeEntry();
- } finally {
- if (zos != null) {
- try {
- zos.close();
- } catch (IOException e) {
- /* swallow */
- }
- }
}
}
@@ -247,12 +237,9 @@ public class UTF8ZipFilesTest {
private static void assertCanRead(ZipFile zf, String fileName) throws IOException {
ZipEntry entry = zf.getEntry(fileName);
assertNotNull("Entry " + fileName + " doesn't exist", entry);
- InputStream is = zf.getInputStream(entry);
- assertNotNull("InputStream is null", is);
- try {
+ try (InputStream is = zf.getInputStream(entry)) {
+ assertNotNull("InputStream is null", is);
is.read();
- } finally {
- is.close();
}
}