< prev index next >

test/jdk/java/text/Format/NumberFormat/DFSSerialization.java

Print this page

  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{
 47     public static void main(String[] args) throws Exception {
 48         new DFSSerialization().run(args);
 49     }



 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 
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 
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 }

  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 }
< prev index next >