8033980: Xerces Update: datatype XMLGregorianCalendarImpl and DurationImpl
authorjoehw
Tue, 18 Feb 2014 10:53:18 -0800
changeset 22985 e9654c36f56d
parent 22984 c7a1582cefa2
child 22986 d2db7c5718ca
8033980: Xerces Update: datatype XMLGregorianCalendarImpl and DurationImpl Reviewed-by: dfuchs, lancea, alanb
jdk/test/javax/xml/jaxp/datatype/8033980/CalendarDuration1097Test.java
jdk/test/javax/xml/jaxp/datatype/8033980/CalendarDuration1243Test.java
jdk/test/javax/xml/jaxp/datatype/8033980/CalendarDuration1416Test.java
jdk/test/javax/xml/jaxp/datatype/8033980/JDK6_Duration.ser
jdk/test/javax/xml/jaxp/datatype/8033980/JDK6_XMLGregorianCalendar.ser
jdk/test/javax/xml/jaxp/datatype/8033980/JDK7_Duration.ser
jdk/test/javax/xml/jaxp/datatype/8033980/JDK7_XMLGregorianCalendar.ser
jdk/test/javax/xml/jaxp/datatype/8033980/JDK8_Duration.ser
jdk/test/javax/xml/jaxp/datatype/8033980/JDK8_XMLGregorianCalendar.ser
jdk/test/javax/xml/jaxp/datatype/8033980/JDK9_Duration.ser
jdk/test/javax/xml/jaxp/datatype/8033980/JDK9_XMLGregorianCalendar.ser
jdk/test/javax/xml/jaxp/datatype/8033980/SerializationTest.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/xml/jaxp/datatype/8033980/CalendarDuration1097Test.java	Tue Feb 18 10:53:18 2014 -0800
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2014, 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 8033980
+ * @summary verify that offset in minutes in getTimeZone is converted correctly
+ * Note that JDK did not have this bug.
+ * @run main CalendarDuration1097Test
+ */
+
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+
+
+/**
+ *
+ * @author Joe Wang huizhe.wang@oracle.com
+ */
+public class CalendarDuration1097Test {
+
+    /**
+     * main method.
+     *
+     * @param args Standard args.
+     */
+    public static void main(String[] args) {
+        try {
+            String dateTimeString = "0001-01-01T00:00:00.0000000-05:00";
+            DatatypeFactory dtf = DatatypeFactory.newInstance();
+            XMLGregorianCalendar cal = dtf.newXMLGregorianCalendar( dateTimeString );
+            System.out.println( "Expected: 0001-01-01T00:00:00.0000000-05:00");
+            System.out.println( "Actual:" + cal.toString() );
+            System.out.println( "toXMLFormat:" + cal.toXMLFormat() );
+            String test = cal.toString();
+            if (test.indexOf("E-7") > -1) {
+                throw new RuntimeException("Expected: 0001-01-01T00:00:00.0000000-05:00");
+            }
+        } catch (DatatypeConfigurationException ex) {
+            throw new RuntimeException(ex.getMessage());
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/xml/jaxp/datatype/8033980/CalendarDuration1243Test.java	Tue Feb 18 10:53:18 2014 -0800
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2014, 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 8033980
+ * @summary verify that offset in minutes in getTimeZone is converted correctly
+ * @run main CalendarDuration1243Test
+ */
+
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+
+
+/**
+ *
+ * @author Joe Wang huizhe.wang@oracle.com
+ */
+public class CalendarDuration1243Test {
+
+    /**
+     * main method.
+     *
+     * @param args Standard args.
+     */
+    public static void main(String[] args) {
+        try {
+            String dateTimeString = "2006-11-22T00:00:00.0+01:02";
+            DatatypeFactory dtf = DatatypeFactory.newInstance();
+            XMLGregorianCalendar cal = dtf.newXMLGregorianCalendar( dateTimeString );
+            System.out.println( "XMLGregCal:" + cal.toString() );
+            System.out.println( "GregCal:" + cal.toGregorianCalendar() );
+            String toGCal = cal.toGregorianCalendar().toString();
+            if (toGCal.indexOf("GMT+12:00") > -1) {
+                throw new RuntimeException("Expected GMT+01:02");
+            }
+        } catch (DatatypeConfigurationException ex) {
+            throw new RuntimeException(ex.getMessage());
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/xml/jaxp/datatype/8033980/CalendarDuration1416Test.java	Tue Feb 18 10:53:18 2014 -0800
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2014, 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 8033980
+ * @summary test that invalid durations are caught
+ * @run main CalendarDuration1416Test
+ */
+
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+
+
+/**
+ *
+ * @author Joe Wang huizhe.wang@oracle.com
+ */
+public class CalendarDuration1416Test {
+
+    /**
+     * main method.
+     *
+     * @param args Standard args.
+     */
+    public static void main(String[] args) {
+        test1416("PT1D1H");
+        test1416("PT1D1H30M");
+        test1416("PT1D1H30S");
+    }
+
+    static void test1416(String d) {
+        try
+        {
+            DatatypeFactory dtf = DatatypeFactory.newInstance();
+            dtf.newDuration(d);
+            throw new Error("no bug for " + d);
+        } catch (DatatypeConfigurationException ex) {
+            fail(ex.getMessage());
+        }
+        catch (NullPointerException e) {
+            fail("NPE bug ! " + d);
+
+        }
+        catch(IllegalArgumentException e)
+        {
+            System.out.println("OK, BUG FIXED for " + d);
+        }
+
+    }
+
+    static void fail(String errMessage) {
+        throw new RuntimeException(errMessage);
+    }
+}
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK6_Duration.ser has changed
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK6_XMLGregorianCalendar.ser has changed
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK7_Duration.ser has changed
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK7_XMLGregorianCalendar.ser has changed
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK8_Duration.ser has changed
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK8_XMLGregorianCalendar.ser has changed
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK9_Duration.ser has changed
Binary file jdk/test/javax/xml/jaxp/datatype/8033980/JDK9_XMLGregorianCalendar.ser has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/xml/jaxp/datatype/8033980/SerializationTest.java	Tue Feb 18 10:53:18 2014 -0800
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2014, 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 8033980
+ * @summary verify serialization compatibility for XMLGregorianCalendar and Duration
+ * @run main SerializationTest read
+ */
+
+import java.io.*;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.Duration;
+import javax.xml.datatype.XMLGregorianCalendar;
+
+/**
+ * use "read" to test compatibility
+ * SerializationTest read
+ *
+ * use "write" to create test files
+ * SerializationTest write javaVersion
+ * where javaVersion is 6, 7, 8, or 9
+ *
+ * @author huizhe.wang@oracle.com</a>
+ */
+public class SerializationTest {
+
+    final String FILENAME_CAL = "_XMLGregorianCalendar.ser";
+    final String FILENAME_DURATION = "_Duration.ser";
+    String filePath;
+
+    {
+        filePath = System.getProperty("test.src");
+        if (filePath == null) {
+            //current directory
+            filePath = System.getProperty("user.dir");
+        }
+        filePath += File.separator;
+    }
+    final String EXPECTED_CAL = "0001-01-01T00:00:00.0000000-05:00";
+    final String EXPECTED_DURATION = "P1Y1M1DT1H1M1S";
+    static String[] JDK = {"JDK6", "JDK7", "JDK8", "JDK9"};
+
+    public static void main(String[] args) {
+        SerializationTest test = new SerializationTest();
+
+        if (args[0].equalsIgnoreCase("read")) {
+            test.testReadCal();
+            test.testReadDuration();
+            test.report();
+        } else {
+            int ver = Integer.valueOf(args[1]).intValue();
+            test.createTestFile(JDK[ver - 6]);
+        }
+
+    }
+
+    public void testReadCal() {
+        try {
+            for (String javaVersion : JDK) {
+                XMLGregorianCalendar d1 = (XMLGregorianCalendar) fromFile(
+                        javaVersion + FILENAME_CAL);
+                if (!d1.toString().equalsIgnoreCase(EXPECTED_CAL)) {
+                    fail("Java version: " + javaVersion
+                            + "\nExpected: " + EXPECTED_CAL
+                            + "\nActual: " + d1.toString());
+                } else {
+                    success("testReadCal: read " + javaVersion + " serialized file, passed.");
+                }
+            }
+        } catch (ClassNotFoundException ex) {
+            fail("testReadCal: " + ex.getMessage());
+        } catch (IOException ex) {
+            fail("testReadCal: " + ex.getMessage());
+        }
+    }
+
+    public void testReadDuration() {
+        try {
+            for (String javaVersion : JDK) {
+                Duration d1 = (Duration) fromFile(
+                        javaVersion + FILENAME_DURATION);
+                if (!d1.toString().equalsIgnoreCase(EXPECTED_DURATION)) {
+                    fail("Java version: " + javaVersion
+                            + "\nExpected: " + EXPECTED_DURATION
+                            + "\nActual: " + d1.toString());
+                } else {
+                    success("testReadDuration: read " + javaVersion + " serialized file, passed.");
+                }
+            }
+        } catch (ClassNotFoundException ex) {
+            fail("testReadDuration: " + ex.getMessage());
+        } catch (IOException ex) {
+            fail("testReadDuration: " + ex.getMessage());
+        }
+    }
+
+    /**
+     * Create test files
+     *
+     * @param javaVersion JDK version
+     */
+    public void createTestFile(String javaVersion) {
+        try {
+            DatatypeFactory dtf = DatatypeFactory.newInstance();
+            XMLGregorianCalendar c = dtf.newXMLGregorianCalendar(EXPECTED_CAL);
+            Duration d = dtf.newDuration(EXPECTED_DURATION);
+            toFile((Serializable) c, filePath + javaVersion + FILENAME_CAL);
+            toFile((Serializable) d, filePath + javaVersion + FILENAME_DURATION);
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Read the object from a file.
+     */
+    private static Object fromFile(String filePath) throws IOException,
+            ClassNotFoundException {
+        InputStream streamIn = SerializationTest.class.getResourceAsStream(
+            filePath);
+        ObjectInputStream objectinputstream = new ObjectInputStream(streamIn);
+        Object o = objectinputstream.readObject();
+        return o;
+    }
+
+    /**
+     * Write the object to a file.
+     */
+    private static void toFile(Serializable o, String filePath) throws IOException {
+        FileOutputStream fout = new FileOutputStream(filePath, true);
+        ObjectOutputStream oos = new ObjectOutputStream(fout);
+        oos.writeObject(o);
+        oos.close();
+    }
+
+    static String errMessage;
+    int passed = 0, failed = 0;
+
+    void fail(String errMsg) {
+        if (errMessage == null) {
+            errMessage = errMsg;
+        } else {
+            errMessage = errMessage + "\n" + errMsg;
+        }
+        failed++;
+    }
+
+    void success(String msg) {
+        passed++;
+        System.out.println(msg);
+    }
+
+    public void report() {
+
+        System.out.println("\nNumber of tests passed: " + passed);
+        System.out.println("Number of tests failed: " + failed + "\n");
+
+        if (errMessage != null) {
+            throw new RuntimeException(errMessage);
+        }
+    }
+}