1 /*
   2  * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
   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  *
  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  *
  25  */
  26 
  27 import java.text.*;
  28 import java.util.*;
  29 import sun.util.locale.provider.*;
  30 import sun.util.resources.*;
  31 
  32 public class CurrencyNameProviderTest extends ProviderTest {
  33 
  34     public static void main(String[] s) {
  35         Locale reservedLocale = Locale.getDefault();
  36         try {
  37             new CurrencyNameProviderTest();
  38         } finally {
  39             // restore the reserved locale
  40             Locale.setDefault(reservedLocale);
  41         }
  42     }
  43 
  44     CurrencyNameProviderTest() {
  45         test1();
  46         test2();
  47     }
  48 
  49     void test1() {
  50         com.bar.CurrencyNameProviderImpl cnp = new com.bar.CurrencyNameProviderImpl();
  51         Locale[] availloc = Locale.getAvailableLocales();
  52         Locale[] testloc = availloc.clone();
  53         List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCurrencyNameProvider().getAvailableLocales());
  54         List<Locale> providerloc = Arrays.asList(cnp.getAvailableLocales());
  55 
  56         for (Locale target: availloc) {
  57             // pure JRE implementation
  58             OpenListResourceBundle rb = (OpenListResourceBundle)LocaleProviderAdapter.forJRE().getLocaleData().getCurrencyNames(target);
  59             boolean jreSupportsTarget = jreimplloc.contains(target);
  60 
  61             for (Locale test: testloc) {
  62                 // get a Currency instance
  63                 Currency c = null;
  64                 try {
  65                     c = Currency.getInstance(test);
  66                 } catch (IllegalArgumentException iae) {}
  67 
  68                 if (c == null) {
  69                     continue;
  70                 }
  71 
  72                 // the localized symbol for the target locale
  73                 String currencyresult = c.getSymbol(target);
  74 
  75                 // the localized name for the target locale
  76                 String nameresult = c.getDisplayName(target);
  77 
  78                 // provider's name (if any)
  79                 String providerscurrency = null;
  80                 String providersname = null;
  81                 if (providerloc.contains(target)) {
  82                     providerscurrency = cnp.getSymbol(c.getCurrencyCode(), target);
  83                     providersname = cnp.getDisplayName(c.getCurrencyCode(), target);
  84                 }
  85 
  86                 // JRE's name
  87                 String jrescurrency = null;
  88                 String jresname = null;
  89                 String key = c.getCurrencyCode();
  90                 String nameKey = key.toLowerCase(Locale.ROOT);
  91                 if (jreSupportsTarget) {
  92                     try {
  93                         jrescurrency = rb.getString(key);
  94                     } catch (MissingResourceException mre) {}
  95                     try {
  96                         jresname = rb.getString(nameKey);
  97                     } catch (MissingResourceException mre) {}
  98                 }
  99 
 100                 checkValidity(target, jrescurrency, providerscurrency, currencyresult, 
 101                               jreSupportsTarget && jrescurrency != null);
 102                 checkValidity(target, jresname, providersname, nameresult,
 103                               jreSupportsTarget && jresname != null);
 104             }
 105         }
 106     }
 107 
 108 
 109     final String pattern = "###,###\u00A4";
 110     final String YEN_IN_OSAKA = "100,000\u5186\u3084\u3002";
 111     final String YEN_IN_KYOTO = "100,000\u5186\u3069\u3059\u3002";
 112     final Locale OSAKA = new Locale("ja", "JP", "osaka");
 113     final Locale KYOTO = new Locale("ja", "JP", "kyoto");
 114     Integer i = new Integer(100000);
 115     String formatted;
 116     DecimalFormat df;
 117 
 118     void test2() {
 119         try {
 120             df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(OSAKA));
 121             System.out.println(formatted = df.format(i));
 122             if(!formatted.equals(YEN_IN_OSAKA)) {
 123                 throw new RuntimeException("formatted zone names mismatch. " +
 124                     "Should match with " + YEN_IN_OSAKA);
 125             }
 126 
 127             df.parse(YEN_IN_OSAKA);
 128 
 129             Locale.setDefault(KYOTO);
 130             df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());
 131             System.out.println(formatted = df.format(i));
 132             if(!formatted.equals(YEN_IN_KYOTO)) {
 133                 throw new RuntimeException("formatted zone names mismatch. " +
 134                     "Should match with " + YEN_IN_KYOTO);
 135             }
 136 
 137             df.parse(YEN_IN_KYOTO);
 138         } catch (ParseException pe) {
 139             throw new RuntimeException("parse error occured" + pe);
 140         }
 141     }
 142 }