jdk/test/java/util/Currency/ValidateISO4217.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8543 e5ec12a932da
child 10293 cdcbdd4c20b8
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8543
diff changeset
     2
 * Copyright (c) 2007, 2011, 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: 1848
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1848
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1848
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
1848
6ecbe9158c6e 6627549: ISO 3166 code addition: Saint Barthelemy and Saint Martin
naoto
parents: 892
diff changeset
    25
 * @bug 4691089 4819436 4942982 5104960 6544471 6627549
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Validate ISO 4217 data for Currency class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * ############################################################################
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  ValidateISO4217 is a tool to detect differences between the latest ISO 4217
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  data and and Java's currency data which is based on ISO 4217.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  If there is a difference, the following file which includes currency data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *  may need to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *      src/share/classes/java/util/CurrencyData.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * ############################################################################
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * 1) Make a golden-data file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *      From BSi's ISO4217 data (TABLE A1.doc), extract four (or eight, if currency is changing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      fields and save as ./tablea1.txt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *        <Country code>\t<Currency code>\t<Numeric code>\t<Minor unit>[\t<Cutover Date>\t<new Currency code>\t<new Numeric code>\t<new Minor unit>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      The Cutover Date is given in SimpleDateFormat's 'yyyy-MM-dd-HH-mm-ss' format in the GMT time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 2) Compile ValidateISO4217.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * 3) Execute ValidateISO4217 as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      java ValidateISO4217
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class ValidateISO4217 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final int ALPHA_NUM = 26;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final byte UNDEFINED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final byte DEFINED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final byte SKIPPED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* input files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final String datafile = "tablea1.txt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* alpha2-code table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static byte[] codes = new byte[ALPHA_NUM * ALPHA_NUM];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final String[][] additionalCodes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /* Defined in ISO 4217 list, but don't have code and minor unit info. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        {"AQ", "", "", "0"},    // Antarctica
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * Defined in ISO 4217 list, but don't have code and minor unit info in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * it. On the othe hand, both code and minor unit are defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * .properties file. I don't know why, though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        {"GS", "GBP", "826", "2"},      // South Georgia And The South Sandwich Islands
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* Not defined in ISO 4217 list, but defined in .properties file. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        {"AX", "EUR", "978", "2"},      // \u00c5LAND ISLANDS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        {"PS", "ILS", "376", "2"},      // Palestinian Territory, Occupied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /* Not defined in ISO 4217 list, but added in ISO 3166 country code list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        {"JE", "GBP", "826", "2"},      // Jersey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        {"GG", "GBP", "826", "2"},      // Guernsey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        {"IM", "GBP", "826", "2"},      // Isle of Man
1848
6ecbe9158c6e 6627549: ISO 3166 code addition: Saint Barthelemy and Saint Martin
naoto
parents: 892
diff changeset
    89
        {"BL", "EUR", "978", "2"},      // Saint Barthelemy
