1 /*
   2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 4691089 4819436 4942982 5104960 6544471 6627549 7066203 7195759
  26  *      8039317 8074350 8074351 8145952
  27  * @summary Validate ISO 4217 data for Currency class.
  28  * @modules java.base/java.util:open
  29  *          jdk.localedata
  30  */
  31 
  32 /*
  33  * ############################################################################
  34  *
  35  *  ValidateISO4217 is a tool to detect differences between the latest ISO 4217
  36  *  data and and Java's currency data which is based on ISO 4217.
  37  *  If there is a difference, the following file which includes currency data
  38  *  may need to be updated.
  39  *      src/share/classes/java/util/CurrencyData.properties
  40  *
  41  * ############################################################################
  42  *
  43  * 1) Make a golden-data file.
  44  *      From BSi's ISO4217 data (TABLE A1.doc), extract four (or eight, if currency is changing)
  45  *      fields and save as ./tablea1.txt.
  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>]
  47  *      The Cutover Date is given in SimpleDateFormat's 'yyyy-MM-dd-HH-mm-ss' format in the GMT time zone.
  48  *
  49  * 2) Compile ValidateISO4217.java
  50  *
  51  * 3) Execute ValidateISO4217 as follows:
  52  *      java ValidateISO4217
  53  */
  54 
  55 import java.io.*;
  56 import java.text.*;
  57 import java.util.*;
  58 
  59 public class ValidateISO4217 {
  60 
  61     static final int ALPHA_NUM = 26;
  62 
  63     static final byte UNDEFINED = 0;
  64     static final byte DEFINED = 1;
  65     static final byte SKIPPED = 2;
  66 
  67     /* input files */
  68     static final String datafile = "tablea1.txt";
  69 
  70     /* alpha2-code table */
  71     static byte[] codes = new byte[ALPHA_NUM * ALPHA_NUM];
  72 
  73     static final String[][] additionalCodes = {
  74         /* Defined in ISO 4217 list, but don't have code and minor unit info. */
  75         {"AQ", "", "", "0"},    // Antarctica
  76 
  77         /*
  78          * Defined in ISO 4217 list, but don't have code and minor unit info in
  79          * it. On the othe hand, both code and minor unit are defined in
  80          * .properties file. I don't know why, though.
  81          */
  82         {"GS", "GBP", "826", "2"},      // South Georgia And The South Sandwich Islands
  83 
  84         /* Not defined in ISO 4217 list, but defined in .properties file. */
  85         {"AX", "EUR", "978", "2"},      // \u00c5LAND ISLANDS
  86         {"PS", "ILS", "376", "2"},      // Palestinian Territory, Occupied
  87 
  88         /* Not defined in ISO 4217 list, but added in ISO 3166 country code list */
  89         {"JE", "GBP", "826", "2"},      // Jersey
  90         {"GG", "GBP", "826", "2"},      // Guernsey
  91         {"IM", "GBP", "826", "2"},      // Isle of Man
  92         {"BL", "EUR", "978", "2"},      // Saint Barthelemy
  93         {"MF", "EUR", "978", "2"},      // Saint Martin
  94     };
  95 
  96     /* Codes that are obsolete, do not have related country */
  97     static final String otherCodes =
  98         "ADP-AFA-ATS-AYM-AZM-BEF-BGL-BOV-BYB-BYR-CHE-CHW-CLF-COU-CUC-CYP-DEM-EEK-ESP-FIM-FRF-GHC-GRD-GWP-IEP-ITL-LUF-MGF-MTL-MXV-MZM-NLG-PTE-ROL-RUR-SDD-SIT-SKK-SRG-TMM-TPE-TRL-VEF-UYI-USN-USS-VEB-XAG-XAU-XBA-XBB-XBC-XBD-XDR-XFO-XFU-XPD-XPT-XSU-XTS-XUA-XXX-YUM-ZMK-ZWD-ZWN-ZWR";
  99 
 100     static boolean err = false;
 101 
 102     static Set<Currency> testCurrencies = new HashSet<Currency>();
 103 
 104     public static void main(String[] args) throws Exception {
 105         CheckDataVersion.check();
 106         test1();
 107         test2();
 108         getAvailableCurrenciesTest();
 109 
 110         if (err) {
 111             throw new RuntimeException("Failed: Validation ISO 4217 data");
 112         }
 113     }
 114 
 115     static void test1() throws Exception {
 116 
 117         try (FileReader fr = new FileReader(new File(System.getProperty("test.src", "."), datafile));
 118              BufferedReader in = new BufferedReader(fr))
 119         {
 120             String line;
 121             SimpleDateFormat format = null;
 122 
 123             while ((line = in.readLine()) != null) {
 124                 if (line.length() == 0 || line.charAt(0) == '#') {
 125                     continue;
 126                 }
 127 
 128                 StringTokenizer tokens = new StringTokenizer(line, "\t");
 129                 String country = tokens.nextToken();
 130                 if (country.length() != 2) {
 131                     continue;
 132                 }
 133 
 134                 String currency;
 135                 String numeric;
 136                 String minorUnit;
 137                 int tokensCount = tokens.countTokens();
 138                 if (tokensCount < 3) {
 139                     currency = "";
 140                     numeric = "0";
 141                     minorUnit = "0";
 142                 } else {
 143                     currency = tokens.nextToken();
 144                     numeric = tokens.nextToken();
 145                     minorUnit = tokens.nextToken();
 146                     testCurrencies.add(Currency.getInstance(currency));
 147 
 148                     // check for the cutover
 149                     if (tokensCount > 3) {
 150                         if (format == null) {
 151                             format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
 152                             format.setTimeZone(TimeZone.getTimeZone("GMT"));
 153                             format.setLenient(false);
 154                         }
 155                         if (format.parse(tokens.nextToken()).getTime() <
 156                             System.currentTimeMillis()) {
 157                             currency = tokens.nextToken();
 158                             numeric = tokens.nextToken();
 159                             minorUnit = tokens.nextToken();
 160                             testCurrencies.add(Currency.getInstance(currency));
 161                         }
 162                     }
 163                 }
 164                 int index = toIndex(country);
 165                 testCountryCurrency(country, currency, Integer.parseInt(numeric),
 166                     Integer.parseInt(minorUnit), index);
 167             }
 168         }
 169 
 170         for (int i = 0; i < additionalCodes.length; i++) {
 171             int index = toIndex(additionalCodes[i][0]);
 172             if (additionalCodes[i][1].length() != 0) {
 173                 testCountryCurrency(additionalCodes[i][0], additionalCodes[i][1],
 174                     Integer.parseInt(additionalCodes[i][2]),
 175                     Integer.parseInt(additionalCodes[i][3]), index);
 176                 testCurrencies.add(Currency.getInstance(additionalCodes[i][1]));
 177             } else {
 178                 codes[index] = SKIPPED;
 179             }
 180         }
 181     }
 182 
 183     static int toIndex(String s) {
 184         return ((s.charAt(0) - 'A') * ALPHA_NUM + s.charAt(1) - 'A');
 185     }
 186 
 187     static void testCountryCurrency(String country, String currencyCode,
 188                                 int numericCode, int digits, int index) {
 189         if (currencyCode.length() == 0) {
 190             return;
 191         }
 192         testCurrencyDefined(currencyCode, numericCode, digits);
 193 
 194         Locale loc = new Locale("", country);
 195         try {
 196             Currency currency = Currency.getInstance(loc);
 197             if (!currency.getCurrencyCode().equals(currencyCode)) {
 198                 System.err.println("Error: [" + country + ":" +
 199                     loc.getDisplayCountry() + "] expected: " + currencyCode +
 200                     ", got: " + currency.getCurrencyCode());
 201                 err = true;
 202             }
 203 
 204             if (codes[index] != UNDEFINED) {
 205                 System.out.println("Warning: [" + country + ":" +
 206                     loc.getDisplayCountry() +
 207                     "] multiple definitions. currency code=" + currencyCode);
 208             }
 209             codes[index] = DEFINED;
 210         }
 211         catch (Exception e) {
 212             System.err.println("Error: " + e + ": Country=" + country);
 213             err = true;
 214         }
 215     }
 216 
 217     static void testCurrencyDefined(String currencyCode, int numericCode, int digits) {
 218         try {
 219             Currency currency = currency = Currency.getInstance(currencyCode);
 220 
 221             if (currency.getNumericCode() != numericCode) {
 222                 System.err.println("Error: [" + currencyCode + "] expected: " +
 223                     numericCode + "; got: " + currency.getNumericCode());
 224                 err = true;
 225             }
 226 
 227             if (currency.getDefaultFractionDigits() != digits) {
 228                 System.err.println("Error: [" + currencyCode + "] expected: " +
 229                     digits + "; got: " + currency.getDefaultFractionDigits());
 230                 err = true;
 231             }
 232         }
 233         catch (Exception e) {
 234             System.err.println("Error: " + e + ": Currency code=" +
 235                 currencyCode);
 236             err = true;
 237         }
 238     }
 239 
 240     static void test2() {
 241         for (int i = 0; i < ALPHA_NUM; i++) {
 242             for (int j = 0; j < ALPHA_NUM; j++) {
 243                 char[] code = new char[2];
 244                 code[0] = (char)('A'+ i);
 245                 code[1] = (char)('A'+ j);
 246                 String country = new String(code);
 247                 boolean ex;
 248 
 249                 if (codes[toIndex(country)] == UNDEFINED) {
 250                     ex = false;
 251                     try {
 252                         Currency.getInstance(new Locale("", country));
 253                     }
 254                     catch (IllegalArgumentException e) {
 255                         ex = true;
 256                     }
 257                     if (!ex) {
 258                         System.err.println("Error: This should be an undefined code and throw IllegalArgumentException: " +
 259                             country);
 260                         err = true;
 261                     }
 262                 } else if (codes[toIndex(country)] == SKIPPED) {
 263                     Currency cur = null;
 264                     try {
 265                         cur = Currency.getInstance(new Locale("", country));
 266                     }
 267                     catch (Exception e) {
 268                         System.err.println("Error: " + e + ": Country=" +
 269                             country);
 270                         err = true;
 271                     }
 272                     if (cur != null) {
 273                         System.err.println("Error: Currency.getInstance() for an this locale should return null: " +
 274                             country);
 275                         err = true;
 276                     }
 277                 }
 278             }
 279         }
 280     }
 281 
 282     /**
 283      * This test depends on test1(), where 'testCurrencies' set is constructed
 284      */
 285     static void getAvailableCurrenciesTest() {
 286         Set<Currency> jreCurrencies = Currency.getAvailableCurrencies();
 287 
 288         // add otherCodes
 289         StringTokenizer st = new StringTokenizer(otherCodes, "-");
 290         while (st.hasMoreTokens()) {
 291             testCurrencies.add(Currency.getInstance(st.nextToken()));
 292         }
 293 
 294         if (!testCurrencies.containsAll(jreCurrencies)) {
 295             System.err.print("Error: getAvailableCurrencies() returned extra currencies than expected: ");
 296             jreCurrencies.removeAll(testCurrencies);
 297             for (Currency c : jreCurrencies) {
 298                 System.err.print(" "+c);
 299             }
 300             System.err.println();
 301             err = true;
 302         }
 303     }
 304 }