jdk/test/java/util/Currency/PropertiesTest.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
child 13790 5b29e3921008
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
public class PropertiesTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
    public static void main(String[] s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
        for (int i = 0; i < s.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
            if ("-d".equals(s[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
                if (i == s.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
                    throw new RuntimeException("-d needs output file name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                    dump(s[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
            } else if ("-c".equals(s[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                if (i+2 == s.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                    throw new RuntimeException("-d needs two file name arguments, before and after respectively");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                    compare(s[++i], s[++i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static void dump(String outfile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        File f = new File(outfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        PrintWriter pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            f.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            pw = new PrintWriter(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        } catch (Exception fnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new RuntimeException(fnfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (char c1 = 'A'; c1 <= 'Z'; c1++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            for (char c2 = 'A'; c2 <= 'Z'; c2++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                String ctry = new StringBuilder().append(c1).append(c2).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    Currency c = Currency.getInstance(new Locale("", ctry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                        pw.printf(Locale.ROOT, "%s=%s,%03d,%1d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                            ctry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                            c.getCurrencyCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                            c.getNumericCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                            c.getDefaultFractionDigits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    // invalid country code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        pw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        pw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static void compare(String beforeFile, String afterFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // load file contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Properties before = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Properties after = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            before.load(new FileReader(beforeFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            after.load(new FileReader(afterFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new RuntimeException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // remove the same contents from the 'after' properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Set<String> keys = before.stringPropertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (String key: keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            String beforeVal = before.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            String afterVal = after.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            System.out.printf("Removing country: %s. before: %s, after: %s", key, beforeVal, afterVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (beforeVal.equals(afterVal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                after.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                System.out.printf(" --- removed\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.out.printf(" --- NOT removed\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // now look at the currency.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        String propFileName = System.getProperty("java.home") + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                              "lib" + File.separator + "currency.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Properties p = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            p.load(new FileReader(propFileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new RuntimeException(ioe);
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 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*([0-3])");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (String key: keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            String val = p.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            String afterVal = after.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.printf("Testing key: %s, val: %s... ", key, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Matcher m = propertiesPattern.matcher(val.toUpperCase(Locale.ROOT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (!m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                // format is not recognized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                System.out.printf("Format is not recognized.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (afterVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    throw new RuntimeException("Currency data replacement for "+key+" failed: It was incorrectly altered to "+afterVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                // ignore this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Matcher mAfter = propertiesPattern.matcher(afterVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            mAfter.find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            String code = m.group(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            String codeAfter = mAfter.group(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            int numeric = Integer.parseInt(m.group(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int numericAfter = Integer.parseInt(mAfter.group(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int fraction = Integer.parseInt(m.group(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int fractionAfter = Integer.parseInt(mAfter.group(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (code.equals(codeAfter) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                (numeric == numericAfter)&&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                (fraction == fractionAfter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                after.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                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
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            System.out.printf("Success!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!after.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            StringBuilder sb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                .append("Currency data replacement failed.  Unnecessary modification was(were) made for the following currencies:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            keys = after.stringPropertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            for (String key : keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                sb.append("    country: ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                .append(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                .append(" currency: ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                .append(after.getProperty(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                .append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new RuntimeException(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}