Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Currency/PropertiesTest.java
47182 views
/*1* Copyright (c) 2007, 2015, 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*/2223import java.io.*;24import java.text.*;25import java.util.*;26import java.util.regex.*;2728public class PropertiesTest {29public static void main(String[] args) throws Exception {30if (args.length == 2 && args[0].equals("-d")) {31dump(args[1]);32} else if (args.length == 4 && args[0].equals("-c")) {33compare(args[1], args[2], args[3]);34} else {35System.err.println("Usage: java PropertiesTest -d <dumpfile>");36System.err.println(" java PropertiesTest -c <beforedump> <afterdump> <propsfile>");37System.exit(-1);38}39}4041private static void dump(String outfile) {42File f = new File(outfile);43PrintWriter pw;44try {45f.createNewFile();46pw = new PrintWriter(f);47} catch (Exception fnfe) {48throw new RuntimeException(fnfe);49}50for (char c1 = 'A'; c1 <= 'Z'; c1++) {51for (char c2 = 'A'; c2 <= 'Z'; c2++) {52String ctry = new StringBuilder().append(c1).append(c2).toString();53try {54Currency c = Currency.getInstance(new Locale("", ctry));55if (c != null) {56pw.printf(Locale.ROOT, "%s=%s,%03d,%1d\n",57ctry,58c.getCurrencyCode(),59c.getNumericCode(),60c.getDefaultFractionDigits());61}62} catch (IllegalArgumentException iae) {63// invalid country code64continue;65}66}67}68pw.flush();69pw.close();70}7172private static void compare(String beforeFile, String afterFile, String propsFile)73throws IOException74{75// load file contents76Properties before = new Properties();77try (Reader reader = new FileReader(beforeFile)) {78before.load(reader);79}80Properties after = new Properties();81try (Reader reader = new FileReader(afterFile)) {82after.load(reader);83}8485// remove the same contents from the 'after' properties86Set<String> keys = before.stringPropertyNames();87for (String key: keys) {88String beforeVal = before.getProperty(key);89String afterVal = after.getProperty(key);90System.out.printf("Removing country: %s. before: %s, after: %s", key, beforeVal, afterVal);91if (beforeVal.equals(afterVal)) {92after.remove(key);93System.out.printf(" --- removed\n");94} else {95System.out.printf(" --- NOT removed\n");96}97}9899// now look at the currency.properties100Properties p = new Properties();101try (Reader reader = new FileReader(propsFile)) {102p.load(reader);103}104105// test each replacements106keys = p.stringPropertyNames();107Pattern propertiesPattern =108Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +109"(\\d+)\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +110"\\d{2}:\\d{2})?");111for (String key: keys) {112String val = p.getProperty(key);113try {114if (countOccurrences(val, ',') == 3 && !isPastCutoverDate(val)) {115System.out.println("Skipping since date is in future");116continue; // skip since date in future (no effect)117}118} catch (ParseException pe) {119// swallow - currency class should not honour this value120continue;121}122String afterVal = after.getProperty(key);123System.out.printf("Testing key: %s, val: %s... ", key, val);124System.out.println("AfterVal is : " + afterVal);125126Matcher m = propertiesPattern.matcher(val.toUpperCase(Locale.ROOT));127if (!m.find()) {128// format is not recognized.129System.out.printf("Format is not recognized.\n");130if (afterVal != null) {131throw new RuntimeException("Currency data replacement for "+key+" failed: It was incorrectly altered to "+afterVal);132}133134// ignore this135continue;136}137138String code = m.group(1);139int numeric = Integer.parseInt(m.group(2));140int fraction = Integer.parseInt(m.group(3));141if (fraction > 9) {142System.out.println("Skipping since the fraction is greater than 9");143continue;144}145146Matcher mAfter = propertiesPattern.matcher(afterVal);147mAfter.find();148149String codeAfter = mAfter.group(1);150int numericAfter = Integer.parseInt(mAfter.group(2));151int fractionAfter = Integer.parseInt(mAfter.group(3));152if (code.equals(codeAfter) &&153(numeric == numericAfter)&&154(fraction == fractionAfter)) {155after.remove(key);156} else {157throw new RuntimeException("Currency data replacement for "+key+" failed: actual: (alphacode: "+codeAfter+", numcode: "+numericAfter+", fraction: "+fractionAfter+"), expected: (alphacode: "+code+", numcode: "+numeric+", fraction: "+fraction+")");158}159System.out.printf("Success!\n");160}161if (!after.isEmpty()) {162StringBuilder sb = new StringBuilder()163.append("Currency data replacement failed. Unnecessary modification was(were) made for the following currencies:\n");164keys = after.stringPropertyNames();165for (String key : keys) {166sb.append(" country: ")167.append(key)168.append(" currency: ")169.append(after.getProperty(key))170.append("\n");171}172throw new RuntimeException(sb.toString());173}174}175176private static boolean isPastCutoverDate(String s)177throws IndexOutOfBoundsException, NullPointerException, ParseException {178String dateString = s.substring(s.lastIndexOf(',')+1, s.length()).trim();179SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT);180format.setTimeZone(TimeZone.getTimeZone("GMT"));181format.setLenient(false);182183long time = format.parse(dateString).getTime();184if (System.currentTimeMillis() - time >= 0L) {185return true;186} else {187return false;188}189}190191private static int countOccurrences(String value, char match) {192int count = 0;193for (char c : value.toCharArray()) {194if (c == match) {195++count;196}197}198return count;199}200}201202203