2 *******************************************************************************
3 * Copyright (C) 2005-2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * New added, 2005-5-10 [Terry/SGL]
10 * Major modification by Ram
13 package com.ibm.icu.dev.test.util;
15 import java.util.HashMap;
17 import com.ibm.icu.impl.ICUResourceBundle;
18 import com.ibm.icu.text.DateFormat;
19 import com.ibm.icu.util.Calendar;
20 import com.ibm.icu.util.ULocale;
21 import com.ibm.icu.util.UResourceBundle;
23 public class LocaleAliasTest extends com.ibm.icu.dev.test.TestFmwk {
24 private static final ULocale[][] _LOCALES = {
26 {new ULocale("en", "RH"), new ULocale("en", "ZW")},
27 {new ULocale("in"), new ULocale("id")},
28 {new ULocale("in", "ID"), new ULocale("id", "ID")},
29 {new ULocale("iw"), new ULocale("he")},
30 {new ULocale("iw", "IL"), new ULocale("he", "IL")},
31 {new ULocale("ji"), new ULocale("yi")},
33 {new ULocale("en", "BU"), new ULocale("en", "MM")},
34 {new ULocale("en", "DY"), new ULocale("en", "BJ")},
35 {new ULocale("en", "HV"), new ULocale("en", "BF")},
36 {new ULocale("en", "NH"), new ULocale("en", "VU")},
37 {new ULocale("en", "TP"), new ULocale("en", "TL")},
38 {new ULocale("en", "ZR"), new ULocale("en", "CD")}
41 private static final int _LOCALE_NUMBER = _LOCALES.length;
42 private static ULocale[] available = null;
43 private HashMap availableMap = new HashMap();
44 private static final ULocale _DEFAULT_LOCALE = ULocale.US;
46 public LocaleAliasTest(){
48 protected void init(){
49 available = ULocale.getAvailableLocales();
50 for(int i=0; i<available.length;i++){
51 availableMap.put(available[i].toString(),"");
54 public static void main(String[] args) {
55 new LocaleAliasTest().run(args);
57 public void TestCalendar() {
58 ULocale defLoc = ULocale.getDefault();
59 ULocale.setDefault(_DEFAULT_LOCALE);
60 for (int i=0; i<_LOCALE_NUMBER; i++) {
61 ULocale oldLoc = _LOCALES[i][0];
62 ULocale newLoc = _LOCALES[i][1];
63 if(availableMap.get(_LOCALES[i][1])==null){
64 logln(_LOCALES[i][1]+" is not available. Skipping!");
67 Calendar c1 = Calendar.getInstance(oldLoc);
68 Calendar c2 = Calendar.getInstance(newLoc);
69 c1.setTime(c2.getTime());
70 //Test function "getFirstDayOfWeek"
71 // int firstDayOfWeek1 = c1.getFirstDayOfWeek();
72 // int firstDayOfWeek2 = c2.getFirstDayOfWeek();
73 // if (firstDayOfWeek1 != firstDayOfWeek2) {
74 // this.logln("Calendar(getFirstDayOfWeek) old:"
75 // +firstDayOfWeek1+" new:"+firstDayOfWeek2);
79 //Test function "getLocale(ULocale.VALID_LOCALE)"
80 ULocale l1 = c1.getLocale(ULocale.VALID_LOCALE);
81 ULocale l2 = c2.getLocale(ULocale.VALID_LOCALE);
82 if (!newLoc.equals(l1)) {
83 errln("CalendarTest: newLoc!=l1: newLoc= "+newLoc +" l1= "+l1);
86 errln("CalendarTest: l1!=l2: l1= "+l1 +" l2= "+l2);
89 errln("CalendarTest: c1!=c2. newLoc= "+newLoc +" oldLoc= "+oldLoc);
91 logln("Calendar(getLocale) old:"+l1+" new:"+l2);
93 ULocale.setDefault(defLoc);
96 public void TestDateFormat() {
97 ULocale defLoc = ULocale.getDefault();
98 ULocale.setDefault(_DEFAULT_LOCALE);
99 for (int i=0; i<_LOCALE_NUMBER; i++) {
100 ULocale oldLoc = _LOCALES[i][0];
101 ULocale newLoc = _LOCALES[i][1];
102 if(availableMap.get(_LOCALES[i][1])==null){
103 logln(_LOCALES[i][1]+" is not available. Skipping!");
106 DateFormat df1 = DateFormat.getDateInstance(DateFormat.FULL, oldLoc);
107 DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, newLoc);
109 //Test function "getLocale"
110 ULocale l1 = df1.getLocale(ULocale.VALID_LOCALE);
111 ULocale l2 = df2.getLocale(ULocale.VALID_LOCALE);
112 if (!newLoc.equals(l1)) {
113 errln("DateFormatTest: newLoc!=l1: newLoc= "+newLoc +" l1= "+l1);
115 if (!l1.equals(l2)) {
116 errln("DateFormatTest: l1!=l2: l1= "+l1 +" l2= "+l2);
118 if (!df1.equals(df2)) {
119 errln("DateFormatTest: df1!=df2: newLoc= "+newLoc +" oldLoc= "+oldLoc);
121 this.logln("DateFormat(getLocale) old:"+l1+" new:"+l2);
123 //Test function "format"
124 // Date d = new Date();
125 // String d1 = df1.format(d);
126 // String d2 = df2.format(d);
127 // if (!d1.equals(d2)) {
130 // this.logln("DateFormat(format) old:"+d1+" new:"+d2);
132 ULocale.setDefault(defLoc);
135 public void TestULocale() {
136 ULocale defLoc = ULocale.getDefault();
137 ULocale.setDefault(_DEFAULT_LOCALE);
138 for (int i=0; i<_LOCALE_NUMBER; i++) {
139 ULocale oldLoc = _LOCALES[i][0];
140 ULocale newLoc = _LOCALES[i][1];
141 if(availableMap.get(_LOCALES[i][1])==null){
142 logln(_LOCALES[i][1]+" is not available. Skipping!");
145 ULocale ul1 = new ULocale(oldLoc.toString());
146 ULocale ul2 = new ULocale(newLoc.toString());
148 String name1 = ul1.getDisplayName();
149 String name2 = ul2.getDisplayName();
150 if (!name1.equals(name2)) {
151 errln("name1!=name2. name1 = " + name1 +" name2 = " +name2);
153 logln("ULocale(getDisplayName) old:"+name1+" new:"+name2);
155 ULocale.setDefault(defLoc);
158 public void TestDisplayName() {
159 ULocale defLoc = ULocale.getDefault();
160 ULocale.setDefault(_DEFAULT_LOCALE);
161 for (int i=0; i<_LOCALE_NUMBER; i++) {
162 ULocale oldLoc = _LOCALES[i][0];
163 ULocale newLoc = _LOCALES[i][1];
165 for(int j=0; j<available.length; j++){
166 String oldCountry = oldLoc.getDisplayCountry(available[j]);
167 String newCountry = newLoc.getDisplayCountry(available[j]);
168 String oldLang = oldLoc.getDisplayLanguage(available[j]);
169 String newLang = newLoc.getDisplayLanguage(available[j]);
171 // is there display name for the current country ID
172 if(!newCountry.equals(newLoc.getCountry())){
173 if(!oldCountry.equals(newCountry)){
174 errln("getCountry() failed for "+ oldLoc +" oldCountry= "+ prettify(oldCountry) +" newCountry = "+prettify(newCountry)+ " in display locale "+ available[j].toString());
177 //there is a display name for the current lang ID
178 if(!newLang.equals(newLoc.getLanguage())){
179 if(!oldLang.equals(newLang)){
180 errln("getLanguage() failed for " + oldLoc + " oldLang = "+ prettify(oldLang) +" newLang = "+prettify(newLang)+ " in display locale "+ available[j].toString());
185 ULocale.setDefault(defLoc);
187 public void TestUResourceBundle() {
188 ULocale defLoc = ULocale.getDefault();
189 ULocale.setDefault(_DEFAULT_LOCALE);
190 for (int i=0; i<_LOCALE_NUMBER; i++) {
191 if(availableMap.get(_LOCALES[i][1])==null){
192 logln(_LOCALES[i][1]+" is not available. Skipping!");
195 ULocale oldLoc = _LOCALES[i][0];
196 ULocale newLoc = _LOCALES[i][1];
197 UResourceBundle urb1 = null;
198 UResourceBundle urb2 = null;
200 urb1 = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, oldLoc);
201 urb2 = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, newLoc);
202 ULocale l1 = urb1.getULocale();
203 ULocale l2 = urb2.getULocale();
204 if (!newLoc.equals(l1)) {
205 errln("ResourceBundleTest: newLoc!=l1: newLoc= "+newLoc +" l1= "+l1);
207 if (!l1.equals(l2)) {
208 errln("ResourceBundleTest: l1!=l2: l1= "+l1 +" l2= "+l2);
210 this.logln("UResourceBundle old:"+l1+" new:"+l2);
212 ULocale.setDefault(defLoc);