8005280: (props) Improve test coverage for small XML parser
authorjoehw
Fri, 21 Dec 2012 17:29:23 -0800
changeset 14922 fdb5ce96f459
parent 14921 e840a91b954b
child 14924 43383e2d85a6
8005280: (props) Improve test coverage for small XML parser Summary: added a few more invalid XML files, international characters to LoadAndStore test, and a behavior compatibility test. Reviewed-by: alanb, lancea
jdk/test/java/util/Properties/Compatibility.xml
jdk/test/java/util/Properties/CompatibilityTest.java
jdk/test/java/util/Properties/LoadAndStoreXML.java
jdk/test/java/util/Properties/invalidxml/BadDocType.xml
jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded
jdk/test/java/util/Properties/invalidxml/DTDRootNotMatch.xml
jdk/test/java/util/Properties/invalidxml/IllegalComment.xml
jdk/test/java/util/Properties/invalidxml/IllegalEntry.xml
jdk/test/java/util/Properties/invalidxml/IllegalEntry1.xml
jdk/test/java/util/Properties/invalidxml/IllegalKeyAttribute.xml
jdk/test/java/util/Properties/invalidxml/NoDocType.xml
jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded
jdk/test/java/util/Properties/invalidxml/NoNamespaceSupport.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/Compatibility.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd" 
+ [<!ENTITY intEnt 'value3'> ]>
+<?PITarget PIContent?>
+<properties>
+<comment>Property With Other Encoding</comment>
+<entry key="Key1">value1</entry>
+<entry key="Key2"><![CDATA[<value2>]]></entry>
+<entry key="Key3">&intEnt;</entry>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/CompatibilityTest.java	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8005280 8004371
+ * @summary Compatibility test
+ * @run main CompatibilityTest
+ * @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider CompatibilityTest
+ */
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * This is a behavior compatibility test.
+ * Although not defined by the properties.dtd, the constructs
+ * in Compatibility.xml are supported by the regular JDK XML
+ * Provider.
+ *
+ * @author: Joe Wang
+ */
+public class CompatibilityTest {
+
+    public static void main(String[] args) {
+        testInternalDTD();
+    }
+
+    /*
+     * Not in the spec, but the constructs work with the current JDK
+     */
+    static void testInternalDTD() {
+        String src = System.getProperty("test.src");
+        if (src == null) {
+            src = ".";
+        }
+        loadPropertyFile(src + "/Compatibility.xml");
+    }
+
+    /*
+     * 'Store' the populated 'Property' with the specified 'Encoding Type' as an
+     * XML file. Retrieve the same XML file and 'load' onto a new 'Property' object.
+     */
+    static void loadPropertyFile(String filename) {
+        try (InputStream in = new FileInputStream(filename)) {
+            Properties prop = new Properties();
+            prop.loadFromXML(in);
+            verifyProperites(prop);
+        } catch (IOException ex) {
+            fail(ex.getMessage());
+        }
+    }
+
+    /*
+     * This method verifies the first key-value with the original string.
+     */
+    static void verifyProperites(Properties prop) {
+        try {
+            for (String key : prop.stringPropertyNames()) {
+                String val = prop.getProperty(key);
+                if (key.equals("Key1")) {
+                    if (!val.equals("value1")) {
+                        fail("Key:" + key + "'s value: \nExpected: value1\nFound: " + val);
+                    }
+                } else if (key.equals("Key2")) {
+                    if (!val.equals("<value2>")) {
+                        fail("Key:" + key + "'s value: \nExpected: <value2>\nFound: " + val);
+                    }
+                } else if (key.equals("Key3")) {
+                    if (!val.equals("value3")) {
+                        fail("Key:" + key + "'s value: \nExpected: value3\nFound: " + val);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+
+    }
+
+    static void fail(String err) {
+        throw new RuntimeException(err);
+    }
+
+}
--- a/jdk/test/java/util/Properties/LoadAndStoreXML.java	Fri Dec 21 10:58:20 2012 +0100
+++ b/jdk/test/java/util/Properties/LoadAndStoreXML.java	Fri Dec 21 17:29:23 2012 -0800
@@ -75,6 +75,9 @@
         Properties props = new Properties();
         props.put("k1", "foo");
         props.put("k2", "bar");
+        props.put("k3", "\\u0020\\u0391\\u0392\\u0393\\u0394\\u0395\\u0396\\u0397");
+        props.put("k4", "\u7532\u9aa8\u6587");
+        props.put("k5", "<java.home>/lib/jaxp.properties");
 
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         props.storeToXML(out, null, encoding);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/BadDocType.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE namevaluepairs SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- The root element for a XML properties document is properties -->
+
+<properties>
+<entry key="foo">bar</entry>
+<entry key="gus">baz</entry>
+</properties>
--- a/jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded	Fri Dec 21 10:58:20 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<!DOCTYPE namevaluepairs SYSTEM "http://java.sun.com/dtd/properties.dtd">
-
-<!-- The root element for a XML properties document is properties -->
-
-<properties>
-<entry key="foo">bar</entry>
-<entry key="gus">baz</entry>
-</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/DTDRootNotMatch.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- XML tags are case sensitve, the root element should have been in lower case -->
+
+<Properties>
+<comment>comment</comment>
+<entry key="firstKey">value of the first key</entry>
+</Properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/IllegalComment.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- The dtd allows zero or one comment element -->
+
+<properties>
+<comment>comment1</comment>
+<comment>comment2</comment>
+<entry key="firstKey">value of the first key</entry>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/IllegalEntry.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- The dtd requires 'entry' element, 'Entry' is illegal -->
+
+<properties>
+<comment>comment</comment>
+<Entry key="firstKey">value of the first key</Entry>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/IllegalEntry1.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- 'entry1' is illegal as per the dtd -->
+
+<properties>
+<comment>comment1</comment>
+
+<entry key="firstkey">value of the first key</entry>
+<entry1 key="secondkey">value of the second key</entry1>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/IllegalKeyAttribute.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- 'key' attribute is expected. 'key1' is mistyped -->
+
+<properties>
+<comment>comment1</comment>
+<entry key1="typo">value of the first key</entry>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/NoDocType.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+
+<!-- An XML properties document has the DOCTYPE declaration with the properties root element -->
+
+<properties>
+<entry key="foo">bar</entry>
+<entry key="gus">baz</entry>
+</properties>
--- a/jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded	Fri Dec 21 10:58:20 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-
-<!-- An XML properties document has the DOCTYPE declaration with the properties root element -->
-
-<properties>
-<entry key="foo">bar</entry>
-<entry key="gus">baz</entry>
-</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/NoNamespaceSupport.xml	Fri Dec 21 17:29:23 2012 -0800
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- namespace is not supported -->
+
+<javautil:properties xmlns:javautil="http://java.sun.com/java/util/Properties">
+<javautil:comment>comment1</javautil:comment>
+
+<javautil:entry key="firstkey">value of the first key</javautil:entry>
+<javautil:entry key="secondkey">value of the second key</javautil:entry>
+</javautil:properties>