Path: blob/master/test/jdk/java/util/Currency/ValidateISO4217.java
41149 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 4691089 4819436 4942982 5104960 6544471 6627549 7066203 719575925* 8039317 8074350 8074351 8145952 8187946 8193552 8202026 820426926* 8208746 820977527* @summary Validate ISO 4217 data for Currency class.28* @modules java.base/java.util:open29* jdk.localedata30*/3132/*33* ############################################################################34*35* ValidateISO4217 is a tool to detect differences between the latest ISO 421736* 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 data38* may need to be updated.39* src/share/classes/java/util/CurrencyData.properties40*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.java50*51* 3) Execute ValidateISO4217 as follows:52* java ValidateISO421753*/5455import java.io.*;56import java.text.*;57import java.util.*;5859public class ValidateISO4217 {6061static final int ALPHA_NUM = 26;6263static final byte UNDEFINED = 0;64static final byte DEFINED = 1;65static final byte SKIPPED = 2;6667/* input files */68static final String datafile = "tablea1.txt";6970/* alpha2-code table */71static byte[] codes = new byte[ALPHA_NUM * ALPHA_NUM];7273static final String[][] additionalCodes = {74/* Defined in ISO 4217 list, but don't have code and minor unit info. */75{"AQ", "", "", "0"}, // Antarctica7677/*78* Defined in ISO 4217 list, but don't have code and minor unit info in79* it. On the othe hand, both code and minor unit are defined in80* .properties file. I don't know why, though.81*/82{"GS", "GBP", "826", "2"}, // South Georgia And The South Sandwich Islands8384/* Not defined in ISO 4217 list, but defined in .properties file. */85{"AX", "EUR", "978", "2"}, // \u00c5LAND ISLANDS86{"PS", "ILS", "376", "2"}, // Palestinian Territory, Occupied8788/* Not defined in ISO 4217 list, but added in ISO 3166 country code list */89{"JE", "GBP", "826", "2"}, // Jersey90{"GG", "GBP", "826", "2"}, // Guernsey91{"IM", "GBP", "826", "2"}, // Isle of Man92{"BL", "EUR", "978", "2"}, // Saint Barthelemy93{"MF", "EUR", "978", "2"}, // Saint Martin94};9596/* Codes that are obsolete, do not have related country */97static final String otherCodes =98"ADP-AFA-ATS-AYM-AZM-BEF-BGL-BOV-BYB-BYR-CHE-CHW-CLF-COU-CUC-CYP-"99+ "DEM-EEK-ESP-FIM-FRF-GHC-GRD-GWP-IEP-ITL-LTL-LUF-LVL-MGF-MRO-MTL-MXV-MZM-NLG-"100+ "PTE-ROL-RUR-SDD-SIT-SKK-SRG-STD-TMM-TPE-TRL-VEF-UYI-USN-USS-VEB-"101+ "XAG-XAU-XBA-XBB-XBC-XBD-XDR-XFO-XFU-XPD-XPT-XSU-XTS-XUA-XXX-"102+ "YUM-ZMK-ZWD-ZWN-ZWR";103104static boolean err = false;105106static Set<Currency> testCurrencies = new HashSet<Currency>();107108public static void main(String[] args) throws Exception {109CheckDataVersion.check();110test1();111test2();112getAvailableCurrenciesTest();113114if (err) {115throw new RuntimeException("Failed: Validation ISO 4217 data");116}117}118119static void test1() throws Exception {120121try (FileReader fr = new FileReader(new File(System.getProperty("test.src", "."), datafile));122BufferedReader in = new BufferedReader(fr))123{124String line;125SimpleDateFormat format = null;126127while ((line = in.readLine()) != null) {128if (line.length() == 0 || line.charAt(0) == '#') {129continue;130}131132StringTokenizer tokens = new StringTokenizer(line, "\t");133String country = tokens.nextToken();134if (country.length() != 2) {135continue;136}137138String currency;139String numeric;140String minorUnit;141int tokensCount = tokens.countTokens();142if (tokensCount < 3) {143currency = "";144numeric = "0";145minorUnit = "0";146} else {147currency = tokens.nextToken();148numeric = tokens.nextToken();149minorUnit = tokens.nextToken();150testCurrencies.add(Currency.getInstance(currency));151152// check for the cutover153if (tokensCount > 3) {154if (format == null) {155format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);156format.setTimeZone(TimeZone.getTimeZone("GMT"));157format.setLenient(false);158}159if (format.parse(tokens.nextToken()).getTime() <160System.currentTimeMillis()) {161currency = tokens.nextToken();162numeric = tokens.nextToken();163minorUnit = tokens.nextToken();164testCurrencies.add(Currency.getInstance(currency));165}166}167}168int index = toIndex(country);169testCountryCurrency(country, currency, Integer.parseInt(numeric),170Integer.parseInt(minorUnit), index);171}172}173174for (int i = 0; i < additionalCodes.length; i++) {175int index = toIndex(additionalCodes[i][0]);176if (additionalCodes[i][1].length() != 0) {177testCountryCurrency(additionalCodes[i][0], additionalCodes[i][1],178Integer.parseInt(additionalCodes[i][2]),179Integer.parseInt(additionalCodes[i][3]), index);180testCurrencies.add(Currency.getInstance(additionalCodes[i][1]));181} else {182codes[index] = SKIPPED;183}184}185}186187static int toIndex(String s) {188return ((s.charAt(0) - 'A') * ALPHA_NUM + s.charAt(1) - 'A');189}190191static void testCountryCurrency(String country, String currencyCode,192int numericCode, int digits, int index) {193if (currencyCode.length() == 0) {194return;195}196testCurrencyDefined(currencyCode, numericCode, digits);197198Locale loc = new Locale("", country);199try {200Currency currency = Currency.getInstance(loc);201if (!currency.getCurrencyCode().equals(currencyCode)) {202System.err.println("Error: [" + country + ":" +203loc.getDisplayCountry() + "] expected: " + currencyCode +204", got: " + currency.getCurrencyCode());205err = true;206}207208if (codes[index] != UNDEFINED) {209System.out.println("Warning: [" + country + ":" +210loc.getDisplayCountry() +211"] multiple definitions. currency code=" + currencyCode);212}213codes[index] = DEFINED;214}215catch (Exception e) {216System.err.println("Error: " + e + ": Country=" + country);217err = true;218}219}220221static void testCurrencyDefined(String currencyCode, int numericCode, int digits) {222try {223Currency currency = currency = Currency.getInstance(currencyCode);224225if (currency.getNumericCode() != numericCode) {226System.err.println("Error: [" + currencyCode + "] expected: " +227numericCode + "; got: " + currency.getNumericCode());228err = true;229}230231if (currency.getDefaultFractionDigits() != digits) {232System.err.println("Error: [" + currencyCode + "] expected: " +233digits + "; got: " + currency.getDefaultFractionDigits());234err = true;235}236}237catch (Exception e) {238System.err.println("Error: " + e + ": Currency code=" +239currencyCode);240err = true;241}242}243244static void test2() {245for (int i = 0; i < ALPHA_NUM; i++) {246for (int j = 0; j < ALPHA_NUM; j++) {247char[] code = new char[2];248code[0] = (char)('A'+ i);249code[1] = (char)('A'+ j);250String country = new String(code);251boolean ex;252253if (codes[toIndex(country)] == UNDEFINED) {254ex = false;255try {256Currency.getInstance(new Locale("", country));257}258catch (IllegalArgumentException e) {259ex = true;260}261if (!ex) {262System.err.println("Error: This should be an undefined code and throw IllegalArgumentException: " +263country);264err = true;265}266} else if (codes[toIndex(country)] == SKIPPED) {267Currency cur = null;268try {269cur = Currency.getInstance(new Locale("", country));270}271catch (Exception e) {272System.err.println("Error: " + e + ": Country=" +273country);274err = true;275}276if (cur != null) {277System.err.println("Error: Currency.getInstance() for an this locale should return null: " +278country);279err = true;280}281}282}283}284}285286/**287* This test depends on test1(), where 'testCurrencies' set is constructed288*/289static void getAvailableCurrenciesTest() {290Set<Currency> jreCurrencies = Currency.getAvailableCurrencies();291292// add otherCodes293StringTokenizer st = new StringTokenizer(otherCodes, "-");294while (st.hasMoreTokens()) {295testCurrencies.add(Currency.getInstance(st.nextToken()));296}297298if (!testCurrencies.containsAll(jreCurrencies)) {299System.err.print("Error: getAvailableCurrencies() returned extra currencies than expected: ");300jreCurrencies.removeAll(testCurrencies);301for (Currency c : jreCurrencies) {302System.err.print(" "+c);303}304System.err.println();305err = true;306}307}308}309310311