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