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