1 /*
2  * Copyright (c) 2005, 2016, 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 4068067
27  * @library /java/text/testlib
28  * @build DFSSerialization IntlTest HexDumpReader
29  * @run main DFSSerialization
30  * @summary Three different tests are done.
31  *    1. read from the object created using jdk1.4.2
32  *    2. create a valid DecimalFormatSymbols object with current JDK, then read the object
33  *    3. Try to create an valid DecimalFormatSymbols object by passing null to set null
34  *       for the exponent separator symbol. Expect the NullPointerException.
35  */
36 
37 import java.io.File;
38 import java.io.FileInputStream;
39 import java.io.FileOutputStream;
40 import java.io.InputStream;
41 import java.io.ObjectInputStream;
42 import java.io.ObjectOutputStream;
43 import java.text.DecimalFormatSymbols;
44 import java.util.Locale;
45 
46 public class DFSSerialization extends IntlTest{
main(String[] args)47     public static void main(String[] args) throws Exception {
48         new DFSSerialization().run(args);
49     }
TestDFSSerialization()50     public void TestDFSSerialization(){
51         /*
52          * 1. read from the object created using jdk1.4.2
53          */
54         File oldFile = new File(System.getProperty("test.src", "."), "DecimalFormatSymbols.142.txt");
55         DecimalFormatSymbols dfs142 = readTestObject(oldFile);
56         if (dfs142 != null){
57             if (dfs142.getExponentSeparator().equals("E") && dfs142.getCurrencySymbol().equals("*SpecialCurrencySymbol*")){
58                 System.out.println("\n  Deserialization of JDK1.4.2 Object from the current JDK: Passed.");
59                 logln(" Deserialization of JDK1.4.2 Object from the current JDK: Passed.");
60             } else {
61                 errln(" Deserialization of JDK1.4.2 Object from the current JDK was Failed:"
62                       +dfs142.getCurrencySymbol()+" "+dfs142.getExponentSeparator());
63                 /*
64                  * logically should not throw this exception as errln throws exception
65                  * if not thrown yet - but in case errln got changed
66                  */
67                 throw new RuntimeException(" Deserialization of JDK1.4.2 Object from the current JDK was Failed:"
68                                            +dfs142.getCurrencySymbol()+" "+dfs142.getExponentSeparator());
69             }
70         }
71         /*
72          * 2. create a valid DecimalFormatSymbols object with current JDK, then read the object
73          */
74         String validObject = "DecimalFormatSymbols.current";
75         File currentFile = createTestObject(validObject, "*SpecialExponentSeparator*");
76 
77         DecimalFormatSymbols dfsValid = readTestObject(currentFile);
78         if (dfsValid != null){
79             if (dfsValid.getExponentSeparator().equals("*SpecialExponentSeparator*") &&
80                 dfsValid.getCurrencySymbol().equals("*SpecialCurrencySymbol*")){
81                 System.out.println("  Deserialization of current JDK Object from the current JDK: Passed.");
82                 logln(" Deserialization of current JDK Object from the current JDK: Passed.");
83             } else {
84                 errln(" Deserialization of current JDK Object from the current JDK was Failed:"
85                       +dfsValid.getCurrencySymbol()+" "+dfsValid.getExponentSeparator());
86                 /*
87                  * logically should not throw this exception as errln throws exception
88                  * if not thrown yet - but in case errln got changed
89                  */
90                 throw new RuntimeException(" Deserialization of current Object from the current JDK was Failed:"
91                                            +dfsValid.getCurrencySymbol()+" "+dfsValid.getExponentSeparator());
92             }
93         }
94         /*
95          * 3. Try to create an valid DecimalFormatSymbols object by passing null
96          *    to set null for the exponent separator symbol. Expect the NullPointerException.
97          */
98         DecimalFormatSymbols symNPE = new DecimalFormatSymbols(Locale.US);
99         boolean npePassed = false;
100         try {
101             symNPE.setExponentSeparator(null);
102         } catch (NullPointerException npe){
103             npePassed = true;
104             System.out.println("  Trying to set exponent separator with null: Passed.");
105             logln(" Trying to set exponent separator with null: Passed.");
106         }
107         if (!npePassed){
108             System.out.println(" Trying to set exponent separator with null:Failed.");
109             errln("  Trying to set exponent separator with null:Failed.");
110             /*
111              * logically should not throw this exception as errln throws exception
112              * if not thrown yet - but in case errln got changed
113              */
114             throw new RuntimeException(" Trying to set exponent separator with null:Failed.");
115         }
116 
117     }
118 
readTestObject(File inputFile)119     private DecimalFormatSymbols readTestObject(File inputFile){
120         try (InputStream istream = inputFile.getName().endsWith(".txt") ?
121                                        HexDumpReader.getStreamFromHexDump(inputFile) :
122                                        new FileInputStream(inputFile)) {
123             ObjectInputStream p = new ObjectInputStream(istream);
124             DecimalFormatSymbols dfs = (DecimalFormatSymbols)p.readObject();
125             return dfs;
126         } catch (Exception e) {
127             errln("Test Malfunction in DFSSerialization: Exception while reading the object");
128             /*
129              * logically should not throw this exception as errln throws exception
130              * if not thrown yet - but in case errln got changed
131              */
132             throw new RuntimeException("Test Malfunction: re-throwing the exception", e);
133         }
134     }
135 
createTestObject(String objectName, String expString)136     private File createTestObject(String objectName, String expString){
137         DecimalFormatSymbols dfs= new DecimalFormatSymbols();
138         dfs.setExponentSeparator(expString);
139         dfs.setCurrencySymbol("*SpecialCurrencySymbol*");
140         logln(" The special exponent separator is set : "  + dfs.getExponentSeparator());
141         logln(" The special currency symbol is set : "  + dfs.getCurrencySymbol());
142 
143         // 6345659: create a test object in the test.class dir where test user has a write permission.
144         File file = new File(System.getProperty("test.class", "."), objectName);
145         try (FileOutputStream ostream = new FileOutputStream(file)) {
146             ObjectOutputStream p = new ObjectOutputStream(ostream);
147             p.writeObject(dfs);
148             //System.out.println(" The special currency symbol is set : "  + dfs.getCurrencySymbol());
149             return file;
150         } catch (Exception e){
151             errln("Test Malfunction in DFSSerialization: Exception while creating an object");
152             /*
153              * logically should not throw this exception as errln throws exception
154              * if not thrown yet - but in case errln got changed
155              */
156             throw new RuntimeException("Test Malfunction: re-throwing the exception", e);
157         }
158     }
159 }
160