Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Currency/CurrencyTest.java
47086 views
/*1* Copyright (c) 2007, 2011, 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 8074350 807435126* @summary Basic tests for Currency class.27*/2829import java.io.ByteArrayInputStream;30import java.io.ByteArrayOutputStream;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.util.Calendar;34import java.util.Date;35import java.util.Currency;36import java.util.GregorianCalendar;37import java.util.Locale;38import java.util.TimeZone;394041public class CurrencyTest {4243public static void main(String[] args) throws Exception {44CheckDataVersion.check();45testCurrencyCodeValidation();46testLocaleMapping();47testSymbols();48testFractionDigits();49testSerialization();50testDisplayNames();51testFundsCodes();52}5354static void testCurrencyCodeValidation() {55// test creation of some valid currencies56testValidCurrency("USD");57testValidCurrency("EUR");58testValidCurrency("GBP");59testValidCurrency("JPY");60testValidCurrency("CNY");61testValidCurrency("CHF");6263// test creation of some fictitious currencies64testInvalidCurrency("AQD");65testInvalidCurrency("US$");66testInvalidCurrency("\u20AC");67}6869static void testValidCurrency(String currencyCode) {70Currency currency1 = Currency.getInstance(currencyCode);71Currency currency2 = Currency.getInstance(currencyCode);72if (currency1 != currency2) {73throw new RuntimeException("Didn't get same instance for same currency code");74}75if (!currency1.getCurrencyCode().equals(currencyCode)) {76throw new RuntimeException("Currency code changed");77}78}7980static void testInvalidCurrency(String currencyCode) {81boolean gotException = false;82try {83Currency currency = Currency.getInstance(currencyCode);84} catch (IllegalArgumentException e) {85gotException = true;86}87if (!gotException) {88throw new RuntimeException("didn't get specified exception");89}90}9192static void testLocaleMapping() {93// very basic test: most countries have their own currency, and then94// their currency code is an extension of their country code.95Locale[] locales = Locale.getAvailableLocales();96int goodCountries = 0;97int ownCurrencies = 0;98for (int i = 0; i < locales.length; i++) {99Locale locale = locales[i];100if (locale.getCountry().length() == 0) {101boolean gotException = false;102try {103Currency.getInstance(locale);104} catch (IllegalArgumentException e) {105gotException = true;106}107if (!gotException) {108throw new RuntimeException("didn't get specified exception");109}110} else {111goodCountries++;112Currency currency = Currency.getInstance(locale);113if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {114ownCurrencies++;115}116}117}118System.out.println("Countries tested: " + goodCountries +119", own currencies: " + ownCurrencies);120if (ownCurrencies < (goodCountries / 2 + 1)) {121throw new RuntimeException("suspicious: not enough countries have their own currency.");122}123124// check a few countries that don't change their currencies too often125String[] country1 = {"US", "CA", "JP", "CN", "SG", "CH"};126String[] currency1 = {"USD", "CAD", "JPY", "CNY", "SGD", "CHF"};127for (int i = 0; i < country1.length; i++) {128checkCountryCurrency(country1[i], currency1[i]);129}130131/*132* check currency changes133* In current implementation, there is no data of old currency and transition date at jdk/src/share/classes/java/util/CurrencyData.properties.134* So, all the switch data arrays are empty. In the future, if data of old currency and transition date are necessary for any country, the135* arrays here can be updated so that the program can check the currency switch.136*/137String[] switchOverCtry = {};138String[] switchOverOld = {};139String[] switchOverNew = {};140String[] switchOverTZ = {};141int[] switchOverYear = {};142int[] switchOverMonth = {};143int[] switchOverDay = {};144145for (int i = 0; i < switchOverCtry.length; i++) {146TimeZone.setDefault(TimeZone.getTimeZone(switchOverTZ[i]));147Calendar date = new GregorianCalendar(switchOverYear[i], switchOverMonth[i], switchOverDay[i]);148long switchOver = date.getTime().getTime();149boolean switchedOver = System.currentTimeMillis() >= switchOver;150checkCountryCurrency(switchOverCtry[i], switchedOver ? switchOverNew[i] : switchOverOld[i]);151}152153// check a country code which doesn't have a currency154checkCountryCurrency("AQ", null);155156// check an invalid country code157boolean gotException = false;158try {159Currency.getInstance(new Locale("", "EU"));160} catch (IllegalArgumentException e) {161gotException = true;162}163if (!gotException) {164throw new RuntimeException("didn't get specified exception.");165}166}167168static void checkCountryCurrency(String countryCode, String expected) {169Locale locale = new Locale("", countryCode);170Currency currency = Currency.getInstance(locale);171String code = (currency != null) ? currency.getCurrencyCode() : null;172if (!(expected == null ? code == null : expected.equals(code))) {173throw new RuntimeException("Wrong currency for " +174locale.getDisplayCountry() +175": expected " + expected + ", got " + code);176}177}178179static void testSymbols() {180testSymbol("USD", Locale.US, "$");181testSymbol("EUR", Locale.GERMANY, "\u20AC");182testSymbol("USD", Locale.PRC, "USD");183}184185static void testSymbol(String currencyCode, Locale locale, String expectedSymbol) {186String symbol = Currency.getInstance(currencyCode).getSymbol(locale);187if (!symbol.equals(expectedSymbol)) {188throw new RuntimeException("Wrong symbol for currency " +189currencyCode +": expected " + expectedSymbol +190", got " + symbol);191}192}193194static void testFractionDigits() {195testFractionDigits("USD", 2);196testFractionDigits("EUR", 2);197testFractionDigits("JPY", 0);198testFractionDigits("XDR", -1);199200testFractionDigits("BHD", 3);201testFractionDigits("IQD", 3);202testFractionDigits("JOD", 3);203testFractionDigits("KWD", 3);204testFractionDigits("LYD", 3);205testFractionDigits("OMR", 3);206testFractionDigits("TND", 3);207208// Turkish Lira209testFractionDigits("TRL", 0);210testFractionDigits("TRY", 2);211}212213static void testFractionDigits(String currencyCode, int expectedFractionDigits) {214int digits = Currency.getInstance(currencyCode).getDefaultFractionDigits();215if (digits != expectedFractionDigits) {216throw new RuntimeException("Wrong number of fraction digits for currency " +217currencyCode +": expected " + expectedFractionDigits +218", got " + digits);219}220}221222static void testSerialization() throws Exception {223Currency currency1 = Currency.getInstance("DEM");224225ByteArrayOutputStream baos = new ByteArrayOutputStream();226ObjectOutputStream oStream = new ObjectOutputStream(baos);227oStream.writeObject(currency1);228oStream.flush();229byte[] bytes = baos.toByteArray();230231ByteArrayInputStream bais = new ByteArrayInputStream(bytes);232ObjectInputStream iStream = new ObjectInputStream(bais);233Currency currency2 = (Currency) iStream.readObject();234235if (currency1 != currency2) {236throw new RuntimeException("serialization breaks class invariant");237}238}239240static void testDisplayNames() {241// null argument test242try {243testDisplayName("USD", null, "");244throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");245} catch (NullPointerException npe) {}246247testDisplayName("USD", Locale.ENGLISH, "US Dollar");248testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");249testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");250testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");251testDisplayName("ITL", new Locale("it"), "Lira Italiana");252testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");253testDisplayName("KRW", Locale.KOREAN, "\ub300\ud55c\ubbfc\uad6d \uc6d0");254testDisplayName("SEK", new Locale("sv"), "svensk krona");255testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");256testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u81fa\u5e63");257}258259static void testDisplayName(String currencyCode, Locale locale, String expectedName) {260String name = Currency.getInstance(currencyCode).getDisplayName(locale);261if (!name.equals(expectedName)) {262throw new RuntimeException("Wrong display name for currency " +263currencyCode +": expected '" + expectedName +264"', got '" + name + "'");265}266}267268static void testFundsCodes() {269testValidCurrency("BOV");270testValidCurrency("CHE");271testValidCurrency("CHW");272testValidCurrency("CLF");273testValidCurrency("COU");274testValidCurrency("MXV");275testValidCurrency("USN");276testValidCurrency("UYI");277278testFractionDigits("BOV", 2);279testFractionDigits("CHE", 2);280testFractionDigits("CHW", 2);281testFractionDigits("CLF", 4);282testFractionDigits("COU", 2);283testFractionDigits("MXV", 2);284testFractionDigits("USN", 2);285testFractionDigits("UYI", 0);286287testNumericCode("BOV", 984);288testNumericCode("CHE", 947);289testNumericCode("CHW", 948);290testNumericCode("CLF", 990);291testNumericCode("COU", 970);292testNumericCode("MXV", 979);293testNumericCode("USN", 997);294testNumericCode("UYI", 940);295}296297static void testNumericCode(String currencyCode, int expectedNumeric) {298int numeric = Currency.getInstance(currencyCode).getNumericCode();299if (numeric != expectedNumeric) {300throw new RuntimeException("Wrong numeric code for currency " +301currencyCode +": expected " + expectedNumeric +302", got " + numeric);303}304}305}306307308