test/java/util/Currency/PropertiesTest.java

Print this page




  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  *
  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.
  22  */
  23 
  24 import java.io.*;
  25 import java.text.*;
  26 import java.util.*;
  27 import java.util.regex.*;
  28 
  29 public class PropertiesTest {
  30     public static void main(String[] s) throws Exception {
  31         for (int i = 0; i < s.length; i ++) {
  32             if ("-d".equals(s[i])) {
  33                 i++;
  34                 if (i == s.length) {
  35                     throw new RuntimeException("-d needs output file name");
  36                 } else {
  37                     dump(s[i]);


  38                 }
  39             } else if ("-c".equals(s[i])) {
  40                 if (i+2 == s.length) {
  41                     throw new RuntimeException("-d needs two file name arguments, before and after respectively");
  42                 } else {
  43                     compare(s[++i], s[++i]);
  44                 }
  45             }
  46         }
  47     }
  48 
  49     private static void dump(String outfile) {
  50         File f = new File(outfile);
  51         PrintWriter pw;
  52         try {
  53             f.createNewFile();
  54             pw = new PrintWriter(f);
  55         } catch (Exception fnfe) {
  56             throw new RuntimeException(fnfe);
  57         }
  58         for (char c1 = 'A'; c1 <= 'Z'; c1++) {
  59             for (char c2 = 'A'; c2 <= 'Z'; c2++) {
  60                 String ctry = new StringBuilder().append(c1).append(c2).toString();
  61                 try {
  62                     Currency c = Currency.getInstance(new Locale("", ctry));
  63                     if (c != null) {
  64                         pw.printf(Locale.ROOT, "%s=%s,%03d,%1d\n",
  65                             ctry,
  66                             c.getCurrencyCode(),
  67                             c.getNumericCode(),
  68                             c.getDefaultFractionDigits());
  69                     }
  70                 } catch (IllegalArgumentException iae) {
  71                     // invalid country code
  72                     continue;
  73                 }
  74             }
  75         }
  76         pw.flush();
  77         pw.close();
  78     }
  79 
  80     private static void compare(String beforeFile, String afterFile) throws Exception {


  81         // load file contents
  82         Properties before = new Properties();



  83         Properties after = new Properties();
  84         try {
  85             before.load(new FileReader(beforeFile));
  86             after.load(new FileReader(afterFile));
  87         } catch (IOException ioe) {
  88             throw new RuntimeException(ioe);
  89         }
  90 
  91         // remove the same contents from the 'after' properties
  92         Set<String> keys = before.stringPropertyNames();
  93         for (String key: keys) {
  94             String beforeVal = before.getProperty(key);
  95             String afterVal = after.getProperty(key);
  96             System.out.printf("Removing country: %s. before: %s, after: %s", key, beforeVal, afterVal);
  97             if (beforeVal.equals(afterVal)) {
  98                 after.remove(key);
  99                 System.out.printf(" --- removed\n");
 100             } else {
 101                 System.out.printf(" --- NOT removed\n");
 102             }
 103         }
 104 
 105         // now look at the currency.properties
 106         String propFileName = System.getProperty("java.home") + File.separator +
 107                               "lib" + File.separator + "currency.properties";
 108         Properties p = new Properties();
 109         try {
 110             p.load(new FileReader(propFileName));
 111         } catch (IOException ioe) {
 112             throw new RuntimeException(ioe);
 113         }
 114 
 115         // test each replacements
 116         keys = p.stringPropertyNames();
 117         Pattern propertiesPattern =
 118             Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +
 119                 "([0-3])\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +
 120                 "\\d{2}:\\d{2})?");
 121         for (String key: keys) {
 122             String val = p.getProperty(key);
 123             try {
 124                 if (countOccurrences(val, ',') == 3 && !isPastCutoverDate(val)) {
 125                     System.out.println("Skipping since date is in future");
 126                     continue; // skip since date in future (no effect)
 127                 }
 128             } catch (ParseException pe) {
 129                 // swallow - currency class should not honour this value
 130                 continue;
 131             }
 132             String afterVal = after.getProperty(key);




  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  *
  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.
  22  */
  23 
  24 import java.io.*;
  25 import java.text.*;
  26 import java.util.*;
  27 import java.util.regex.*;
  28 
  29 public class PropertiesTest {
  30     public static void main(String[] args) throws Exception {
  31         if (args.length == 2 && args[0].equals("-d")) {
  32             dump(args[1]);
  33         } else if (args.length == 4 && args[0].equals("-c")) {
  34             compare(args[1], args[2], args[3]);

  35         } else {
  36             System.err.println("Usage:  java PropertiesTest -d <dumpfile>");
  37             System.err.println("        java PropertiesTest -c <beforedump> <afterdump> <propsfile>");
  38             System.exit(-1);
  39         }





  40     }



  41 
  42     private static void dump(String outfile) {
  43         File f = new File(outfile);
  44         PrintWriter pw;
  45         try {
  46             f.createNewFile();
  47             pw = new PrintWriter(f);
  48         } catch (Exception fnfe) {
  49             throw new RuntimeException(fnfe);
  50         }
  51         for (char c1 = 'A'; c1 <= 'Z'; c1++) {
  52             for (char c2 = 'A'; c2 <= 'Z'; c2++) {
  53                 String ctry = new StringBuilder().append(c1).append(c2).toString();
  54                 try {
  55                     Currency c = Currency.getInstance(new Locale("", ctry));
  56                     if (c != null) {
  57                         pw.printf(Locale.ROOT, "%s=%s,%03d,%1d\n",
  58                             ctry,
  59                             c.getCurrencyCode(),
  60                             c.getNumericCode(),
  61                             c.getDefaultFractionDigits());
  62                     }
  63                 } catch (IllegalArgumentException iae) {
  64                     // invalid country code
  65                     continue;
  66                 }
  67             }
  68         }
  69         pw.flush();
  70         pw.close();
  71     }
  72 
  73     private static void compare(String beforeFile, String afterFile, String propsFile)
  74         throws IOException
  75     {
  76         // load file contents
  77         Properties before = new Properties();
  78         try (Reader reader = new FileReader(beforeFile)) {
  79             before.load(reader);
  80         }
  81         Properties after = new Properties();
  82         try (Reader reader = new FileReader(afterFile)) {
  83             after.load(reader);



  84         }
  85 
  86         // remove the same contents from the 'after' properties
  87         Set<String> keys = before.stringPropertyNames();
  88         for (String key: keys) {
  89             String beforeVal = before.getProperty(key);
  90             String afterVal = after.getProperty(key);
  91             System.out.printf("Removing country: %s. before: %s, after: %s", key, beforeVal, afterVal);
  92             if (beforeVal.equals(afterVal)) {
  93                 after.remove(key);
  94                 System.out.printf(" --- removed\n");
  95             } else {
  96                 System.out.printf(" --- NOT removed\n");
  97             }
  98         }
  99 
 100         // now look at the currency.properties


 101         Properties p = new Properties();
 102         try (Reader reader = new FileReader(propsFile)) {
 103             p.load(reader);


 104         }
 105 
 106         // test each replacements
 107         keys = p.stringPropertyNames();
 108         Pattern propertiesPattern =
 109             Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +
 110                 "([0-3])\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +
 111                 "\\d{2}:\\d{2})?");
 112         for (String key: keys) {
 113             String val = p.getProperty(key);
 114             try {
 115                 if (countOccurrences(val, ',') == 3 && !isPastCutoverDate(val)) {
 116                     System.out.println("Skipping since date is in future");
 117                     continue; // skip since date in future (no effect)
 118                 }
 119             } catch (ParseException pe) {
 120                 // swallow - currency class should not honour this value
 121                 continue;
 122             }
 123             String afterVal = after.getProperty(key);