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