jdk/test/java/util/Currency/CurrencyTest.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 37305 bd7a1dd863f8
child 42930 b878b7b2788e
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31263
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 6371531
37305
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
    26
 *    6488442 7036905 8008577 8039317 8074350 8074351 8150324
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic tests for Currency class.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37305
diff changeset
    28
 * @modules java.base/java.util:open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37305
diff changeset
    29
 *          jdk.localedata
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectOutputStream;
37305
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
    36
import java.time.LocalDate;
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
    37
import java.time.LocalTime;
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
    38
import java.time.ZoneId;
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
    39
import java.time.ZonedDateTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Currency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class CurrencyTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        CheckDataVersion.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        testCurrencyCodeValidation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        testLocaleMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        testSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        testFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        testSerialization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        testDisplayNames();
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
    54
        testFundsCodes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static void testCurrencyCodeValidation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // test creation of some valid currencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        testValidCurrency("USD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        testValidCurrency("EUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        testValidCurrency("GBP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        testValidCurrency("JPY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        testValidCurrency("CNY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        testValidCurrency("CHF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // test creation of some fictitious currencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        testInvalidCurrency("AQD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        testInvalidCurrency("US$");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        testInvalidCurrency("\u20AC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static void testValidCurrency(String currencyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Currency currency1 = Currency.getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Currency currency2 = Currency.getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (currency1 != currency2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new RuntimeException("Didn't get same instance for same currency code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (!currency1.getCurrencyCode().equals(currencyCode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new RuntimeException("Currency code changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static void testInvalidCurrency(String currencyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        boolean gotException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Currency currency = Currency.getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            gotException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (!gotException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new RuntimeException("didn't get specified exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static void testLocaleMapping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // very basic test: most countries have their own currency, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // their currency code is an extension of their country code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Locale[] locales = Locale.getAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int goodCountries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int ownCurrencies = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        for (int i = 0; i < locales.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            Locale locale = locales[i];
31263
a81a0af34ca0 8008577: Use CLDR Locale Data by Default
naoto
parents: 29524
diff changeset
   103
            String ctryCode = locale.getCountry();
a81a0af34ca0 8008577: Use CLDR Locale Data by Default
naoto
parents: 29524
diff changeset
   104
            int ctryLength = ctryCode.length();
