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