1 /* 2 * Copyright (c) 2018, 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 * @test 25 * @bug 8177552 26 * @modules jdk.localedata 27 * @summary Checks the serialization feature of CompactNumberFormat 28 * @run testng/othervm TestSerialization 29 */ 30 31 import org.testng.annotations.BeforeTest; 32 import org.testng.annotations.Test; 33 34 import java.io.FileInputStream; 35 import java.io.FileOutputStream; 36 import java.io.IOException; 37 import java.io.ObjectInputStream; 38 import java.io.ObjectOutputStream; 39 import java.math.RoundingMode; 40 import java.text.CompactNumberFormat; 41 import java.text.NumberFormat; 42 import java.util.Locale; 43 import static org.testng.Assert.*; 44 45 public class TestSerialization { 46 47 private static final NumberFormat FORMAT_HI = NumberFormat.getCompactNumberInstance( 48 new Locale("hi"), NumberFormat.Style.SHORT); 49 private static final CompactNumberFormat FORMAT_EN_US = (CompactNumberFormat) NumberFormat 50 .getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG); 51 private static final NumberFormat FORMAT_JA_JP = NumberFormat.getCompactNumberInstance( 52 Locale.JAPAN, NumberFormat.Style.SHORT); 53 private static final NumberFormat FORMAT_FR_FR = NumberFormat.getCompactNumberInstance( 54 Locale.FRANCE, NumberFormat.Style.LONG); 55 private static final NumberFormat FORMAT_DE_DE = NumberFormat.getCompactNumberInstance( 56 Locale.GERMANY, NumberFormat.Style.SHORT); 57 private static final NumberFormat FORMAT_KO_KR = NumberFormat.getCompactNumberInstance( 58 Locale.KOREA, NumberFormat.Style.SHORT); 59 60 @BeforeTest 61 public void mutateInstances() { 62 FORMAT_HI.setMinimumFractionDigits(2); 63 FORMAT_HI.setMinimumIntegerDigits(5); 64 65 FORMAT_EN_US.setRoundingMode(RoundingMode.HALF_UP); 66 FORMAT_EN_US.setGroupingSize(3); 67 FORMAT_EN_US.setParseBigDecimal(true); 68 69 FORMAT_JA_JP.setMaximumFractionDigits(30); 70 FORMAT_JA_JP.setMaximumIntegerDigits(30); 71 72 FORMAT_FR_FR.setParseIntegerOnly(true); 73 FORMAT_FR_FR.setGroupingUsed(true); 74 75 // Setting minimum integer digits beyond the allowed range 76 FORMAT_DE_DE.setMinimumIntegerDigits(320); 77 78 // Setting minimum fraction digits beyond the allowed range 79 FORMAT_KO_KR.setMinimumFractionDigits(350); 80 } 81 82 @Test 83 public void testSerialization() throws IOException, ClassNotFoundException { 84 // Serialize 85 serialize("cdf.ser", FORMAT_HI, FORMAT_EN_US, FORMAT_JA_JP, FORMAT_FR_FR, FORMAT_DE_DE, FORMAT_KO_KR); 86 // Deserialize 87 deserialize("cdf.ser", FORMAT_HI, FORMAT_EN_US, FORMAT_JA_JP, FORMAT_FR_FR, FORMAT_DE_DE, FORMAT_KO_KR); 88 } 89 90 private void serialize(String fileName, NumberFormat... formats) 91 throws IOException { 92 try (ObjectOutputStream os = new ObjectOutputStream( 93 new FileOutputStream(fileName))) { 94 for (NumberFormat fmt : formats) { 95 os.writeObject(fmt); 96 } 97 } 98 } 99 100 private static void deserialize(String fileName, NumberFormat... formats) 101 throws IOException, ClassNotFoundException { 102 try (ObjectInputStream os = new ObjectInputStream( 103 new FileInputStream(fileName))) { 104 for (NumberFormat fmt : formats) { 105 NumberFormat obj = (NumberFormat) os.readObject(); 106 assertEquals(fmt, obj, "Serialized and deserialized" 107 + " objects do not match"); 108 109 long number = 123456789789L; 110 String expected = fmt.format(number); 111 String actual = obj.format(number); 112 assertEquals(actual, expected, "Serialized and deserialized" 113 + " objects are expected to return same formatted" 114 + " output for number: " + number); 115 } 116 } 117 } 118 }