Path: blob/master/test/jdk/java/text/Format/DecimalFormat/Bug7196316.java
41152 views
/*1* Copyright (c) 2012, 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*/22/*23* @test24* @bug 719631625* @summary Confirm that a non-default rounding mode is used even after deserialization.26*/272829import java.io.*;30import java.math.*;31import java.text.*;3233public class Bug7196316 {3435private static final String filename = "bug7196316.ser";3637public static void main(String[] args) throws Exception {38DecimalFormat df;39RoundingMode mode = RoundingMode.DOWN;40double given = 6.6;41String expected;42String actual;4344try (ObjectOutputStream os45= new ObjectOutputStream(new FileOutputStream(filename))) {46df = new DecimalFormat("#");47df.setRoundingMode(mode);48expected = df.format(given);49os.writeObject(df);50}5152try (ObjectInputStream is53= new ObjectInputStream(new FileInputStream(filename))) {54df = (DecimalFormat)is.readObject();55}5657RoundingMode newMode = df.getRoundingMode();58if (mode != newMode) {59throw new RuntimeException("Unexpected roundig mode: " + newMode);60} else {61actual = df.format(given);62if (!expected.equals(actual)) {63throw new RuntimeException("Unexpected formatted result: \""64+ actual + "\"");65} else {66System.out.println("Passed: Expected rounding mode (" + newMode67+ ") & formatted result: \"" + actual + "\"");68}69}70}7172}737475