jdk/test/java/util/Currency/CurrencyTest.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 9148 8837e7172929
child 10835 bff1afbf602a
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, 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
9148
8837e7172929 7036905: [de] dem - the german mark display name is incorrect
yhuang
parents: 5506
diff changeset
    26
 *    6488442 7036905
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic tests for Currency class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Currency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.GregorianCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class CurrencyTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        CheckDataVersion.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        testCurrencyCodeValidation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        testLocaleMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        testSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        testFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        testSerialization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        testDisplayNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void testCurrencyCodeValidation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // test creation of some valid currencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        testValidCurrency("USD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        testValidCurrency("EUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        testValidCurrency("GBP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        testValidCurrency("JPY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        testValidCurrency("CNY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        testValidCurrency("CHF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // test creation of some fictitious currencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        testInvalidCurrency("AQD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        testInvalidCurrency("US$");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        testInvalidCurrency("\u20AC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static void testValidCurrency(String currencyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Currency currency1 = Currency.getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Currency currency2 = Currency.getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (currency1 != currency2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new RuntimeException("Didn't get same instance for same currency code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (!currency1.getCurrencyCode().equals(currencyCode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new RuntimeException("Currency code changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static void testInvalidCurrency(String currencyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        boolean gotException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Currency currency = Currency.getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            gotException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (!gotException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException("didn't get specified exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static void testLocaleMapping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // very basic test: most countries have their own currency, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // their currency code is an extension of their country code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Locale[] locales = Locale.getAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int goodCountries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int ownCurrencies = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        for (int i = 0; i < locales.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Locale locale = locales[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (locale.getCountry().length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                boolean gotException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    Currency.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    gotException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (!gotException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    throw new RuntimeException("didn't get specified exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                goodCountries++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                Currency currency = Currency.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    ownCurrencies++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.out.println("Countries tested: " + goodCountries +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                ", own currencies: " + ownCurrencies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (ownCurrencies < (goodCountries / 2 + 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new RuntimeException("suspicious: not enough countries have their own currency.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // check a few countries that don't change their currencies too often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String[] country1 = {"US", "CA", "JP", "CN", "SG", "CH"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String[] currency1 = {"USD", "CAD", "JPY", "CNY", "SGD", "CHF"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (int i = 0; i < country1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            checkCountryCurrency(country1[i], currency1[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // check currency changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String[] switchOverCtry = {"DE", "FR", "ES", "IT", "NL", "BE", "TR", "RO", "AZ", "MZ", "GH", "VE"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String[] switchOverOld = {"DEM", "FRF", "ESP", "ITL", "NLG", "BEF", "TRL", "ROL", "AZM", "MZM", "GHC", "VEB"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String[] switchOverNew = {"EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "TRY", "RON", "AZN", "MZN", "GHS", "VEF"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        String[] switchOverTZ = {"Europe/Paris", "Europe/Paris", "Europe/Paris", "Europe/Paris",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                 "Europe/Paris", "Europe/Paris", "Asia/Istanbul", "Europe/Bucharest",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                 "Asia/Baku", "Africa/Maputo", "Africa/Accra", "America/Caracas"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int[] switchOverYear = {2002, 2002, 2002, 2002, 2002, 2002, 2005, 2005, 2006, 2006, 2007, 2008};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int[] switchOverMonth = {Calendar.JANUARY, Calendar.JANUARY, Calendar.JANUARY, Calendar.JANUARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                 Calendar.JANUARY, Calendar.JANUARY, Calendar.JANUARY, Calendar.JULY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                 Calendar.JANUARY, Calendar.JULY, Calendar.JULY, Calendar.JANUARY};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int[] switchOverDay = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        for (int i = 0; i < switchOverCtry.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            TimeZone.setDefault(TimeZone.getTimeZone(switchOverTZ[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Calendar date = new GregorianCalendar(switchOverYear[i], switchOverMonth[i], switchOverDay[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            long switchOver = date.getTime().getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            boolean switchedOver = System.currentTimeMillis() >= switchOver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            checkCountryCurrency(switchOverCtry[i], switchedOver ? switchOverNew[i] : switchOverOld[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // check a country code which doesn't have a currency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        checkCountryCurrency("AQ", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // check an invalid country code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        boolean gotException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            Currency.getInstance(new Locale("", "EU"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            gotException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (!gotException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new RuntimeException("didn't get specified exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static void checkCountryCurrency(String countryCode, String expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        Locale locale = new Locale("", countryCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Currency currency = Currency.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        String code = (currency != null) ? currency.getCurrencyCode() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (!(expected == null ? code == null : expected.equals(code))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new RuntimeException("Wrong currency for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    locale.getDisplayCountry() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    ": expected " + expected + ", got " + code);
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 testSymbols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        testSymbol("USD", Locale.US, "$");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        testSymbol("EUR", Locale.GERMANY, "\u20AC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        testSymbol("USD", Locale.PRC, "USD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static void testSymbol(String currencyCode, Locale locale, String expectedSymbol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        String symbol = Currency.getInstance(currencyCode).getSymbol(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!symbol.equals(expectedSymbol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new RuntimeException("Wrong symbol for currency " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    currencyCode +": expected " + expectedSymbol +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    ", got " + symbol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static void testFractionDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        testFractionDigits("USD", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        testFractionDigits("EUR", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        testFractionDigits("JPY", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        testFractionDigits("XDR", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        testFractionDigits("BHD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        testFractionDigits("IQD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        testFractionDigits("JOD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        testFractionDigits("KWD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        testFractionDigits("LYD", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        testFractionDigits("OMR", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        testFractionDigits("TND", 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // Turkish Lira
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        testFractionDigits("TRL", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        testFractionDigits("TRY", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static void testFractionDigits(String currencyCode, int expectedFractionDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int digits = Currency.getInstance(currencyCode).getDefaultFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (digits != expectedFractionDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new RuntimeException("Wrong number of fraction digits for currency " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    currencyCode +": expected " + expectedFractionDigits +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    ", got " + digits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    static void testSerialization() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Currency currency1 = Currency.getInstance("DEM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        ObjectOutputStream oStream = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        oStream.writeObject(currency1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        oStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        byte[] bytes = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        ObjectInputStream iStream = new ObjectInputStream(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Currency currency2 = (Currency) iStream.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (currency1 != currency2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new RuntimeException("serialization breaks class invariant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    static void testDisplayNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // null argument test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            testDisplayName("USD", null, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } catch (NullPointerException npe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        testDisplayName("USD", Locale.ENGLISH, "US Dollar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        testDisplayName("ITL", new Locale("it"), "Lira Italiana");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        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
   252
        testDisplayName("SEK", new Locale("sv"), "svensk krona");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u81fa\u5e63");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static void testDisplayName(String currencyCode, Locale locale, String expectedName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        String name = Currency.getInstance(currencyCode).getDisplayName(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (!name.equals(expectedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new RuntimeException("Wrong display name for currency " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    currencyCode +": expected '" + expectedName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    "', got '" + name + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}