CurrencyFormat.java revision 14630:29af931514f5
1/* 2 * Copyright (c) 2001, 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 4290801 4942982 5102005 8008577 8021121 27 * @summary Basic tests for currency formatting. 28 * @run main/othervm -Djava.locale.providers=JRE,SPI CurrencyFormat 29 */ 30 31import java.io.File; 32import java.io.FileInputStream; 33import java.util.Currency; 34import java.util.Locale; 35import java.util.Properties; 36import java.util.StringTokenizer; 37import java.util.TimeZone; 38import java.text.DecimalFormatSymbols; 39import java.text.NumberFormat; 40import java.text.SimpleDateFormat; 41 42public class CurrencyFormat { 43 44 public static void main(String[] args) throws Exception { 45 testFormatting(); 46 testSymbols(); 47 } 48 49 static void testFormatting() { 50 boolean failed = false; 51 Locale[] locales = { 52 Locale.US, 53 Locale.JAPAN, 54 Locale.GERMANY, 55 Locale.ITALY, 56 new Locale("it", "IT", "EURO") }; 57 Currency[] currencies = { 58 null, 59 Currency.getInstance("USD"), 60 Currency.getInstance("JPY"), 61 Currency.getInstance("DEM"), 62 Currency.getInstance("EUR"), 63 }; 64 String[][] expecteds = { 65 {"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"}, 66 {"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"}, 67 {"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"}, 68 {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"}, 69 {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"}, 70 }; 71 72 for (int i = 0; i < locales.length; i++) { 73 Locale locale = locales[i]; 74 NumberFormat format = NumberFormat.getCurrencyInstance(locale); 75 for (int j = 0; j < currencies.length; j++) { 76 Currency currency = currencies[j]; 77 String expected = expecteds[i][j]; 78 if (currency != null) { 79 format.setCurrency(currency); 80 int digits = currency.getDefaultFractionDigits(); 81 format.setMinimumFractionDigits(digits); 82 format.setMaximumFractionDigits(digits); 83 } 84 String result = format.format(1234.56); 85 if (!result.equals(expected)) { 86 failed = true; 87 System.out.println("FAIL: Locale " + locale 88 + (currency == null ? ", default currency" : (", currency: " + currency)) 89 + ", expected: " + expected 90 + ", actual: " + result); 91 } 92 } 93 } 94 95 if (failed) { 96 throw new RuntimeException(); 97 } 98 } 99 100 static void testSymbols() throws Exception { 101 FileInputStream stream = new FileInputStream(new File(System.getProperty("test.src", "."), "CurrencySymbols.properties")); 102 Properties props = new Properties(); 103 props.load(stream); 104 SimpleDateFormat format = null; 105 106 Locale[] locales = NumberFormat.getAvailableLocales(); 107 for (int i = 0; i < locales.length; i++) { 108 Locale locale = locales[i]; 109 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); 110 String result = symbols.getCurrencySymbol(); 111 String expected = (String) props.get(locale.toString()); 112 113 if (expected == null) { 114 System.out.println("Warning: No expected currency symbol defined for locale " + locale); 115 } else { 116 if (expected.contains(";")) { 117 StringTokenizer tokens = new StringTokenizer(expected, ";"); 118 int tokensCount = tokens.countTokens(); 119 120 if (tokensCount == 3) { 121 expected = tokens.nextToken(); 122 if (format == null) { 123 format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US); 124 format.setTimeZone(TimeZone.getTimeZone("GMT")); 125 format.setLenient(false); 126 } 127 128 if (format.parse(tokens.nextToken()).getTime() < System.currentTimeMillis()) { 129 expected = tokens.nextToken(); 130 } 131 } 132 } 133 134 if (!expected.equals(result)) { 135 throw new RuntimeException("Wrong currency symbol for locale " + 136 locale + ", expected: " + expected + ", got: " + result); 137 } 138 } 139 } 140 } 141} 142