test/jdk/java/util/Currency/PropertiesTest.java
author nishjain
Thu, 22 Feb 2018 11:52:01 +0530
changeset 48929 28d8fc8cd3cd
parent 47216 71c04702a3d5
permissions -rw-r--r--
8190904: Incorrect currency instance returned by java.util.Currency.getInstance() Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
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: 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
import java.io.*;
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
    25
import java.text.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.regex.*;
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
    28
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
public class PropertiesTest {
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    31
    public static void main(String[] args) throws Exception {
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    32
        if (args.length == 2 && args[0].equals("-d")) {
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    33
            dump(args[1]);
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    34
        } else if (args.length == 4 && args[0].equals("-c")) {
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    35
            compare(args[1], args[2], args[3]);
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
    36
        } else if (args.length == 1 && args[0].equals("bug7102969")) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
    37
            bug7102969();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
    38
        } else if (args.length == 1 && args[0].equals("bug8157138")) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
    39
            bug8157138();
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
    40
        } else if (args.length == 1 && args[0].equals("bug8190904")) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
    41
            bug8190904();
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    42
        } else {
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    43
            System.err.println("Usage:  java PropertiesTest -d <dumpfile>");
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    44
            System.err.println("        java PropertiesTest -c <beforedump> <afterdump> <propsfile>");
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
    45
            System.err.println("        java PropertiesTest bug[JBS bug id number] e.g. bug7102969");
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    46
            System.exit(-1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static void dump(String outfile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        File f = new File(outfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        PrintWriter pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            pw = new PrintWriter(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } catch (Exception fnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw new RuntimeException(fnfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (char c1 = 'A'; c1 <= 'Z'; c1++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            for (char c2 = 'A'; c2 <= 'Z'; c2++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                String ctry = new StringBuilder().append(c1).append(c2).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    Currency c = Currency.getInstance(new Locale("", ctry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        pw.printf(Locale.ROOT, "%s=%s,%03d,%1d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                            ctry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                            c.getCurrencyCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                            c.getNumericCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                            c.getDefaultFractionDigits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    // invalid country code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        pw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        pw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    81
    private static void compare(String beforeFile, String afterFile, String propsFile)
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    82
        throws IOException
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    83
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // load file contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Properties before = new Properties();
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    86
        try (Reader reader = new FileReader(beforeFile)) {
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    87
            before.load(reader);
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    88
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Properties after = new Properties();
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    90
        try (Reader reader = new FileReader(afterFile)) {
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
    91
            after.load(reader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // remove the same contents from the 'after' properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Set<String> keys = before.stringPropertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        for (String key: keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            String beforeVal = before.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            String afterVal = after.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.out.printf("Removing country: %s. before: %s, after: %s", key, beforeVal, afterVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (beforeVal.equals(afterVal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                after.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                System.out.printf(" --- removed\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                System.out.printf(" --- NOT removed\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // now look at the currency.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Properties p = new Properties();
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
   110
        try (Reader reader = new FileReader(propsFile)) {
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 13790
diff changeset
   111
            p.load(reader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // test each replacements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        keys = p.stringPropertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Pattern propertiesPattern =
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   117
            Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   118
                "(\\d+)\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   119
                "\\d{2}:\\d{2})?");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for (String key: keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            String val = p.getProperty(key);
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   122
            try {
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   123
                if (val.chars().map(c -> c == ',' ? 1 : 0).sum() >= 3
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   124
                        && !isPastCutoverDate(val)) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   125
                    System.out.println("Skipping " + key + " since date is in future");
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   126
                    continue; // skip since date in future (no effect)
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   127
                }
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   128
            } catch (ParseException pe) {
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   129
                // swallow - currency class should not honour this value
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   130
                continue;
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   131
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            String afterVal = after.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.printf("Testing key: %s, val: %s... ", key, val);
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   134
            System.out.println("AfterVal is : " + afterVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   136
            if (afterVal == null) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   137
                System.out.println("Testing key " + key + " is ignored"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   138
                        + " because of the inconsistent numeric code and/or"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   139
                        + " dfd for the given currency code: "+val);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   140
                continue;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   141
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Matcher m = propertiesPattern.matcher(val.toUpperCase(Locale.ROOT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (!m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // format is not recognized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                System.out.printf("Format is not recognized.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if (afterVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    throw new RuntimeException("Currency data replacement for "+key+" failed: It was incorrectly altered to "+afterVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // ignore this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   154
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   155
            String code = m.group(1);
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   156
            int numeric = Integer.parseInt(m.group(2));
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   157
            int fraction = Integer.parseInt(m.group(3));
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   158
            if (fraction > 9) {
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   159
                System.out.println("Skipping since the fraction is greater than 9");
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   160
                continue;
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   161
            }
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 14697
diff changeset
   162
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            Matcher mAfter = propertiesPattern.matcher(afterVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            mAfter.find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            String codeAfter = mAfter.group(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            int numericAfter = Integer.parseInt(mAfter.group(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int fractionAfter = Integer.parseInt(mAfter.group(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (code.equals(codeAfter) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                (numeric == numericAfter)&&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                (fraction == fractionAfter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                after.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new RuntimeException("Currency data replacement for "+key+" failed: actual: (alphacode: "+codeAfter+", numcode: "+numericAfter+", fraction: "+fractionAfter+"), expected:  (alphacode: "+code+", numcode: "+numeric+", fraction: "+fraction+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.out.printf("Success!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (!after.isEmpty()) {
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   179
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            keys = after.stringPropertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            for (String key : keys) {
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   182
                String modified = after.getProperty(key);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   183
                if(!p.containsValue(modified)) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   184
                    throw new RuntimeException("Unnecessary modification was"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   185
                            + " made to county: "+ key + " with currency value:"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   186
                                    + " " + modified);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   187
                } else {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   188
                    System.out.println(key + " modified by an entry in"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   189
                            + " currency.properties with currency value "
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   190
                            + modified);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   191
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   195
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   196
    private static void bug7102969() {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   197
        // check the correct overriding of special case entries
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   198
        Currency cur = Currency.getInstance(new Locale("", "JP"));
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   199
        if (!cur.getCurrencyCode().equals("ABC")) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   200
            throw new RuntimeException("[Expected: ABC as currency code of JP, found: "
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   201
                    + cur.getCurrencyCode() + "]");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   202
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   203
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   204
        /* check if the currency instance is returned by
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   205
         * getAvailableCurrencies() method
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   206
         */
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   207
        if (!Currency.getAvailableCurrencies().contains(cur)) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   208
            throw new RuntimeException("[The Currency instance ["
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   209
                    + cur.getCurrencyCode() + ", "
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   210
                    + cur.getNumericCode() + ", "
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   211
                    + cur.getDefaultFractionDigits()
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   212
                    + "] is not available in the currencies list]");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   213
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   214
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   215
    }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   216
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   217
    private static void bug8157138() {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   218
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   219
        /* check the currencies which exist only as a special case are
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   220
         * accessible i.e. it should not throw IllegalArgumentException
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   221
         */
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   222
        try {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   223
            Currency.getInstance("MAD");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   224
        } catch (IllegalArgumentException ex) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   225
            throw new RuntimeException("Test Failed: "
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   226
                    + "special case currency instance MAD not found"
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   227
                    + " via Currency.getInstance(\"MAD\")");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   228
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   229
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   230
        try {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   231
            Currency.getInstance("ABC");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   232
        } catch (IllegalArgumentException ex) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   233
            throw new RuntimeException("Test Failed: "
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   234
                    + "special case currency instance ABC not found"
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   235
                    + " via Currency.getInstance(\"ABC\")");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   236
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   237
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   238
        /* check the currency value is returned by getAvailableCurrencies()
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   239
         * method
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   240
        */
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   241
        List<Currency> list = Currency.getAvailableCurrencies().stream()
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   242
                .filter(cur -> cur.getCurrencyCode().equals("MAD"))
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   243
                .collect(Collectors.toList());
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   244
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   245
        if (list.isEmpty()) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   246
            throw new RuntimeException("Test Failed: "
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   247
                    + "special case currency instance MAD not found"
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   248
                    + " in Currency.getAvailableCurrencies() list");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   249
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   250
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   251
        list = Currency.getAvailableCurrencies().stream()
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   252
                .filter(cur -> cur.getCurrencyCode().equals("ABC"))
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   253
                .collect(Collectors.toList());
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   254
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   255
        if (list.isEmpty()) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   256
            throw new RuntimeException("Test Failed: "
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   257
                    + "special case currency instance ABC not found"
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   258
                    + " in Currency.getAvailableCurrencies() list");
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   259
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   260
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   261
    }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 29524
diff changeset
   262
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   263
    private static void bug8190904() {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   264
        // should throw IllegalArgumentException as currency code
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   265
        // does not exist as valid ISO 4217 code and failed to load
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   266
        // from currency.properties file because of inconsistent numeric/dfd
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   267
        try {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   268
            Currency.getInstance("MCC");
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   269
            throw new RuntimeException("[FAILED: Should throw"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   270
                    + " IllegalArgumentException for invalid currency code]");
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   271
        } catch (IllegalArgumentException ex) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   272
            // expected to throw IllegalArgumentException
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   273
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   274
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   275
        // should keep the XOF instance as XOF,952,0, as the XOF entries in
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   276
        // currency.properties IT=XOF,952,1, XY=XOF,955,0 are ignored because
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   277
        // of inconsistency in numeric code and/or dfd
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   278
        checkCurrencyInstance("XOF", 952, 0);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   279
        // property entry "AS=USD,841,2" should change all occurences
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   280
        // of USD with USD,841,2
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   281
        checkCurrencyInstance("USD", 841, 2);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   282
    }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   283
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   284
    /**
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   285
     * Test the numeric code and fraction of the Currency instance obtained
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   286
     * by given currencyCode, with the expected numericCode and fraction
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   287
     */
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   288
    private static void checkCurrencyInstance(String currencyCode,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   289
            int numericCode, int fraction) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   290
        Currency cur = Currency.getInstance(currencyCode);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   291
        if (cur.getNumericCode() != numericCode
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   292
                || cur.getDefaultFractionDigits() != fraction) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   293
            throw new RuntimeException("[FAILED: Incorrect numeric code or"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   294
                    + " dfd for currency code: " + currencyCode + "]");
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   295
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   296
    }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 47216
diff changeset
   297
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   298
    private static boolean isPastCutoverDate(String s)
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   299
            throws IndexOutOfBoundsException, NullPointerException, ParseException {
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   300
        String dateString = s.substring(s.lastIndexOf(',')+1, s.length()).trim();
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   301
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT);
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   302
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   303
        format.setLenient(false);
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   304
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   305
        long time = format.parse(dateString).getTime();
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   306
        if (System.currentTimeMillis() - time >= 0L) {
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   307
            return true;
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   308
        } else {
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   309
            return false;
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   310
        }
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   311
    }
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 5506
diff changeset
   312
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}