test/java/util/zip/TestEmptyZip.java

Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: XXX

*** 76,146 **** check(ze.getMessage().contains("zip file is empty")); } else { pass(); } } ! ZipInputStream zis = null; ! try { ! zis = new ZipInputStream(new FileInputStream(f)); ZipEntry ze = zis.getNextEntry(); check(ze == null); } catch (IOException ex) { unexpected(ex); - } finally { - if (zis != null) zis.close(); } } static void write(File f) throws Exception { ! ZipOutputStream zos = null; ! try { ! zos = new ZipOutputStream(new FileOutputStream(f)); zos.finish(); - zos.close(); pass(); } catch (Exception ex) { unexpected(ex); - } finally { - if (zos != null) { - zos.close(); - } } } static void readFile(File f) throws Exception { ! ZipFile zf = null; ! try { ! zf = new ZipFile(f); Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); fail(); } - zf.close(); pass(); } catch (Exception ex) { unexpected(ex); - } finally { - if (zf != null) { - zf.close(); - } } } static void readStream(File f) throws Exception { ! ZipInputStream zis = null; ! try { ! zis = new ZipInputStream(new FileInputStream(f)); ZipEntry ze = zis.getNextEntry(); check(ze == null); byte[] buf = new byte[1024]; check(zis.read(buf, 0, 1024) == -1); - } finally { - if (zis != null) { - zis.close(); - } } } //--------------------- Infrastructure --------------------------- static volatile int passed = 0, failed = 0; --- 76,128 ---- check(ze.getMessage().contains("zip file is empty")); } else { pass(); } } ! try (FileInputStream fis = new FileInputStream(f); ! ZipInputStream zis = new ZipInputStream(fis)) ! { ZipEntry ze = zis.getNextEntry(); check(ze == null); } catch (IOException ex) { unexpected(ex); } } static void write(File f) throws Exception { ! try (FileOutputStream fis = new FileOutputStream(f); ! ZipOutputStream zos = new ZipOutputStream(fis)) ! { zos.finish(); pass(); } catch (Exception ex) { unexpected(ex); } } static void readFile(File f) throws Exception { ! try (ZipFile zf = new ZipFile(f)) { Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); fail(); } pass(); } catch (Exception ex) { unexpected(ex); } } static void readStream(File f) throws Exception { ! try (FileInputStream fis = new FileInputStream(f); ! ZipInputStream zis = new ZipInputStream(fis)) ! { ZipEntry ze = zis.getNextEntry(); check(ze == null); byte[] buf = new byte[1024]; check(zis.read(buf, 0, 1024) == -1); } } //--------------------- Infrastructure --------------------------- static volatile int passed = 0, failed = 0;