Path: blob/master/test/jdk/java/util/Currency/CurrencyTest.java
41149 views
/*1* Copyright (c) 2007, 2016, 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*/22/*23* @test24* @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 637153125* 6488442 7036905 8008577 8039317 8074350 8074351 8150324 816714326* @summary Basic tests for Currency class.27* @modules java.base/java.util:open28* jdk.localedata29*/3031import java.io.ByteArrayInputStream;32import java.io.ByteArrayOutputStream;33import java.io.ObjectInputStream;34import java.io.ObjectOutputStream;35import java.time.LocalDate;36import java.time.LocalTime;37import java.time.ZoneId;38import java.time.ZonedDateTime;39import java.util.Currency;40import java.util.Locale;414243public class CurrencyTest {4445public static void main(String[] args) throws Exception {46CheckDataVersion.check();47testCurrencyCodeValidation();48testLocaleMapping();49testSymbols();50testFractionDigits();51testSerialization();52testDisplayNames();53testFundsCodes();54}5556static void testCurrencyCodeValidation() {57// test creation of some valid currencies58testValidCurrency("USD");59testValidCurrency("EUR");60testValidCurrency("GBP");61testValidCurrency("JPY");62testValidCurrency("CNY");63testValidCurrency("CHF");6465// test creation of some fictitious currencies66testInvalidCurrency("AQD");67testInvalidCurrency("US$");68testInvalidCurrency("\u20AC");69}7071static void testValidCurrency(String currencyCode) {72Currency currency1 = Currency.getInstance(currencyCode);73Currency currency2 = Currency.getInstance(currencyCode);74if (currency1 != currency2) {75throw new RuntimeException("Didn't get same instance for same currency code");76}77if (!currency1.getCurrencyCode().equals(currencyCode)) {78throw new RuntimeException("Currency code changed");79}80}8182static void testInvalidCurrency(String currencyCode) {83boolean gotException = false;84try {85Currency currency = Currency.getInstance(currencyCode);86} catch (IllegalArgumentException e) {87gotException = true;88}89if (!gotException) {90throw new RuntimeException("didn't get specified exception");91}92}9394static void testLocaleMapping() {95// very basic test: most countries have their own currency, and then96// their currency code is an extension of their country code.97Locale[] locales = Locale.getAvailableLocales();98int goodCountries = 0;99int ownCurrencies = 0;100for (int i = 0; i < locales.length; i++) {101Locale locale = locales[i];102String ctryCode = locale.getCountry();103int ctryLength = ctryCode.length();104if (ctryLength == 0 ||105ctryLength == 3 || // UN M.49 code106ctryCode.matches("AA|Q[M-Z]|X[A-Z]|ZZ" + // user defined codes107"AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes108boolean gotException = false;109try {110Currency.getInstance(locale);111} catch (IllegalArgumentException e) {112gotException = true;113}114if (!gotException) {115throw new RuntimeException("didn't get specified exception");116}117} else {118goodCountries++;119Currency currency = Currency.getInstance(locale);120if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {121ownCurrencies++;122}123}124}125System.out.println("Countries tested: " + goodCountries +126", own currencies: " + ownCurrencies);127if (ownCurrencies < (goodCountries / 2 + 1)) {128throw new RuntimeException("suspicious: not enough countries have their own currency.");129}130131// check a few countries that don't change their currencies too often132String[] country1 = {"US", "CA", "JP", "CN", "SG", "CH"};133String[] currency1 = {"USD", "CAD", "JPY", "CNY", "SGD", "CHF"};134for (int i = 0; i < country1.length; i++) {135checkCountryCurrency(country1[i], currency1[i]);136}137138/*139* check currency changes140* In current implementation, there is no data of old currency and transition date at jdk/make/data/currency/CurrencyData.properties.141* So, all the switch data arrays are empty. In the future, if data of old currency and transition date are necessary for any country, the142* arrays here can be updated so that the program can check the currency switch.143*/144String[] switchOverCtry = {};145String[] switchOverOld = {};146String[] switchOverNew = {};147String[] switchOverTZ = {};148int[] switchOverYear = {};149int[] switchOverMonth = {}; // java.time APIs accept month starting from 1 i.e. 01 for January150int[] switchOverDay = {};151152for (int i = 0; i < switchOverCtry.length; i++) {153ZoneId zoneId = ZoneId.of(switchOverTZ[i]);154ZonedDateTime zonedDateAndTime = ZonedDateTime.of(LocalDate.of(switchOverYear[i], switchOverMonth[i], switchOverDay[i]),155LocalTime.MIDNIGHT, zoneId);156ZonedDateTime currentZonedDateAndTime = ZonedDateTime.now(zoneId);157checkCountryCurrency(switchOverCtry[i], (currentZonedDateAndTime.isAfter(zonedDateAndTime) ||158currentZonedDateAndTime.isEqual(zonedDateAndTime)) ? switchOverNew[i] : switchOverOld[i]);159}160161// check a country code which doesn't have a currency162checkCountryCurrency("AQ", null);163164// check an invalid country code165boolean gotException = false;166try {167Currency.getInstance(new Locale("", "EU"));168} catch (IllegalArgumentException e) {169gotException = true;170}171if (!gotException) {172throw new RuntimeException("didn't get specified exception.");173}174}175176static void checkCountryCurrency(String countryCode, String expected) {177Locale locale = new Locale("", countryCode);178Currency currency = Currency.getInstance(locale);179String code = (currency != null) ? currency.getCurrencyCode() : null;180if (!(expected == null ? code == null : expected.equals(code))) {181throw new RuntimeException("Wrong currency for " +182locale.getDisplayCountry() +183": expected " + expected + ", got " + code);184}185}186187static void testSymbols() {188testSymbol("USD", Locale.US, "$");189testSymbol("EUR", Locale.GERMANY, "\u20AC");190testSymbol("USD", Locale.PRC, "US$");191}192193static void testSymbol(String currencyCode, Locale locale, String expectedSymbol) {194String symbol = Currency.getInstance(currencyCode).getSymbol(locale);195if (!symbol.equals(expectedSymbol)) {196throw new RuntimeException("Wrong symbol for currency " +197currencyCode +": expected " + expectedSymbol +198", got " + symbol);199}200}201202static void testFractionDigits() {203testFractionDigits("USD", 2);204testFractionDigits("EUR", 2);205testFractionDigits("JPY", 0);206testFractionDigits("XDR", -1);207208testFractionDigits("BHD", 3);209testFractionDigits("IQD", 3);210testFractionDigits("JOD", 3);211testFractionDigits("KWD", 3);212testFractionDigits("LYD", 3);213testFractionDigits("OMR", 3);214testFractionDigits("TND", 3);215216// Turkish Lira217testFractionDigits("TRL", 0);218testFractionDigits("TRY", 2);219}220221static void testFractionDigits(String currencyCode, int expectedFractionDigits) {222int digits = Currency.getInstance(currencyCode).getDefaultFractionDigits();223if (digits != expectedFractionDigits) {224throw new RuntimeException("Wrong number of fraction digits for currency " +225currencyCode +": expected " + expectedFractionDigits +226", got " + digits);227}228}229230static void testSerialization() throws Exception {231Currency currency1 = Currency.getInstance("DEM");232233ByteArrayOutputStream baos = new ByteArrayOutputStream();234ObjectOutputStream oStream = new ObjectOutputStream(baos);235oStream.writeObject(currency1);236oStream.flush();237byte[] bytes = baos.toByteArray();238239ByteArrayInputStream bais = new ByteArrayInputStream(bytes);240ObjectInputStream iStream = new ObjectInputStream(bais);241Currency currency2 = (Currency) iStream.readObject();242243if (currency1 != currency2) {244throw new RuntimeException("serialization breaks class invariant");245}246}247248static void testDisplayNames() {249// null argument test250try {251testDisplayName("USD", null, "");252throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");253} catch (NullPointerException npe) {}254255testDisplayName("USD", Locale.ENGLISH, "US Dollar");256testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");257testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");258testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");259testDisplayName("ITL", new Locale("it"), "lira italiana");260testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");261testDisplayName("KRW", Locale.KOREAN, "\ub300\ud55c\ubbfc\uad6d \uc6d0");262testDisplayName("SEK", new Locale("sv"), "svensk krona");263testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");264testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u53f0\u5e63");265}266267static void testDisplayName(String currencyCode, Locale locale, String expectedName) {268String name = Currency.getInstance(currencyCode).getDisplayName(locale);269if (!name.equals(expectedName)) {270throw new RuntimeException("Wrong display name for currency " +271currencyCode +": expected '" + expectedName +272"', got '" + name + "'");273}274}275static void testFundsCodes() {276testValidCurrency("BOV");277testValidCurrency("CHE");278testValidCurrency("CHW");279testValidCurrency("CLF");280testValidCurrency("COU");281testValidCurrency("MXV");282testValidCurrency("USN");283testValidCurrency("UYI");284285testFractionDigits("BOV", 2);286testFractionDigits("CHE", 2);287testFractionDigits("CHW", 2);288testFractionDigits("CLF", 4);289testFractionDigits("COU", 2);290testFractionDigits("MXV", 2);291testFractionDigits("USN", 2);292testFractionDigits("UYI", 0);293294testNumericCode("BOV", 984);295testNumericCode("CHE", 947);296testNumericCode("CHW", 948);297testNumericCode("CLF", 990);298testNumericCode("COU", 970);299testNumericCode("MXV", 979);300testNumericCode("USN", 997);301testNumericCode("UYI", 940);302}303304static void testNumericCode(String currencyCode, int expectedNumeric) {305int numeric = Currency.getInstance(currencyCode).getNumericCode();306if (numeric != expectedNumeric) {307throw new RuntimeException("Wrong numeric code for currency " +308currencyCode +": expected " + expectedNumeric +309", got " + numeric);310}311}312}313314315