Path: blob/master/test/jdk/java/text/Format/NumberFormat/DFSSerialization.java
41152 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 406806726* @library /java/text/testlib27* @build DFSSerialization IntlTest HexDumpReader28* @run main DFSSerialization29* @summary Three different tests are done.30* 1. read from the object created using jdk1.4.231* 2. create a valid DecimalFormatSymbols object with current JDK, then read the object32* 3. Try to create an valid DecimalFormatSymbols object by passing null to set null33* for the exponent separator symbol. Expect the NullPointerException.34*/3536import java.io.File;37import java.io.FileInputStream;38import java.io.FileOutputStream;39import java.io.InputStream;40import java.io.ObjectInputStream;41import java.io.ObjectOutputStream;42import java.text.DecimalFormatSymbols;43import java.util.Locale;4445public class DFSSerialization extends IntlTest{46public static void main(String[] args) throws Exception {47new DFSSerialization().run(args);48}49public void TestDFSSerialization(){50/*51* 1. read from the object created using jdk1.4.252*/53File oldFile = new File(System.getProperty("test.src", "."), "DecimalFormatSymbols.142.txt");54DecimalFormatSymbols dfs142 = readTestObject(oldFile);55if (dfs142 != null){56if (dfs142.getExponentSeparator().equals("E") && dfs142.getCurrencySymbol().equals("*SpecialCurrencySymbol*")){57System.out.println("\n Deserialization of JDK1.4.2 Object from the current JDK: Passed.");58logln(" Deserialization of JDK1.4.2 Object from the current JDK: Passed.");59} else {60errln(" Deserialization of JDK1.4.2 Object from the current JDK was Failed:"61+dfs142.getCurrencySymbol()+" "+dfs142.getExponentSeparator());62/*63* logically should not throw this exception as errln throws exception64* if not thrown yet - but in case errln got changed65*/66throw new RuntimeException(" Deserialization of JDK1.4.2 Object from the current JDK was Failed:"67+dfs142.getCurrencySymbol()+" "+dfs142.getExponentSeparator());68}69}70/*71* 2. create a valid DecimalFormatSymbols object with current JDK, then read the object72*/73String validObject = "DecimalFormatSymbols.current";74File currentFile = createTestObject(validObject, "*SpecialExponentSeparator*");7576DecimalFormatSymbols dfsValid = readTestObject(currentFile);77if (dfsValid != null){78if (dfsValid.getExponentSeparator().equals("*SpecialExponentSeparator*") &&79dfsValid.getCurrencySymbol().equals("*SpecialCurrencySymbol*")){80System.out.println(" Deserialization of current JDK Object from the current JDK: Passed.");81logln(" Deserialization of current JDK Object from the current JDK: Passed.");82} else {83errln(" Deserialization of current JDK Object from the current JDK was Failed:"84+dfsValid.getCurrencySymbol()+" "+dfsValid.getExponentSeparator());85/*86* logically should not throw this exception as errln throws exception87* if not thrown yet - but in case errln got changed88*/89throw new RuntimeException(" Deserialization of current Object from the current JDK was Failed:"90+dfsValid.getCurrencySymbol()+" "+dfsValid.getExponentSeparator());91}92}93/*94* 3. Try to create an valid DecimalFormatSymbols object by passing null95* to set null for the exponent separator symbol. Expect the NullPointerException.96*/97DecimalFormatSymbols symNPE = new DecimalFormatSymbols(Locale.US);98boolean npePassed = false;99try {100symNPE.setExponentSeparator(null);101} catch (NullPointerException npe){102npePassed = true;103System.out.println(" Trying to set exponent separator with null: Passed.");104logln(" Trying to set exponent separator with null: Passed.");105}106if (!npePassed){107System.out.println(" Trying to set exponent separator with null:Failed.");108errln(" Trying to set exponent separator with null:Failed.");109/*110* logically should not throw this exception as errln throws exception111* if not thrown yet - but in case errln got changed112*/113throw new RuntimeException(" Trying to set exponent separator with null:Failed.");114}115116}117118private DecimalFormatSymbols readTestObject(File inputFile){119try (InputStream istream = inputFile.getName().endsWith(".txt") ?120HexDumpReader.getStreamFromHexDump(inputFile) :121new FileInputStream(inputFile)) {122ObjectInputStream p = new ObjectInputStream(istream);123DecimalFormatSymbols dfs = (DecimalFormatSymbols)p.readObject();124return dfs;125} catch (Exception e) {126errln("Test Malfunction in DFSSerialization: Exception while reading the object");127/*128* logically should not throw this exception as errln throws exception129* if not thrown yet - but in case errln got changed130*/131throw new RuntimeException("Test Malfunction: re-throwing the exception", e);132}133}134135private File createTestObject(String objectName, String expString){136DecimalFormatSymbols dfs= new DecimalFormatSymbols();137dfs.setExponentSeparator(expString);138dfs.setCurrencySymbol("*SpecialCurrencySymbol*");139logln(" The special exponent separator is set : " + dfs.getExponentSeparator());140logln(" The special currency symbol is set : " + dfs.getCurrencySymbol());141142// 6345659: create a test object in the test.class dir where test user has a write permission.143File file = new File(System.getProperty("test.class", "."), objectName);144try (FileOutputStream ostream = new FileOutputStream(file)) {145ObjectOutputStream p = new ObjectOutputStream(ostream);146p.writeObject(dfs);147//System.out.println(" The special currency symbol is set : " + dfs.getCurrencySymbol());148return file;149} catch (Exception e){150errln("Test Malfunction in DFSSerialization: Exception while creating an object");151/*152* logically should not throw this exception as errln throws exception153* if not thrown yet - but in case errln got changed154*/155throw new RuntimeException("Test Malfunction: re-throwing the exception", e);156}157}158}159160161