From 47d4546fde461463aa27215ced67e02546114956 Mon Sep 17 00:00:00 2001 From: alanb Date: Fri, 18 Jun 2010 20:59:13 +0100 Subject: [PATCH] 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open Reviewed-by: mchung --- test/ProblemList.txt | 81 ------------------- .../ClassCastExceptionDetail/Read.java | 6 +- .../AuditStreamSubclass.java | 2 + .../backRefCNFException/Read.java | 14 ++-- .../checkModifiers/CheckModifiers.java | 58 +++++++------ .../classDescFlagConflict/Read.java | 13 ++- .../classDescHooks/ClassDescHooks.java | 21 +++-- .../duplicateSerialFields/Test.java | 12 ++- .../io/Serializable/enum/badResolve/Read.java | 6 +- .../enum/constantSubclasses/Read.java | 16 ++-- .../enum/missingConstant/Read.java | 28 ++++--- .../io/Serializable/fieldTypeString/Read.java | 27 ++++--- .../io/Serializable/illegalHandle/Test.java | 26 ++++-- .../Serializable/longString/LongString.java | 20 +++-- .../Serializable/oldTests/AnnotateClass.java | 61 +++++++------- .../io/Serializable/oldTests/ArrayFields.java | 11 ++- .../Serializable/oldTests/ArraysOfArrays.java | 8 +- .../io/Serializable/oldTests/BinaryTree.java | 42 +++++----- .../Serializable/oldTests/CircularList.java | 11 ++- .../Serializable/oldTests/SimpleArrays.java | 8 +- .../Serializable/oldTests/WritePrimitive.java | 8 +- .../io/Serializable/packageAccess/Test.java | 3 +- .../io/Serializable/parents/EvolvedClass.java | 8 +- .../Serializable/parents/OriginalClass.java | 10 ++- test/java/io/Serializable/proxy/Basic.java | 2 +- .../Serializable/proxy/skipMissing/Read.java | 30 ++++--- .../Serializable/proxy/skipMissing/Write.java | 2 +- .../Serializable/readObjectNoData/Read.java | 20 +++-- .../io/Serializable/skipWriteObject/Read.java | 10 ++- .../skippedObjCNFException/Read.java | 10 ++- .../stopCustomDeserialization/Read.java | 22 ++--- .../unresolvedClassDesc/Read.java | 24 +++--- test/java/io/Serializable/unshared/Read.java | 28 ++++--- .../Serializable/wrongReturnTypes/Read.java | 22 ++--- 34 files changed, 371 insertions(+), 299 deletions(-) diff --git a/test/ProblemList.txt b/test/ProblemList.txt index 560d37b56..6fa0de273 100644 --- a/test/ProblemList.txt +++ b/test/ProblemList.txt @@ -196,80 +196,6 @@ java/beans/XMLEncoder/6329581/Test6329581.java generic-all java/io/BufferedReader/BigMark.java generic-all java/io/BufferedReader/ReadLineSync.java generic-all -# One of these is leaving "a.ser" file open, windows samevm -java/io/Serializable/duplicateSerialFields/Setup.java generic-all -java/io/Serializable/duplicateSerialFields/Test.java generic-all - -# One of these leaving foo.ser open, windows samevm problem -java/io/Serializable/enum/constantSubclasses/Read.java generic-all -java/io/Serializable/enum/constantSubclasses/Write.java generic-all -java/io/Serializable/enum/missingConstant/Read.java generic-all -java/io/Serializable/enum/missingConstant/Write.java generic-all - -# This is leaving subtest1.tmp open, windows samevm problem -java/io/Serializable/oldTests/AnnotateClass.java generic-all - -# One or more of these leave a piotest* file open, windows samevm -java/io/Serializable/oldTests/ArrayFields.java generic-all -java/io/Serializable/oldTests/ArraysOfArrays.java generic-all -java/io/Serializable/oldTests/BinaryTree.java generic-all -java/io/Serializable/oldTests/CircularList.java generic-all -java/io/Serializable/oldTests/SerializeWithException.java generic-all -java/io/Serializable/oldTests/SimpleArrays.java generic-all -java/io/Serializable/oldTests/WritePrimitive.java generic-all - -# Missing close on file 0.ser, windows samevm -java/io/Serializable/enum/badResolve/Read.java generic-all -java/io/Serializable/enum/badResolve/Write.java generic-all - -# One of these tests is leaving parents.ser open, windows samevm -java/io/Serializable/parents/EvolvedClass.java generic-all -java/io/Serializable/parents/OriginalClass.java generic-all - -# One of these tests is leaving file foo.ser and/or bar.ser open, windows samevm -java/io/Serializable/fieldTypeString/Read.java generic-all -java/io/Serializable/fieldTypeString/Write.java generic-all - -# One of these tests is leaving tmp.ser file open, windows samevm -java/io/Serializable/ClassCastExceptionDetail/Read.java generic-all -java/io/Serializable/ClassCastExceptionDetail/Write.java generic-all -java/io/Serializable/GetField/Read.java generic-all -java/io/Serializable/GetField/Read2.java generic-all -java/io/Serializable/GetField/Write.java generic-all -java/io/Serializable/PutField/Read.java generic-all -java/io/Serializable/PutField/Read2.java generic-all -java/io/Serializable/PutField/Write.java generic-all -java/io/Serializable/PutField/Write2.java generic-all -java/io/Serializable/arraySuidConflict/Read.java generic-all -java/io/Serializable/arraySuidConflict/Write.java generic-all -java/io/Serializable/backRefCNFException/Read.java generic-all -java/io/Serializable/backRefCNFException/Write.java generic-all -java/io/Serializable/class/Test.java generic-all -java/io/Serializable/evolution/AddedExternField/ReadAddedField.java generic-all -java/io/Serializable/evolution/AddedExternField/WriteAddedField.java generic-all -java/io/Serializable/evolution/AddedExternField/run.sh generic-all -java/io/Serializable/evolution/AddedField/ReadAddedField.java generic-all -java/io/Serializable/evolution/AddedField/WriteAddedField.java generic-all -java/io/Serializable/evolution/AddedSuperClass/ReadAddedSuperClass.java generic-all -java/io/Serializable/evolution/AddedSuperClass/ReadAddedSuperClass2.java generic-all -java/io/Serializable/evolution/AddedSuperClass/WriteAddedSuperClass.java generic-all -java/io/Serializable/proxy/skipMissing/Read.java generic-all -java/io/Serializable/proxy/skipMissing/Write.java generic-all -java/io/Serializable/readObjectNoData/Read.java generic-all -java/io/Serializable/readObjectNoData/Write.java generic-all -java/io/Serializable/skipWriteObject/Read.java generic-all -java/io/Serializable/skipWriteObject/Write.java generic-all -java/io/Serializable/skippedObjCNFException/Read.java generic-all -java/io/Serializable/skippedObjCNFException/Write.java generic-all -java/io/Serializable/stopCustomDeserialization/Read.java generic-all -java/io/Serializable/stopCustomDeserialization/Write.java generic-all -java/io/Serializable/unresolvedClassDesc/Read.java generic-all -java/io/Serializable/unresolvedClassDesc/Write.java generic-all -java/io/Serializable/unshared/Read.java generic-all -java/io/Serializable/unshared/Write.java generic-all -java/io/Serializable/wrongReturnTypes/Read.java generic-all -java/io/Serializable/wrongReturnTypes/Write.java generic-all - # Windows samevm issues? triggers other tests to fail, missing close() on f.txt? java/io/DataInputStream/OpsAfterClose.java generic-all @@ -323,13 +249,6 @@ java/io/RandomAccessFile/skipBytes/SkipBytes.java generic-all java/io/readBytes/MemoryLeak.java generic-all java/io/readBytes/ReadBytesBounds.java generic-all -# Missing close on fields.ser, windows samevm -java/io/Serializable/checkModifiers/CheckModifiers.java generic-all - -# Should be othervm, or corrected for samevm, fails with samevm: -java/io/Serializable/auditStreamSubclass/AuditStreamSubclass.java generic-all -java/io/Serializable/proxy/Basic.java generic-all - # Possibly not doing a close() on input.txt, windows samevm issues. java/io/StreamTokenizer/Comment.java generic-all diff --git a/test/java/io/Serializable/ClassCastExceptionDetail/Read.java b/test/java/io/Serializable/ClassCastExceptionDetail/Read.java index a6063106b..52373c500 100644 --- a/test/java/io/Serializable/ClassCastExceptionDetail/Read.java +++ b/test/java/io/Serializable/ClassCastExceptionDetail/Read.java @@ -40,9 +40,9 @@ class Gub extends Foo {} public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("tmp.ser")); + FileInputStream in = new FileInputStream("tmp.ser"); try { + ObjectInputStream oin = new ObjectInputStream(in); oin.readObject(); throw new Error("readObject should not succeed"); } catch (ClassCastException e) { @@ -57,6 +57,8 @@ public class Read { { throw new Error("ClassNotFoundException message incomplete"); } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/auditStreamSubclass/AuditStreamSubclass.java b/test/java/io/Serializable/auditStreamSubclass/AuditStreamSubclass.java index c5b5bdd24..e27eaf4e9 100644 --- a/test/java/io/Serializable/auditStreamSubclass/AuditStreamSubclass.java +++ b/test/java/io/Serializable/auditStreamSubclass/AuditStreamSubclass.java @@ -26,6 +26,8 @@ * @summary Verify that unauthorized ObjectOutputStream and ObjectInputStream * cannot be constructed if they override security-sensitive non-final * methods. + * @build AuditStreamSubclass + * @run main/othervm AuditStreamSubclass */ import java.io.*; diff --git a/test/java/io/Serializable/backRefCNFException/Read.java b/test/java/io/Serializable/backRefCNFException/Read.java index e40689987..9bc69ada6 100644 --- a/test/java/io/Serializable/backRefCNFException/Read.java +++ b/test/java/io/Serializable/backRefCNFException/Read.java @@ -38,13 +38,17 @@ class A implements Serializable { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("tmp.ser")); - oin.readObject(); + FileInputStream in = new FileInputStream("tmp.ser"); try { + ObjectInputStream oin = new ObjectInputStream(in); oin.readObject(); - throw new Error("back reference read succeeded"); - } catch (ClassNotFoundException ex) { + try { + oin.readObject(); + throw new Error("back reference read succeeded"); + } catch (ClassNotFoundException ex) { + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/checkModifiers/CheckModifiers.java b/test/java/io/Serializable/checkModifiers/CheckModifiers.java index 6519af87b..d984ad543 100644 --- a/test/java/io/Serializable/checkModifiers/CheckModifiers.java +++ b/test/java/io/Serializable/checkModifiers/CheckModifiers.java @@ -213,33 +213,39 @@ public class CheckModifiers { FileOutputStream fos = new FileOutputStream("fields.ser"); - ObjectOutputStream oos = new ObjectOutputStream(fos); - - System.out.println("Writing obj 1"); - oos.writeObject(tc1); - System.out.println("Writing obj 2"); - oos.writeObject(tc2); - System.out.println("Writing obj 3"); - oos.writeObject(tc3); - System.out.println("Writing obj 4"); - oos.writeObject(tc4); + try { + ObjectOutputStream oos = new ObjectOutputStream(fos); + System.out.println("Writing obj 1"); + oos.writeObject(tc1); + System.out.println("Writing obj 2"); + oos.writeObject(tc2); + System.out.println("Writing obj 3"); + oos.writeObject(tc3); + System.out.println("Writing obj 4"); + oos.writeObject(tc4); + oos.flush(); + } finally { + fos.close(); + } FileInputStream fis = new FileInputStream("fields.ser"); - ObjectInputStream ois = new ObjectInputStream(fis); - - - System.out.println("Test modifiers for serialPeristentFields "); - System.out.println("---------------------------------------- "); - System.out.println("Declaration missing final modifier"); - ois.readObject(); - System.out.println(); - System.out.println("Declaration with public instead of private access"); - ois.readObject(); - System.out.println(); - System.out.println("Declaration with different type"); - ois.readObject(); - System.out.println(); - System.out.println("Declaration as in specification"); - ois.readObject(); + try { + ObjectInputStream ois = new ObjectInputStream(fis); + System.out.println("Test modifiers for serialPeristentFields "); + System.out.println("---------------------------------------- "); + System.out.println("Declaration missing final modifier"); + ois.readObject(); + System.out.println(); + System.out.println("Declaration with public instead of private access"); + ois.readObject(); + System.out.println(); + System.out.println("Declaration with different type"); + ois.readObject(); + System.out.println(); + System.out.println("Declaration as in specification"); + ois.readObject(); + } finally { + fis.close(); + } } }; diff --git a/test/java/io/Serializable/classDescFlagConflict/Read.java b/test/java/io/Serializable/classDescFlagConflict/Read.java index c484379de..f7eb5e512 100644 --- a/test/java/io/Serializable/classDescFlagConflict/Read.java +++ b/test/java/io/Serializable/classDescFlagConflict/Read.java @@ -42,10 +42,15 @@ public class Read { * descriptor. */ File f = new File(System.getProperty("test.src", "."), "Foo.ser"); - new ObjectInputStream(new FileInputStream(f)).readObject(); - throw new Error( - "read succeeded for object whose class descriptor has " + - "both SC_SERIALIZABLE and SC_EXTERNALIZABLE flags set"); + FileInputStream in = new FileInputStream(f); + try { + new ObjectInputStream(in).readObject(); + throw new Error( + "read succeeded for object whose class descriptor has " + + "both SC_SERIALIZABLE and SC_EXTERNALIZABLE flags set"); + } finally { + in.close(); + } } catch (InvalidClassException e) { } } diff --git a/test/java/io/Serializable/classDescHooks/ClassDescHooks.java b/test/java/io/Serializable/classDescHooks/ClassDescHooks.java index c71cf338a..9e382fe70 100644 --- a/test/java/io/Serializable/classDescHooks/ClassDescHooks.java +++ b/test/java/io/Serializable/classDescHooks/ClassDescHooks.java @@ -93,8 +93,12 @@ public class ClassDescHooks implements ObjectStreamConstants { bout = new ByteArrayOutputStream(); foof = new File(System.getProperty("test.src", "."), "Foo.ser"); fin = new FileInputStream(foof); - while (fin.available() > 0) - bout.write(fin.read()); + try { + while (fin.available() > 0) + bout.write(fin.read()); + } finally { + fin.close(); + } byte[] buf1 = bout.toByteArray(); bout = new ByteArrayOutputStream(); @@ -107,11 +111,16 @@ public class ClassDescHooks implements ObjectStreamConstants { if (! Arrays.equals(buf1, buf2)) throw new Error("Incompatible stream format (write)"); + Foo foocopy; fin = new FileInputStream(foof); - oin = new ObjectInputStream(fin); - Foo foocopy = (Foo) oin.readObject(); - if (! foo.equals(foocopy)) - throw new Error("Incompatible stream format (read)"); + try { + oin = new ObjectInputStream(fin); + foocopy = (Foo) oin.readObject(); + if (! foo.equals(foocopy)) + throw new Error("Incompatible stream format (read)"); + } finally { + fin.close(); + } // make sure write hook not called when old protocol in use bout = new ByteArrayOutputStream(); diff --git a/test/java/io/Serializable/duplicateSerialFields/Test.java b/test/java/io/Serializable/duplicateSerialFields/Test.java index a9f21b646..020912f21 100644 --- a/test/java/io/Serializable/duplicateSerialFields/Test.java +++ b/test/java/io/Serializable/duplicateSerialFields/Test.java @@ -82,18 +82,26 @@ public class Test { } catch (InvalidClassException e) { } + FileInputStream in = new FileInputStream("a.ser"); try { - new ObjectInputStream(new FileInputStream("a.ser")).readObject(); + ObjectInputStream oin = new ObjectInputStream(in); + oin.readObject(); throw new Error( "read of A should fail with InvalidClassException"); } catch (InvalidClassException e) { + } finally { + in.close(); } + in = new FileInputStream("b.ser"); try { - new ObjectInputStream(new FileInputStream("b.ser")).readObject(); + ObjectInputStream oin = new ObjectInputStream(in); + oin.readObject(); throw new Error( "read of B should fail with InvalidClassException"); } catch (InvalidClassException e) { + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/enum/badResolve/Read.java b/test/java/io/Serializable/enum/badResolve/Read.java index fb8067853..c2b4a5c1d 100644 --- a/test/java/io/Serializable/enum/badResolve/Read.java +++ b/test/java/io/Serializable/enum/badResolve/Read.java @@ -44,13 +44,15 @@ public class Read { } static void read(String filename) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream(filename)); + FileInputStream in = new FileInputStream(filename); try { + ObjectInputStream oin = new ObjectInputStream(in); Object obj = oin.readObject(); throw new Error("read of " + obj + " should not have succeeded"); } catch (InvalidClassException e) { System.out.println("caught expected exception " + e); + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/enum/constantSubclasses/Read.java b/test/java/io/Serializable/enum/constantSubclasses/Read.java index e9f47fdac..d1145768b 100644 --- a/test/java/io/Serializable/enum/constantSubclasses/Read.java +++ b/test/java/io/Serializable/enum/constantSubclasses/Read.java @@ -38,13 +38,17 @@ enum Foo { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("foo.ser")); - for (Foo f : Foo.values()) { - Object obj = oin.readObject(); - if (obj != f) { - throw new Error("expected " + f + ", got " + obj); + FileInputStream in = new FileInputStream("foo.ser"); + try { + ObjectInputStream oin = new ObjectInputStream(in); + for (Foo f : Foo.values()) { + Object obj = oin.readObject(); + if (obj != f) { + throw new Error("expected " + f + ", got " + obj); + } } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/enum/missingConstant/Read.java b/test/java/io/Serializable/enum/missingConstant/Read.java index c4965b8bf..4bd6075ad 100644 --- a/test/java/io/Serializable/enum/missingConstant/Read.java +++ b/test/java/io/Serializable/enum/missingConstant/Read.java @@ -33,19 +33,23 @@ enum Foo { foo, bar } public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("foo.ser")); - for (Foo f : Foo.values()) { - Object obj = oin.readObject(); - if (obj != f) { - throw new Error("expected " + f + ", got " + obj); - } - } + FileInputStream in = new FileInputStream("foo.ser"); try { - Object obj = oin.readObject(); - throw new Error("read of " + obj + " should not succeed"); - } catch (InvalidObjectException e) { - System.out.println("caught expected exception: " + e); + ObjectInputStream oin = new ObjectInputStream(in); + for (Foo f : Foo.values()) { + Object obj = oin.readObject(); + if (obj != f) { + throw new Error("expected " + f + ", got " + obj); + } + } + try { + Object obj = oin.readObject(); + throw new Error("read of " + obj + " should not succeed"); + } catch (InvalidObjectException e) { + System.out.println("caught expected exception: " + e); + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/fieldTypeString/Read.java b/test/java/io/Serializable/fieldTypeString/Read.java index 3056ca31d..b8b7dd68b 100644 --- a/test/java/io/Serializable/fieldTypeString/Read.java +++ b/test/java/io/Serializable/fieldTypeString/Read.java @@ -44,23 +44,30 @@ class Bar implements Serializable { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("foo.ser")); - Foo foo = (Foo) oin.readObject(); - if (! foo.obj.equals("foo")) { - throw new Error(); - } + FileInputStream in = new FileInputStream("foo.ser"); try { - oin.readObject(); - throw new Error(); - } catch (ClassCastException ex) { + ObjectInputStream oin = new ObjectInputStream(in); + Foo foo = (Foo) oin.readObject(); + if (! foo.obj.equals("foo")) { + throw new Error(); + } + try { + oin.readObject(); + throw new Error(); + } catch (ClassCastException ex) { + } + } finally { + in.close(); } - oin = new ObjectInputStream(new FileInputStream("bar.ser")); + in = new FileInputStream("bar.ser"); try { + ObjectInputStream oin = new ObjectInputStream(in); oin.readObject(); throw new Error(); } catch (InvalidClassException ex) { + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/illegalHandle/Test.java b/test/java/io/Serializable/illegalHandle/Test.java index af2d52663..74c7ad96c 100644 --- a/test/java/io/Serializable/illegalHandle/Test.java +++ b/test/java/io/Serializable/illegalHandle/Test.java @@ -38,21 +38,31 @@ public class Test { * serialized String object followed by an illegal handle */ File f = new File(base, "negativeHandle.ser"); - ObjectInputStream oin = new ObjectInputStream(new FileInputStream(f)); - oin.readObject(); + FileInputStream in = new FileInputStream(f); try { + ObjectInputStream oin = new ObjectInputStream(in); oin.readObject(); - throw new Error("negative handle read should not succeed"); - } catch (StreamCorruptedException ex) { + try { + oin.readObject(); + throw new Error("negative handle read should not succeed"); + } catch (StreamCorruptedException ex) { + } + } finally { + in.close(); } f = new File(base, "tooHighHandle.ser"); - oin = new ObjectInputStream(new FileInputStream(f)); - oin.readObject(); + in = new FileInputStream(f); try { + ObjectInputStream oin = new ObjectInputStream(in); oin.readObject(); - throw new Error("too-high handle read should not succeed"); - } catch (StreamCorruptedException ex) { + try { + oin.readObject(); + throw new Error("too-high handle read should not succeed"); + } catch (StreamCorruptedException ex) { + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/longString/LongString.java b/test/java/io/Serializable/longString/LongString.java index bbb670100..7d342b1b9 100644 --- a/test/java/io/Serializable/longString/LongString.java +++ b/test/java/io/Serializable/longString/LongString.java @@ -68,17 +68,25 @@ public class LongString { mesgf = new File(System.getProperty("test.src", "."), "mesg.ser"); fin = new FileInputStream(mesgf); bout = new ByteArrayOutputStream(); - while (fin.available() > 0) - bout.write(fin.read()); + try { + while (fin.available() > 0) + bout.write(fin.read()); + } finally { + fin.close(); + } byte[] buf2 = bout.toByteArray(); if (! Arrays.equals(buf1, buf2)) throw new Error("incompatible string format (write)"); fin = new FileInputStream(mesgf); - oin = new ObjectInputStream(fin); - String mesgcopy = (String) oin.readObject(); - if (! mesg.equals(mesgcopy)) - throw new Error("incompatible string format (read)"); + try { + oin = new ObjectInputStream(fin); + String mesgcopy = (String) oin.readObject(); + if (! mesg.equals(mesgcopy)) + throw new Error("incompatible string format (read)"); + } finally { + fin.close(); + } } } diff --git a/test/java/io/Serializable/oldTests/AnnotateClass.java b/test/java/io/Serializable/oldTests/AnnotateClass.java index 4680b4c45..17f3ab118 100644 --- a/test/java/io/Serializable/oldTests/AnnotateClass.java +++ b/test/java/io/Serializable/oldTests/AnnotateClass.java @@ -37,36 +37,43 @@ public class AnnotateClass { "methods \n"); try { FileOutputStream ostream = new FileOutputStream("subtest1.tmp"); - TestOutputStream p = new TestOutputStream(ostream); - - p.writeObject(System.out); - p.writeObject(System.err); - p.writeObject(new PrintStream(ostream)); - p.flush(); - ostream.close(); + try { + TestOutputStream p = new TestOutputStream(ostream); + p.writeObject(System.out); + p.writeObject(System.err); + p.writeObject(new PrintStream(ostream)); + p.flush(); + } finally { + ostream.close(); + } FileInputStream istream = new FileInputStream("subtest1.tmp"); - TestInputStream q = new TestInputStream(istream); - - PrintStream out = (PrintStream)q.readObject(); - PrintStream err = (PrintStream)q.readObject(); - Object other = q.readObject(); - if (out != System.out) { - System.err.println( - "\nTEST FAILED: System.out not read correctly"); - throw new Error(); - } - if (err != System.err) { - System.err.println( - "\nTEST FAILED: System.err not read correctly"); - throw new Error(); - } - if (other != null) { - System.err.println( - "\nTEST FAILED: Non-system PrintStream should have " + - "been written/read as null"); - throw new Error(); + try { + TestInputStream q = new TestInputStream(istream); + + PrintStream out = (PrintStream)q.readObject(); + PrintStream err = (PrintStream)q.readObject(); + Object other = q.readObject(); + if (out != System.out) { + System.err.println( + "\nTEST FAILED: System.out not read correctly"); + throw new Error(); + } + if (err != System.err) { + System.err.println( + "\nTEST FAILED: System.err not read correctly"); + throw new Error(); + } + if (other != null) { + System.err.println( + "\nTEST FAILED: Non-system PrintStream should have " + + "been written/read as null"); + throw new Error(); + } + } finally { + istream.close(); } + System.err.println("\nTEST PASSED"); } catch (Exception e) { System.err.print("TEST FAILED: "); diff --git a/test/java/io/Serializable/oldTests/ArrayFields.java b/test/java/io/Serializable/oldTests/ArrayFields.java index 743268f8a..6833a039a 100644 --- a/test/java/io/Serializable/oldTests/ArrayFields.java +++ b/test/java/io/Serializable/oldTests/ArrayFields.java @@ -35,20 +35,22 @@ import java.io.*; public class ArrayFields { - public static void main (String argv[]) { + public static void main (String argv[]) throws IOException { System.err.println("\nRegression test for testing of " + "serialization/deserialization of objects with " + "fields of array type\n"); + FileOutputStream ostream = null; + FileInputStream istream = null; try { - FileOutputStream ostream = new FileOutputStream("piotest4.tmp"); + ostream = new FileOutputStream("piotest4.tmp"); ObjectOutputStream p = new ObjectOutputStream(ostream); ArrayTest array = new ArrayTest(); p.writeObject(array); p.flush(); - FileInputStream istream = new FileInputStream("piotest4.tmp"); + istream = new FileInputStream("piotest4.tmp"); ObjectInputStream q = new ObjectInputStream(istream); Object obj = null; @@ -73,6 +75,9 @@ public class ArrayFields { System.err.print("TEST FAILED: "); e.printStackTrace(); throw new Error(); + } finally { + if (istream != null) istream.close(); + if (ostream != null) ostream.close(); } } } diff --git a/test/java/io/Serializable/oldTests/ArraysOfArrays.java b/test/java/io/Serializable/oldTests/ArraysOfArrays.java index 934966936..ba5fc0dec 100644 --- a/test/java/io/Serializable/oldTests/ArraysOfArrays.java +++ b/test/java/io/Serializable/oldTests/ArraysOfArrays.java @@ -31,14 +31,15 @@ import java.io.*; public class ArraysOfArrays { - public static void main (String argv[]) { + public static void main (String argv[]) throws IOException { System.err.println("\nRegression test for testing of " + "serialization/deserialization of objects as " + "arrays of arrays \n"); FileInputStream istream = null; + FileOutputStream ostream = null; try { - FileOutputStream ostream = new FileOutputStream("piotest5.tmp"); + ostream = new FileOutputStream("piotest5.tmp"); ObjectOutputStream p = new ObjectOutputStream(ostream); byte b[][] = {{ 0, 1}, {2,3}}; @@ -207,6 +208,9 @@ public class ArraysOfArrays { throw new Error(); } throw new Error(); + } finally { + if (istream != null) istream.close(); + if (ostream != null) ostream.close(); } } } diff --git a/test/java/io/Serializable/oldTests/BinaryTree.java b/test/java/io/Serializable/oldTests/BinaryTree.java index 81e8e1d78..b4d824fdf 100644 --- a/test/java/io/Serializable/oldTests/BinaryTree.java +++ b/test/java/io/Serializable/oldTests/BinaryTree.java @@ -37,28 +37,34 @@ public class BinaryTree { "with BinaryTree types \n"); try { - FileOutputStream ostream = new FileOutputStream("piotest3.tmp"); - ObjectOutputStream p = new ObjectOutputStream(ostream); - BinaryTreeTest base = new BinaryTreeTest(2); - p.writeObject(null); - p.writeObject(base); - p.flush(); - ostream.close(); + FileOutputStream ostream = new FileOutputStream("piotest3.tmp"); + try { + ObjectOutputStream p = new ObjectOutputStream(ostream); + p.writeObject(null); + p.writeObject(base); + p.flush(); + } finally { + ostream.close(); + } FileInputStream istream = new FileInputStream("piotest3.tmp"); - ObjectInputStream q = new ObjectInputStream(istream); - - Object n = q.readObject(); - if (n != null) { - System.err.println("\nnull read as " + n); - } - BinaryTreeTest nbase = (BinaryTreeTest)q.readObject(); - if (!base.equals(nbase)) { - System.err.println("\nTEST FAILED: BinaryTree read " + - "incorrectly."); - throw new Error(); + try { + ObjectInputStream q = new ObjectInputStream(istream); + Object n = q.readObject(); + if (n != null) { + System.err.println("\nnull read as " + n); + } + BinaryTreeTest nbase = (BinaryTreeTest)q.readObject(); + if (!base.equals(nbase)) { + System.err.println("\nTEST FAILED: BinaryTree read " + + "incorrectly."); + throw new Error(); + } + } finally { + istream.close(); } + System.err.println("\nTEST PASSED"); } catch (Exception e) { System.err.print("TEST FAILED: "); diff --git a/test/java/io/Serializable/oldTests/CircularList.java b/test/java/io/Serializable/oldTests/CircularList.java index 1a9723aae..bd5814e08 100644 --- a/test/java/io/Serializable/oldTests/CircularList.java +++ b/test/java/io/Serializable/oldTests/CircularList.java @@ -31,20 +31,22 @@ import java.io.*; public class CircularList { - public static void main (String argv[]) { + public static void main (String argv[]) throws IOException { System.err.println("\nRegression test for testing of " + "serialization/deserialization of " + "objects with CirculalListType types \n"); + FileInputStream istream = null; + FileOutputStream ostream = null; try { - FileOutputStream ostream = new FileOutputStream("piotest7.tmp"); + ostream = new FileOutputStream("piotest7.tmp"); ObjectOutputStream p = new ObjectOutputStream(ostream); CircularListTest.setup(); p.writeObject(CircularListTest.list); p.flush(); - FileInputStream istream = new FileInputStream("piotest7.tmp"); + istream = new FileInputStream("piotest7.tmp"); ObjectInputStream q = new ObjectInputStream(istream); CircularListTest cv = (CircularListTest)q.readObject(); @@ -58,6 +60,9 @@ public class CircularList { System.err.print("TEST FAILED: "); e.printStackTrace(); throw new Error(); + } finally { + if (istream != null) istream.close(); + if (ostream != null) ostream.close(); } } } diff --git a/test/java/io/Serializable/oldTests/SimpleArrays.java b/test/java/io/Serializable/oldTests/SimpleArrays.java index c35b9b068..bbf4c8556 100644 --- a/test/java/io/Serializable/oldTests/SimpleArrays.java +++ b/test/java/io/Serializable/oldTests/SimpleArrays.java @@ -35,13 +35,14 @@ import java.io.*; public class SimpleArrays { - public static void main (String argv[]) { + public static void main (String argv[]) throws IOException { System.err.println("\nRegression test for testing of " + "serialization/deserialization of objects with Arrays types\n"); FileInputStream istream = null; + FileOutputStream ostream = null; try { - FileOutputStream ostream = new FileOutputStream("piotest2.tmp"); + ostream = new FileOutputStream("piotest2.tmp"); ObjectOutputStream p = new ObjectOutputStream(ostream); byte b[] = { 0, 1}; @@ -177,6 +178,9 @@ public class SimpleArrays { throw new Error(); } throw new Error(); + } finally { + if (istream != null) istream.close(); + if (ostream != null) ostream.close(); } } } diff --git a/test/java/io/Serializable/oldTests/WritePrimitive.java b/test/java/io/Serializable/oldTests/WritePrimitive.java index d955045a7..3976cd416 100644 --- a/test/java/io/Serializable/oldTests/WritePrimitive.java +++ b/test/java/io/Serializable/oldTests/WritePrimitive.java @@ -34,11 +34,12 @@ import java.io.*; public class WritePrimitive { - public static void main (String argv[]) { + public static void main (String argv[]) throws IOException { System.err.println("\nRegression test for testing of " + "serialization/deserialization of primitives \n"); FileInputStream istream = null; + FileOutputStream ostream = null; try { int i = 123456; byte b = 12; @@ -51,7 +52,7 @@ public class WritePrimitive { String string = "The String"; PrimitivesTest prim = new PrimitivesTest(); - FileOutputStream ostream = new FileOutputStream("piotest1.tmp"); + ostream = new FileOutputStream("piotest1.tmp"); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeInt(i); @@ -154,6 +155,9 @@ public class WritePrimitive { throw new Error(); } throw new Error(); + } finally { + if (istream != null) istream.close(); + if (ostream != null) ostream.close(); } } } diff --git a/test/java/io/Serializable/packageAccess/Test.java b/test/java/io/Serializable/packageAccess/Test.java index 180461180..15d2e4ce0 100644 --- a/test/java/io/Serializable/packageAccess/Test.java +++ b/test/java/io/Serializable/packageAccess/Test.java @@ -38,7 +38,8 @@ public class Test { public static void main(String[] args) throws Exception { ClassLoader ldr = - new URLClassLoader(new URL[]{ new URL("file:foo.jar") }); + new URLClassLoader(new URL[]{ new URL("file:foo.jar") }, + Test.class.getClassLoader()); bcl = Class.forName("B", true, ldr); dcl = Class.forName("D", true, ldr); diff --git a/test/java/io/Serializable/parents/EvolvedClass.java b/test/java/io/Serializable/parents/EvolvedClass.java index db8b11d1c..edb8dee81 100644 --- a/test/java/io/Serializable/parents/EvolvedClass.java +++ b/test/java/io/Serializable/parents/EvolvedClass.java @@ -34,8 +34,12 @@ public class EvolvedClass { // Deserialize in to new class object FileInputStream fi = new FileInputStream("parents.ser"); - ObjectInputStream si = new ObjectInputStream(fi); - cnew = (ASubClass) si.readObject(); + try { + ObjectInputStream si = new ObjectInputStream(fi); + cnew = (ASubClass) si.readObject(); + } finally { + fi.close(); + } System.out.println("Printing the deserialized class: "); System.out.println(); diff --git a/test/java/io/Serializable/parents/OriginalClass.java b/test/java/io/Serializable/parents/OriginalClass.java index c83b76e22..2f21c494d 100644 --- a/test/java/io/Serializable/parents/OriginalClass.java +++ b/test/java/io/Serializable/parents/OriginalClass.java @@ -43,9 +43,13 @@ public class OriginalClass { // Serialize the subclass FileOutputStream fo = new FileOutputStream("parents.ser"); - ObjectOutputStream so = new ObjectOutputStream(fo); - so.writeObject(corg); - so.flush(); + try { + ObjectOutputStream so = new ObjectOutputStream(fo); + so.writeObject(corg); + so.flush(); + } finally { + fo.close(); + } System.out.println("Printing the serialized class: "); System.out.println(); diff --git a/test/java/io/Serializable/proxy/Basic.java b/test/java/io/Serializable/proxy/Basic.java index a9152fd78..92c989b2b 100644 --- a/test/java/io/Serializable/proxy/Basic.java +++ b/test/java/io/Serializable/proxy/Basic.java @@ -81,7 +81,7 @@ class ProxyBlindInputStream extends ObjectInputStream { public class Basic { public static void main(String[] args) throws Exception { - ClassLoader loader = ClassLoader.getSystemClassLoader(); + ClassLoader loader = Basic.class.getClassLoader(); Class[] interfaces = new Class[] { Foo.class, Bar.class }; Random rand = new Random(); int foo = rand.nextInt(); diff --git a/test/java/io/Serializable/proxy/skipMissing/Read.java b/test/java/io/Serializable/proxy/skipMissing/Read.java index 2a7ede9cf..511bb42d7 100644 --- a/test/java/io/Serializable/proxy/skipMissing/Read.java +++ b/test/java/io/Serializable/proxy/skipMissing/Read.java @@ -49,20 +49,24 @@ class B implements Serializable { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = new ObjectInputStream( - new FileInputStream("tmp.ser")); - A a = (A) oin.readObject(); - if (! (a.a.equals("a") && a.z.equals("z"))) { - throw new Error("A fields corrupted"); - } - B b = (B) oin.readObject(); - if (! b.s.equals("s")) { - throw new Error("B fields corrupted"); - } + FileInputStream in = new FileInputStream("tmp.ser"); try { - oin.readObject(); - throw new Error("proxy read should not succeed"); - } catch (ClassNotFoundException ex) { + ObjectInputStream oin = new ObjectInputStream(in); + A a = (A) oin.readObject(); + if (! (a.a.equals("a") && a.z.equals("z"))) { + throw new Error("A fields corrupted"); + } + B b = (B) oin.readObject(); + if (! b.s.equals("s")) { + throw new Error("B fields corrupted"); + } + try { + oin.readObject(); + throw new Error("proxy read should not succeed"); + } catch (ClassNotFoundException ex) { + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/proxy/skipMissing/Write.java b/test/java/io/Serializable/proxy/skipMissing/Write.java index df1d5ca3a..c4b06f2be 100644 --- a/test/java/io/Serializable/proxy/skipMissing/Write.java +++ b/test/java/io/Serializable/proxy/skipMissing/Write.java @@ -64,7 +64,7 @@ class B implements Serializable { public class Write { public static void main(String[] args) throws Exception { Object proxy = Proxy.newProxyInstance( - ClassLoader.getSystemClassLoader(), + Write.class.getClassLoader(), new Class[] { I.class }, new Handler()); ObjectOutputStream oout = new ObjectOutputStream( new FileOutputStream("tmp.ser")); diff --git a/test/java/io/Serializable/readObjectNoData/Read.java b/test/java/io/Serializable/readObjectNoData/Read.java index ccd485f1b..e786c6cc4 100644 --- a/test/java/io/Serializable/readObjectNoData/Read.java +++ b/test/java/io/Serializable/readObjectNoData/Read.java @@ -95,14 +95,18 @@ class F extends E { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("tmp.ser")); - F f = (F) oin.readObject(); - if (f.aCalled || f.bCalled || f.dCalled || f.eCalled) { - throw new Error("readObjectNoData invoked erroneously"); - } - if (! f.cCalled) { - throw new Error("readObjectNoData not invoked"); + FileInputStream in = new FileInputStream("tmp.ser"); + try { + ObjectInputStream oin = new ObjectInputStream(in); + F f = (F) oin.readObject(); + if (f.aCalled || f.bCalled || f.dCalled || f.eCalled) { + throw new Error("readObjectNoData invoked erroneously"); + } + if (! f.cCalled) { + throw new Error("readObjectNoData not invoked"); + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/skipWriteObject/Read.java b/test/java/io/Serializable/skipWriteObject/Read.java index d8873eb40..e1fcfe79f 100644 --- a/test/java/io/Serializable/skipWriteObject/Read.java +++ b/test/java/io/Serializable/skipWriteObject/Read.java @@ -38,9 +38,13 @@ public class Read { public static void main(String[] args) throws Exception { ObjectInputStream oin = new ObjectInputStream(new FileInputStream("tmp.ser")); - Object[] array = (Object[]) oin.readObject(); - if (! (array[0].equals("before") && array[2].equals("after"))) { - throw new Error(); + try { + Object[] array = (Object[]) oin.readObject(); + if (! (array[0].equals("before") && array[2].equals("after"))) { + throw new Error(); + } + } finally { + oin.close(); } } } diff --git a/test/java/io/Serializable/skippedObjCNFException/Read.java b/test/java/io/Serializable/skippedObjCNFException/Read.java index 9effc2426..b50265099 100644 --- a/test/java/io/Serializable/skippedObjCNFException/Read.java +++ b/test/java/io/Serializable/skippedObjCNFException/Read.java @@ -44,8 +44,12 @@ class B implements Serializable { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("tmp.ser")); - oin.readObject(); + FileInputStream in = new FileInputStream("tmp.ser"); + try { + ObjectInputStream oin = new ObjectInputStream(in); + oin.readObject(); + } finally { + in.close(); + } } } diff --git a/test/java/io/Serializable/stopCustomDeserialization/Read.java b/test/java/io/Serializable/stopCustomDeserialization/Read.java index c77a0cba2..156fa50b8 100644 --- a/test/java/io/Serializable/stopCustomDeserialization/Read.java +++ b/test/java/io/Serializable/stopCustomDeserialization/Read.java @@ -53,16 +53,20 @@ class C extends B { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("tmp.ser")); + FileInputStream in = new FileInputStream("tmp.ser"); try { - oin.readObject(); - throw new Error("readObject should not succeed"); - } catch (ClassNotFoundException e) { - // expected - } - if (!oin.readObject().equals("after")) { - throw new Error("subsequent object corrupted"); + ObjectInputStream oin = new ObjectInputStream(in); + try { + oin.readObject(); + throw new Error("readObject should not succeed"); + } catch (ClassNotFoundException e) { + // expected + } + if (!oin.readObject().equals("after")) { + throw new Error("subsequent object corrupted"); + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/unresolvedClassDesc/Read.java b/test/java/io/Serializable/unresolvedClassDesc/Read.java index 78a35453f..c3fc28bde 100644 --- a/test/java/io/Serializable/unresolvedClassDesc/Read.java +++ b/test/java/io/Serializable/unresolvedClassDesc/Read.java @@ -32,19 +32,23 @@ import java.io.*; public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("tmp.ser")); - oin.readObject(); - oin.readObject(); + FileInputStream in = new FileInputStream("tmp.ser"); try { + ObjectInputStream oin = new ObjectInputStream(in); oin.readObject(); - throw new Error("read of Foo instance succeeded"); - } catch (ClassNotFoundException ex) { - } - try { oin.readObject(); - throw new Error("indirect read of Foo instance succeeded"); - } catch (ClassNotFoundException ex) { + try { + oin.readObject(); + throw new Error("read of Foo instance succeeded"); + } catch (ClassNotFoundException ex) { + } + try { + oin.readObject(); + throw new Error("indirect read of Foo instance succeeded"); + } catch (ClassNotFoundException ex) { + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/unshared/Read.java b/test/java/io/Serializable/unshared/Read.java index c92d07ebf..7d7c9d49b 100644 --- a/test/java/io/Serializable/unshared/Read.java +++ b/test/java/io/Serializable/unshared/Read.java @@ -80,20 +80,30 @@ public class Read { } // read in objects written by Write.main() - oin = new ObjectInputStream(new FileInputStream("tmp.ser")); - oin.readObject(); + FileInputStream in = new FileInputStream("tmp.ser"); try { + oin = new ObjectInputStream(in); oin.readObject(); - throw new Error(); - } catch (ObjectStreamException ex) { + try { + oin.readObject(); + throw new Error(); + } catch (ObjectStreamException ex) { + } + } finally { + in.close(); } - oin = new ObjectInputStream(new FileInputStream("tmp.ser")); - oin.readObject(); + in = new FileInputStream("tmp.ser"); try { - oin.readUnshared(); - throw new Error(); - } catch (ObjectStreamException ex) { + oin = new ObjectInputStream(in); + oin.readObject(); + try { + oin.readUnshared(); + throw new Error(); + } catch (ObjectStreamException ex) { + } + } finally { + in.close(); } } } diff --git a/test/java/io/Serializable/wrongReturnTypes/Read.java b/test/java/io/Serializable/wrongReturnTypes/Read.java index b5ef5e086..a41599f1e 100644 --- a/test/java/io/Serializable/wrongReturnTypes/Read.java +++ b/test/java/io/Serializable/wrongReturnTypes/Read.java @@ -62,15 +62,19 @@ class B extends A { public class Read { public static void main(String[] args) throws Exception { - ObjectInputStream oin = - new ObjectInputStream(new FileInputStream("tmp.ser")); - B b = (B) oin.readObject(); - if (A.readObjectNoDataCalled) { - throw new Error("readObjectNoData with wrong return type called"); - } else if (B.readObjectCalled) { - throw new Error("readObject with wrong return type called"); - } else if (B.readResolveCalled) { - throw new Error("readResolve with wrong return type called"); + FileInputStream in = new FileInputStream("tmp.ser"); + try { + ObjectInputStream oin = new ObjectInputStream(in); + B b = (B) oin.readObject(); + if (A.readObjectNoDataCalled) { + throw new Error("readObjectNoData with wrong return type called"); + } else if (B.readObjectCalled) { + throw new Error("readObject with wrong return type called"); + } else if (B.readResolveCalled) { + throw new Error("readResolve with wrong return type called"); + } + } finally { + in.close(); } } } -- GitLab