jdk/test/java/util/Properties/LoadAndStoreXML.java
changeset 14187 9e193f8bab66
parent 14029 c684694164c2
child 14909 eded175efec2
--- a/jdk/test/java/util/Properties/LoadAndStoreXML.java	Tue Oct 16 15:23:17 2012 -0700
+++ b/jdk/test/java/util/Properties/LoadAndStoreXML.java	Wed Oct 17 11:43:56 2012 +0100
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8000354
+ * @bug 8000354 8000685
  * @summary Basic test of storeToXML and loadToXML
  */
 
@@ -66,13 +66,13 @@
      * Sanity test that properties saved with Properties#storeToXML can be
      * read with Properties#loadFromXML.
      */
-    static void test() throws IOException {
+    static void testLoadAndStore(String encoding) throws IOException {
         Properties props = new Properties();
         props.put("k1", "foo");
         props.put("k2", "bar");
 
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        props.storeToXML(out, "no comment");
+        props.storeToXML(out, null, encoding);
 
         Properties p = new Properties();
         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
@@ -85,19 +85,74 @@
         }
     }
 
+    /**
+     * Test loadFromXML with a document that does not have an encoding declaration
+     */
+    static void testLoadWithoutEncoding() throws IOException {
+        Properties expected = new Properties();
+        expected.put("foo", "bar");
+
+        String s = "<?xml version=\"1.0\"?>" +
+                   "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
+                   "<properties>" +
+                   "<entry key=\"foo\">bar</entry>" +
+                   "</properties>";
+        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
+        Properties props = new Properties();
+        props.loadFromXML(in);
+
+        if (!props.equals(expected)) {
+            System.err.println("loaded: " + props + ", expected: " + expected);
+            throw new RuntimeException("Test failed");
+        }
+    }
+
+     /**
+      * Test loadFromXML with unsupported encoding
+      */
+     static void testLoadWithBadEncoding() throws IOException {
+        String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
+                   "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
+                   "<properties>" +
+                   "<entry key=\"foo\">bar</entry>" +
+                   "</properties>";
+        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
+        Properties props = new Properties();
+        try {
+            props.loadFromXML(in);
+            throw new RuntimeException("UnsupportedEncodingException expected");
+        } catch (UnsupportedEncodingException expected) { }
+    }
+
+    /**
+     * Test storeToXML with unsupported encoding
+     */
+    static void testStoreWithBadEncoding() throws IOException {
+        Properties props = new Properties();
+        props.put("foo", "bar");
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        try {
+            props.storeToXML(out, null, "BAD");
+            throw new RuntimeException("UnsupportedEncodingException expected");
+        } catch (UnsupportedEncodingException expected) { }
+    }
+
     public static void main(String[] args) throws IOException {
 
-        // run test without security manager
-        test();
+        testLoadAndStore("UTF-8");
+        testLoadAndStore("UTF-16");
+        testLoadWithoutEncoding();
+        testLoadWithBadEncoding();
+        testStoreWithBadEncoding();
 
-        // re-run test with security manager
+        // re-run sanity test with security manager
         Policy orig = Policy.getPolicy();
         Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
                                     new PropertyPermission("line.separator", "read"));
         Policy.setPolicy(p);
         System.setSecurityManager(new SecurityManager());
         try {
-            test();
+            testLoadAndStore("UTF-8");
         } finally {
             // turn off security manager and restore policy
             System.setSecurityManager(null);