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