Browse Source

Use try with resources

master
Gintas Grigelionis 7 years ago
parent
commit
6319e4988b
15 changed files with 52 additions and 185 deletions
  1. +1
    -3
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
  2. +2
    -9
      src/tests/junit/org/apache/tools/ant/FileUtilities.java
  3. +2
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java
  4. +1
    -4
      src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java
  5. +2
    -12
      src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java
  6. +6
    -9
      src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java
  7. +5
    -18
      src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
  8. +2
    -11
      src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
  9. +2
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java
  10. +2
    -13
      src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
  11. +6
    -38
      src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java
  12. +1
    -10
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java
  13. +2
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java
  14. +1
    -4
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java
  15. +17
    -30
      src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java

+ 1
- 3
src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java View File

@@ -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();
}
}



+ 2
- 9
src/tests/junit/org/apache/tools/ant/FileUtilities.java View File

@@ -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);
}
}



+ 2
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java View File

@@ -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);
}


+ 1
- 4
src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java View File

@@ -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();
}


+ 2
- 12
src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java View File

@@ -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();
}
}
}



+ 6
- 9
src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java View File

@@ -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));
}


+ 5
- 18
src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java View File

@@ -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


+ 2
- 11
src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java View File

@@ -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) {
}
}

}
}



+ 2
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java View File

@@ -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();
}
}
}

+ 2
- 13
src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java View File

@@ -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();
}
}
}
}

+ 6
- 38
src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java View File

@@ -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();
}
}
}



+ 1
- 10
src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java View File

@@ -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;
}


+ 2
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java View File

@@ -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>&#x5d;&#x5d;&gt;"
+ "</ERROR>") > 0);
assertTrue(content.contains("</RESPONSE>&#x5d;&#x5d;&gt;</ERROR>"));
} finally {
if (reader != null) {
reader.close();
}
f.delete();
}
}


+ 1
- 4
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java View File

@@ -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);


+ 17
- 30
src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java View File

@@ -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();
}
}



Loading…
Cancel
Save