1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /** 4 ******************************************************************************* 5 * Copyright (C) 2001-2016, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.dev.test.util; 10 11 import java.io.BufferedReader; 12 import java.io.InputStreamReader; 13 import java.net.JarURLConnection; 14 import java.net.URL; 15 import java.net.URLConnection; 16 import java.nio.ByteBuffer; 17 import java.util.Enumeration; 18 import java.util.HashSet; 19 import java.util.Locale; 20 import java.util.MissingResourceException; 21 import java.util.NoSuchElementException; 22 import java.util.Set; 23 import java.util.TreeSet; 24 import java.util.jar.JarEntry; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 import com.ibm.icu.dev.test.TestFmwk; 31 import com.ibm.icu.impl.ICUData; 32 import com.ibm.icu.impl.ICUResourceBundle; 33 import com.ibm.icu.impl.Utility; 34 import com.ibm.icu.text.BreakIterator; 35 import com.ibm.icu.text.UTF16; 36 import com.ibm.icu.util.Calendar; 37 import com.ibm.icu.util.Holiday; 38 import com.ibm.icu.util.ULocale; 39 import com.ibm.icu.util.UResourceBundle; 40 import com.ibm.icu.util.UResourceBundleIterator; 41 import com.ibm.icu.util.UResourceTypeMismatchException; 42 43 @RunWith(JUnit4.class) 44 public final class ICUResourceBundleTest extends TestFmwk { 45 private static final ClassLoader testLoader = ICUResourceBundleTest.class.getClassLoader(); 46 47 @Test 48 public void TestGetResources(){ 49 try{ 50 // It does not work well in eclipse plug-in test because of class loader configuration?? 51 // For now, specify resource path explicitly in this test case 52 //Enumeration en = testLoader.getResources("META-INF"); 53 Enumeration en = testLoader.getResources("com.ibm.icu.dev.data"); 54 for(;en.hasMoreElements();) { 55 URL url = (URL)en.nextElement(); 56 if (url == null) { 57 warnln("could not load resource data"); 58 return; 59 } 60 URLConnection c = url.openConnection(); 61 62 if (c instanceof JarURLConnection) { 63 JarURLConnection jc = (JarURLConnection)c; 64 JarEntry je = jc.getJarEntry(); 65 logln("jar entry: " + je.toString()); 66 } else { 67 BufferedReader br = new BufferedReader( 68 new InputStreamReader(c.getInputStream())); 69 logln("input stream:"); 70 try { 71 String line = null; 72 int n = 0; 73 while ((line = br.readLine()) != null) { 74 logln(" " + ++n + ": " + line); 75 } 76 } finally { 77 br.close(); 78 } 79 } 80 } 81 }catch(SecurityException ex) { 82 warnln("could not load resource data: " + ex); 83 ex.printStackTrace(); 84 }catch(NullPointerException ex) { 85 // thrown by ibm 1.4.2 windows jvm security manager 86 warnln("could not load resource data: " + ex); 87 }catch(Exception ex){ 88 ex.printStackTrace(); 89 errln("Unexpected exception: "+ ex); 90 } 91 } 92 @Test 93 public void TestResourceBundleWrapper(){ 94 UResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", "da_DK"); 95 Object o = bundle.getObject("holidays"); 96 if(o instanceof Holiday[] ){ 97 logln("wrapper mechanism works for Weekend data"); 98 }else{ 99 errln("Did not get the expected output for Weekend data"); 100 } 101 102 bundle = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "bogus"); 103 if(bundle instanceof UResourceBundle && bundle.getULocale().getName().equals("en_US")){ 104 logln("wrapper mechanism works for bogus locale"); 105 }else{ 106 errln("wrapper mechanism failed for bogus locale."); 107 } 108 109 try{ 110 bundle = UResourceBundle.getBundleInstance("bogus", "bogus"); 111 if(bundle!=null){ 112 errln("Did not get the expected exception"); 113 } 114 }catch(MissingResourceException ex){ 115 logln("got the expected exception"); 116 } 117 118 119 } 120 @Test 121 public void TestJB3879(){ 122 // this tests tests loading of root bundle when a resource bundle 123 // for the default locale is requested 124 try { 125 UResourceBundle bundle = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", ULocale.getDefault(), testLoader); 126 if(bundle==null){ 127 errln("could not create the resource bundle"); 128 } 129 } 130 catch (MissingResourceException ex) { 131 warnln("could not load test data: " + ex.getMessage()); 132 } 133 } 134 @Test 135 public void TestOpen(){ 136 UResourceBundle bundle = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "en_US_POSIX"); 137 138 if(bundle==null){ 139 errln("could not create the resource bundle"); 140 } 141 142 UResourceBundle obj = bundle.get("NumberElements").get("latn").get("patterns"); 143 144 int size = obj.getSize(); 145 int type = obj.getType(); 146 if(type == UResourceBundle.TABLE){ 147 UResourceBundle sub; 148 for(int i=0; i<size; i++) { 149 sub = obj.get(i); 150 String temp =sub.getString(); 151 if(temp.length()==0){ 152 errln("Failed to get the items from number patterns table in bundle: "+ 153 bundle.getULocale().getBaseName()); 154 } 155 //System.out.println("\""+prettify(temp)+"\""); 156 } 157 } 158 159 obj = bundle.get("NumberElements").get("latn").get("symbols"); 160 161 size = obj.getSize(); 162 type = obj.getType(); 163 if(type == UResourceBundle.TABLE){ 164 UResourceBundle sub; 165 for(int i=0; i<size; i++){ 166 sub = obj.get(i); 167 String temp =sub.getString(); 168 if(temp.length()==0){ 169 errln("Failed to get the items from number symbols table in bundle: "+ 170 bundle.getULocale().getBaseName()); 171 } 172 // System.out.println("\""+prettify(temp)+"\""); 173 } 174 } 175 176 if(bundle==null){ 177 errln("could not create the resource bundle"); 178 } 179 180 bundle = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "zzz_ZZ_very_very_very_long_bogus_bundle"); 181 if(!bundle.getULocale().equals(ULocale.getDefault())){ 182 errln("UResourceBundle did not load the default bundle when bundle was not found. Default: " + ULocale.getDefault() + 183 ", Bundle locale: " + bundle.getULocale()); 184 } 185 } 186 187 @Test 188 public void TestBasicTypes(){ 189 UResourceBundle bundle = null; 190 try { 191 bundle = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes", testLoader); 192 } 193 catch (MissingResourceException e) { 194 warnln("could not load test data: " + e.getMessage()); 195 return; 196 } 197 { 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"); 202 } 203 sub = bundle.get("emptyexplicitstring"); 204 expected =""; 205 if(!expected.equals(sub.getString())){ 206 errln("Did not get the expected string for key emptyexplicitstring in bundle testtypes"); 207 } 208 sub = bundle.get("emptystring"); 209 expected =""; 210 if(!expected.equals(sub.getString())){ 211 errln("Did not get the expected string for key emptystring in bundle testtypes"); 212 } 213 } 214 { 215 int expected = 123; 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"); 219 } 220 sub = bundle.get("emptyint"); 221 expected=0; 222 if(expected!=sub.getInt()){ 223 errln("Did not get the expected int value for key emptyint in bundle testtypes"); 224 } 225 } 226 { 227 int expected = 1; 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"); 231 } 232 } 233 { 234 int expected = -1; 235 UResourceBundle sub = bundle.get("minusone"); 236 int got = sub.getInt(); 237 if(expected!=got){ 238 errln("Did not get the expected int value for key minusone in bundle testtypes"); 239 } 240 expected = 0xFFFFFFF; 241 got = sub.getUInt(); 242 if(expected!=got){ 243 errln("Did not get the expected int value for key minusone in bundle testtypes"); 244 } 245 } 246 { 247 int expected = 1; 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"); 251 } 252 253 } 254 { 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"); 259 } 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"); 264 } 265 266 } 267 { 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"); 272 } 273 for(int i=0; i< got.remaining(); i++){ 274 byte b = got.get(); 275 if(b!=i){ 276 errln("Did not get the expected value for binary buffer at index: "+i); 277 } 278 } 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"); 283 } 284 285 } 286 { 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"); 291 } 292 if(sub.getSize()!=0){ 293 errln("Did not get the expected length for emptytable item"); 294 } 295 } 296 { 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"); 301 } 302 UResourceBundle sub1 = sub.get("file"); 303 key = sub1.getKey(); 304 if(!key.equals("file")){ 305 errln("Did not get the expected key for file item"); 306 } 307 UResourceBundle sub2 = sub1.get("open"); 308 key = sub2.getKey(); 309 if(!key.equals("open")){ 310 errln("Did not get the expected key for file item"); 311 } 312 String value = sub2.getString(); 313 if(!value.equals("Open")){ 314 errln("Did not get the expected value for key for oen item"); 315 } 316 317 sub = bundle.get("emptytable"); 318 key = sub.getKey(); 319 if(!key.equals("emptytable")){ 320 errln("Did not get the expected key for emptytable item"); 321 } 322 if(sub.getSize()!=0){ 323 errln("Did not get the expected length for emptytable item"); 324 } 325 sub = bundle.get("menu").get("file"); 326 int size = sub.getSize(); 327 String expected; 328 for(int i=0; i<size; i++){ 329 sub1 = sub.get(i); 330 331 switch(i){ 332 case 0: 333 expected = "exit"; 334 break; 335 case 1: 336 expected = "open"; 337 break; 338 case 2: 339 expected = "save"; 340 break; 341 default: 342 expected =""; 343 } 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); 347 }else{ 348 logln("Got the expected key at index: "+i); 349 } 350 } 351 } 352 353 } 354 private static final class TestCase{ 355 String key; 356 int value; 357 TestCase(String key, int value){ 358 this.key = key; 359 this.value = value; 360 } 361 } 362 @Test 363 public void TestTable32(){ 364 TestCase[] arr = new TestCase[]{ 365 new TestCase ( "ooooooooooooooooo", 0 ), 366 new TestCase ( "oooooooooooooooo1", 1 ), 367 new TestCase ( "ooooooooooooooo1o", 2 ), 368 new TestCase ( "oo11ooo1ooo11111o", 25150 ), 369 new TestCase ( "oo11ooo1ooo111111", 25151 ), 370 new TestCase ( "o1111111111111111", 65535 ), 371 new TestCase ( "1oooooooooooooooo", 65536 ), 372 new TestCase ( "1ooooooo11o11ooo1", 65969 ), 373 new TestCase ( "1ooooooo11o11oo1o", 65970 ), 374 new TestCase ( "1ooooooo111oo1111", 65999 ) 375 }; 376 UResourceBundle bundle = null; 377 try { 378 bundle = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testtable32", testLoader); 379 } 380 catch (MissingResourceException ex) { 381 warnln("could not load resource data: " + ex.getMessage()); 382 return; 383 } 384 385 if(bundle.getType()!= UResourceBundle.TABLE){ 386 errln("Could not get the correct type for bundle testtable32"); 387 } 388 389 int size =bundle.getSize(); 390 if(size!=66000){ 391 errln("Could not get the correct size for bundle testtable32"); 392 } 393 394 int number = -1; 395 396 // get the items by index 397 for(int i =0; i<size; i++){ 398 UResourceBundle item = bundle.get(i); 399 String key = item.getKey(); 400 int parsedNumber = parseTable32Key(key); 401 switch(item.getType()){ 402 case UResourceBundle.STRING: 403 String value = item.getString(); 404 number = UTF16.charAt(value,0); 405 break; 406 case UResourceBundle.INT: 407 number = item.getInt(); 408 break; 409 default: 410 errln("Got unexpected resource type in testtable32"); 411 } 412 if(number!=parsedNumber){ 413 errln("Did not get expected value in testtypes32 for key"+ 414 key+". Expected: "+parsedNumber+" Got:"+number); 415 } 416 417 } 418 419 // search for some items by key 420 for(int i=0;i<arr.length; i++){ 421 UResourceBundle item = bundle.get(arr[i].key); 422 switch(item.getType()){ 423 case UResourceBundle.STRING: 424 String value = item.getString(); 425 number = UTF16.charAt(value,0); 426 break; 427 case UResourceBundle.INT: 428 number = item.getInt(); 429 break; 430 default: 431 errln("Got unexpected resource type in testtable32"); 432 } 433 434 if(number != arr[i].value){ 435 errln("Did not get expected value in testtypes32 for key" + 436 arr[i].key +". Expected: " + arr[i].value + " Got:" + number); 437 } 438 } 439 } 440 private static int parseTable32Key(String key) { 441 int number; 442 char c; 443 444 number=0; 445 for(int i=0; i<key.length(); i++){ 446 c = key.charAt(i); 447 number<<=1; 448 if(c=='1') { 449 number|=1; 450 } 451 } 452 return number; 453 } 454 455 @Test 456 public void TestAliases(){ 457 String simpleAlias = "Open"; 458 459 UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases", testLoader); 460 if (rb == null) { 461 warnln("could not load testaliases data"); 462 return; 463 } 464 UResourceBundle sub = rb.get("simplealias"); 465 String s1 = sub.getString("simplealias"); 466 if(s1.equals(simpleAlias)){ 467 logln("Alias mechanism works for simplealias"); 468 }else{ 469 errln("Did not get the expected output for simplealias"); 470 } 471 { 472 try{ 473 rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader); 474 sub = rb.get("nonexisting"); 475 errln("Did not get the expected exception for nonexisting"); 476 }catch(MissingResourceException ex){ 477 logln("Alias mechanism works for nonexisting alias"); 478 } 479 } 480 { 481 rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader); 482 sub = rb.get("referencingalias"); 483 s1 = sub.getString(); 484 if(s1.equals("H:mm:ss")){ 485 logln("Alias mechanism works for referencingalias"); 486 }else{ 487 errln("Did not get the expected output for referencingalias"); 488 } 489 } 490 { 491 UResourceBundle rb1 = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader); 492 if(rb1!=rb){ 493 errln("Caching of the resource bundle failed"); 494 }else{ 495 logln("Caching of resource bundle passed"); 496 } 497 sub = rb1.get("testGetStringByKeyAliasing" ); 498 499 s1 = sub.get("KeyAlias0PST").getString(); 500 if(s1.equals("America/Los_Angeles")){ 501 logln("Alias mechanism works for KeyAlias0PST"); 502 }else{ 503 errln("Did not get the expected output for KeyAlias0PST"); 504 } 505 506 s1 = sub.getString("KeyAlias1PacificStandardTime"); 507 if(s1.equals("Pacific Standard Time")){ 508 logln("Alias mechanism works for KeyAlias1PacificStandardTime"); 509 }else{ 510 errln("Did not get the expected output for KeyAlias1PacificStandardTime"); 511 } 512 s1 = sub.getString("KeyAlias2PDT"); 513 if(s1.equals("PDT")){ 514 logln("Alias mechanism works for KeyAlias2PDT"); 515 }else{ 516 errln("Did not get the expected output for KeyAlias2PDT"); 517 } 518 519 s1 = sub.getString("KeyAlias3LosAngeles"); 520 if(s1.equals("Los Angeles")){ 521 logln("Alias mechanism works for KeyAlias3LosAngeles. Got: "+s1); 522 }else{ 523 errln("Did not get the expected output for KeyAlias3LosAngeles. Got: "+s1); 524 } 525 } 526 { 527 sub = rb.get("testGetStringByIndexAliasing" ); 528 s1 = sub.getString(0); 529 if(s1.equals("America/Los_Angeles")){ 530 logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: "+s1); 531 }else{ 532 errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: "+s1); 533 } 534 s1 = sub.getString(1); 535 if(s1.equals("Pacific Standard Time")){ 536 logln("Alias mechanism works for testGetStringByIndexAliasing/1"); 537 }else{ 538 errln("Did not get the expected output for testGetStringByIndexAliasing/1"); 539 } 540 s1 = sub.getString(2); 541 if(s1.equals("PDT")){ 542 logln("Alias mechanism works for testGetStringByIndexAliasing/2"); 543 }else{ 544 errln("Did not get the expected output for testGetStringByIndexAliasing/2"); 545 } 546 547 s1 = sub.getString(3); 548 if(s1.equals("Los Angeles")){ 549 logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: "+s1); 550 }else{ 551 errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: "+s1); 552 } 553 } 554 555 // Note: Following test cases are no longer working because collation data is now in the collation module 556 // { 557 // sub = rb.get("testAliasToTree" ); 558 // 559 // ByteBuffer buf = sub.get("standard").get("%%CollationBin").getBinary(); 560 // if(buf==null){ 561 // errln("Did not get the expected output for %%CollationBin"); 562 // } 563 // } 564 // 565 // rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW"); 566 // UResourceBundle b = (UResourceBundle) rb.getObject("collations"); 567 // if(b != null){ 568 // if(b.get(0).getKey().equals( "default")){ 569 // logln("Alias mechanism works"); 570 // }else{ 571 // errln("Alias mechanism failed for zh_TW collations"); 572 // } 573 // }else{ 574 // errln("Did not get the expected object for collations"); 575 // } 576 577 // Test case for #7996 578 { 579 UResourceBundle bundle = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te", testLoader); 580 UResourceBundle table = bundle.get("tableT7996"); 581 try { 582 String s = table.getString("a7996"); 583 logln("Alias in nested table referring one in sh worked - " + s); 584 } catch (MissingResourceException e) { 585 errln("Alias in nested table referring one in sh failed"); 586 } 587 588 try { 589 String s = ((ICUResourceBundle)table).getStringWithFallback("b7996"); 590 logln("Alias with /LOCALE/ in nested table in root referring back to another key in the current locale bundle worked - " + s); 591 } catch (MissingResourceException e) { 592 errln("Alias with /LOCALE/ in nested table in root referring back to another key in the current locale bundle failed"); 593 } 594 } 595 596 } 597 @Test 598 public void TestAlias(){ 599 logln("Testing %%ALIAS"); 600 UResourceBundle rb = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME,"iw_IL"); 601 UResourceBundle b = rb.get("NumberElements"); 602 if(b != null){ 603 if(b.getSize()>0){ 604 logln("%%ALIAS mechanism works"); 605 }else{ 606 errln("%%ALIAS mechanism failed for iw_IL NumberElements"); 607 } 608 }else{ 609 errln("%%ALIAS mechanism failed for iw_IL"); 610 } 611 } 612 @Test 613 public void TestXPathAlias(){ 614 UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader); 615 UResourceBundle b = rb.get("aliasClient"); 616 String result = b.getString(); 617 String expResult= "correct"; 618 619 if(!result.equals(expResult)){ 620 errln("Did not get the expected result for XPath style alias"); 621 } 622 try{ 623 UResourceBundle c = rb.get("rootAliasClient"); 624 result = c.getString(); 625 expResult = "correct"; 626 if(!result.equals(expResult)){ 627 errln("Did not get the expected result for XPath style alias for rootAliasClient"); 628 } 629 }catch( MissingResourceException ex){ 630 errln("Could not get rootAliasClient"); 631 } 632 } 633 @Test 634 public void TestCircularAliases(){ 635 try{ 636 UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader); 637 UResourceBundle sub = rb.get("aaa"); 638 String s1 = sub.getString(); 639 if(s1!=null){ 640 errln("Did not get the expected exception"); 641 } 642 }catch(IllegalArgumentException ex){ 643 logln("got expected exception for circular references"); 644 } 645 catch (MissingResourceException ex) { 646 warnln("could not load resource data: " + ex.getMessage()); 647 } 648 } 649 650 @Test 651 public void TestPreventFallback() { 652 String noFallbackResource = "string_in_te_no_te_IN_fallback"; 653 ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN_NE",testLoader); 654 try { 655 rb.getStringWithFallback(noFallbackResource); 656 fail("Expected MissingResourceException."); 657 } catch (MissingResourceException e) { 658 // Expected 659 } 660 rb.getStringWithFallback("string_only_in_te"); 661 rb = (ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te",testLoader); 662 rb.getStringWithFallback(noFallbackResource); 663 } 664 665 @Test 666 public void TestGetWithFallback(){ 667 /* 668 UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN"); 669 String key = bundle.getStringWithFallback("Keys/collation"); 670 if(!key.equals("COLLATION")){ 671 errln("Did not get the expected result from getStringWithFallback method."); 672 } 673 String type = bundle.getStringWithFallback("Types/collation/direct"); 674 if(!type.equals("DIRECT")){ 675 errln("Did not get the expected result form getStringWithFallback method."); 676 } 677 */ 678 ICUResourceBundle bundle = null; 679 680 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME,"fr_FR"); 681 ICUResourceBundle b1 = bundle.getWithFallback("calendar"); 682 String defaultCal = b1.getStringWithFallback("default"); 683 if(!defaultCal.equals("gregorian")){ 684 errln("Did not get the expected default calendar string: Expected: gregorian, Got: "+defaultCal); 685 } 686 ICUResourceBundle b2 = b1.getWithFallback(defaultCal); 687 ICUResourceBundle b3 = b2.getWithFallback("monthNames"); 688 ICUResourceBundle b4 = b3.getWithFallback("format"); 689 ICUResourceBundle b5 = b4.getWithFallback("narrow"); 690 if(b5.getSize()!=12){ 691 errln("Did not get the expected size for the monthNames"); 692 } 693 } 694 695 private static final String CALENDAR_RESNAME = "calendar"; 696 private static final String CALENDAR_KEYWORD = "calendar"; 697 698 @Test 699 public void TestLocaleDisplayNames() { 700 ULocale[] locales = ULocale.getAvailableLocales(); 701 702 Set<String> localCountryExceptions = new HashSet<String>(); 703 if (logKnownIssue("cldrbug:8903", 704 "No localized region name for lrc_IQ, lrc_IR, nus_SS, nds_DE, ti_ER, ti_ET")) { 705 localCountryExceptions.add("lrc_IQ"); 706 localCountryExceptions.add("lrc_IR"); 707 localCountryExceptions.add("nus_SS"); 708 localCountryExceptions.add("nds_DE"); 709 localCountryExceptions.add("nds_NL"); 710 localCountryExceptions.add("ti_ER"); 711 localCountryExceptions.add("ti_ET"); 712 } 713 714 Set<String> localLangExceptions = new HashSet<String>(); 715 if (logKnownIssue("cldrbug:8903", "No localized language name for nmg, nds")) { 716 localLangExceptions.add("nmg"); 717 localLangExceptions.add("nds"); 718 } 719 720 for (int i = 0; i < locales.length; ++i) { 721 if (!hasLocalizedCountryFor(ULocale.ENGLISH, locales[i]) 722 && (locales[i].getLanguage().compareTo("ti") != 0) // TODO: restore test for ti_* when cldrbug 3058 is fixed 723 // Android patch begin. 724 && (locales[i].getBaseName().compareTo("en_XA") != 0) 725 && (locales[i].getBaseName().compareTo("ar_XB") != 0)){ 726 // Android patch end. 727 errln("Could not get English localized country for " + locales[i]); 728 } 729 if(!hasLocalizedLanguageFor(ULocale.ENGLISH, locales[i])){ 730 errln("Could not get English localized language for " + locales[i]); 731 } 732 733 if(!hasLocalizedCountryFor(locales[i], locales[i]) 734 && !localCountryExceptions.contains(locales[i].toString())) { 735 errln("Could not get native localized country for " + locales[i]); 736 hasLocalizedCountryFor(locales[i], locales[i]); 737 } 738 if(!hasLocalizedLanguageFor(locales[i], locales[i]) 739 && !localLangExceptions.contains(locales[i].getLanguage())) { 740 errln("Could not get native localized language for " + locales[i]); 741 } 742 743 logln(locales[i] + "\t" + locales[i].getDisplayName(ULocale.ENGLISH) + "\t" + locales[i].getDisplayName(locales[i])); 744 } 745 } 746 747 private static boolean hasLocalizedLanguageFor(ULocale locale, ULocale otherLocale) { 748 String lang = otherLocale.getLanguage(); 749 String localizedVersion = otherLocale.getDisplayLanguage(locale); 750 return !lang.equals(localizedVersion); 751 } 752 753 private static boolean hasLocalizedCountryFor(ULocale locale, ULocale otherLocale) { 754 String country = otherLocale.getCountry(); 755 if (country.equals("")) return true; 756 String localizedVersion = otherLocale.getDisplayCountry(locale); 757 return !country.equals(localizedVersion); 758 } 759 760 @Test 761 public void TestFunctionalEquivalent(){ 762 // Android patch: Force default Gregorian calendar. 763 String[] calCases = { 764 // avail locale equiv 765 "t", "en_US_POSIX", "en@calendar=gregorian", 766 "f", "ja_JP_TOKYO", "ja@calendar=gregorian", 767 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese", 768 "t", "sr@calendar=gregorian", "sr@calendar=gregorian", 769 "t", "en", "en@calendar=gregorian", 770 "t", "th_TH", "th@calendar=gregorian", 771 "t", "th_TH@calendar=gregorian", "th@calendar=gregorian", 772 "f", "th_TH_Bangkok", "th@calendar=gregorian", 773 }; 774 // Android patch end. 775 776 logln("Testing functional equivalents for calendar..."); 777 getFunctionalEquivalentTestCases(ICUData.ICU_BASE_NAME, 778 Calendar.class.getClassLoader(), 779 CALENDAR_RESNAME, CALENDAR_KEYWORD, false, calCases); 780 781 logln("Testing error conditions:"); 782 try { 783 ClassLoader cl = BreakIterator.class.getClassLoader(); 784 ICUResourceBundle.getFunctionalEquivalent(ICUData.ICU_BRKITR_BASE_NAME, cl, "calendar", 785 "calendar", new ULocale("ar_EG@calendar=islamic"), new boolean[1], true); 786 errln("Err: expected MissingResourceException"); 787 } catch ( MissingResourceException t ) { 788 logln("expected MissingResourceException caught (PASS): " + t.toString()); 789 } 790 } 791 792 private void getFunctionalEquivalentTestCases(String path, ClassLoader cl, String resName, String keyword, 793 boolean truncate, String[] testCases) { 794 //String F_STR = "f"; 795 String T_STR = "t"; 796 boolean isAvail[] = new boolean[1]; 797 798 logln("Testing functional equivalents..."); 799 for(int i = 0; i < testCases.length ;i+=3) { 800 boolean expectAvail = T_STR.equals(testCases[i+0]); 801 ULocale inLocale = new ULocale(testCases[i+1]); 802 ULocale expectLocale = new ULocale(testCases[i+2]); 803 804 logln(new Integer(i/3).toString() + ": " + new Boolean(expectAvail).toString() + "\t\t" + 805 inLocale.toString() + "\t\t" + expectLocale.toString()); 806 807 ULocale equivLocale = ICUResourceBundle.getFunctionalEquivalent(path, cl, resName, keyword, inLocale, isAvail, truncate); 808 boolean gotAvail = isAvail[0]; 809 810 if((gotAvail != expectAvail) || !equivLocale.equals(expectLocale)) { 811 errln(new Integer(i/3).toString() + ": Error, expected Equiv=" + new Boolean(expectAvail).toString() + "\t\t" + 812 inLocale.toString() + "\t\t--> " + expectLocale.toString() + ", but got " + new Boolean(gotAvail).toString() + " " + 813 equivLocale.toString()); 814 } 815 } 816 } 817 818 @Test 819 public void TestNorwegian(){ 820 try{ 821 UResourceBundle rb = UResourceBundle.getBundleInstance(ICUData.ICU_REGION_BASE_NAME, "no_NO_NY"); 822 UResourceBundle sub = rb.get("Countries"); 823 String s1 = sub.getString("NO"); 824 if(s1.equals("Noreg")){ 825 logln("got expected output "); 826 }else{ 827 errln("did not get the expected result"); 828 } 829 }catch(IllegalArgumentException ex){ 830 errln("Caught an unexpected expected"); 831 } 832 } 833 @Test 834 public void TestJB4102(){ 835 try { 836 ICUResourceBundle root =(ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "root"); 837 ICUResourceBundle t = null; 838 // AmPmMarkers now exist in root/islamic calendar, so this test is rendered useless. 839 // try{ 840 // t = root.getWithFallback("calendar/islamic-civil/AmPmMarkers"); 841 // errln("Second resource does not exist. How did it get here?\n"); 842 // }catch(MissingResourceException ex){ 843 // logln("Got the expected exception"); 844 // } 845 try{ 846 t = root.getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera"); 847 errln("Second resource does not exist. How did it get here?\n"); 848 }catch(MissingResourceException ex){ 849 logln("Got the expected exception"); 850 } 851 if(t!=null){ 852 errln("t is not null!"); 853 } 854 } catch (MissingResourceException e) { 855 warnln("Could not load the locale data: " + e.getMessage()); 856 } 857 } 858 859 @Test 860 public void TestCLDRStyleAliases() { 861 String result = null; 862 String expected = null; 863 String[]expects = new String[] { "", "a41", "a12", "a03", "ar4" }; 864 865 logln("Testing CLDR style aliases......\n"); 866 867 UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED", testLoader); 868 ICUResourceBundle alias = (ICUResourceBundle)rb.get("a"); 869 870 for(int i = 1; i < 5 ; i++) { 871 String resource="a"+i; 872 UResourceBundle a = (alias).getWithFallback(resource); 873 result = a.getString(); 874 if(result.equals(expected)) { 875 errln("CLDR style aliases failed resource with name "+resource+"resource, exp "+expects[i] +" , got " + result); 876 } 877 } 878 879 } 880 881 @Test 882 public void TestCoverage(){ 883 UResourceBundle bundle; 884 bundle = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME); 885 if (bundle == null){ 886 errln("UResourceBundle.getBundleInstance(String baseName) failed"); 887 } 888 bundle = null; 889 bundle = UResourceBundle.getBundleInstance(ULocale.getDefault()); 890 if (bundle == null){ 891 errln("UResourceBundle.getBundleInstance(ULocale) failed"); 892 return; 893 } 894 if (new UResourceTypeMismatchException("coverage") == null){ 895 errln("Create UResourceTypeMismatchException error"); 896 } 897 class Stub extends UResourceBundle{ 898 @Override 899 public ULocale getULocale() {return ULocale.ROOT;} 900 @Override 901 protected String getLocaleID() {return null;} 902 @Override 903 protected String getBaseName() {return null;} 904 @Override 905 protected UResourceBundle getParent() {return null;} 906 @Override 907 public Enumeration getKeys() {return null;} 908 @Override 909 protected Object handleGetObject(String aKey) {return null;} 910 } 911 Stub stub = new Stub(); 912 913 if (!stub.getLocale().equals(ULocale.ROOT.toLocale())){ 914 errln("UResourceBundle.getLoclae(Locale) should delegate to (ULocale)"); 915 } 916 } 917 @Test 918 public void TestJavaULocaleBundleLoading(){ 919 String baseName="com.ibm.icu.dev.data.resources.TestDataElements"; 920 String locName = "en_Latn_US"; 921 UResourceBundle bundle = UResourceBundle.getBundleInstance(baseName, locName, testLoader); 922 String fromRoot = bundle.getString("from_root"); 923 if(!fromRoot.equals("This data comes from root")){ 924 errln("Did not get the expected string for from_root"); 925 } 926 String fromEn = bundle.getString("from_en"); 927 if(!fromEn.equals("This data comes from en")){ 928 errln("Did not get the expected string for from_en"); 929 } 930 String fromEnLatn = bundle.getString("from_en_Latn"); 931 if(!fromEnLatn.equals("This data comes from en_Latn")){ 932 errln("Did not get the expected string for from_en_Latn"); 933 } 934 String fromEnLatnUs = bundle.getString("from_en_Latn_US"); 935 if(!fromEnLatnUs.equals("This data comes from en_Latn_US")){ 936 errln("Did not get the expected string for from_en_Latn_US"); 937 } 938 UResourceBundle bundle1 = UResourceBundle.getBundleInstance(baseName, new ULocale(locName), testLoader); 939 if(!bundle1.equals(bundle)){ 940 errln("Did not get the expected bundle for "+baseName +"."+locName); 941 } 942 if(bundle1!=bundle){ 943 errln("Did not load the bundle from cache"); 944 } 945 946 UResourceBundle bundle2 = UResourceBundle.getBundleInstance(baseName, "en_IN", testLoader); 947 if(!bundle2.getLocale().toString().equals("en")){ 948 errln("Did not get the expected fallback locale. Expected: en Got: "+bundle2.getLocale().toString()); 949 } 950 UResourceBundle bundle3 = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader); 951 if(!bundle3.getLocale().toString().equals("te")){ 952 errln("Did not get the expected fallback locale. Expected: te Got: "+bundle2.getLocale().toString()); 953 } 954 // non-existent bundle .. should return default 955 UResourceBundle defaultBundle = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader); 956 ULocale defaultLocale = ULocale.getDefault(); 957 if(!defaultBundle.getULocale().equals(defaultLocale)){ 958 errln("Did not get the default bundle for non-existent bundle"); 959 } 960 // non-existent bundle, non-existent default locale 961 // so return the root bundle. 962 ULocale.setDefault(ULocale.CANADA_FRENCH); 963 UResourceBundle root = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader); 964 if(!root.getULocale().toString().equals("")){ 965 errln("Did not get the root bundle for non-existent default bundle for non-existent bundle"); 966 } 967 //reset the default 968 ULocale.setDefault(defaultLocale); 969 Enumeration keys = bundle.getKeys(); 970 int i=0; 971 while(keys.hasMoreElements()){ 972 logln("key: "+ keys.nextElement()); 973 i++; 974 } 975 if(i!=4){ 976 errln("Did not get the expected number of keys: got " + i + ", expected 4"); 977 } 978 UResourceBundle bundle4 = UResourceBundle.getBundleInstance(baseName, "fr_Latn_FR", testLoader); 979 if(bundle4==null){ 980 errln("Could not load bundle fr_Latn_FR"); 981 } 982 } 983 @Test 984 public void TestAliasFallback(){ 985 try{ 986 ULocale loc = new ULocale("en_US"); 987 ICUResourceBundle b = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, loc); 988 ICUResourceBundle b1 = b.getWithFallback("calendar/hebrew/monthNames/format/abbreviated"); 989 if(b1!=null){ 990 logln("loaded data for abbreviated month names: "+ b1.getKey()); 991 } 992 }catch(MissingResourceException ex){ 993 warnln("Failed to load data for abbreviated month names"); 994 } 995 } 996 private Set<String> setFromEnumeration(Enumeration<String> e) { 997 TreeSet<String> set = new TreeSet<String>(); 998 while (e.hasMoreElements()) { 999 set.add(e.nextElement()); 1000 } 1001 return set; 1002 } 1003 /** 1004 * Test ICUResourceBundle.getKeys() for a whole bundle (top-level resource). 1005 * JDK JavaDoc for ResourceBundle.getKeys() says that it returns 1006 * "an Enumeration of the keys contained in this ResourceBundle and its parent bundles." 1007 */ 1008 @Test 1009 public void TestICUGetKeysAtTopLevel() { 1010 String baseName="com/ibm/icu/dev/data/testdata"; 1011 UResourceBundle te_IN = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader); 1012 UResourceBundle te = UResourceBundle.getBundleInstance(baseName, "te", testLoader); 1013 Set<String> te_set = setFromEnumeration(te.getKeys()); 1014 Set<String> te_IN_set = setFromEnumeration(te_IN.getKeys()); 1015 assertTrue("te.getKeys().contains(string_only_in_Root)", te_set.contains("string_only_in_Root")); 1016 assertTrue("te.getKeys().contains(string_only_in_te)", te_set.contains("string_only_in_te")); 1017 assertFalse("te.getKeys().contains(string_only_in_te_IN)", te_set.contains("string_only_in_te_IN")); 1018 assertTrue("te_IN.getKeys().contains(string_only_in_Root)", te_IN_set.contains("string_only_in_Root")); 1019 assertTrue("te_IN.getKeys().contains(string_only_in_te)", te_IN_set.contains("string_only_in_te")); 1020 assertTrue("te_IN.getKeys().contains(string_only_in_te_IN)", te_IN_set.contains("string_only_in_te_IN")); 1021 // TODO: Check for keys of alias resource items 1022 } 1023 /** 1024 * Test ICUResourceBundle.getKeys() for a resource item (not a whole bundle/top-level resource). 1025 * This does not take parent bundles into account. 1026 */ 1027 @Test 1028 public void TestICUGetKeysForResourceItem() { 1029 String baseName="com/ibm/icu/dev/data/testdata"; 1030 UResourceBundle te = UResourceBundle.getBundleInstance(baseName, "te", testLoader); 1031 UResourceBundle tagged_array_in_Root_te = te.get("tagged_array_in_Root_te"); 1032 Set<String> keys = setFromEnumeration(tagged_array_in_Root_te.getKeys()); 1033 assertTrue("tagged_array_in_Root_te.getKeys().contains(tag0)", keys.contains("tag0")); 1034 assertTrue("tagged_array_in_Root_te.getKeys().contains(tag1)", keys.contains("tag1")); 1035 assertFalse("tagged_array_in_Root_te.getKeys().contains(tag7)", keys.contains("tag7")); 1036 assertFalse("tagged_array_in_Root_te.getKeys().contains(tag12)", keys.contains("tag12")); 1037 UResourceBundle array_in_Root_te = te.get("array_in_Root_te"); 1038 assertFalse("array_in_Root_te.getKeys().hasMoreElements()", array_in_Root_te.getKeys().hasMoreElements()); 1039 UResourceBundle string_in_Root_te = te.get("string_in_Root_te"); 1040 assertFalse("string_in_Root_te.getKeys().hasMoreElements()", string_in_Root_te.getKeys().hasMoreElements()); 1041 } 1042 1043 /* 1044 * UResouceBundle should be able to load a resource bundle even if 1045 * a similarly named class (only case differences) exists in the 1046 * same package. See Ticket#6844 1047 */ 1048 @Test 1049 public void TestT6844() { 1050 try { 1051 UResourceBundle rb1 1052 = UResourceBundle.getBundleInstance("com.ibm.icu.dev.data.resources.TestMessages", ULocale.getDefault(), testLoader); 1053 assertEquals("bundleContainer in TestMessages", "TestMessages.class", rb1.getString("bundleContainer")); 1054 1055 UResourceBundle rb2 1056 = UResourceBundle.getBundleInstance("com.ibm.icu.dev.data.resources.testmessages", ULocale.getDefault(), testLoader); 1057 assertEquals("bundleContainer in testmessages", "testmessages.properties", rb2.getString("bundleContainer")); 1058 } catch (Throwable t) { 1059 errln(t.getMessage()); 1060 } 1061 } 1062 1063 @Test 1064 public void TestUResourceBundleCoverage() { 1065 Locale locale = null; 1066 ULocale ulocale = null; 1067 String baseName = null; 1068 UResourceBundle rb1, rb2, rb3, rb4, rb5, rb6, rb7; 1069 1070 rb1 = UResourceBundle.getBundleInstance(ulocale); 1071 rb2 = UResourceBundle.getBundleInstance(baseName); 1072 rb3 = UResourceBundle.getBundleInstance(baseName, ulocale); 1073 rb4 = UResourceBundle.getBundleInstance(baseName, locale); 1074 1075 rb5 = UResourceBundle.getBundleInstance(baseName, ulocale, testLoader); 1076 rb6 = UResourceBundle.getBundleInstance(baseName, locale, testLoader); 1077 try { 1078 rb7 = UResourceBundle.getBundleInstance("bogus", Locale.getDefault(), testLoader); 1079 errln("Should have thrown exception with bogus baseName."); 1080 } catch (java.util.MissingResourceException ex) { 1081 } 1082 if (rb1 == null || rb2 == null || rb3 == null || rb4 == null || rb5 == null || rb6 == null) { 1083 errln("Error getting resource bundle."); 1084 } 1085 1086 rb7 = UResourceBundle.getBundleInstance("com.ibm.icu.dev.data.resources.TestDataElements", Locale.getDefault(), testLoader); 1087 1088 try { 1089 rb1.getBinary(); 1090 errln("getBinary() call should have thrown UResourceTypeMismatchException."); 1091 } catch (UResourceTypeMismatchException ex) { 1092 } 1093 try { 1094 rb1.getStringArray(); 1095 errln("getStringArray() call should have thrown UResourceTypeMismatchException."); 1096 } catch (UResourceTypeMismatchException ex) { 1097 } 1098 try { 1099 byte [] ba = { 0x00 }; 1100 rb1.getBinary(ba); 1101 errln("getBinary(byte[]) call should have thrown UResourceTypeMismatchException."); 1102 } catch (UResourceTypeMismatchException ex) { 1103 } 1104 try { 1105 rb1.getInt(); 1106 errln("getInt() call should have thrown UResourceTypeMismatchException."); 1107 } catch (UResourceTypeMismatchException ex) { 1108 } 1109 try { 1110 rb1.getIntVector(); 1111 errln("getIntVector() call should have thrown UResourceTypeMismatchException."); 1112 } catch (UResourceTypeMismatchException ex) { 1113 } 1114 try { 1115 rb1.getUInt(); 1116 errln("getUInt() call should have thrown UResourceTypeMismatchException."); 1117 } catch (UResourceTypeMismatchException ex) { 1118 } 1119 if (rb1.getVersion() != null) { 1120 errln("getVersion() call should have returned null."); 1121 } 1122 if (rb7.getType() != UResourceBundle.NONE) { 1123 errln("getType() call should have returned NONE."); 1124 } 1125 if (rb7.getKey() != null) { 1126 errln("getKey() call should have returned null."); 1127 } 1128 if (((ICUResourceBundle)rb1).findTopLevel(0) == null) { 1129 errln("Error calling findTopLevel()."); 1130 } 1131 if (ICUResourceBundle.getFullLocaleNameSet() == null) { 1132 errln("Error calling getFullLocaleNameSet()."); 1133 } 1134 UResourceBundleIterator itr = rb1.getIterator(); 1135 while (itr.hasNext()) { 1136 itr.next(); 1137 } 1138 try { 1139 itr.next(); 1140 errln("NoSuchElementException exception should have been thrown."); 1141 } catch (NoSuchElementException ex) { 1142 } 1143 try { 1144 itr.nextString(); 1145 errln("NoSuchElementException exception should have been thrown."); 1146 } catch (NoSuchElementException ex) { 1147 } 1148 } 1149 } 1150