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