6ecbe9158c6e 6627549: ISO 3166 code addition: Saint Barthelemy and Saint Martin
naoto
parents: 892
diff changeset
    90
        {"MF", "EUR", "978", "2"},      // Saint Martin
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* Codes that are obsolete, do not have related country */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static final String otherCodes =
892
ff14a2ead53d 6608572: Currency change for Malta and Cyprus
jtusla
parents: 2
diff changeset
    95
        "ADP-AFA-ATS-AYM-BEF-BGL-BOV-BYB-CLF-CYP-DEM-ESP-FIM-FRF-GRD-GWP-IEP-ITL-LUF-MGF-MTL-MXV-NLG-PTE-RUR-SDD-SIT-SRG-TPE-TRL-VEF-USN-USS-XAG-XAU-XBA-XBB-XBC-XBD-XDR-XFO-XFU-XPD-XPT-XTS-XXX-YUM-ZWN";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static boolean err = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static Set<Currency> testCurrencies = new HashSet<Currency>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        CheckDataVersion.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        test1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        test2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        getAvailableCurrenciesTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException("Failed: Validation ISO 4217 data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static void test1() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   114
        try (FileReader fr = new FileReader(new File(System.getProperty("test.src", "."), datafile));
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   115
             BufferedReader in = new BufferedReader(fr))
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   116
        {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   117
            String line;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   118
            SimpleDateFormat format = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   120
            while ((line = in.readLine()) != null) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   121
                if (line.length() == 0 || line.charAt(0) == '#') {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   122
                    continue;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   123
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   125
                StringTokenizer tokens = new StringTokenizer(line, "\t");
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   126
                String country = tokens.nextToken();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   127
                if (country.length() != 2) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   128
                    continue;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   129
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   131
                String currency;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   132
                String numeric;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   133
                String minorUnit;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   134
                int tokensCount = tokens.countTokens();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   135
                if (tokensCount < 3) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   136
                    currency = "";
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   137
                    numeric = "0";
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   138
                    minorUnit = "0";
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   139
                } else {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   140
                    currency = tokens.nextToken();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   141
                    numeric = tokens.nextToken();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   142
                    minorUnit = tokens.nextToken();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   143
                    testCurrencies.add(Currency.getInstance(currency));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   145
                    // check for the cutover
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   146
                    if (tokensCount > 3) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   147
                        if (format == null) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   148
                            format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   149
                            format.setTimeZone(TimeZone.getTimeZone("GMT"));
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   150
                            format.setLenient(false);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   151
                        }
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   152
                        if (format.parse(tokens.nextToken()).getTime() <
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   153
                            System.currentTimeMillis()) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   154
                            currency = tokens.nextToken();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   155
                            numeric = tokens.nextToken();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   156
                            minorUnit = tokens.nextToken();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   157
                            testCurrencies.add(Currency.getInstance(currency));
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   158
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   161
                int index = toIndex(country);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   162
                testCountryCurrency(country, currency, Integer.parseInt(numeric),
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   163
                    Integer.parseInt(minorUnit), index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        for (int i = 0; i < additionalCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int index = toIndex(additionalCodes[i][0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (additionalCodes[i][1].length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                testCountryCurrency(additionalCodes[i][0], additionalCodes[i][1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    Integer.parseInt(additionalCodes[i][2]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    Integer.parseInt(additionalCodes[i][3]), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                testCurrencies.add(Currency.getInstance(additionalCodes[i][1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                codes[index] = SKIPPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    static int toIndex(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return ((s.charAt(0) - 'A') * ALPHA_NUM + s.charAt(1) - 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    static void testCountryCurrency(String country, String currencyCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                int numericCode, int digits, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (currencyCode.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        testCurrencyDefined(currencyCode, numericCode, digits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Locale loc = new Locale("", country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            Currency currency = Currency.getInstance(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (!currency.getCurrencyCode().equals(currencyCode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                System.err.println("Error: [" + country + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    loc.getDisplayCountry() + "] expected: " + currencyCode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    ", got: " + currency.getCurrencyCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (codes[index] != UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                System.out.println("Warning: [" + country + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    loc.getDisplayCountry() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    "] multiple definitions. currency code=" + currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            codes[index] = DEFINED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            System.err.println("Error: " + e + ": Country=" + country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    static void testCurrencyDefined(String currencyCode, int numericCode, int digits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            Currency currency = currency = Currency.getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (currency.getNumericCode() != numericCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                System.err.println("Error: [" + currencyCode + "] expected: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    numericCode + "; got: " + currency.getNumericCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (currency.getDefaultFractionDigits() != digits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                System.err.println("Error: [" + currencyCode + "] expected: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    digits + "; got: " + currency.getDefaultFractionDigits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            System.err.println("Error: " + e + ": Currency code=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    static void test2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        for (int i = 0; i < ALPHA_NUM; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            for (int j = 0; j < ALPHA_NUM; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                char[] code = new char[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                code[0] = (char)('A'+ i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                code[1] = (char)('A'+ j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                String country = new String(code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                boolean ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                if (codes[toIndex(country)] == UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    ex = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        Currency.getInstance(new Locale("", country));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        ex = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if (!ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        System.err.println("Error: This should be an undefined code and throw IllegalArgumentException: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                } else if (codes[toIndex(country)] == SKIPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    Currency cur = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        cur = Currency.getInstance(new Locale("", country));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        System.err.println("Error: " + e + ": Country=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                            country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    if (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        System.err.println("Error: Currency.getInstance() for an this locale should return null: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                            country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * This test depends on test1(), where 'testCurrencies' set is constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static void getAvailableCurrenciesTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        Set<Currency> jreCurrencies = Currency.getAvailableCurrencies();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // add otherCodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        StringTokenizer st = new StringTokenizer(otherCodes, "-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            testCurrencies.add(Currency.getInstance(st.nextToken()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (!testCurrencies.containsAll(jreCurrencies)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            System.err.print("Error: getAvailableCurrencies() returned extra currencies than expected: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            jreCurrencies.removeAll(testCurrencies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            for (Currency c : jreCurrencies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                System.err.print(" "+c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
}