3 *******************************************************************************
4 * Copyright (C) 2001-2009, International Business Machines Corporation and *
5 * others. All Rights Reserved. *
6 *******************************************************************************
8 package com.ibm.icu.dev.test.util;
10 import java.io.BufferedReader;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
14 import java.net.URLConnection;
15 import java.net.JarURLConnection;
16 //#if defined(FOUNDATION10) || defined(J2SE13)
17 //##import com.ibm.icu.impl.ByteBuffer;
19 import java.nio.ByteBuffer;
21 import java.util.MissingResourceException;
22 import java.util.Enumeration;
23 import java.util.jar.JarEntry;
25 import com.ibm.icu.dev.test.TestFmwk;
26 import com.ibm.icu.impl.ICUResourceBundle;
27 import com.ibm.icu.impl.Utility;
28 import com.ibm.icu.text.UTF16;
29 import com.ibm.icu.util.Holiday;
30 import com.ibm.icu.util.ULocale;
31 import com.ibm.icu.util.UResourceBundle;
32 import com.ibm.icu.util.UResourceTypeMismatchException;
35 public final class ICUResourceBundleTest extends TestFmwk {
36 private static final ClassLoader testLoader = ICUResourceBundleTest.class.getClassLoader();
38 public static void main(String args[]) throws Exception {
39 ICUResourceBundleTest test = new ICUResourceBundleTest();
43 public void TestGetResources(){
45 // It does not work well in eclipse plug-in test because of class loader configuration??
46 // For now, specify resource path explicitly in this test case
47 //Enumeration en = testLoader.getResources("META-INF");
48 Enumeration en = testLoader.getResources("com.ibm.icu.dev.data");
49 for(;en.hasMoreElements();) {
50 URL url = (URL)en.nextElement();
52 warnln("could not load resource data");
55 URLConnection c = url.openConnection();
57 if (c instanceof JarURLConnection) {
58 JarURLConnection jc = (JarURLConnection)c;
59 JarEntry je = jc.getJarEntry();
60 logln("jar entry: " + je.toString());
62 InputStream is = c.getInputStream();
63 logln("input stream:");
64 InputStreamReader r = new InputStreamReader(is);
65 BufferedReader br = new BufferedReader(r);
68 while ((line = br.readLine()) != null) {
69 logln(" " + ++n + ": " + line);
73 }catch(SecurityException ex) {
74 warnln("could not load resource data: " + ex);
76 }catch(NullPointerException ex) {
77 // thrown by ibm 1.4.2 windows jvm security manager
78 warnln("could not load resource data: " + ex);
81 errln("Unexpected exception: "+ ex);
84 public void TestResourceBundleWrapper(){
85 UResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", "da_DK");
86 Object o = bundle.getObject("holidays");
87 if(o instanceof Holiday[] ){
88 logln("wrapper mechanism works for Weekend data");
90 errln("Did not get the expected output for Weekend data");
93 bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "bogus");
94 if(bundle instanceof UResourceBundle && bundle.getULocale().equals("en_US")){
95 logln("wrapper mechanism works for bogus locale");
97 errln("wrapper mechanism failed for bogus locale.");
101 bundle = UResourceBundle.getBundleInstance("bogus", "bogus");
103 errln("Did not get the expected exception");
105 }catch(MissingResourceException ex){
106 logln("got the expected exception");
111 public void TestJB3879(){
112 // this tests tests loading of root bundle when a resource bundle
113 // for the default locale is requested
115 UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", ULocale.getDefault().toString(), testLoader);
117 errln("could not create the resource bundle");
120 catch (MissingResourceException ex) {
121 warnln("could not load test data: " + ex.getMessage());
124 public void TestOpen(){
125 UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "en_US_POSIX");
128 errln("could not create the resource bundle");
131 UResourceBundle obj = bundle.get("NumberPatterns");
133 int size = obj.getSize();
134 int type = obj.getType();
135 if(type == UResourceBundle.ARRAY){
137 for(int i=0; i<size; i++){
139 String temp =sub.getString();
140 if(temp.length()==0){
141 errln("Failed to get the items from NumberPatterns array in bundle: "+
142 bundle.getULocale().getBaseName());
144 //System.out.println("\""+prettify(temp)+"\"");
148 String[] strings = bundle.getStringArray("NumberPatterns");
149 if(size!=strings.length){
150 errln("Failed to get the items from NumberPatterns array in bundle: "+
151 bundle.getULocale().getBaseName());
154 obj = bundle.get("NumberElements");
156 size = obj.getSize();
157 type = obj.getType();
158 if(type == UResourceBundle.ARRAY){
160 for(int i=0; i<size; i++){
162 String temp =sub.getString();
163 if(temp.length()==0){
164 errln("Failed to get the items from NumberPatterns array in bundle: "+
165 bundle.getULocale().getBaseName());
167 // System.out.println("\""+prettify(temp)+"\"");
173 errln("could not create the resource bundle");
175 bundle = (UResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "en_US_POSIX");
177 errln("could not load the stream");
179 bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zzz_ZZ_very_very_very_long_bogus_bundle");
180 if(!bundle.getULocale().equals(ULocale.getDefault())){
181 errln("UResourceBundle did not load the default bundle when bundle was not found. Default: " + ULocale.getDefault() +
182 ", Bundle locale: " + bundle.getULocale());
188 public void TestBasicTypes(){
189 UResourceBundle bundle = null;
191 bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes", testLoader);
193 catch (MissingResourceException e) {
194 warnln("could not load test data: " + e.getMessage());
198 String expected = "abc\u0000def";
199 UResourceBundle sub = bundle.get("zerotest");
200 if(!expected.equals(sub.getString())){
201 errln("Did not get the expected string for key zerotest in bundle testtypes");
203 sub = bundle.get("emptyexplicitstring");
205 if(!expected.equals(sub.getString())){
206 errln("Did not get the expected string for key emptyexplicitstring in bundle testtypes");
208 sub = bundle.get("emptystring");
210 if(!expected.equals(sub.getString())){
211 errln("Did not get the expected string for key emptystring in bundle testtypes");
216 UResourceBundle sub = bundle.get("onehundredtwentythree");
217 if(expected!=sub.getInt()){
218 errln("Did not get the expected int value for key onehundredtwentythree in bundle testtypes");
220 sub = bundle.get("emptyint");
222 if(expected!=sub.getInt()){
223 errln("Did not get the expected int value for key emptyint in bundle testtypes");
228 UResourceBundle sub = bundle.get("one");
229 if(expected!=sub.getInt()){
230 errln("Did not get the expected int value for key one in bundle testtypes");
235 UResourceBundle sub = bundle.get("minusone");
236 int got = sub.getInt();
238 errln("Did not get the expected int value for key minusone in bundle testtypes");
240 expected = 0xFFFFFFF;
243 errln("Did not get the expected int value for key minusone in bundle testtypes");
248 UResourceBundle sub = bundle.get("plusone");
249 if(expected!=sub.getInt()){
250 errln("Did not get the expected int value for key minusone in bundle testtypes");
255 int[] expected = new int[]{ 1, 2, 3, -3, 4, 5, 6, 7 } ;
256 UResourceBundle sub = bundle.get("integerarray");
257 if(!Utility.arrayEquals(expected,sub.getIntVector())){
258 errln("Did not get the expected int vector value for key integerarray in bundle testtypes");
260 sub = bundle.get("emptyintv");
261 expected = new int[0];
262 if(!Utility.arrayEquals(expected,sub.getIntVector())){
263 errln("Did not get the expected int vector value for key emptyintv in bundle testtypes");
268 UResourceBundle sub = bundle.get("binarytest");
269 ByteBuffer got = sub.getBinary();
270 if(got.remaining()!=15){
271 errln("Did not get the expected length for the binary ByteBuffer");
273 for(int i=0; i< got.remaining(); i++){
276 errln("Did not get the expected value for binary buffer at index: "+i);
279 sub = bundle.get("emptybin");
280 got = sub.getBinary();
281 if(got.remaining()!=0){
282 errln("Did not get the expected length for the emptybin ByteBuffer");
287 UResourceBundle sub = bundle.get("emptyarray");
288 String key = sub.getKey();
289 if(!key.equals("emptyarray")){
290 errln("Did not get the expected key for emptytable item");
292 if(sub.getSize()!=0){
293 errln("Did not get the expected length for emptytable item");
297 UResourceBundle sub = bundle.get("menu");
298 String key = sub.getKey();
299 if(!key.equals("menu")){
300 errln("Did not get the expected key for menu item");
302 UResourceBundle sub1 = sub.get("file");
304 if(!key.equals("file")){
305 errln("Did not get the expected key for file item");
307 UResourceBundle sub2 = sub1.get("open");
309 if(!key.equals("open")){
310 errln("Did not get the expected key for file item");
312 String value = sub2.getString();
313 if(!value.equals("Open")){
314 errln("Did not get the expected value for key for oen item");
317 sub = bundle.get("emptytable");
319 if(!key.equals("emptytable")){
320 errln("Did not get the expected key for emptytable item");
322 if(sub.getSize()!=0){
323 errln("Did not get the expected length for emptytable item");
325 sub = bundle.get("menu").get("file");
326 int size = sub.getSize();
328 for(int i=0; i<size; i++){
344 String got = sub1.getKey();
345 if(!expected.equals(got)){
346 errln("Did not get the expected key at index"+i+". Expected: "+expected+" Got: "+got);
348 logln("Got the expected key at index: "+i);
354 private static final class TestCase{
357 TestCase(String key, int value){
362 public void TestTable32(){
363 TestCase[] arr = new TestCase[]{
364 new TestCase ( "ooooooooooooooooo", 0 ),
365 new TestCase ( "oooooooooooooooo1", 1 ),
366 new TestCase ( "ooooooooooooooo1o", 2 ),
367 new TestCase ( "oo11ooo1ooo11111o", 25150 ),
368 new TestCase ( "oo11ooo1ooo111111", 25151 ),
369 new TestCase ( "o1111111111111111", 65535 ),
370 new TestCase ( "1oooooooooooooooo", 65536 ),
371 new TestCase ( "1ooooooo11o11ooo1", 65969 ),
372 new TestCase ( "1ooooooo11o11oo1o", 65970 ),
373 new TestCase ( "1ooooooo111oo1111", 65999 )
375 UResourceBundle bundle = null;
377 bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testtable32", testLoader);
379 catch (MissingResourceException ex) {
380 warnln("could not load resource data: " + ex.getMessage());
384 if(bundle.getType()!= UResourceBundle.TABLE){
385 errln("Could not get the correct type for bundle testtable32");
387 int size =bundle.getSize();
389 errln("Could not get the correct size for bundle testtable32");
391 for(int i =0; i<size; i++){
392 UResourceBundle item = bundle.get(i);
393 String key = item.getKey();
394 int parsedNumber = parseTable32Key(key);
396 switch(item.getType()){
397 case UResourceBundle.STRING:
398 String value = item.getString();
399 number = UTF16.charAt(value,0);
401 case UResourceBundle.INT:
402 number = item.getInt();
405 errln("Got unexpected resource type in testtable32");
408 if(number!=parsedNumber){
409 errln("Did not get expected value in testtypes32 for key"+
410 key+". Expected: "+parsedNumber+" Got:"+number);
414 for(int i=0;i<arr.length; i++){
415 String expected = arr[i].key;
416 UResourceBundle item = bundle.get(expected);
418 String key = item.getKey();
419 int parsedNumber = parseTable32Key(key);
420 if(!key.equals(expected)){
421 errln("Did not get the expected key. Expected: "+expected+" Got:"+key);
423 switch(item.getType()){
424 case UResourceBundle.STRING:
425 String value = item.getString();
426 number = UTF16.charAt(value,0);
428 case UResourceBundle.INT:
429 number = item.getInt();
432 errln("Got unexpected resource type in testtable32");
435 if(number!=parsedNumber){
436 errln("Did not get expected value in testtypes32 for key"+
437 key+". Expected: "+parsedNumber+" Got:"+number);
441 private static int parseTable32Key(String key) {
446 for(int i=0; i<key.length(); i++){
456 public void TestAliases(){
458 String simpleAlias = "Open";
460 UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases", testLoader);
462 warnln("could not load testaliases data");
465 UResourceBundle sub = rb.get("simplealias");
466 String s1 = sub.getString("simplealias");
467 if(s1.equals(simpleAlias)){
468 logln("Alias mechanism works for simplealias");
470 errln("Did not get the expected output for simplealias");
474 rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
475 sub = rb.get("nonexisting");
476 errln("Did not get the expected exception for nonexisting");
477 }catch(MissingResourceException ex){
478 logln("Alias mechanism works for nonexisting alias");
482 rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
483 sub = rb.get("referencingalias");
484 s1 = sub.getString();
485 if(s1.equals("Hani")){
486 logln("Alias mechanism works for referencingalias");
488 errln("Did not get the expected output for referencingalias");
492 rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
493 sub = rb.get("boundaries");
494 String word = sub.getString("word");
496 if(word.equals("word_ja.brk")){
497 logln("Got the expected output for boundaries/word");
499 errln("Did not get the expected type for boundaries/word");
504 UResourceBundle rb1 = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
506 errln("Caching of the resource bundle failed");
508 logln("Caching of resource bundle passed");
510 sub = rb1.get("testGetStringByKeyAliasing" );
512 s1 = sub.get("KeyAlias0PST").getString();
513 if(s1.equals("America/Los_Angeles")){
514 logln("Alias mechanism works for KeyAlias0PST");
516 errln("Did not get the expected output for KeyAlias0PST");
519 s1 = sub.getString("KeyAlias1PacificStandardTime");
520 if(s1.equals("Pacific Standard Time")){
521 logln("Alias mechanism works for KeyAlias1PacificStandardTime");
523 errln("Did not get the expected output for KeyAlias1PacificStandardTime");
525 s1 = sub.getString("KeyAlias2PDT");
526 if(s1.equals("PDT")){
527 logln("Alias mechanism works for KeyAlias2PDT");
529 errln("Did not get the expected output for KeyAlias2PDT");
532 s1 = sub.getString("KeyAlias3LosAngeles");
533 if(s1.equals("Los Angeles")){
534 logln("Alias mechanism works for KeyAlias3LosAngeles. Got: "+s1);
536 errln("Did not get the expected output for KeyAlias3LosAngeles. Got: "+s1);
540 sub = rb.get("testGetStringByIndexAliasing" );
541 s1 = sub.getString(0);
542 if(s1.equals("America/Los_Angeles")){
543 logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: "+s1);
545 errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: "+s1);
547 s1 = sub.getString(1);
548 if(s1.equals("Pacific Standard Time")){
549 logln("Alias mechanism works for testGetStringByIndexAliasing/1");
551 errln("Did not get the expected output for testGetStringByIndexAliasing/1");
553 s1 = sub.getString(2);
554 if(s1.equals("PDT")){
555 logln("Alias mechanism works for testGetStringByIndexAliasing/2");
557 errln("Did not get the expected output for testGetStringByIndexAliasing/2");
560 s1 = sub.getString(3);
561 if(s1.equals("Los Angeles")){
562 logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: "+s1);
564 errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: "+s1);
568 sub = rb.get("testAliasToTree" );
570 ByteBuffer buf = sub.get("standard").get("%%CollationBin").getBinary();
572 errln("Did not get the expected output for %%CollationBin");
575 // should not get an exception
576 rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_RBNF_BASE_NAME,"fr_BE");
577 String str = rb.getString("SpelloutRules");
578 if(str !=null && str.length()>0){
579 logln("Alias mechanism works");
581 errln("Alias mechanism failed for fr_BE SpelloutRules");
583 rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW");
584 UResourceBundle b = (UResourceBundle) rb.getObject("collations");
586 if(b.get(0).getKey().equals( "default")){
587 logln("Alias mechanism works");
589 errln("Alias mechanism failed for zh_TW collations");
592 errln("Did not get the expected object for collations");
596 public void TestAlias(){
597 logln("Testing %%ALIAS");
598 UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"iw_IL");
599 UResourceBundle b = rb.get("NumberPatterns");
602 logln("%%ALIAS mechanism works");
604 errln("%%ALIAS mechanism failed for iw_IL collations");
607 errln("%%ALIAS mechanism failed for iw_IL");
610 public void TestXPathAlias(){
611 UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader);
612 UResourceBundle b = rb.get("aliasClient");
613 String result = b.getString();
614 String expResult= "correct";
616 if(!result.equals(expResult)){
617 errln("Did not get the expected result for XPath style alias");
620 UResourceBundle c = rb.get("rootAliasClient");
621 result = c.getString();
622 expResult = "correct";
623 if(!result.equals(expResult)){
624 errln("Did not get the expected result for XPath style alias for rootAliasClient");
626 }catch( MissingResourceException ex){
627 errln("Could not get rootAliasClient");
630 public void TestCircularAliases(){
631 // Aliases no longer supported
633 // UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
634 // UResourceBundle sub = rb.get("aaa");
635 // String s1 = sub.getString();
637 // errln("Did not get the expected exception");
639 // }catch(IllegalArgumentException ex){
640 // logln("got expected exception for circular references");
642 // catch (MissingResourceException ex) {
643 // warnln("could not load resource data: " + ex.getMessage());
647 public void TestGetWithFallback(){
649 UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");
650 String key = bundle.getStringWithFallback("Keys/collation");
651 if(!key.equals("COLLATION")){
652 errln("Did not get the expected result from getStringWithFallback method.");
654 String type = bundle.getStringWithFallback("Types/collation/direct");
655 if(!type.equals("DIRECT")){
656 errln("Did not get the expected result form getStringWithFallback method.");
659 ICUResourceBundle bundle = null;
662 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,ULocale.canonicalize("de__PHONEBOOK"));
664 if(!bundle.getULocale().equals("de")){
665 errln("did not get the expected bundle");
667 key = bundle.getStringWithFallback("collations/collation/default");
668 if(!key.equals("phonebook")){
669 errln("Did not get the expected result from getStringWithFallback method.");
672 }catch(MissingResourceException ex){
673 logln("got the expected exception");
677 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"fr_FR");
678 key = bundle.getStringWithFallback("collations/default");
679 if(!key.equals("standard")){
680 errln("Did not get the expected result from getStringWithFallback method.");
682 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"fr_FR");
683 ICUResourceBundle b1 = bundle.getWithFallback("calendar");
684 String defaultCal = b1.getStringWithFallback("default");
685 if(!defaultCal.equals("gregorian")){
686 errln("Did not get the expected default calendar string: Expected: gregorian, Got: "+defaultCal);
688 ICUResourceBundle b2 = b1.getWithFallback(defaultCal);
689 ICUResourceBundle b3 = b2.getWithFallback("monthNames");
690 String defaultContext = b3.getStringWithFallback("default");
691 ICUResourceBundle b4 = b3.getWithFallback(defaultContext);
692 String defaultWidth = b4.getStringWithFallback("default");
693 ICUResourceBundle b5 = b4.getWithFallback(defaultWidth);
694 if(b5.getSize()!=12){
695 errln("Did not get the expected size for the default monthNames");
699 private static final String COLLATION_RESNAME = "collations";
700 private static final String COLLATION_KEYWORD = "collation";
701 private static final String DEFAULT_NAME = "default";
702 private static final String STANDARD_NAME = "standard";
703 private static final String CALENDAR_RESNAME = "calendar";
704 private static final String CALENDAR_KEYWORD = "calendar";
706 public void TestKeywordValues(){
708 boolean foundStandard = false;
711 logln("Testing getting collation values:");
712 kwVals = ICUResourceBundle.getKeywordValues(ICUResourceBundle.ICU_COLLATION_BASE_NAME,COLLATION_RESNAME);
713 for(n=0;n<kwVals.length;n++) {
714 logln(new Integer(n).toString() + ": " + kwVals[n]);
715 if(DEFAULT_NAME.equals(kwVals[n])) {
716 errln("getKeywordValues for collation returned 'default' in the list.");
717 } else if(STANDARD_NAME.equals(kwVals[n])) {
718 if(foundStandard == false) {
719 foundStandard = true;
720 logln("found 'standard'");
722 errln("Error - 'standard' is in the keyword list twice!");
727 if(foundStandard == false) {
728 errln("Error - 'standard' was not in the collation tree as a keyword.");
730 logln("'standard' was found as a collation keyword.");
734 public void TestLocaleDisplayNames() {
735 ULocale[] locales = ULocale.getAvailableLocales();
736 for (int i = 0; i < locales.length; ++i) {
737 if (!hasLocalizedCountryFor(ULocale.ENGLISH, locales[i])){
738 errln("Could not get localized country for "+ locales[i]);
740 if(!hasLocalizedLanguageFor(ULocale.ENGLISH, locales[i])){
741 errln("Could not get localized language for "+ locales[i]);
743 if(!hasLocalizedCountryFor(locales[i], locales[i])){
744 errln("Could not get localized country for "+ locales[i]);
745 hasLocalizedCountryFor(locales[i], locales[i]);
747 if(!hasLocalizedLanguageFor(locales[i], locales[i])){
748 errln("Could not get localized language for "+ locales[i]);
751 logln(locales[i] + "\t" + locales[i].getDisplayName(ULocale.ENGLISH) + "\t" + locales[i].getDisplayName(locales[i]));
755 private static boolean hasLocalizedLanguageFor(ULocale locale, ULocale otherLocale) {
756 String lang = otherLocale.getLanguage();
757 String localizedVersion = otherLocale.getDisplayLanguage(locale);
758 return !lang.equals(localizedVersion);
761 private static boolean hasLocalizedCountryFor(ULocale locale, ULocale otherLocale) {
762 String country = otherLocale.getCountry();
763 if (country.equals("")) return true;
764 String localizedVersion = otherLocale.getDisplayCountry(locale);
765 return !country.equals(localizedVersion);
768 public void TestFunctionalEquivalent(){
769 String[] collCases = {
770 // avail locale equiv
771 "f", "de_US_CALIFORNIA", "de",
772 "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */
773 "t", "zh_Hant_TW@collation=stroke", "zh@collation=stroke",
774 "f", "de_CN@collation=pinyin", "de",
775 "t", "zh@collation=pinyin", "zh",
776 "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */
777 "t", "zh_Hans_CN@collation=pinyin", "zh",
778 "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */
779 "t", "zh_Hant_HK@collation=pinyin", "zh",
780 "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */
781 "t", "zh_Hant_HK@collation=stroke", "zh@collation=stroke",
782 "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */
783 "t", "zh_Hant_HK", "zh@collation=stroke",
784 "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */
785 "t", "zh_Hant_MO", "zh@collation=stroke",
786 "f", "zh_TW_STROKE", "zh@collation=stroke",
787 "f", "zh_TW_STROKE@collation=big5han", "zh@collation=big5han",
788 "f", "de_CN@calendar=japanese", "de",
789 "t", "de@calendar=japanese", "de",
790 "f", "zh_TW@collation=big5han", "zh@collation=big5han", /* alias of zh_Hant_TW */
791 "t", "zh_Hant_TW@collation=big5han", "zh@collation=big5han",
792 "f", "zh_TW@collation=gb2312han", "zh@collation=gb2312han", /* alias of zh_Hant_TW */
793 "t", "zh_Hant_TW@collation=gb2312han", "zh@collation=gb2312han",
794 "f", "zh_CN@collation=big5han", "zh@collation=big5han", /* alias of zh_Hans_CN */
795 "t", "zh_Hans_CN@collation=big5han", "zh@collation=big5han",
796 "f", "zh_CN@collation=gb2312han", "zh@collation=gb2312han", /* alias of zh_Hans_CN */
797 "t", "zh_Hans_CN@collation=gb2312han", "zh@collation=gb2312han",
798 "t", "zh@collation=big5han", "zh@collation=big5han",
799 "t", "zh@collation=gb2312han", "zh@collation=gb2312han",
800 "t", "hi_IN@collation=direct", "hi@collation=direct",
801 "t", "hi@collation=standard", "hi",
802 "t", "hi@collation=direct", "hi@collation=direct",
803 "f", "hi_AU@collation=direct;currency=CHF;calendar=buddhist", "hi@collation=direct",
804 "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi",
805 "t", "de_DE@collation=pinyin", "de", /* bug 4582 tests */
806 "f", "de_DE_BONN@collation=pinyin", "de",
808 "t", "nl_NL", "root",
809 "f", "nl_NL_EEXT", "root",
810 "t", "nl@collation=stroke", "root",
811 "t", "nl_NL@collation=stroke", "root",
812 "f", "nl_NL_EEXT@collation=stroke", "root",
815 String[] calCases = {
816 // avail locale equiv
817 "t", "en_US_POSIX", "en_US@calendar=gregorian",
818 "f", "ja_JP_TOKYO", "ja_JP@calendar=gregorian",
819 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
820 "t", "sr@calendar=gregorian", "sr@calendar=gregorian",
821 "t", "en", "en@calendar=gregorian",
822 "t", "th_TH", "th@calendar=buddhist",
823 "t", "th_TH@calendar=gregorian", "th_TH@calendar=gregorian",
824 "f", "th_TH_Bangkok", "th@calendar=buddhist",
827 logln("Testing functional equivalents for collation...");
828 getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
829 COLLATION_RESNAME, COLLATION_KEYWORD, true, collCases);
831 logln("Testing functional equivalents for calenadr...");
832 getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_BASE_NAME,
833 CALENDAR_RESNAME, CALENDAR_KEYWORD, false, calCases);
835 logln("Testing error conditions:");
837 ICUResourceBundle.getFunctionalEquivalent(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "calendar",
838 "calendar", new ULocale("ar_EG@calendar=islamic"), new boolean[1], true);
839 errln("Err: expected MissingResourceException");
840 } catch ( MissingResourceException t ) {
841 logln("expected MissingResourceException caught (PASS): " + t.toString());
845 private void getFunctionalEquivalentTestCases(String path, String resName, String keyword,
846 boolean truncate, String[] testCases) {
847 //String F_STR = "f";
849 boolean isAvail[] = new boolean[1];
851 logln("Testing functional equivalents...");
852 for(int i = 0; i < testCases.length ;i+=3) {
853 boolean expectAvail = T_STR.equals(testCases[i+0]);
854 ULocale inLocale = new ULocale(testCases[i+1]);
855 ULocale expectLocale = new ULocale(testCases[i+2]);
857 logln(new Integer(i/3).toString() + ": " + new Boolean(expectAvail).toString() + "\t\t" +
858 inLocale.toString() + "\t\t" + expectLocale.toString());
860 ULocale equivLocale = ICUResourceBundle.getFunctionalEquivalent(path, resName, keyword, inLocale, isAvail, truncate);
861 boolean gotAvail = isAvail[0];
863 if((gotAvail != expectAvail) || !equivLocale.equals(expectLocale)) {
864 errln(new Integer(i/3).toString() + ": Error, expected Equiv=" + new Boolean(expectAvail).toString() + "\t\t" +
865 inLocale.toString() + "\t\t--> " + expectLocale.toString() + ", but got " + new Boolean(gotAvail).toString() + " " +
866 equivLocale.toString());
871 public void TestNorwegian(){
873 UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "no_NO_NY");
874 UResourceBundle sub = rb.get("Countries");
875 String s1 = sub.getString("NO");
876 if(s1.equals("Noreg")){
877 logln("got expected output ");
879 errln("did not get the expected result");
881 }catch(IllegalArgumentException ex){
882 errln("Caught an unexpected expected");
885 public void TestJB4102(){
887 ICUResourceBundle root =(ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "root");
888 ICUResourceBundle t = null;
889 // AmPmMarkers now exist in root/islamic calendar, so this test is rendered useless.
891 // t = root.getWithFallback("calendar/islamic-civil/AmPmMarkers");
892 // errln("Second resource does not exist. How did it get here?\n");
893 // }catch(MissingResourceException ex){
894 // logln("Got the expected exception");
897 t = root.getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera");
898 errln("Second resource does not exist. How did it get here?\n");
899 }catch(MissingResourceException ex){
900 logln("Got the expected exception");
903 errln("t is not null!");
905 } catch (MissingResourceException e) {
906 warnln("Could not load the locale data: " + e.getMessage());
910 public void TestCLDRStyleAliases() {
911 String result = null;
912 String expected = null;
913 String[]expects = new String[] { "", "a41", "a12", "a03", "ar4" };
915 logln("Testing CLDR style aliases......\n");
917 UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED",testLoader);
918 ICUResourceBundle alias = (ICUResourceBundle)rb.get("a");
920 for(int i = 1; i < 5 ; i++) {
921 String resource="a"+i;
922 UResourceBundle a = (alias).getWithFallback(resource);
923 result = a.getString();
924 if(result.equals(expected)) {
925 errln("CLDR style aliases failed resource with name "+resource+"resource, exp "+expects[i] +" , got " + result);
930 private String getLSString(int status){
932 case ICUResourceBundle.FROM_FALLBACK:
933 return "FROM_FALLBACK";
934 case ICUResourceBundle.FROM_DEFAULT:
935 return "FROM_DEFAULT";
936 case ICUResourceBundle.FROM_ROOT:
938 case ICUResourceBundle.FROM_LOCALE:
939 return "FROM_LOCALE";
944 public void TestLoadingStatus(){
945 ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "yi_IL");
946 int status = bundle.getLoadingStatus();
947 if(status != ICUResourceBundle.FROM_DEFAULT){
948 errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_DEFAULT)
949 + " Got: " + getLSString(status));
951 bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "eo_DE");
952 status = bundle.getLoadingStatus();
953 if(status != ICUResourceBundle.FROM_FALLBACK){
954 errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK)
955 + " Got: " + getLSString(status));
958 logln("Test to verify loading status of get(String)");
959 bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "te_IN");
960 UResourceBundle countries = bundle.get("Countries");
961 status = ((ICUResourceBundle)countries).getLoadingStatus();
962 if(status != ICUResourceBundle.FROM_FALLBACK){
963 errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK)
964 + " Got: " + getLSString(status));
967 UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters");
968 status = auxExemplar.getLoadingStatus();
969 if(status != UResourceBundle.FROM_ROOT){
970 errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT)
971 + " Got: " + getLSString(status));
974 logln("Test to verify loading status of get(int)");
975 ICUResourceBundle ms = (ICUResourceBundle)bundle.get("MeasurementSystem");
976 status = ms.getLoadingStatus();
977 if(status != ICUResourceBundle.FROM_ROOT){
978 errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT)
979 + " Got: " + getLSString(status));
982 logln("Test to verify loading status of getwithFallback");
983 bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU",testLoader);
984 ICUResourceBundle temp = (ICUResourceBundle)bundle.getWithFallback("a/a2");
985 status = temp.getLoadingStatus();
986 if(status != ICUResourceBundle.FROM_LOCALE){
987 errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_LOCALE)
988 + " Got: " + getLSString(status));
990 temp = bundle.getWithFallback("a/a1");
991 status = temp.getLoadingStatus();
992 if(status != ICUResourceBundle.FROM_FALLBACK){
993 errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK)
994 + " Got: " + getLSString(status));
996 temp = bundle.getWithFallback("a/a4");
997 status = temp.getLoadingStatus();
998 if(status != ICUResourceBundle.FROM_ROOT){
999 errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT)
1000 + " Got: " + getLSString(status));
1003 public void TestCoverage(){
1004 UResourceBundle bundle;
1005 bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);
1006 if (bundle == null){
1007 errln("UResourceBundle.getBundleInstance(String baseName) failed");
1010 bundle = UResourceBundle.getBundleInstance(ULocale.getDefault());
1011 if (bundle == null){
1012 errln("UResourceBundle.getBundleInstance(ULocale) failed");
1015 if (new UResourceTypeMismatchException("coverage") == null){
1016 errln("Create UResourceTypeMismatchException error");
1018 class Stub extends UResourceBundle{
1019 public ULocale getULocale() {return ULocale.ROOT;}
1020 protected String getLocaleID() {return null;}
1021 protected String getBaseName() {return null;}
1022 protected UResourceBundle getParent() {return null;}
1023 protected void setLoadingStatus(int newStatus) {}
1024 public Enumeration getKeys() {return null;}
1025 protected Object handleGetObject(String aKey) {return null;}
1027 Stub stub = new Stub();
1029 if (!stub.getLocale().equals(ULocale.ROOT.toLocale())){
1030 errln("UResourceBundle.getLoclae(Locale) should delegate to (ULocale)");
1033 public void TestJavaULocaleBundleLoading(){
1034 String baseName="com.ibm.icu.dev.data.resources.TestDataElements";
1035 String locName = "en_Latn_US";
1036 UResourceBundle bundle = UResourceBundle.getBundleInstance(baseName, locName, testLoader);
1037 String fromRoot = bundle.getString("from_root");
1038 if(!fromRoot.equals("This data comes from root")){
1039 errln("Did not get the expected string for from_root");
1041 String fromEn = bundle.getString("from_en");
1042 if(!fromEn.equals("This data comes from en")){
1043 errln("Did not get the expected string for from_en");
1045 String fromEnLatn = bundle.getString("from_en_Latn");
1046 if(!fromEnLatn.equals("This data comes from en_Latn")){
1047 errln("Did not get the expected string for from_en_Latn");
1049 String fromEnLatnUs = bundle.getString("from_en_Latn_US");
1050 if(!fromEnLatnUs.equals("This data comes from en_Latn_US")){
1051 errln("Did not get the expected string for from_en_Latn_US");
1053 UResourceBundle bundle1 = UResourceBundle.getBundleInstance(baseName, new ULocale(locName), testLoader);
1054 if(!bundle1.equals(bundle)){
1055 errln("Did not get the expected bundle for "+baseName +"."+locName);
1057 if(bundle1!=bundle){
1058 errln("Did not load the bundle from cache");
1061 UResourceBundle bundle2 = UResourceBundle.getBundleInstance(baseName, "en_IN", testLoader);
1062 if(!bundle2.getLocale().toString().equals("en")){
1063 errln("Did not get the expected fallback locale. Expected: en Got: "+bundle2.getLocale().toString());
1065 UResourceBundle bundle3 = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader);
1066 if(!bundle3.getLocale().toString().equals("te")){
1067 errln("Did not get the expected fallback locale. Expected: te Got: "+bundle2.getLocale().toString());
1069 // non-existent bundle .. should return default
1070 UResourceBundle defaultBundle = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
1071 ULocale defaultLocale = ULocale.getDefault();
1072 if(!defaultBundle.getULocale().equals(defaultLocale)){
1073 errln("Did not get the default bundle for non-existent bundle");
1075 // non-existent bundle, non-existent default locale
1076 // so return the root bundle.
1077 ULocale.setDefault(ULocale.CANADA_FRENCH);
1078 UResourceBundle root = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
1079 if(!root.getULocale().toString().equals("")){
1080 errln("Did not get the root bundle for non-existent default bundle for non-existent bundle");
1083 ULocale.setDefault(defaultLocale);
1084 Enumeration keys = bundle.getKeys();
1086 while(keys.hasMoreElements()){
1087 logln("key: "+ keys.nextElement());
1091 errln("Did not get the expected number of keys: got " + i + ", expected 4");
1093 UResourceBundle bundle4 = UResourceBundle.getBundleInstance(baseName,"fr_Latn_FR", testLoader);
1095 errln("Could not load bundle fr_Latn_FR");
1098 public void TestAliasFallback(){
1100 ULocale loc = new ULocale("en_US");
1101 ICUResourceBundle b = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc);
1102 ICUResourceBundle b1 = (ICUResourceBundle)b.getWithFallback("calendar/hebrew/monthNames/format/abbreviated");
1104 logln("loaded data for abbreviated month names: "+ b1.getKey());
1106 }catch(MissingResourceException ex){
1107 warnln("Failed to load data for abbreviated month names");