1 /*
2 * Copyright (c) 1997, 2016, 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 /**
25 * @test
26 * @bug 4052223 4059870 4061302 4062486 4066646 4068693 4070798 4071005 4071014
27 * 4071492 4071859 4074454 4074620 4075713 4083018 4086575 4087244 4087245
28 * 4087251 4087535 4088161 4088503 4090489 4090504 4092480 4092561 4095713
29 * 4098741 4099404 4101481 4106658 4106662 4106664 4108738 4110936 4122840
30 * 4125885 4134034 4134300 4140009 4141750 4145457 4147295 4147706 4162198
31 * 4162852 4167494 4170798 4176114 4179818 4185761 4212072 4212073 4216742
32 * 4217661 4243011 4243108 4330377 4233840 4241880 4833877 8008577
33 * @summary Regression tests for NumberFormat and associated classes
34 * @library /java/text/testlib
35 * @build IntlTest HexDumpReader TestUtils
36 * @modules java.base/sun.util.resources
37 * jdk.localedata
38 * @compile -XDignore.symbol.file NumberRegression.java
39 * @run main/othervm -Djava.locale.providers=COMPAT,SPI NumberRegression
40 */
41
42 /*
43 (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
44 (C) Copyright IBM Corp. 1996 - All Rights Reserved
45
46 The original version of this source code and documentation is copyrighted and
47 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
48 provided under terms of a License Agreement between Taligent and Sun. This
49 technology is protected by multiple US and International patents. This notice and
50 attribution to Taligent may not be removed.
51 Taligent is a registered trademark of Taligent, Inc.
52 */
1785 "#,##0.00", "#,##0.000", "#,##0.000",
1786 };
1787 String[] expected = {
1788 "2%", "1%", "2%", "2%", "1%",
1789 "0%", "0%", "1%", "1%", "1%",
1790 "0", "2", "0.2", "0.6", "0.04",
1791 "0.04", "0.000", "0.002",
1792 };
1793 for (int i = 0; i < input.length; i++) {
1794 DecimalFormat format = new DecimalFormat(pattern[i]);
1795 String result = format.format(input[i]);
1796 if (!result.equals(expected[i])) {
1797 errln("FAIL: input: " + input[i] +
1798 ", pattern: " + pattern[i] +
1799 ", expected: " + expected[i] +
1800 ", got: " + result);
1801 }
1802 }
1803 Locale.setDefault(savedLocale);
1804 }
1805 }
1806
1807 @SuppressWarnings("serial")
1808 class myformat implements Serializable
1809 {
1810 DateFormat _dateFormat = DateFormat.getDateInstance();
1811
1812 public String Now()
1813 {
1814 GregorianCalendar calendar = new GregorianCalendar();
1815 Date t = calendar.getTime();
1816 String nowStr = _dateFormat.format(t);
1817 return nowStr;
1818 }
1819 }
1820
1821 @SuppressWarnings("serial")
1822 class MyNumberFormatTest extends NumberFormat {
1823 public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
1824 return new StringBuffer("");
|
1 /*
2 * Copyright (c) 1997, 2020, 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 /**
25 * @test
26 * @bug 4052223 4059870 4061302 4062486 4066646 4068693 4070798 4071005 4071014
27 * 4071492 4071859 4074454 4074620 4075713 4083018 4086575 4087244 4087245
28 * 4087251 4087535 4088161 4088503 4090489 4090504 4092480 4092561 4095713
29 * 4098741 4099404 4101481 4106658 4106662 4106664 4108738 4110936 4122840
30 * 4125885 4134034 4134300 4140009 4141750 4145457 4147295 4147706 4162198
31 * 4162852 4167494 4170798 4176114 4179818 4185761 4212072 4212073 4216742
32 * 4217661 4243011 4243108 4330377 4233840 4241880 4833877 8008577 8227313
33 * @summary Regression tests for NumberFormat and associated classes
34 * @library /java/text/testlib
35 * @build IntlTest HexDumpReader TestUtils
36 * @modules java.base/sun.util.resources
37 * jdk.localedata
38 * @compile -XDignore.symbol.file NumberRegression.java
39 * @run main/othervm -Djava.locale.providers=COMPAT,SPI NumberRegression
40 */
41
42 /*
43 (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
44 (C) Copyright IBM Corp. 1996 - All Rights Reserved
45
46 The original version of this source code and documentation is copyrighted and
47 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
48 provided under terms of a License Agreement between Taligent and Sun. This
49 technology is protected by multiple US and International patents. This notice and
50 attribution to Taligent may not be removed.
51 Taligent is a registered trademark of Taligent, Inc.
52 */
1785 "#,##0.00", "#,##0.000", "#,##0.000",
1786 };
1787 String[] expected = {
1788 "2%", "1%", "2%", "2%", "1%",
1789 "0%", "0%", "1%", "1%", "1%",
1790 "0", "2", "0.2", "0.6", "0.04",
1791 "0.04", "0.000", "0.002",
1792 };
1793 for (int i = 0; i < input.length; i++) {
1794 DecimalFormat format = new DecimalFormat(pattern[i]);
1795 String result = format.format(input[i]);
1796 if (!result.equals(expected[i])) {
1797 errln("FAIL: input: " + input[i] +
1798 ", pattern: " + pattern[i] +
1799 ", expected: " + expected[i] +
1800 ", got: " + result);
1801 }
1802 }
1803 Locale.setDefault(savedLocale);
1804 }
1805
1806 /**
1807 * Test for get/setMonetaryGroupingSeparator() methods.
1808 * @since 15
1809 */
1810 public void test8227313() throws ParseException {
1811 var nrmSep = 'n';
1812 var monSep = 'm';
1813 var curSym = "Cur";
1814 var inputNum = 10;
1815 var nrmPattern = ",#";
1816 var monPattern = "\u00a4 ,#";
1817 var expectedNrmFmt = "1n0";
1818 var expectedMonFmt = "Cur 1m0";
1819
1820 var ndfs = DecimalFormatSymbols.getInstance();
1821 ndfs.setGroupingSeparator(nrmSep);
1822 var nf = new DecimalFormat(nrmPattern, ndfs);
1823 var mdfs = DecimalFormatSymbols.getInstance();
1824 mdfs.setMonetaryGroupingSeparator(monSep);
1825 mdfs.setCurrencySymbol(curSym);
1826 var mf = new DecimalFormat(monPattern, mdfs);
1827
1828 // get test
1829 char gotNrmSep = mdfs.getGroupingSeparator();
1830 char gotMonSep = mdfs.getMonetaryGroupingSeparator();
1831 if (gotMonSep != monSep) {
1832 errln("FAIL: getMonetaryGroupingSeparator() returned incorrect value. expected: "
1833 + monSep + ", got: " + gotMonSep);
1834 }
1835 if (gotMonSep == gotNrmSep) {
1836 errln("FAIL: getMonetaryGroupingSeparator() returned the same value with " +
1837 "getGroupingSeparator(): monetary sep: " + gotMonSep +
1838 ", normal sep: " + gotNrmSep);
1839 }
1840
1841 // format test
1842 var formatted = mf.format(inputNum);
1843 if (!formatted.equals(expectedMonFmt)) {
1844 errln("FAIL: format failed. expected: " + expectedMonFmt +
1845 ", got: " + formatted);
1846 }
1847 formatted = nf.format(inputNum);
1848 if (!formatted.equals(expectedNrmFmt)) {
1849 errln("FAIL: normal format failed. expected: " + expectedNrmFmt +
1850 ", got: " + formatted);
1851 }
1852
1853 // parse test
1854 Number parsed = mf.parse(expectedMonFmt);
1855 if (parsed.intValue() != inputNum) {
1856 errln("FAIL: parse failed. expected: " + inputNum +
1857 ", got: " + parsed);
1858 }
1859 parsed = nf.parse(expectedNrmFmt);
1860 if (parsed.intValue() != inputNum) {
1861 errln("FAIL: normal parse failed. expected: " + inputNum +
1862 ", got: " + parsed);
1863 }
1864 }
1865 }
1866
1867 @SuppressWarnings("serial")
1868 class myformat implements Serializable
1869 {
1870 DateFormat _dateFormat = DateFormat.getDateInstance();
1871
1872 public String Now()
1873 {
1874 GregorianCalendar calendar = new GregorianCalendar();
1875 Date t = calendar.getTime();
1876 String nowStr = _dateFormat.format(t);
1877 return nowStr;
1878 }
1879 }
1880
1881 @SuppressWarnings("serial")
1882 class MyNumberFormatTest extends NumberFormat {
1883 public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
1884 return new StringBuffer("");
|