Path: blob/master/test/jdk/java/text/Format/NumberFormat/DFSMinusPerCentMill.java
41152 views
/*1* Copyright (c) 2019, 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 8220309 823028426* @library /java/text/testlib27* @summary Test String representation of MinusSign/Percent/PerMill symbols.28* This test assumes CLDR has numbering systems for "arab" and29* "arabext", and their minus/percent representations include30* BiDi formatting control characters.31* @run testng/othervm DFSMinusPerCentMill32*/3334import java.io.*;35import java.util.*;36import java.text.*;3738import static org.testng.Assert.*;39import org.testng.annotations.DataProvider;40import org.testng.annotations.Test;4142public class DFSMinusPerCentMill {43private enum Type {44NUMBER, PERCENT, CURRENCY, INTEGER, COMPACT, PERMILL45}4647private static final Locale US_ARAB = Locale.forLanguageTag("en-US-u-nu-arab");48private static final Locale US_ARABEXT = Locale.forLanguageTag("en-US-u-nu-arabext");49private static final double SRC_NUM = -1234.56;5051@DataProvider52Object[][] formatData() {53return new Object[][] {54// Locale, FormatStyle, expected format, expected single char symbol55{US_ARAB, Type.NUMBER, "\u061c-\u0661\u066c\u0662\u0663\u0664\u066b\u0665\u0666"},56{US_ARAB, Type.PERCENT, "\u061c-\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066a\u061c"},57{US_ARAB, Type.CURRENCY, "\u061c-\u0661\u066c\u0662\u0663\u0664\u066b\u0665\u0666\u00a0$"},58{US_ARAB, Type.INTEGER, "\u061c-\u0661\u066c\u0662\u0663\u0665"},59{US_ARAB, Type.COMPACT, "\u061c-\u0661K"},60{US_ARAB, Type.PERMILL, "\u061c-\u0661\u0662\u0663\u0664\u0665\u0666\u0660\u0609"},6162{US_ARABEXT, Type.NUMBER, "\u200e-\u200e\u06f1\u066c\u06f2\u06f3\u06f4\u066b\u06f5\u06f6"},63{US_ARABEXT, Type.PERCENT, "\u200e-\u200e\u06f1\u06f2\u06f3\u066c\u06f4\u06f5\u06f6\u066a"},64{US_ARABEXT, Type.CURRENCY, "\u200e-\u200e$\u00a0\u06f1\u066c\u06f2\u06f3\u06f4\u066b\u06f5\u06f6"},65{US_ARABEXT, Type.INTEGER, "\u200e-\u200e\u06f1\u066c\u06f2\u06f3\u06f5"},66{US_ARABEXT, Type.COMPACT, "\u200e-\u200e\u06f1K"},67{US_ARABEXT, Type.PERMILL, "\u200e-\u200e\u06f1\u06f2\u06f3\u06f4\u06f5\u06f6\u06f0\u0609"},68};69}7071@DataProvider72Object[][] charSymbols() {73return new Object[][]{74// Locale, percent, per mille, minus sign75{US_ARAB, '\u066a', '\u0609', '-'},76{US_ARABEXT, '\u066a', '\u0609', '-'},77};78}7980@Test(dataProvider="formatData")81public void testFormatData(Locale l, Type style, String expected) {82NumberFormat nf = null;83switch (style) {84case NUMBER:85nf = NumberFormat.getNumberInstance(l);86break;87case PERCENT:88nf = NumberFormat.getPercentInstance(l);89break;90case CURRENCY:91nf = NumberFormat.getCurrencyInstance(l);92break;93case INTEGER:94nf = NumberFormat.getIntegerInstance(l);95break;96case COMPACT:97nf = NumberFormat.getCompactNumberInstance(l, NumberFormat.Style.SHORT);98break;99case PERMILL:100nf = new DecimalFormat("#.#\u2030", DecimalFormatSymbols.getInstance(l));101break;102}103104assertEquals(nf.format(SRC_NUM), expected);105}106107@Test(dataProvider="charSymbols")108public void testCharSymbols(Locale l, char percent, char permill, char minus) {109DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);110assertEquals(dfs.getPercent(), percent);111assertEquals(dfs.getPerMill(), permill);112assertEquals(dfs.getMinusSign(), minus);113}114115@Test116public void testSerialization() throws Exception {117DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance();118ByteArrayOutputStream bos = new ByteArrayOutputStream();119new ObjectOutputStream(bos).writeObject(dfs);120DecimalFormatSymbols dfsSerialized = (DecimalFormatSymbols)new ObjectInputStream(121new ByteArrayInputStream(bos.toByteArray())122).readObject();123124assertEquals(dfs, dfsSerialized);125126// set minus/percent/permille127dfs.setMinusSign('a');128dfs.setPercent('b');129dfs.setPerMill('c');130bos = new ByteArrayOutputStream();131new ObjectOutputStream(bos).writeObject(dfs);132dfsSerialized = (DecimalFormatSymbols)new ObjectInputStream(133new ByteArrayInputStream(bos.toByteArray())134).readObject();135136assertEquals(dfs, dfsSerialized);137}138}139140141