/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 8177552 * @modules jdk.localedata * @summary Checks the serialization feature of CompactNumberFormat */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.RoundingMode; import java.text.CompactNumberFormat; import java.text.NumberFormat; import java.util.Locale; public class SerializationTest { public static void main(String[] args) { NumberFormat format1 = NumberFormat.getCompactNumberInstance( new Locale("hi"), NumberFormat.Style.SHORT); format1.setMinimumFractionDigits(2); format1.setMinimumIntegerDigits(5); CompactNumberFormat format2 = (CompactNumberFormat) NumberFormat .getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG); format2.setRoundingMode(RoundingMode.HALF_UP); format2.setGroupingSize(3); NumberFormat format3 = NumberFormat.getCompactNumberInstance( Locale.JAPAN, NumberFormat.Style.SHORT); format3.setMaximumFractionDigits(30); format3.setMaximumIntegerDigits(30); NumberFormat format4 = NumberFormat.getCompactNumberInstance( Locale.FRANCE, NumberFormat.Style.LONG); format4.setParseIntegerOnly(true); format4.setGroupingUsed(true); // setting minimum integer digits beyond the allowed range NumberFormat format5 = NumberFormat.getCompactNumberInstance( Locale.GERMANY, NumberFormat.Style.SHORT); format5.setMinimumIntegerDigits(320); // setting minimum fraction digits beyond the allowed range NumberFormat format6 = NumberFormat.getCompactNumberInstance( Locale.KOREA, NumberFormat.Style.SHORT); format6.setMinimumFractionDigits(350); try { // serialize serialize("cdf.ser", format1, format2, format3, format4, format5, format6); // deserialize deserialize("cdf.ser", format1, format2, format3, format4, format5, format6); } catch (IOException ex) { throw new RuntimeException("[FAILED: Exception occured while" + " serializing/deserializing]", ex); } catch (ClassNotFoundException ex) { throw new RuntimeException("[FAILED: Exception occured while" + " deserializing the object]", ex); } } private static void serialize(String fileName, NumberFormat... formats) throws IOException { try (ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream(fileName))) { for (NumberFormat fmt : formats) { os.writeObject(fmt); } } } private static void deserialize(String fileName, NumberFormat... formats) throws IOException, ClassNotFoundException { try (ObjectInputStream os = new ObjectInputStream( new FileInputStream(fileName))) { for (NumberFormat fmt : formats) { NumberFormat obj = (NumberFormat) os.readObject(); if (!fmt.equals(obj)) { throw new RuntimeException("serialized and deserialized" + " objects do not match"); } long number = 123456789789L; String expected = fmt.format(number); String actual = obj.format(number); if (!expected.equals(actual)) { throw new RuntimeException("serialized and deserialized" + " objects are expected to return similar formatted" + " output for number: " + number + ", Expected: " + expected + ", Actual: " + actual); } } } } }