提交 d58c230b 编写于 作者: S sherman

6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open

Summary: Close zipfile and io stream when done
Reviewed-by: alanb
上级 a830a508
...@@ -1205,17 +1205,5 @@ java/util/Locale/LocaleTest.java generic-all ...@@ -1205,17 +1205,5 @@ java/util/Locale/LocaleTest.java generic-all
# Need to be marked othervm, or changed to be samevm safe # Need to be marked othervm, or changed to be samevm safe
java/util/WeakHashMap/GCDuringIteration.java generic-all java/util/WeakHashMap/GCDuringIteration.java generic-all
# Possible missing input stream close()? Causes samevm issues on windows
java/util/zip/InfoZip.java generic-all
# Missing a close() on file Test0.zip? windows samevm cascade problem
java/util/zip/ZipFile/Comment.java generic-all
# Suspect missing close() on bad*.zip files, windows cascade errors with samevm
java/util/zip/ZipFile/CorruptedZipFiles.java generic-all
# Should be samevm but causes problems with samevm, no details:
java/util/zip/ZipFile/ManyEntries.java generic-all
############################################################################ ############################################################################
/* /*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -103,19 +103,25 @@ public class InfoZip { ...@@ -103,19 +103,25 @@ public class InfoZip {
os.close(); os.close();
ZipFile zf = new ZipFile(f); ZipFile zf = new ZipFile(f);
Enumeration<? extends ZipEntry> entries = zf.entries(); ZipEntry ze = null;
ZipEntry ze = entries.nextElement(); try {
check(! entries.hasMoreElements()); Enumeration<? extends ZipEntry> entries = zf.entries();
checkZipEntry(ze, contents(zf, ze)); ze = entries.nextElement();
zf.close(); check(! entries.hasMoreElements());
checkZipEntry(ze, contents(zf, ze));
} finally {
zf.close();
}
ZipInputStream is = new ZipInputStream(new FileInputStream(f)); ZipInputStream is = new ZipInputStream(new FileInputStream(f));
ze = is.getNextEntry(); try {
checkZipEntry(ze, contents(is)); ze = is.getNextEntry();
check(is.getNextEntry() == null); checkZipEntry(ze, contents(is));
check(is.getNextEntry() == null);
} finally {
is.close();
}
f.delete(); f.delete();
System.out.printf("passed = %d, failed = %d%n", passed, failed); System.out.printf("passed = %d, failed = %d%n", passed, failed);
if (failed > 0) throw new Exception("Some tests failed"); if (failed > 0) throw new Exception("Some tests failed");
} }
......
/* /*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -58,13 +58,16 @@ public class Comment { ...@@ -58,13 +58,16 @@ public class Comment {
throws IOException throws IOException
{ {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(name)); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(name));
zos.setComment(comment); try {
ZipEntry ze = new ZipEntry(entryName); zos.setComment(comment);
ze.setMethod(ZipEntry.DEFLATED); ZipEntry ze = new ZipEntry(entryName);
zos.putNextEntry(ze); ze.setMethod(ZipEntry.DEFLATED);
new DataOutputStream(zos).writeUTF(entryContents); zos.putNextEntry(ze);
zos.closeEntry(); new DataOutputStream(zos).writeUTF(entryContents);
zos.close(); zos.closeEntry();
} finally {
zos.close();
}
} }
private static void verifyZipFile(String name, String comment) private static void verifyZipFile(String name, String comment)
...@@ -91,6 +94,8 @@ public class Comment { ...@@ -91,6 +94,8 @@ public class Comment {
file.seek(file.length() - comment.length()); file.seek(file.length() - comment.length());
byte [] bytes = new byte [comment.length()]; byte [] bytes = new byte [comment.length()];
file.readFully(bytes); file.readFully(bytes);
zipFile.close();
file.close();
if (! comment.equals(new String(bytes, "UTF8"))) if (! comment.equals(new String(bytes, "UTF8")))
throw new Exception("Zip file comment corrupted"); throw new Exception("Zip file comment corrupted");
} }
......
/* /*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -47,13 +47,14 @@ public class CorruptedZipFiles { ...@@ -47,13 +47,14 @@ public class CorruptedZipFiles {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ZipOutputStream zos = new ZipOutputStream( ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("x.zip"));
new FileOutputStream("x.zip")); try {
ZipEntry e = new ZipEntry("x"); ZipEntry e = new ZipEntry("x");
zos.putNextEntry(e); zos.putNextEntry(e);
zos.write((int)'x'); zos.write((int)'x');
zos.close(); } finally {
zos = null; zos.close();
}
int len = (int)(new File("x.zip").length()); int len = (int)(new File("x.zip").length());
byte[] good = new byte[len]; byte[] good = new byte[len];
...@@ -153,12 +154,15 @@ public class CorruptedZipFiles { ...@@ -153,12 +154,15 @@ public class CorruptedZipFiles {
fos.write(data); fos.write(data);
fos.close(); fos.close();
ZipFile zf = new ZipFile(zipName); ZipFile zf = new ZipFile(zipName);
if (getInputStream) { try {
InputStream is = zf.getInputStream(new ZipEntry("x")); if (getInputStream) {
is.read(); InputStream is = zf.getInputStream(new ZipEntry("x"));
is.read();
}
} finally {
zf.close();
} }
fail("Failed to throw expected ZipException"); fail("Failed to throw expected ZipException");
zf.close();
} catch (ZipException e) { } catch (ZipException e) {
if (e.getMessage().matches(msgPattern)) if (e.getMessage().matches(msgPattern))
passed++; passed++;
......
/* /*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -55,52 +55,58 @@ public class ManyEntries { ...@@ -55,52 +55,58 @@ public class ManyEntries {
File zipFile = new File(++uniquifier + ".zip"); File zipFile = new File(++uniquifier + ".zip");
try { try {
zipFile.delete(); zipFile.delete();
ZipOutputStream zos = ZipOutputStream zos = new ZipOutputStream(
new ZipOutputStream( new BufferedOutputStream(
new BufferedOutputStream( new FileOutputStream(zipFile)));
new FileOutputStream(zipFile))); try {
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
ZipEntry e = new ZipEntry("DIR/"+i); ZipEntry e = new ZipEntry("DIR/"+i);
e.setMethod(method); e.setMethod(method);
e.setTime(time); e.setTime(time);
if (method == ZipEntry.STORED) { if (method == ZipEntry.STORED) {
e.setSize(1); e.setSize(1);
crc32.reset(); crc32.reset();
crc32.update((byte)i); crc32.update((byte)i);
e.setCrc(crc32.getValue()); e.setCrc(crc32.getValue());
} else { } else {
e.setSize(0); e.setSize(0);
e.setCrc(0); e.setCrc(0);
}
zos.putNextEntry(e);
zos.write(i);
} }
zos.putNextEntry(e); } finally {
zos.write(i); zos.close();
zos = null;
} }
zos.close();
zos = null;
ZipFile zip = new ZipFile(zipFile); ZipFile zip = zip = new ZipFile(zipFile);
if (! (zip.size() == N)) try {
throw new Exception("Bad ZipFile size: " + zip.size()); if (! (zip.size() == N))
Enumeration entries = zip.entries(); throw new Exception("Bad ZipFile size: " + zip.size());
Enumeration entries = zip.entries();
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
if (i % 1000 == 0) {System.gc(); System.runFinalization();} if (i % 1000 == 0) {System.gc(); System.runFinalization();}
if (! (entries.hasMoreElements())) if (! (entries.hasMoreElements()))
throw new Exception("only " + i + " elements"); throw new Exception("only " + i + " elements");
ZipEntry e = (ZipEntry)entries.nextElement(); ZipEntry e = (ZipEntry)entries.nextElement();
if (! (e.getSize() == 1)) if (! (e.getSize() == 1))
throw new Exception("bad size: " + e.getSize()); throw new Exception("bad size: " + e.getSize());
if (! (e.getName().equals("DIR/" + i))) if (! (e.getName().equals("DIR/" + i)))
throw new Exception("bad name: " + i); throw new Exception("bad name: " + i);
if (! (e.getMethod() == method)) if (! (e.getMethod() == method))
throw new Exception("getMethod="+e.getMethod()+", method=" + method); throw new Exception("getMethod="+e.getMethod()+", method=" + method);
if (! (e.getTime() == time)) if (! (e.getTime() == time))
throw new Exception("getTime="+e.getTime()+", time=" + time); throw new Exception("getTime="+e.getTime()+", time=" + time);
if (! (zip.getInputStream(e).read() == (i & 0xff))) if (! (zip.getInputStream(e).read() == (i & 0xff)))
throw new Exception("Bad file contents: " + i); throw new Exception("Bad file contents: " + i);
}
if (entries.hasMoreElements())
throw new Exception("too many elements");
} finally {
zip.close();
} }
if (entries.hasMoreElements())
throw new Exception("too many elements");
} }
finally { finally {
zipFile.delete(); zipFile.delete();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册