0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * @test
0N/A * @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 6371531
3928N/A * 6488442 7036905
0N/A * @summary Basic tests for Currency class.
0N/A */
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.util.Calendar;
0N/Aimport java.util.Date;
0N/Aimport java.util.Currency;
0N/Aimport java.util.GregorianCalendar;
0N/Aimport java.util.Locale;
0N/Aimport java.util.TimeZone;
0N/A
0N/A
0N/Apublic class CurrencyTest {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A CheckDataVersion.check();
0N/A testCurrencyCodeValidation();
0N/A testLocaleMapping();
0N/A testSymbols();
0N/A testFractionDigits();
0N/A testSerialization();
0N/A testDisplayNames();
0N/A }
0N/A
0N/A static void testCurrencyCodeValidation() {
0N/A // test creation of some valid currencies
0N/A testValidCurrency("USD");
0N/A testValidCurrency("EUR");
0N/A testValidCurrency("GBP");
0N/A testValidCurrency("JPY");
0N/A testValidCurrency("CNY");
0N/A testValidCurrency("CHF");
0N/A
0N/A // test creation of some fictitious currencies
0N/A testInvalidCurrency("AQD");
0N/A testInvalidCurrency("US$");
0N/A testInvalidCurrency("\u20AC");
0N/A }
0N/A
0N/A static void testValidCurrency(String currencyCode) {
0N/A Currency currency1 = Currency.getInstance(currencyCode);
0N/A Currency currency2 = Currency.getInstance(currencyCode);
0N/A if (currency1 != currency2) {
0N/A throw new RuntimeException("Didn't get same instance for same currency code");
0N/A }
0N/A if (!currency1.getCurrencyCode().equals(currencyCode)) {
0N/A throw new RuntimeException("Currency code changed");
0N/A }
0N/A }
0N/A
0N/A static void testInvalidCurrency(String currencyCode) {
0N/A boolean gotException = false;
0N/A try {
0N/A Currency currency = Currency.getInstance(currencyCode);
0N/A } catch (IllegalArgumentException e) {
0N/A gotException = true;
0N/A }
0N/A if (!gotException) {
0N/A throw new RuntimeException("didn't get specified exception");
0N/A }
0N/A }
0N/A
0N/A static void testLocaleMapping() {
0N/A // very basic test: most countries have their own currency, and then
0N/A // their currency code is an extension of their country code.
0N/A Locale[] locales = Locale.getAvailableLocales();
0N/A int goodCountries = 0;
0N/A int ownCurrencies = 0;
0N/A for (int i = 0; i < locales.length; i++) {
0N/A Locale locale = locales[i];
0N/A if (locale.getCountry().length() == 0) {
0N/A boolean gotException = false;
0N/A try {
0N/A Currency.getInstance(locale);
0N/A } catch (IllegalArgumentException e) {
0N/A gotException = true;
0N/A }
0N/A if (!gotException) {
0N/A throw new RuntimeException("didn't get specified exception");
0N/A }
0N/A } else {
0N/A goodCountries++;
0N/A Currency currency = Currency.getInstance(locale);
0N/A if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
0N/A ownCurrencies++;
0N/A }
0N/A }
0N/A }
0N/A System.out.println("Countries tested: " + goodCountries +
0N/A ", own currencies: " + ownCurrencies);
0N/A if (ownCurrencies < (goodCountries / 2 + 1)) {
0N/A throw new RuntimeException("suspicious: not enough countries have their own currency.");
0N/A }
0N/A
0N/A // check a few countries that don't change their currencies too often
0N/A String[] country1 = {"US", "CA", "JP", "CN", "SG", "CH"};
0N/A String[] currency1 = {"USD", "CAD", "JPY", "CNY", "SGD", "CHF"};
0N/A for (int i = 0; i < country1.length; i++) {
0N/A checkCountryCurrency(country1[i], currency1[i]);
0N/A }
0N/A
0N/A // check currency changes
0N/A String[] switchOverCtry = {"DE", "FR", "ES", "IT", "NL", "BE", "TR", "RO", "AZ", "MZ", "GH", "VE"};
0N/A String[] switchOverOld = {"DEM", "FRF", "ESP", "ITL", "NLG", "BEF", "TRL", "ROL", "AZM", "MZM", "GHC", "VEB"};
0N/A String[] switchOverNew = {"EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "TRY", "RON", "AZN", "MZN", "GHS", "VEF"};
0N/A String[] switchOverTZ = {"Europe/Paris", "Europe/Paris", "Europe/Paris", "Europe/Paris",
0N/A "Europe/Paris", "Europe/Paris", "Asia/Istanbul", "Europe/Bucharest",
0N/A "Asia/Baku", "Africa/Maputo", "Africa/Accra", "America/Caracas"};
0N/A int[] switchOverYear = {2002, 2002, 2002, 2002, 2002, 2002, 2005, 2005, 2006, 2006, 2007, 2008};
0N/A int[] switchOverMonth = {Calendar.JANUARY, Calendar.JANUARY, Calendar.JANUARY, Calendar.JANUARY,
0N/A Calendar.JANUARY, Calendar.JANUARY, Calendar.JANUARY, Calendar.JULY,
0N/A Calendar.JANUARY, Calendar.JULY, Calendar.JULY, Calendar.JANUARY};
0N/A int[] switchOverDay = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
0N/A for (int i = 0; i < switchOverCtry.length; i++) {
0N/A TimeZone.setDefault(TimeZone.getTimeZone(switchOverTZ[i]));
0N/A Calendar date = new GregorianCalendar(switchOverYear[i], switchOverMonth[i], switchOverDay[i]);
0N/A long switchOver = date.getTime().getTime();
0N/A boolean switchedOver = System.currentTimeMillis() >= switchOver;
0N/A checkCountryCurrency(switchOverCtry[i], switchedOver ? switchOverNew[i] : switchOverOld[i]);
0N/A }
0N/A
0N/A // check a country code which doesn't have a currency
0N/A checkCountryCurrency("AQ", null);
0N/A
0N/A // check an invalid country code
0N/A boolean gotException = false;
0N/A try {
0N/A Currency.getInstance(new Locale("", "EU"));
0N/A } catch (IllegalArgumentException e) {
0N/A gotException = true;
0N/A }
0N/A if (!gotException) {
0N/A throw new RuntimeException("didn't get specified exception.");
0N/A }
0N/A }
0N/A
0N/A static void checkCountryCurrency(String countryCode, String expected) {
0N/A Locale locale = new Locale("", countryCode);
0N/A Currency currency = Currency.getInstance(locale);
0N/A String code = (currency != null) ? currency.getCurrencyCode() : null;
0N/A if (!(expected == null ? code == null : expected.equals(code))) {
0N/A throw new RuntimeException("Wrong currency for " +
0N/A locale.getDisplayCountry() +
0N/A ": expected " + expected + ", got " + code);
0N/A }
0N/A }
0N/A
0N/A static void testSymbols() {
0N/A testSymbol("USD", Locale.US, "$");
0N/A testSymbol("EUR", Locale.GERMANY, "\u20AC");
0N/A testSymbol("USD", Locale.PRC, "USD");
0N/A }
0N/A
0N/A static void testSymbol(String currencyCode, Locale locale, String expectedSymbol) {
0N/A String symbol = Currency.getInstance(currencyCode).getSymbol(locale);
0N/A if (!symbol.equals(expectedSymbol)) {
0N/A throw new RuntimeException("Wrong symbol for currency " +
0N/A currencyCode +": expected " + expectedSymbol +
0N/A ", got " + symbol);
0N/A }
0N/A }
0N/A
0N/A static void testFractionDigits() {
0N/A testFractionDigits("USD", 2);
0N/A testFractionDigits("EUR", 2);
0N/A testFractionDigits("JPY", 0);
0N/A testFractionDigits("XDR", -1);
0N/A
0N/A testFractionDigits("BHD", 3);
0N/A testFractionDigits("IQD", 3);
0N/A testFractionDigits("JOD", 3);
0N/A testFractionDigits("KWD", 3);
0N/A testFractionDigits("LYD", 3);
0N/A testFractionDigits("OMR", 3);
0N/A testFractionDigits("TND", 3);
0N/A
0N/A // Turkish Lira
0N/A testFractionDigits("TRL", 0);
0N/A testFractionDigits("TRY", 2);
0N/A }
0N/A
0N/A static void testFractionDigits(String currencyCode, int expectedFractionDigits) {
0N/A int digits = Currency.getInstance(currencyCode).getDefaultFractionDigits();
0N/A if (digits != expectedFractionDigits) {
0N/A throw new RuntimeException("Wrong number of fraction digits for currency " +
0N/A currencyCode +": expected " + expectedFractionDigits +
0N/A ", got " + digits);
0N/A }
0N/A }
0N/A
0N/A static void testSerialization() throws Exception {
0N/A Currency currency1 = Currency.getInstance("DEM");
0N/A
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A ObjectOutputStream oStream = new ObjectOutputStream(baos);
0N/A oStream.writeObject(currency1);
0N/A oStream.flush();
0N/A byte[] bytes = baos.toByteArray();
0N/A
0N/A ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0N/A ObjectInputStream iStream = new ObjectInputStream(bais);
0N/A Currency currency2 = (Currency) iStream.readObject();
0N/A
0N/A if (currency1 != currency2) {
0N/A throw new RuntimeException("serialization breaks class invariant");
0N/A }
0N/A }
0N/A
0N/A static void testDisplayNames() {
0N/A // null argument test
0N/A try {
0N/A testDisplayName("USD", null, "");
0N/A throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");
0N/A } catch (NullPointerException npe) {}
0N/A
0N/A testDisplayName("USD", Locale.ENGLISH, "US Dollar");
0N/A testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");
0N/A testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");
0N/A testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");
0N/A testDisplayName("ITL", new Locale("it"), "Lira Italiana");
0N/A testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");
0N/A testDisplayName("KRW", Locale.KOREAN, "\ub300\ud55c\ubbfc\uad6d \uc6d0");
3928N/A testDisplayName("SEK", new Locale("sv"), "svensk krona");
0N/A testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");
0N/A testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u81fa\u5e63");
0N/A }
0N/A
0N/A static void testDisplayName(String currencyCode, Locale locale, String expectedName) {
0N/A String name = Currency.getInstance(currencyCode).getDisplayName(locale);
0N/A if (!name.equals(expectedName)) {
0N/A throw new RuntimeException("Wrong display name for currency " +
0N/A currencyCode +": expected '" + expectedName +
0N/A "', got '" + name + "'");
0N/A }
0N/A }
0N/A
0N/A}