a81a0af34ca0 8008577: Use CLDR Locale Data by Default
naoto
parents: 29524
diff changeset
   105
            if (ctryLength == 0 ||
a81a0af34ca0 8008577: Use CLDR Locale Data by Default
naoto
parents: 29524
diff changeset
   106
                ctryLength == 3 || // UN M.49 code
a81a0af34ca0 8008577: Use CLDR Locale Data by Default
naoto
parents: 29524
diff changeset
   107
                ctryCode.matches("AA|Q[M-Z]|X[A-Z]|ZZ" + // user defined codes
a81a0af34ca0 8008577: Use CLDR Locale Data by Default
naoto
parents: 29524
diff changeset
   108
                                 "AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                boolean gotException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    Currency.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    gotException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (!gotException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    throw new RuntimeException("didn't get specified exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                goodCountries++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                Currency currency = Currency.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    ownCurrencies++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println("Countries tested: " + goodCountries +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                ", own currencies: " + ownCurrencies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (ownCurrencies < (goodCountries / 2 + 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new RuntimeException("suspicious: not enough countries have their own currency.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // check a few countries that don't change their currencies too often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String[] country1 = {"US", "CA", "JP", "CN", "SG", "CH"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String[] currency1 = {"USD", "CAD", "JPY", "CNY", "SGD", "CHF"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        for (int i = 0; i < country1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            checkCountryCurrency(country1[i], currency1[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
10835
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   139
        /*
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   140
        * check currency changes
37305
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   141
        * In current implementation, there is no data of old currency and transition date at jdk/make/data/currency/CurrencyData.properties.
10835
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   142
        * So, all the switch data arrays are empty. In the future, if data of old currency and transition date are necessary for any country, the
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   143
        * arrays here can be updated so that the program can check the currency switch.
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   144
        */
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   145
        String[] switchOverCtry = {};
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   146
        String[] switchOverOld = {};
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   147
        String[] switchOverNew = {};
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   148
        String[] switchOverTZ = {};
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   149
        int[] switchOverYear = {};
37305
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   150
        int[] switchOverMonth = {}; // java.time APIs accept month starting from 1 i.e. 01 for January
10835
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   151
        int[] switchOverDay = {};
bff1afbf602a 7077119: remove past transition dates from CurrencyData.properties file
yhuang
parents: 9148
diff changeset
   152
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        for (int i = 0; i < switchOverCtry.length; i++) {
37305
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   154
            ZoneId zoneId = ZoneId.of(switchOverTZ[i]);
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   155
            ZonedDateTime zonedDateAndTime  = ZonedDateTime.of(LocalDate.of(switchOverYear[i], switchOverMonth[i], switchOverDay[i]),
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   156
                                                  LocalTime.MIDNIGHT, zoneId);
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   157
            ZonedDateTime currentZonedDateAndTime =  ZonedDateTime.now(zoneId);
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   158
            checkCountryCurrency(switchOverCtry[i], (currentZonedDateAndTime.isAfter(zonedDateAndTime) ||
bd7a1dd863f8 8150324: java/util/Currency/CurrencyTest.java does not restore default TimeZone
peytoia
parents: 36511
diff changeset
   159
                        currentZonedDateAndTime.isEqual(zonedDateAndTime)) ? switchOverNew[i] : switchOverOld[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // check a country code which doesn't have a currency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        checkCountryCurrency("AQ", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // check an invalid country code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean gotException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            Currency.getInstance(new Locale("", "EU"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            gotException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (!gotException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new RuntimeException("didn't get specified exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static void checkCountryCurrency(String countryCode, String expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Locale locale = new Locale("", countryCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        Currency currency = Currency.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        String code = (currency != null) ? currency.getCurrencyCode() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (!(expected == null ? code == null : expected.equals(code))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new RuntimeException("Wrong currency for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    locale.getDisplayCountry() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    ": expected " + expected + ", got " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static void testSymbols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        testSymbol("USD", Locale.US, "$");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        testSymbol("EUR", Locale.GERMANY, "\u20AC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        testSymbol("USD", Locale.PRC, "USD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static void testSymbol(String currencyCode, Locale locale, String expectedSymbol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        String symbol = Currency.getInstance(currencyCode).getSymbol(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (!symbol.equals(expectedSymbol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new RuntimeException("Wrong symbol for currency " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    currencyCode +": expected " + expectedSymbol +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    ", got " + symbol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static void testFractionDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        testFractionDigits("USD", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        testFractionDigits("EUR", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        testFractionDigits("JPY", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        testFractionDigits("XDR", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        testFractionDigits("BHD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        testFractionDigits("IQD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        testFractionDigits("JOD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        testFractionDigits("KWD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        testFractionDigits("LYD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        testFractionDigits("OMR", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        testFractionDigits("TND", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // Turkish Lira
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        testFractionDigits("TRL", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        testFractionDigits("TRY", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    static void testFractionDigits(String currencyCode, int expectedFractionDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int digits = Currency.getInstance(currencyCode).getDefaultFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (digits != expectedFractionDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new RuntimeException("Wrong number of fraction digits for currency " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    currencyCode +": expected " + expectedFractionDigits +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    ", got " + digits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    static void testSerialization() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Currency currency1 = Currency.getInstance("DEM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        ObjectOutputStream oStream = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        oStream.writeObject(currency1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        oStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        byte[] bytes = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        ObjectInputStream iStream = new ObjectInputStream(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        Currency currency2 = (Currency) iStream.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (currency1 != currency2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new RuntimeException("serialization breaks class invariant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    static void testDisplayNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // null argument test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            testDisplayName("USD", null, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (NullPointerException npe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        testDisplayName("USD", Locale.ENGLISH, "US Dollar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");
31263
a81a0af34ca0 8008577: Use CLDR Locale Data by Default
naoto
parents: 29524
diff changeset
   260
        testDisplayName("ITL", new Locale("it"), "lira italiana");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        testDisplayName("KRW", Locale.KOREAN, "\ub300\ud55c\ubbfc\uad6d \uc6d0");
9148
8837e7172929 7036905: [de] dem - the german mark display name is incorrect
yhuang
parents: 5506
diff changeset
   263
        testDisplayName("SEK", new Locale("sv"), "svensk krona");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u81fa\u5e63");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    static void testDisplayName(String currencyCode, Locale locale, String expectedName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        String name = Currency.getInstance(currencyCode).getDisplayName(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (!name.equals(expectedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw new RuntimeException("Wrong display name for currency " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    currencyCode +": expected '" + expectedName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    "', got '" + name + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   276
    static void testFundsCodes() {
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   277
        testValidCurrency("BOV");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   278
        testValidCurrency("CHE");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   279
        testValidCurrency("CHW");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   280
        testValidCurrency("CLF");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   281
        testValidCurrency("COU");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   282
        testValidCurrency("MXV");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   283
        testValidCurrency("USN");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   284
        testValidCurrency("UYI");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   285
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   286
        testFractionDigits("BOV", 2);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   287
        testFractionDigits("CHE", 2);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   288
        testFractionDigits("CHW", 2);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   289
        testFractionDigits("CLF", 4);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   290
        testFractionDigits("COU", 2);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   291
        testFractionDigits("MXV", 2);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   292
        testFractionDigits("USN", 2);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   293
        testFractionDigits("UYI", 0);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   294
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   295
        testNumericCode("BOV", 984);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   296
        testNumericCode("CHE", 947);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   297
        testNumericCode("CHW", 948);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   298
        testNumericCode("CLF", 990);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   299
        testNumericCode("COU", 970);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   300
        testNumericCode("MXV", 979);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   301
        testNumericCode("USN", 997);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   302
        testNumericCode("UYI", 940);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   303
    }
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   304
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   305
    static void testNumericCode(String currencyCode, int expectedNumeric) {
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   306
        int numeric = Currency.getInstance(currencyCode).getNumericCode();
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   307
        if (numeric != expectedNumeric) {
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   308
            throw new RuntimeException("Wrong numeric code for currency " +
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   309
                    currencyCode +": expected " + expectedNumeric +
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   310
                    ", got " + numeric);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   311
        }
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 25174
diff changeset
   312
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}