changeset 24373:d20f4a10c235

8043119: (props) Properties.storeToXML closes output stream Reviewed-by: lancea
author alanb
date Wed, 14 May 2014 16:40:53 +0100
parents 2ff88b15e82e
children a38282cba2fc
files jdk/src/share/classes/jdk/internal/util/xml/PropertiesDefaultHandler.java jdk/test/java/util/Properties/LoadAndStoreXML.java
diffstat 2 files changed, 70 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/share/classes/jdk/internal/util/xml/PropertiesDefaultHandler.java	Wed May 14 11:16:41 2014 +0100
+++ b/jdk/src/share/classes/jdk/internal/util/xml/PropertiesDefaultHandler.java	Wed May 14 16:40:53 2014 +0100
@@ -123,7 +123,7 @@
 
             writer.writeEndElement();
             writer.writeEndDocument();
-            writer.close();
+            writer.flush();
         } catch (XMLStreamException e) {
             if (e.getCause() instanceof UnsupportedEncodingException) {
                 throw (UnsupportedEncodingException) e.getCause();
--- a/jdk/test/java/util/Properties/LoadAndStoreXML.java	Wed May 14 11:16:41 2014 +0100
+++ b/jdk/test/java/util/Properties/LoadAndStoreXML.java	Wed May 14 16:40:53 2014 +0100
@@ -23,14 +23,28 @@
 
 /*
  * @test
- * @bug 8000354 8000685 8004371
+ * @bug 8000354 8000685 8004371 8043119
  * @summary Basic test of storeToXML and loadToXML
  */
 
-import java.io.*;
-import java.util.*;
-import java.security.*;
-import java.nio.file.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.security.CodeSource;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.security.Policy;
+import java.security.ProtectionDomain;
+import java.util.InvalidPropertiesFormatException;
+import java.util.Properties;
+import java.util.PropertyPermission;
 
 public class LoadAndStoreXML {
 
@@ -64,6 +78,50 @@
     }
 
     /**
+     * A {@code ByteArrayInputStream} that allows testing if the
+     * {@code close} method has been invoked.
+     */
+    static class TestInputStream extends ByteArrayInputStream {
+        private boolean closed;
+
+        TestInputStream(byte[] buf) {
+            super(buf);
+        }
+
+        boolean isOpen() {
+            return !closed;
+        }
+
+        public void close() throws IOException {
+            try {
+                super.close();
+            } finally {
+                closed = true;
+            }
+        }
+    }
+
+    /**
+     * A {@code ByteArrayOutputStream} that allows testing if the
+     * {@code close} method has been invoked.
+     */
+    static class TestOutputStream extends ByteArrayOutputStream {
+        private boolean closed;
+
+        boolean isOpen() {
+            return !closed;
+        }
+
+        public void close() throws IOException {
+            try {
+                super.close();
+            } finally {
+                closed = true;
+            }
+        }
+    }
+
+    /**
      * Sanity test that properties saved with Properties#storeToXML can be
      * read with Properties#loadFromXML.
      */
@@ -77,12 +135,16 @@
         props.put("k4", "\u7532\u9aa8\u6587");
         props.put("k5", "<java.home>/lib/jaxp.properties");
 
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        TestOutputStream out = new TestOutputStream();
         props.storeToXML(out, null, encoding);
+        if (!out.isOpen())
+            throw new RuntimeException("OutputStream closed by storeToXML");
 
         Properties p = new Properties();
-        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        TestInputStream in = new TestInputStream(out.toByteArray());
         p.loadFromXML(in);
+        if (in.isOpen())
+            throw new RuntimeException("InputStream not closed by loadFromXML");
 
         if (!p.equals(props)) {
             System.err.println("stored: " + props);