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