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: alanb, dholmes, sherman


  61         write(f);
  62 
  63         // Verify 0-entries file can be read
  64         readFile(f);
  65         readStream(f);
  66 
  67         f.delete();
  68     }
  69 
  70     static void checkCannotRead(File f) throws IOException {
  71         try {
  72             new ZipFile(f).close();
  73             fail();
  74         } catch (ZipException ze) {
  75             if (f.length() == 0) {
  76                 check(ze.getMessage().contains("zip file is empty"));
  77             } else {
  78                 pass();
  79             }
  80         }
  81         ZipInputStream zis = null;
  82         try {
  83             zis = new ZipInputStream(new FileInputStream(f));
  84             ZipEntry ze = zis.getNextEntry();
  85             check(ze == null);
  86         } catch (IOException ex) {
  87             unexpected(ex);
  88         } finally {
  89             if (zis != null) zis.close();
  90         }
  91     }
  92 
  93     static void write(File f) throws Exception {
  94         ZipOutputStream zos = null;
  95         try {
  96             zos = new ZipOutputStream(new FileOutputStream(f));
  97             zos.finish();
  98             zos.close();
  99             pass();
 100         } catch (Exception ex) {
 101             unexpected(ex);
 102         } finally {
 103             if (zos != null) {
 104                 zos.close();
 105             }
 106         }
 107     }
 108 
 109     static void readFile(File f) throws Exception {
 110         ZipFile zf = null;
 111         try {
 112             zf = new ZipFile(f);
 113 
 114             Enumeration e = zf.entries();
 115             while (e.hasMoreElements()) {
 116                 ZipEntry entry = (ZipEntry) e.nextElement();
 117                 fail();
 118             }
 119             zf.close();
 120             pass();
 121         } catch (Exception ex) {
 122             unexpected(ex);
 123         } finally {
 124             if (zf != null) {
 125                 zf.close();
 126             }
 127         }
 128     }
 129 
 130     static void readStream(File f) throws Exception {
 131         ZipInputStream zis = null;
 132         try {
 133             zis = new ZipInputStream(new FileInputStream(f));
 134             ZipEntry ze = zis.getNextEntry();
 135             check(ze == null);
 136             byte[] buf = new byte[1024];
 137             check(zis.read(buf, 0, 1024) == -1);
 138         } finally {
 139             if (zis != null) {
 140                 zis.close();
 141             }
 142         }
 143     }
 144 
 145     //--------------------- Infrastructure ---------------------------
 146     static volatile int passed = 0, failed = 0;
 147     static boolean pass() {passed++; return true;}
 148     static boolean fail() {failed++; Thread.dumpStack(); return false;}
 149     static boolean fail(String msg) {System.out.println(msg); return fail();}
 150     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 151     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
 152     static boolean equal(Object x, Object y) {
 153         if (x == null ? y == null : x.equals(y)) return pass();
 154         else return fail(x + " not equal to " + y);}
 155     public static void main(String[] args) throws Throwable {
 156         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 157         System.out.println("\nPassed = " + passed + " failed = " + failed);
 158         if (failed > 0) throw new AssertionError("Some tests failed");}
 159 }


  61         write(f);
  62 
  63         // Verify 0-entries file can be read
  64         readFile(f);
  65         readStream(f);
  66 
  67         f.delete();
  68     }
  69 
  70     static void checkCannotRead(File f) throws IOException {
  71         try {
  72             new ZipFile(f).close();
  73             fail();
  74         } catch (ZipException ze) {
  75             if (f.length() == 0) {
  76                 check(ze.getMessage().contains("zip file is empty"));
  77             } else {
  78                 pass();
  79             }
  80         }
  81         try (FileInputStream fis = new FileInputStream(f);
  82              ZipInputStream zis = new ZipInputStream(fis))
  83         {
  84             ZipEntry ze = zis.getNextEntry();
  85             check(ze == null);
  86         } catch (IOException ex) {
  87             unexpected(ex);


  88         }
  89     }
  90 
  91     static void write(File f) throws Exception {
  92         try (FileOutputStream fis = new FileOutputStream(f);
  93              ZipOutputStream zos = new ZipOutputStream(fis))
  94         {
  95             zos.finish();

  96             pass();
  97         } catch (Exception ex) {
  98             unexpected(ex);




  99         }
 100     }
 101 
 102     static void readFile(File f) throws Exception {
 103         try (ZipFile zf = new ZipFile(f)) {


 104 
 105             Enumeration e = zf.entries();
 106             while (e.hasMoreElements()) {
 107                 ZipEntry entry = (ZipEntry) e.nextElement();
 108                 fail();
 109             }

 110             pass();
 111         } catch (Exception ex) {
 112             unexpected(ex);




 113         }
 114     }
 115 
 116     static void readStream(File f) throws Exception {
 117         try (FileInputStream fis = new FileInputStream(f);
 118              ZipInputStream zis = new ZipInputStream(fis))
 119         {
 120             ZipEntry ze = zis.getNextEntry();
 121             check(ze == null);
 122             byte[] buf = new byte[1024];
 123             check(zis.read(buf, 0, 1024) == -1);




 124         }
 125     }
 126 
 127     //--------------------- Infrastructure ---------------------------
 128     static volatile int passed = 0, failed = 0;
 129     static boolean pass() {passed++; return true;}
 130     static boolean fail() {failed++; Thread.dumpStack(); return false;}
 131     static boolean fail(String msg) {System.out.println(msg); return fail();}
 132     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 133     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
 134     static boolean equal(Object x, Object y) {
 135         if (x == null ? y == null : x.equals(y)) return pass();
 136         else return fail(x + " not equal to " + y);}
 137     public static void main(String[] args) throws Throwable {
 138         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 139         System.out.println("\nPassed = " + passed + " failed = " + failed);
 140         if (failed > 0) throw new AssertionError("Some tests failed");}
 141 }