|
1 /* |
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 8033980 |
|
27 * @summary verify serialization compatibility for XMLGregorianCalendar and Duration |
|
28 * @run main SerializationTest read |
|
29 */ |
|
30 |
|
31 import java.io.*; |
|
32 import javax.xml.datatype.DatatypeConfigurationException; |
|
33 import javax.xml.datatype.DatatypeFactory; |
|
34 import javax.xml.datatype.Duration; |
|
35 import javax.xml.datatype.XMLGregorianCalendar; |
|
36 |
|
37 /** |
|
38 * use "read" to test compatibility |
|
39 * SerializationTest read |
|
40 * |
|
41 * use "write" to create test files |
|
42 * SerializationTest write javaVersion |
|
43 * where javaVersion is 6, 7, 8, or 9 |
|
44 * |
|
45 * @author huizhe.wang@oracle.com</a> |
|
46 */ |
|
47 public class SerializationTest { |
|
48 |
|
49 final String FILENAME_CAL = "_XMLGregorianCalendar.ser"; |
|
50 final String FILENAME_DURATION = "_Duration.ser"; |
|
51 String filePath; |
|
52 |
|
53 { |
|
54 filePath = System.getProperty("test.src"); |
|
55 if (filePath == null) { |
|
56 //current directory |
|
57 filePath = System.getProperty("user.dir"); |
|
58 } |
|
59 filePath += File.separator; |
|
60 } |
|
61 final String EXPECTED_CAL = "0001-01-01T00:00:00.0000000-05:00"; |
|
62 final String EXPECTED_DURATION = "P1Y1M1DT1H1M1S"; |
|
63 static String[] JDK = {"JDK6", "JDK7", "JDK8", "JDK9"}; |
|
64 |
|
65 public static void main(String[] args) { |
|
66 SerializationTest test = new SerializationTest(); |
|
67 |
|
68 if (args[0].equalsIgnoreCase("read")) { |
|
69 test.testReadCal(); |
|
70 test.testReadDuration(); |
|
71 test.report(); |
|
72 } else { |
|
73 int ver = Integer.valueOf(args[1]).intValue(); |
|
74 test.createTestFile(JDK[ver - 6]); |
|
75 } |
|
76 |
|
77 } |
|
78 |
|
79 public void testReadCal() { |
|
80 try { |
|
81 for (String javaVersion : JDK) { |
|
82 XMLGregorianCalendar d1 = (XMLGregorianCalendar) fromFile( |
|
83 javaVersion + FILENAME_CAL); |
|
84 if (!d1.toString().equalsIgnoreCase(EXPECTED_CAL)) { |
|
85 fail("Java version: " + javaVersion |
|
86 + "\nExpected: " + EXPECTED_CAL |
|
87 + "\nActual: " + d1.toString()); |
|
88 } else { |
|
89 success("testReadCal: read " + javaVersion + " serialized file, passed."); |
|
90 } |
|
91 } |
|
92 } catch (ClassNotFoundException ex) { |
|
93 fail("testReadCal: " + ex.getMessage()); |
|
94 } catch (IOException ex) { |
|
95 fail("testReadCal: " + ex.getMessage()); |
|
96 } |
|
97 } |
|
98 |
|
99 public void testReadDuration() { |
|
100 try { |
|
101 for (String javaVersion : JDK) { |
|
102 Duration d1 = (Duration) fromFile( |
|
103 javaVersion + FILENAME_DURATION); |
|
104 if (!d1.toString().equalsIgnoreCase(EXPECTED_DURATION)) { |
|
105 fail("Java version: " + javaVersion |
|
106 + "\nExpected: " + EXPECTED_DURATION |
|
107 + "\nActual: " + d1.toString()); |
|
108 } else { |
|
109 success("testReadDuration: read " + javaVersion + " serialized file, passed."); |
|
110 } |
|
111 } |
|
112 } catch (ClassNotFoundException ex) { |
|
113 fail("testReadDuration: " + ex.getMessage()); |
|
114 } catch (IOException ex) { |
|
115 fail("testReadDuration: " + ex.getMessage()); |
|
116 } |
|
117 } |
|
118 |
|
119 /** |
|
120 * Create test files |
|
121 * |
|
122 * @param javaVersion JDK version |
|
123 */ |
|
124 public void createTestFile(String javaVersion) { |
|
125 try { |
|
126 DatatypeFactory dtf = DatatypeFactory.newInstance(); |
|
127 XMLGregorianCalendar c = dtf.newXMLGregorianCalendar(EXPECTED_CAL); |
|
128 Duration d = dtf.newDuration(EXPECTED_DURATION); |
|
129 toFile((Serializable) c, filePath + javaVersion + FILENAME_CAL); |
|
130 toFile((Serializable) d, filePath + javaVersion + FILENAME_DURATION); |
|
131 } catch (Exception e) { |
|
132 fail(e.getMessage()); |
|
133 } |
|
134 } |
|
135 |
|
136 /** |
|
137 * Read the object from a file. |
|
138 */ |
|
139 private static Object fromFile(String filePath) throws IOException, |
|
140 ClassNotFoundException { |
|
141 InputStream streamIn = SerializationTest.class.getResourceAsStream( |
|
142 filePath); |
|
143 ObjectInputStream objectinputstream = new ObjectInputStream(streamIn); |
|
144 Object o = objectinputstream.readObject(); |
|
145 return o; |
|
146 } |
|
147 |
|
148 /** |
|
149 * Write the object to a file. |
|
150 */ |
|
151 private static void toFile(Serializable o, String filePath) throws IOException { |
|
152 FileOutputStream fout = new FileOutputStream(filePath, true); |
|
153 ObjectOutputStream oos = new ObjectOutputStream(fout); |
|
154 oos.writeObject(o); |
|
155 oos.close(); |
|
156 } |
|
157 |
|
158 static String errMessage; |
|
159 int passed = 0, failed = 0; |
|
160 |
|
161 void fail(String errMsg) { |
|
162 if (errMessage == null) { |
|
163 errMessage = errMsg; |
|
164 } else { |
|
165 errMessage = errMessage + "\n" + errMsg; |
|
166 } |
|
167 failed++; |
|
168 } |
|
169 |
|
170 void success(String msg) { |
|
171 passed++; |
|
172 System.out.println(msg); |
|
173 } |
|
174 |
|
175 public void report() { |
|
176 |
|
177 System.out.println("\nNumber of tests passed: " + passed); |
|
178 System.out.println("Number of tests failed: " + failed + "\n"); |
|
179 |
|
180 if (errMessage != null) { |
|
181 throw new RuntimeException(errMessage); |
|
182 } |
|
183 } |
|
184 } |