changeset 3829:98d2d57d9e73

7022624: use try-with-resources in java.io tests Reviewed-by: alanb
author smarks
date Tue, 01 Mar 2011 15:05:32 -0800
parents b7e763a573a4
children 895687e879ca
files test/java/io/File/SetLastModified.java test/java/io/FileOutputStream/AtomicAppend.java test/java/io/OutputStreamWriter/Encode.java test/java/io/PrintStream/EncodingConstructor.java test/java/io/PrintStream/FailingConstructors.java test/java/io/Serializable/evolution/RenamePackage/install/SerialDriver.java test/java/io/Serializable/evolution/RenamePackage/test/SerialDriver.java
diffstat 7 files changed, 57 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/io/File/SetLastModified.java	Tue Mar 01 12:03:44 2011 +0000
+++ b/test/java/io/File/SetLastModified.java	Tue Mar 01 15:05:32 2011 -0800
@@ -105,9 +105,9 @@
             System.getProperty("os.name").startsWith("Windows") ? 0L : 3L*G;
         long pos = 0L;
         while (pos <= MAX_POSITION) {
-            FileChannel fc = new FileOutputStream(f).getChannel();
-            fc.position(pos).write(ByteBuffer.wrap("x".getBytes()));
-            fc.close();
+            try (FileChannel fc = new FileOutputStream(f).getChannel()) {
+                fc.position(pos).write(ByteBuffer.wrap("x".getBytes()));
+            }
             ot = f.lastModified();
             System.out.format("check with file size: %d\n", f.length());
             if (!f.setLastModified(nt))
--- a/test/java/io/FileOutputStream/AtomicAppend.java	Tue Mar 01 12:03:44 2011 +0000
+++ b/test/java/io/FileOutputStream/AtomicAppend.java	Tue Mar 01 15:05:32 2011 -0800
@@ -47,12 +47,12 @@
             for (int i = 0; i < nThreads; i++)
                 es.execute(new Runnable() { public void run() {
                     try {
-                        FileOutputStream s = new FileOutputStream(file, true);
-                        for (int j = 0; j < 1000; j++) {
-                            s.write((int) 'x');
-                            s.flush();
+                        try (FileOutputStream s = new FileOutputStream(file, true)) {
+                            for (int j = 0; j < 1000; j++) {
+                                s.write((int) 'x');
+                                s.flush();
+                            }
                         }
-                        s.close();
                     } catch (Throwable t) { unexpected(t); }}});
             es.shutdown();
             es.awaitTermination(10L, TimeUnit.MINUTES);
--- a/test/java/io/OutputStreamWriter/Encode.java	Tue Mar 01 12:03:44 2011 +0000
+++ b/test/java/io/OutputStreamWriter/Encode.java	Tue Mar 01 15:05:32 2011 -0800
@@ -35,8 +35,9 @@
         new Encode();
     }
 
+    final ServerSocket ss = new ServerSocket(0);
+
     Encode() throws Exception {
-        ss = new ServerSocket(0);
         (new Thread(this)).start();
         String toEncode = "\uD800\uDC00 \uD801\uDC01 ";
         String enc1 = URLEncoder.encode(toEncode, "UTF-8");
@@ -47,27 +48,31 @@
             "/missing.nothtml";
         HttpURLConnection uc =  (HttpURLConnection)new URL(url).openConnection();
         uc.connect();
-        String enc2 = URLEncoder.encode(toEncode, "UTF-8");
-        if (!enc1.equals(enc2))
-            throw new RuntimeException("test failed");
-        uc.disconnect();
+        try {
+            String enc2 = URLEncoder.encode(toEncode, "UTF-8");
+            if (!enc1.equals(enc2)) {
+                System.out.println("test failed");
+                throw new RuntimeException("test failed");
+            }
+        } finally {
+            uc.disconnect();
+        }
     }
 
-    ServerSocket ss;
-
     public void run() {
-        try {
-            Socket s = ss.accept();
-            BufferedReader in = new BufferedReader(
-                new InputStreamReader(s.getInputStream()));
+        try (ServerSocket serv = ss;
+             Socket s = serv.accept();
+             BufferedReader in =
+                 new BufferedReader(new InputStreamReader(s.getInputStream())))
+        {
             String req = in.readLine();
-            PrintStream out = new PrintStream(new BufferedOutputStream(
-                s.getOutputStream()));
-            out.print("HTTP/1.1 403 Forbidden\r\n");
-            out.print("\r\n");
-            out.flush();
-            s.close();
-            ss.close();
+            try (OutputStream os = s.getOutputStream();
+                 BufferedOutputStream bos = new BufferedOutputStream(os);
+                 PrintStream out = new PrintStream(bos))
+            {
+                out.print("HTTP/1.1 403 Forbidden\r\n");
+                out.print("\r\n");
+            }
         } catch (Exception e) {
             e.printStackTrace();
         }
--- a/test/java/io/PrintStream/EncodingConstructor.java	Tue Mar 01 12:03:44 2011 +0000
+++ b/test/java/io/PrintStream/EncodingConstructor.java	Tue Mar 01 15:05:32 2011 -0800
@@ -34,11 +34,11 @@
 
     public static void main(String args[]) throws Exception {
         ByteArrayOutputStream bo = new ByteArrayOutputStream();
-        PrintStream ps = new PrintStream(bo, false, "UTF-8");
         String s = "xyzzy";
         int n = s.length();
-        ps.print(s);
-        ps.close();
+        try (PrintStream ps = new PrintStream(bo, false, "UTF-8")) {
+            ps.print(s);
+        }
         byte[] ba = bo.toByteArray();
         if (ba.length != n)
             throw new Exception("Length mismatch: " + n + " " + ba.length);
--- a/test/java/io/PrintStream/FailingConstructors.java	Tue Mar 01 12:03:44 2011 +0000
+++ b/test/java/io/PrintStream/FailingConstructors.java	Tue Mar 01 15:05:32 2011 -0800
@@ -35,6 +35,8 @@
 import java.io.IOException;
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 public class FailingConstructors {
     static final String fileName = "FailingConstructorsTest";
@@ -45,14 +47,13 @@
         test(false, new File(fileName));
 
         /* create the file and write its contents */
-        File file = File.createTempFile(fileName, null);
-        file.deleteOnExit();
-        FileOutputStream fos = new FileOutputStream(file);
-        fos.write(FILE_CONTENTS.getBytes());
-        fos.close();
-
-        test(true, file);
-        file.delete();
+        Path path = Files.createTempFile(fileName, null);
+        try {
+            Files.write(path, FILE_CONTENTS.getBytes());
+            test(true, path.toFile());
+        } finally {
+            Files.delete(path);
+        }
     }
 
     private static void test(boolean exists, File file) throws Throwable {
--- a/test/java/io/Serializable/evolution/RenamePackage/install/SerialDriver.java	Tue Mar 01 12:03:44 2011 +0000
+++ b/test/java/io/Serializable/evolution/RenamePackage/install/SerialDriver.java	Tue Mar 01 15:05:32 2011 -0800
@@ -27,7 +27,7 @@
  * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
  * @summary Enable resolveClass() to accommodate package renaming.
  * This fix enables one to implement a resolveClass method that maps a
- * Serialiazable class within a serialization stream to the same class
+ * Serializable class within a serialization stream to the same class
  * in a different package within the JVM runtime. See run shell script
  * for instructions on how to run this test.
  */
@@ -86,16 +86,15 @@
         File f = new File("stream.ser");
         if (serialize) {
             // Serialize the subclass
-            try {
-                FileOutputStream fo = new FileOutputStream(f);
-                ObjectOutputStream so = new ObjectOutputStream(fo);
+            try (FileOutputStream fo = new FileOutputStream(f);
+                 ObjectOutputStream so = new ObjectOutputStream(fo))
+            {
                 so.writeObject(obj);
                 /* Skip arrays since they do not work with rename yet.
                    The serialVersionUID changes due to the name change
                    and there is no way to set the serialVersionUID for an
                    array. */
                 so.writeObject(array);
-                so.flush();
             } catch (Exception e) {
                 System.out.println(e);
                 throw e;
@@ -103,16 +102,14 @@
         }
         if (deserialize) {
             // Deserialize the subclass
-            try {
-                FileInputStream fi = new FileInputStream(f);
-                ExtendedObjectInputStream si =
-                    new ExtendedObjectInputStream(fi);
+            try (FileInputStream fi = new FileInputStream(f);
+                 ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
+            {
                 si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");
                 si.addRenamedClassName("[Ltest.SerialDriver;",
                                        "[Linstall.SerialDriver");
                 obj = (SerialDriver) si.readObject();
                 array = (SerialDriver[]) si.readObject();
-                si.close();
             } catch (Exception e) {
                 System.out.println(e);
                 throw e;
--- a/test/java/io/Serializable/evolution/RenamePackage/test/SerialDriver.java	Tue Mar 01 12:03:44 2011 +0000
+++ b/test/java/io/Serializable/evolution/RenamePackage/test/SerialDriver.java	Tue Mar 01 15:05:32 2011 -0800
@@ -27,7 +27,7 @@
  * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
  * @summary Enable resolveClass() to accommodate package renaming.
  * This fix enables one to implement a resolveClass method that maps a
- * Serialiazable class within a serialization stream to the same class
+ * Serializable class within a serialization stream to the same class
  * in a different package within the JVM runtime. See run shell script
  * for instructions on how to run this test.
  */
@@ -83,14 +83,13 @@
         File f = new File("stream.ser");
         if (serialize) {
             // Serialize the subclass
-            try {
-                FileOutputStream fo = new FileOutputStream(f);
-                ObjectOutputStream so = new ObjectOutputStream(fo);
+            try (FileOutputStream fo = new FileOutputStream(f);
+                 ObjectOutputStream so = new ObjectOutputStream(fo))
+            {
                 so.writeObject(obj);
                 /* Comment out since renaming arrays does not work
                    since it changes the serialVersionUID. */
                 so.writeObject(array);
-                so.flush();
             } catch (Exception e) {
                 System.out.println(e);
                 throw e;
@@ -98,16 +97,15 @@
         }
         if (deserialize) {
             // Deserialize the subclass
-            try {
-                FileInputStream fi = new FileInputStream(f);
-                ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi);
+            try (FileInputStream fi = new FileInputStream(f);
+                 ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
+            {
                 si.addRenamedClassName("install.SerialDriver",
                                        "test.SerialDriver");
                 si.addRenamedClassName("[Linstall.SerialDriver;",
                                        "[Ltest.SerialDriver");
                 obj = (SerialDriver) si.readObject();
                 array = (SerialDriver[]) si.readObject();
-                si.close();
             } catch (Exception e) {
                 System.out.println(e);
                 throw e;