2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\r
9 import java.io.BufferedReader;
\r
10 import java.io.InputStream;
\r
11 import java.io.InputStreamReader;
\r
12 import java.net.JarURLConnection;
\r
13 import java.net.URL;
\r
14 import java.net.URLConnection;
\r
15 import java.nio.ByteBuffer;
\r
16 import java.util.Enumeration;
\r
17 import java.util.MissingResourceException;
\r
18 import java.util.Set;
\r
19 import java.util.TreeSet;
\r
20 import java.util.jar.JarEntry;
\r
22 import com.ibm.icu.dev.test.TestFmwk;
\r
23 import com.ibm.icu.impl.ICUResourceBundle;
\r
24 import com.ibm.icu.impl.Utility;
\r
25 import com.ibm.icu.text.BreakIterator;
\r
26 import com.ibm.icu.text.UTF16;
\r
27 import com.ibm.icu.util.Calendar;
\r
28 import com.ibm.icu.util.Holiday;
\r
29 import com.ibm.icu.util.ULocale;
\r
30 import com.ibm.icu.util.UResourceBundle;
\r
31 import com.ibm.icu.util.UResourceTypeMismatchException;
\r
34 public final class ICUResourceBundleTest extends TestFmwk {
\r
35 private static final ClassLoader testLoader = ICUResourceBundleTest.class.getClassLoader();
\r
37 public static void main(String args[]) throws Exception {
\r
38 ICUResourceBundleTest test = new ICUResourceBundleTest();
\r
42 public void TestGetResources(){
\r
44 // It does not work well in eclipse plug-in test because of class loader configuration??
\r
45 // For now, specify resource path explicitly in this test case
\r
46 //Enumeration en = testLoader.getResources("META-INF");
\r
47 Enumeration en = testLoader.getResources("com.ibm.icu.dev.data");
\r
48 for(;en.hasMoreElements();) {
\r
49 URL url = (URL)en.nextElement();
\r
51 warnln("could not load resource data");
\r
54 URLConnection c = url.openConnection();
\r
56 if (c instanceof JarURLConnection) {
\r
57 JarURLConnection jc = (JarURLConnection)c;
\r
58 JarEntry je = jc.getJarEntry();
\r
59 logln("jar entry: " + je.toString());
\r
61 InputStream is = c.getInputStream();
\r
62 logln("input stream:");
\r
63 InputStreamReader r = new InputStreamReader(is);
\r
64 BufferedReader br = new BufferedReader(r);
\r
67 while ((line = br.readLine()) != null) {
\r
68 logln(" " + ++n + ": " + line);
\r
72 }catch(SecurityException ex) {
\r
73 warnln("could not load resource data: " + ex);
\r
74 ex.printStackTrace();
\r
75 }catch(NullPointerException ex) {
\r
76 // thrown by ibm 1.4.2 windows jvm security manager
\r
77 warnln("could not load resource data: " + ex);
\r
78 }catch(Exception ex){
\r
79 ex.printStackTrace();
\r
80 errln("Unexpected exception: "+ ex);
\r
83 public void TestResourceBundleWrapper(){
\r
84 UResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", "da_DK");
\r
85 Object o = bundle.getObject("holidays");
\r
86 if(o instanceof Holiday[] ){
\r
87 logln("wrapper mechanism works for Weekend data");
\r
89 errln("Did not get the expected output for Weekend data");
\r
92 bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "bogus");
\r
93 if(bundle instanceof UResourceBundle && bundle.getULocale().equals("en_US")){
\r
94 logln("wrapper mechanism works for bogus locale");
\r
96 errln("wrapper mechanism failed for bogus locale.");
\r
100 bundle = UResourceBundle.getBundleInstance("bogus", "bogus");
\r
102 errln("Did not get the expected exception");
\r
104 }catch(MissingResourceException ex){
\r
105 logln("got the expected exception");
\r
110 public void TestJB3879(){
\r
111 // this tests tests loading of root bundle when a resource bundle
\r
112 // for the default locale is requested
\r
114 UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", ULocale.getDefault().toString(), testLoader);
\r
116 errln("could not create the resource bundle");
\r
119 catch (MissingResourceException ex) {
\r
120 warnln("could not load test data: " + ex.getMessage());
\r
123 public void TestOpen(){
\r
124 UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "en_US_POSIX");
\r
127 errln("could not create the resource bundle");
\r
130 UResourceBundle obj = bundle.get("NumberPatterns");
\r
132 int size = obj.getSize();
\r
133 int type = obj.getType();
\r
134 if(type == UResourceBundle.ARRAY){
\r
135 UResourceBundle sub;
\r
136 for(int i=0; i<size; i++){
\r
138 String temp =sub.getString();
\r
139 if(temp.length()==0){
\r
140 errln("Failed to get the items from NumberPatterns array in bundle: "+
\r
141 bundle.getULocale().getBaseName());
\r
143 //System.out.println("\""+prettify(temp)+"\"");
\r
147 String[] strings = bundle.getStringArray("NumberPatterns");
\r
148 if(size!=strings.length){
\r
149 errln("Failed to get the items from NumberPatterns array in bundle: "+
\r
150 bundle.getULocale().getBaseName());
\r
153 obj = bundle.get("NumberElements");
\r
155 size = obj.getSize();
\r
156 type = obj.getType();
\r
157 if(type == UResourceBundle.ARRAY){
\r
158 UResourceBundle sub;
\r
159 for(int i=0; i<size; i++){
\r
161 String temp =sub.getString();
\r
162 if(temp.length()==0){
\r
163 errln("Failed to get the items from NumberPatterns array in bundle: "+
\r
164 bundle.getULocale().getBaseName());
\r
166 // System.out.println("\""+prettify(temp)+"\"");
\r
172 errln("could not create the resource bundle");
\r
175 bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zzz_ZZ_very_very_very_long_bogus_bundle");
\r
176 if(!bundle.getULocale().equals(ULocale.getDefault())){
\r
177 errln("UResourceBundle did not load the default bundle when bundle was not found. Default: " + ULocale.getDefault() +
\r
178 ", Bundle locale: " + bundle.getULocale());
\r
182 public void TestBasicTypes(){
\r
183 UResourceBundle bundle = null;
\r
185 bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes", testLoader);
\r
187 catch (MissingResourceException e) {
\r
188 warnln("could not load test data: " + e.getMessage());
\r
192 String expected = "abc\u0000def";
\r
193 UResourceBundle sub = bundle.get("zerotest");
\r
194 if(!expected.equals(sub.getString())){
\r
195 errln("Did not get the expected string for key zerotest in bundle testtypes");
\r
197 sub = bundle.get("emptyexplicitstring");
\r
199 if(!expected.equals(sub.getString())){
\r
200 errln("Did not get the expected string for key emptyexplicitstring in bundle testtypes");
\r
202 sub = bundle.get("emptystring");
\r
204 if(!expected.equals(sub.getString())){
\r
205 errln("Did not get the expected string for key emptystring in bundle testtypes");
\r
209 int expected = 123;
\r
210 UResourceBundle sub = bundle.get("onehundredtwentythree");
\r
211 if(expected!=sub.getInt()){
\r
212 errln("Did not get the expected int value for key onehundredtwentythree in bundle testtypes");
\r
214 sub = bundle.get("emptyint");
\r
216 if(expected!=sub.getInt()){
\r
217 errln("Did not get the expected int value for key emptyint in bundle testtypes");
\r
222 UResourceBundle sub = bundle.get("one");
\r
223 if(expected!=sub.getInt()){
\r
224 errln("Did not get the expected int value for key one in bundle testtypes");
\r
229 UResourceBundle sub = bundle.get("minusone");
\r
230 int got = sub.getInt();
\r
232 errln("Did not get the expected int value for key minusone in bundle testtypes");
\r
234 expected = 0xFFFFFFF;
\r
235 got = sub.getUInt();
\r
237 errln("Did not get the expected int value for key minusone in bundle testtypes");
\r
242 UResourceBundle sub = bundle.get("plusone");
\r
243 if(expected!=sub.getInt()){
\r
244 errln("Did not get the expected int value for key minusone in bundle testtypes");
\r
249 int[] expected = new int[]{ 1, 2, 3, -3, 4, 5, 6, 7 } ;
\r
250 UResourceBundle sub = bundle.get("integerarray");
\r
251 if(!Utility.arrayEquals(expected,sub.getIntVector())){
\r
252 errln("Did not get the expected int vector value for key integerarray in bundle testtypes");
\r
254 sub = bundle.get("emptyintv");
\r
255 expected = new int[0];
\r
256 if(!Utility.arrayEquals(expected,sub.getIntVector())){
\r
257 errln("Did not get the expected int vector value for key emptyintv in bundle testtypes");
\r
262 UResourceBundle sub = bundle.get("binarytest");
\r
263 ByteBuffer got = sub.getBinary();
\r
264 if(got.remaining()!=15){
\r
265 errln("Did not get the expected length for the binary ByteBuffer");
\r
267 for(int i=0; i< got.remaining(); i++){
\r
268 byte b = got.get();
\r
270 errln("Did not get the expected value for binary buffer at index: "+i);
\r
273 sub = bundle.get("emptybin");
\r
274 got = sub.getBinary();
\r
275 if(got.remaining()!=0){
\r
276 errln("Did not get the expected length for the emptybin ByteBuffer");
\r
281 UResourceBundle sub = bundle.get("emptyarray");
\r
282 String key = sub.getKey();
\r
283 if(!key.equals("emptyarray")){
\r
284 errln("Did not get the expected key for emptytable item");
\r
286 if(sub.getSize()!=0){
\r
287 errln("Did not get the expected length for emptytable item");
\r
291 UResourceBundle sub = bundle.get("menu");
\r
292 String key = sub.getKey();
\r
293 if(!key.equals("menu")){
\r
294 errln("Did not get the expected key for menu item");
\r
296 UResourceBundle sub1 = sub.get("file");
\r
297 key = sub1.getKey();
\r
298 if(!key.equals("file")){
\r
299 errln("Did not get the expected key for file item");
\r
301 UResourceBundle sub2 = sub1.get("open");
\r
302 key = sub2.getKey();
\r
303 if(!key.equals("open")){
\r
304 errln("Did not get the expected key for file item");
\r
306 String value = sub2.getString();
\r
307 if(!value.equals("Open")){
\r
308 errln("Did not get the expected value for key for oen item");
\r
311 sub = bundle.get("emptytable");
\r
312 key = sub.getKey();
\r
313 if(!key.equals("emptytable")){
\r
314 errln("Did not get the expected key for emptytable item");
\r
316 if(sub.getSize()!=0){
\r
317 errln("Did not get the expected length for emptytable item");
\r
319 sub = bundle.get("menu").get("file");
\r
320 int size = sub.getSize();
\r
322 for(int i=0; i<size; i++){
\r
338 String got = sub1.getKey();
\r
339 if(!expected.equals(got)){
\r
340 errln("Did not get the expected key at index"+i+". Expected: "+expected+" Got: "+got);
\r
342 logln("Got the expected key at index: "+i);
\r
348 private static final class TestCase{
\r
351 TestCase(String key, int value){
\r
353 this.value = value;
\r
356 public void TestTable32(){
\r
357 TestCase[] arr = new TestCase[]{
\r
358 new TestCase ( "ooooooooooooooooo", 0 ),
\r
359 new TestCase ( "oooooooooooooooo1", 1 ),
\r
360 new TestCase ( "ooooooooooooooo1o", 2 ),
\r
361 new TestCase ( "oo11ooo1ooo11111o", 25150 ),
\r
362 new TestCase ( "oo11ooo1ooo111111", 25151 ),
\r
363 new TestCase ( "o1111111111111111", 65535 ),
\r
364 new TestCase ( "1oooooooooooooooo", 65536 ),
\r
365 new TestCase ( "1ooooooo11o11ooo1", 65969 ),
\r
366 new TestCase ( "1ooooooo11o11oo1o", 65970 ),
\r
367 new TestCase ( "1ooooooo111oo1111", 65999 )
\r
369 UResourceBundle bundle = null;
\r
371 bundle = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testtable32", testLoader);
\r
373 catch (MissingResourceException ex) {
\r
374 warnln("could not load resource data: " + ex.getMessage());
\r
378 if(bundle.getType()!= UResourceBundle.TABLE){
\r
379 errln("Could not get the correct type for bundle testtable32");
\r
382 int size =bundle.getSize();
\r
384 errln("Could not get the correct size for bundle testtable32");
\r
389 // get the items by index
\r
390 for(int i =0; i<size; i++){
\r
391 UResourceBundle item = bundle.get(i);
\r
392 String key = item.getKey();
\r
393 int parsedNumber = parseTable32Key(key);
\r
394 switch(item.getType()){
\r
395 case UResourceBundle.STRING:
\r
396 String value = item.getString();
\r
397 number = UTF16.charAt(value,0);
\r
399 case UResourceBundle.INT:
\r
400 number = item.getInt();
\r
403 errln("Got unexpected resource type in testtable32");
\r
405 if(number!=parsedNumber){
\r
406 errln("Did not get expected value in testtypes32 for key"+
\r
407 key+". Expected: "+parsedNumber+" Got:"+number);
\r
412 // search for some items by key
\r
413 for(int i=0;i<arr.length; i++){
\r
414 UResourceBundle item = bundle.get(arr[i].key);
\r
415 switch(item.getType()){
\r
416 case UResourceBundle.STRING:
\r
417 String value = item.getString();
\r
418 number = UTF16.charAt(value,0);
\r
420 case UResourceBundle.INT:
\r
421 number = item.getInt();
\r
424 errln("Got unexpected resource type in testtable32");
\r
427 if(number != arr[i].value){
\r
428 errln("Did not get expected value in testtypes32 for key" +
\r
429 arr[i].key +". Expected: " + arr[i].value + " Got:" + number);
\r
433 private static int parseTable32Key(String key) {
\r
438 for(int i=0; i<key.length(); i++){
\r
448 public void TestAliases(){
\r
450 String simpleAlias = "Open";
\r
452 UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases", testLoader);
\r
454 warnln("could not load testaliases data");
\r
457 UResourceBundle sub = rb.get("simplealias");
\r
458 String s1 = sub.getString("simplealias");
\r
459 if(s1.equals(simpleAlias)){
\r
460 logln("Alias mechanism works for simplealias");
\r
462 errln("Did not get the expected output for simplealias");
\r
466 rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
\r
467 sub = rb.get("nonexisting");
\r
468 errln("Did not get the expected exception for nonexisting");
\r
469 }catch(MissingResourceException ex){
\r
470 logln("Alias mechanism works for nonexisting alias");
\r
474 rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
\r
475 sub = rb.get("referencingalias");
\r
476 s1 = sub.getString();
\r
477 if(s1.equals("Hani")){
\r
478 logln("Alias mechanism works for referencingalias");
\r
480 errln("Did not get the expected output for referencingalias");
\r
484 rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
\r
485 sub = rb.get("boundaries");
\r
486 String word = sub.getString("word");
\r
488 if(word.equals("word_ja.brk")){
\r
489 logln("Got the expected output for boundaries/word");
\r
491 errln("Did not get the expected type for boundaries/word");
\r
496 UResourceBundle rb1 = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
\r
498 errln("Caching of the resource bundle failed");
\r
500 logln("Caching of resource bundle passed");
\r
502 sub = rb1.get("testGetStringByKeyAliasing" );
\r
504 s1 = sub.get("KeyAlias0PST").getString();
\r
505 if(s1.equals("America/Los_Angeles")){
\r
506 logln("Alias mechanism works for KeyAlias0PST");
\r
508 errln("Did not get the expected output for KeyAlias0PST");
\r
511 s1 = sub.getString("KeyAlias1PacificStandardTime");
\r
512 if(s1.equals("Pacific Standard Time")){
\r
513 logln("Alias mechanism works for KeyAlias1PacificStandardTime");
\r
515 errln("Did not get the expected output for KeyAlias1PacificStandardTime");
\r
517 s1 = sub.getString("KeyAlias2PDT");
\r
518 if(s1.equals("PDT")){
\r
519 logln("Alias mechanism works for KeyAlias2PDT");
\r
521 errln("Did not get the expected output for KeyAlias2PDT");
\r
524 s1 = sub.getString("KeyAlias3LosAngeles");
\r
525 if(s1.equals("Los Angeles")){
\r
526 logln("Alias mechanism works for KeyAlias3LosAngeles. Got: "+s1);
\r
528 errln("Did not get the expected output for KeyAlias3LosAngeles. Got: "+s1);
\r
532 sub = rb.get("testGetStringByIndexAliasing" );
\r
533 s1 = sub.getString(0);
\r
534 if(s1.equals("America/Los_Angeles")){
\r
535 logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: "+s1);
\r
537 errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: "+s1);
\r
539 s1 = sub.getString(1);
\r
540 if(s1.equals("Pacific Standard Time")){
\r
541 logln("Alias mechanism works for testGetStringByIndexAliasing/1");
\r
543 errln("Did not get the expected output for testGetStringByIndexAliasing/1");
\r
545 s1 = sub.getString(2);
\r
546 if(s1.equals("PDT")){
\r
547 logln("Alias mechanism works for testGetStringByIndexAliasing/2");
\r
549 errln("Did not get the expected output for testGetStringByIndexAliasing/2");
\r
552 s1 = sub.getString(3);
\r
553 if(s1.equals("Los Angeles")){
\r
554 logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: "+s1);
\r
556 errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: "+s1);
\r
560 sub = rb.get("testAliasToTree" );
\r
562 ByteBuffer buf = sub.get("standard").get("%%CollationBin").getBinary();
\r
564 errln("Did not get the expected output for %%CollationBin");
\r
567 // should not get an exception
\r
568 rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_RBNF_BASE_NAME,"fr_BE");
\r
569 String str = rb.getString("SpelloutRules");
\r
570 if(str !=null && str.length()>0){
\r
571 logln("Alias mechanism works");
\r
573 errln("Alias mechanism failed for fr_BE SpelloutRules");
\r
575 rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW");
\r
576 UResourceBundle b = (UResourceBundle) rb.getObject("collations");
\r
578 if(b.get(0).getKey().equals( "default")){
\r
579 logln("Alias mechanism works");
\r
581 errln("Alias mechanism failed for zh_TW collations");
\r
584 errln("Did not get the expected object for collations");
\r
588 public void TestAlias(){
\r
589 logln("Testing %%ALIAS");
\r
590 UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"iw_IL");
\r
591 UResourceBundle b = rb.get("NumberPatterns");
\r
594 logln("%%ALIAS mechanism works");
\r
596 errln("%%ALIAS mechanism failed for iw_IL NumberPatterns");
\r
599 errln("%%ALIAS mechanism failed for iw_IL");
\r
602 public void TestXPathAlias(){
\r
603 UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader);
\r
604 UResourceBundle b = rb.get("aliasClient");
\r
605 String result = b.getString();
\r
606 String expResult= "correct";
\r
608 if(!result.equals(expResult)){
\r
609 errln("Did not get the expected result for XPath style alias");
\r
612 UResourceBundle c = rb.get("rootAliasClient");
\r
613 result = c.getString();
\r
614 expResult = "correct";
\r
615 if(!result.equals(expResult)){
\r
616 errln("Did not get the expected result for XPath style alias for rootAliasClient");
\r
618 }catch( MissingResourceException ex){
\r
619 errln("Could not get rootAliasClient");
\r
622 public void TestCircularAliases(){
\r
623 // Aliases no longer supported
\r
625 // UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
\r
626 // UResourceBundle sub = rb.get("aaa");
\r
627 // String s1 = sub.getString();
\r
629 // errln("Did not get the expected exception");
\r
631 // }catch(IllegalArgumentException ex){
\r
632 // logln("got expected exception for circular references");
\r
634 // catch (MissingResourceException ex) {
\r
635 // warnln("could not load resource data: " + ex.getMessage());
\r
639 public void TestGetWithFallback(){
\r
641 UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");
\r
642 String key = bundle.getStringWithFallback("Keys/collation");
\r
643 if(!key.equals("COLLATION")){
\r
644 errln("Did not get the expected result from getStringWithFallback method.");
\r
646 String type = bundle.getStringWithFallback("Types/collation/direct");
\r
647 if(!type.equals("DIRECT")){
\r
648 errln("Did not get the expected result form getStringWithFallback method.");
\r
651 ICUResourceBundle bundle = null;
\r
653 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"fr_FR");
\r
654 ICUResourceBundle b1 = bundle.getWithFallback("calendar");
\r
655 String defaultCal = b1.getStringWithFallback("default");
\r
656 if(!defaultCal.equals("gregorian")){
\r
657 errln("Did not get the expected default calendar string: Expected: gregorian, Got: "+defaultCal);
\r
659 ICUResourceBundle b2 = b1.getWithFallback(defaultCal);
\r
660 ICUResourceBundle b3 = b2.getWithFallback("monthNames");
\r
661 String defaultContext = b3.getStringWithFallback("default");
\r
662 ICUResourceBundle b4 = b3.getWithFallback(defaultContext);
\r
663 String defaultWidth = b4.getStringWithFallback("default");
\r
664 ICUResourceBundle b5 = b4.getWithFallback(defaultWidth);
\r
665 if(b5.getSize()!=12){
\r
666 errln("Did not get the expected size for the default monthNames");
\r
670 private static final String CALENDAR_RESNAME = "calendar";
\r
671 private static final String CALENDAR_KEYWORD = "calendar";
\r
673 public void TestLocaleDisplayNames() {
\r
674 ULocale[] locales = ULocale.getAvailableLocales();
\r
675 for (int i = 0; i < locales.length; ++i) {
\r
676 if (!hasLocalizedCountryFor(ULocale.ENGLISH, locales[i])){
\r
677 errln("Could not get localized country for "+ locales[i]);
\r
679 if(!hasLocalizedLanguageFor(ULocale.ENGLISH, locales[i])){
\r
680 errln("Could not get localized language for "+ locales[i]);
\r
682 if(!hasLocalizedCountryFor(locales[i], locales[i])){
\r
683 errln("Could not get localized country for "+ locales[i]);
\r
684 hasLocalizedCountryFor(locales[i], locales[i]);
\r
686 if(!hasLocalizedLanguageFor(locales[i], locales[i])){
\r
687 errln("Could not get localized language for "+ locales[i]);
\r
690 logln(locales[i] + "\t" + locales[i].getDisplayName(ULocale.ENGLISH) + "\t" + locales[i].getDisplayName(locales[i]));
\r
694 private static boolean hasLocalizedLanguageFor(ULocale locale, ULocale otherLocale) {
\r
695 String lang = otherLocale.getLanguage();
\r
696 String localizedVersion = otherLocale.getDisplayLanguage(locale);
\r
697 return !lang.equals(localizedVersion);
\r
700 private static boolean hasLocalizedCountryFor(ULocale locale, ULocale otherLocale) {
\r
701 String country = otherLocale.getCountry();
\r
702 if (country.equals("")) return true;
\r
703 String localizedVersion = otherLocale.getDisplayCountry(locale);
\r
704 return !country.equals(localizedVersion);
\r
707 public void TestFunctionalEquivalent(){
\r
708 String[] calCases = {
\r
709 // avail locale equiv
\r
710 "t", "en_US_POSIX", "en_US@calendar=gregorian",
\r
711 "f", "ja_JP_TOKYO", "ja_JP@calendar=gregorian",
\r
712 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
\r
713 "t", "sr@calendar=gregorian", "sr@calendar=gregorian",
\r
714 "t", "en", "en@calendar=gregorian",
\r
715 "t", "th_TH", "th@calendar=buddhist",
\r
716 "t", "th_TH@calendar=gregorian", "th_TH@calendar=gregorian",
\r
717 "f", "th_TH_Bangkok", "th@calendar=buddhist",
\r
720 logln("Testing functional equivalents for calendar...");
\r
721 getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_BASE_NAME,
\r
722 Calendar.class.getClassLoader(),
\r
723 CALENDAR_RESNAME, CALENDAR_KEYWORD, false, calCases);
\r
725 logln("Testing error conditions:");
\r
727 ClassLoader cl = BreakIterator.class.getClassLoader();
\r
728 ICUResourceBundle.getFunctionalEquivalent(ICUResourceBundle.ICU_BRKITR_BASE_NAME, cl, "calendar",
\r
729 "calendar", new ULocale("ar_EG@calendar=islamic"), new boolean[1], true);
\r
730 errln("Err: expected MissingResourceException");
\r
731 } catch ( MissingResourceException t ) {
\r
732 logln("expected MissingResourceException caught (PASS): " + t.toString());
\r
736 private void getFunctionalEquivalentTestCases(String path, ClassLoader cl, String resName, String keyword,
\r
737 boolean truncate, String[] testCases) {
\r
738 //String F_STR = "f";
\r
739 String T_STR = "t";
\r
740 boolean isAvail[] = new boolean[1];
\r
742 logln("Testing functional equivalents...");
\r
743 for(int i = 0; i < testCases.length ;i+=3) {
\r
744 boolean expectAvail = T_STR.equals(testCases[i+0]);
\r
745 ULocale inLocale = new ULocale(testCases[i+1]);
\r
746 ULocale expectLocale = new ULocale(testCases[i+2]);
\r
748 logln(new Integer(i/3).toString() + ": " + new Boolean(expectAvail).toString() + "\t\t" +
\r
749 inLocale.toString() + "\t\t" + expectLocale.toString());
\r
751 ULocale equivLocale = ICUResourceBundle.getFunctionalEquivalent(path, cl, resName, keyword, inLocale, isAvail, truncate);
\r
752 boolean gotAvail = isAvail[0];
\r
754 if((gotAvail != expectAvail) || !equivLocale.equals(expectLocale)) {
\r
755 errln(new Integer(i/3).toString() + ": Error, expected Equiv=" + new Boolean(expectAvail).toString() + "\t\t" +
\r
756 inLocale.toString() + "\t\t--> " + expectLocale.toString() + ", but got " + new Boolean(gotAvail).toString() + " " +
\r
757 equivLocale.toString());
\r
762 public void TestNorwegian(){
\r
764 UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_REGION_BASE_NAME, "no_NO_NY");
\r
765 UResourceBundle sub = rb.get("Countries");
\r
766 String s1 = sub.getString("NO");
\r
767 if(s1.equals("Noreg")){
\r
768 logln("got expected output ");
\r
770 errln("did not get the expected result");
\r
772 }catch(IllegalArgumentException ex){
\r
773 errln("Caught an unexpected expected");
\r
776 public void TestJB4102(){
\r
778 ICUResourceBundle root =(ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "root");
\r
779 ICUResourceBundle t = null;
\r
780 // AmPmMarkers now exist in root/islamic calendar, so this test is rendered useless.
\r
782 // t = root.getWithFallback("calendar/islamic-civil/AmPmMarkers");
\r
783 // errln("Second resource does not exist. How did it get here?\n");
\r
784 // }catch(MissingResourceException ex){
\r
785 // logln("Got the expected exception");
\r
788 t = root.getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera");
\r
789 errln("Second resource does not exist. How did it get here?\n");
\r
790 }catch(MissingResourceException ex){
\r
791 logln("Got the expected exception");
\r
794 errln("t is not null!");
\r
796 } catch (MissingResourceException e) {
\r
797 warnln("Could not load the locale data: " + e.getMessage());
\r
801 public void TestCLDRStyleAliases() {
\r
802 String result = null;
\r
803 String expected = null;
\r
804 String[]expects = new String[] { "", "a41", "a12", "a03", "ar4" };
\r
806 logln("Testing CLDR style aliases......\n");
\r
808 UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED",testLoader);
\r
809 ICUResourceBundle alias = (ICUResourceBundle)rb.get("a");
\r
811 for(int i = 1; i < 5 ; i++) {
\r
812 String resource="a"+i;
\r
813 UResourceBundle a = (alias).getWithFallback(resource);
\r
814 result = a.getString();
\r
815 if(result.equals(expected)) {
\r
816 errln("CLDR style aliases failed resource with name "+resource+"resource, exp "+expects[i] +" , got " + result);
\r
821 private String getLSString(int status){
\r
823 case ICUResourceBundle.FROM_FALLBACK:
\r
824 return "FROM_FALLBACK";
\r
825 case ICUResourceBundle.FROM_DEFAULT:
\r
826 return "FROM_DEFAULT";
\r
827 case ICUResourceBundle.FROM_ROOT:
\r
828 return "FROM_ROOT";
\r
829 case ICUResourceBundle.FROM_LOCALE:
\r
830 return "FROM_LOCALE";
\r
836 private void assertEqualLoadingStatus(String msg, int target, int result) {
\r
837 if (result != target) {
\r
838 errln(msg + " expected: "+ getLSString(target)
\r
839 + " got: " + getLSString(result));
\r
843 private void assertDefaultLoadingStatus(String msg, int result) {
\r
844 assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_DEFAULT, result);
\r
847 private void assertFallbackLoadingStatus(String msg, int result) {
\r
848 assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_FALLBACK, result);
\r
851 private void assertRootLoadingStatus(String msg, int result) {
\r
852 assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_ROOT, result);
\r
855 private void assertLocaleLoadingStatus(String msg, int result) {
\r
856 assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_LOCALE, result);
\r
859 public void TestLoadingStatus(){
\r
860 ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "yi_IL");
\r
861 assertDefaultLoadingStatus("base/yi_IL", bundle.getLoadingStatus());
\r
863 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "eo_DE");
\r
864 assertFallbackLoadingStatus("base/eo_DE", bundle.getLoadingStatus());
\r
866 logln("Test to verify loading status of get(String)");
\r
867 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_LANG_BASE_NAME, "zh_Hant_TW");
\r
868 ICUResourceBundle countries = (ICUResourceBundle) bundle.get("Languages");
\r
869 assertFallbackLoadingStatus("lang/Languages/zh_Hant_TW", countries.getLoadingStatus());
\r
872 UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters");
\r
873 status = auxExemplar.getLoadingStatus();
\r
874 if(status != UResourceBundle.FROM_ROOT){
\r
875 errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT)
\r
876 + " Got: " + getLSString(status));
\r
880 logln("Test to verify loading status of get(int)");
\r
881 bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "te_IN");
\r
882 ICUResourceBundle ms = (ICUResourceBundle) bundle.get("MeasurementSystem");
\r
883 assertRootLoadingStatus("base/MeasurementSystem/te_IN", ms.getLoadingStatus());
\r
885 logln("Test to verify loading status of getwithFallback");
\r
886 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU", testLoader);
\r
887 ICUResourceBundle temp = (ICUResourceBundle) bundle.getWithFallback("a/a2");
\r
888 assertLocaleLoadingStatus("testdata/a/a2/sh_YU", temp.getLoadingStatus());
\r
890 temp = bundle.getWithFallback("a/a1");
\r
891 assertFallbackLoadingStatus("testdata/a/a1/sh_YU", temp.getLoadingStatus());
\r
893 temp = bundle.getWithFallback("a/a4");
\r
894 assertRootLoadingStatus("testdata/a/a4/sh_YU", temp.getLoadingStatus());
\r
897 public void TestCoverage(){
\r
898 UResourceBundle bundle;
\r
899 bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);
\r
900 if (bundle == null){
\r
901 errln("UResourceBundle.getBundleInstance(String baseName) failed");
\r
904 bundle = UResourceBundle.getBundleInstance(ULocale.getDefault());
\r
905 if (bundle == null){
\r
906 errln("UResourceBundle.getBundleInstance(ULocale) failed");
\r
909 if (new UResourceTypeMismatchException("coverage") == null){
\r
910 errln("Create UResourceTypeMismatchException error");
\r
912 class Stub extends UResourceBundle{
\r
913 public ULocale getULocale() {return ULocale.ROOT;}
\r
914 protected String getLocaleID() {return null;}
\r
915 protected String getBaseName() {return null;}
\r
916 protected UResourceBundle getParent() {return null;}
\r
917 protected void setLoadingStatus(int newStatus) {}
\r
918 public Enumeration getKeys() {return null;}
\r
919 protected Object handleGetObject(String aKey) {return null;}
\r
921 Stub stub = new Stub();
\r
923 if (!stub.getLocale().equals(ULocale.ROOT.toLocale())){
\r
924 errln("UResourceBundle.getLoclae(Locale) should delegate to (ULocale)");
\r
927 public void TestJavaULocaleBundleLoading(){
\r
928 String baseName="com.ibm.icu.dev.data.resources.TestDataElements";
\r
929 String locName = "en_Latn_US";
\r
930 UResourceBundle bundle = UResourceBundle.getBundleInstance(baseName, locName, testLoader);
\r
931 String fromRoot = bundle.getString("from_root");
\r
932 if(!fromRoot.equals("This data comes from root")){
\r
933 errln("Did not get the expected string for from_root");
\r
935 String fromEn = bundle.getString("from_en");
\r
936 if(!fromEn.equals("This data comes from en")){
\r
937 errln("Did not get the expected string for from_en");
\r
939 String fromEnLatn = bundle.getString("from_en_Latn");
\r
940 if(!fromEnLatn.equals("This data comes from en_Latn")){
\r
941 errln("Did not get the expected string for from_en_Latn");
\r
943 String fromEnLatnUs = bundle.getString("from_en_Latn_US");
\r
944 if(!fromEnLatnUs.equals("This data comes from en_Latn_US")){
\r
945 errln("Did not get the expected string for from_en_Latn_US");
\r
947 UResourceBundle bundle1 = UResourceBundle.getBundleInstance(baseName, new ULocale(locName), testLoader);
\r
948 if(!bundle1.equals(bundle)){
\r
949 errln("Did not get the expected bundle for "+baseName +"."+locName);
\r
951 if(bundle1!=bundle){
\r
952 errln("Did not load the bundle from cache");
\r
955 UResourceBundle bundle2 = UResourceBundle.getBundleInstance(baseName, "en_IN", testLoader);
\r
956 if(!bundle2.getLocale().toString().equals("en")){
\r
957 errln("Did not get the expected fallback locale. Expected: en Got: "+bundle2.getLocale().toString());
\r
959 UResourceBundle bundle3 = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader);
\r
960 if(!bundle3.getLocale().toString().equals("te")){
\r
961 errln("Did not get the expected fallback locale. Expected: te Got: "+bundle2.getLocale().toString());
\r
963 // non-existent bundle .. should return default
\r
964 UResourceBundle defaultBundle = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
\r
965 ULocale defaultLocale = ULocale.getDefault();
\r
966 if(!defaultBundle.getULocale().equals(defaultLocale)){
\r
967 errln("Did not get the default bundle for non-existent bundle");
\r
969 // non-existent bundle, non-existent default locale
\r
970 // so return the root bundle.
\r
971 ULocale.setDefault(ULocale.CANADA_FRENCH);
\r
972 UResourceBundle root = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
\r
973 if(!root.getULocale().toString().equals("")){
\r
974 errln("Did not get the root bundle for non-existent default bundle for non-existent bundle");
\r
976 //reset the default
\r
977 ULocale.setDefault(defaultLocale);
\r
978 Enumeration keys = bundle.getKeys();
\r
980 while(keys.hasMoreElements()){
\r
981 logln("key: "+ keys.nextElement());
\r
985 errln("Did not get the expected number of keys: got " + i + ", expected 4");
\r
987 UResourceBundle bundle4 = UResourceBundle.getBundleInstance(baseName,"fr_Latn_FR", testLoader);
\r
989 errln("Could not load bundle fr_Latn_FR");
\r
992 public void TestAliasFallback(){
\r
994 ULocale loc = new ULocale("en_US");
\r
995 ICUResourceBundle b = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc);
\r
996 ICUResourceBundle b1 = (ICUResourceBundle)b.getWithFallback("calendar/hebrew/monthNames/format/abbreviated");
\r
998 logln("loaded data for abbreviated month names: "+ b1.getKey());
\r
1000 }catch(MissingResourceException ex){
\r
1001 warnln("Failed to load data for abbreviated month names");
\r
1004 private Set<String> setFromEnumeration(Enumeration<String> e) {
\r
1005 TreeSet<String> set = new TreeSet<String>();
\r
1006 while (e.hasMoreElements()) {
\r
1007 set.add(e.nextElement());
\r
1012 * Test ICUResourceBundle.getKeys() for a whole bundle (top-level resource).
\r
1013 * JDK JavaDoc for ResourceBundle.getKeys() says that it returns
\r
1014 * "an Enumeration of the keys contained in this ResourceBundle and its parent bundles."
\r
1016 public void TestICUGetKeysAtTopLevel() {
\r
1017 String baseName="com/ibm/icu/dev/data/testdata";
\r
1018 UResourceBundle te_IN = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader);
\r
1019 UResourceBundle te = UResourceBundle.getBundleInstance(baseName, "te", testLoader);
\r
1020 Set<String> te_set = setFromEnumeration(te.getKeys());
\r
1021 Set<String> te_IN_set = setFromEnumeration(te_IN.getKeys());
\r
1022 assertTrue("te.getKeys().contains(string_only_in_Root)", te_set.contains("string_only_in_Root"));
\r
1023 assertTrue("te.getKeys().contains(string_only_in_te)", te_set.contains("string_only_in_te"));
\r
1024 assertFalse("te.getKeys().contains(string_only_in_te_IN)", te_set.contains("string_only_in_te_IN"));
\r
1025 assertTrue("te_IN.getKeys().contains(string_only_in_Root)", te_IN_set.contains("string_only_in_Root"));
\r
1026 assertTrue("te_IN.getKeys().contains(string_only_in_te)", te_IN_set.contains("string_only_in_te"));
\r
1027 assertTrue("te_IN.getKeys().contains(string_only_in_te_IN)", te_IN_set.contains("string_only_in_te_IN"));
\r
1028 // TODO: Check for keys of alias resource items
\r
1031 * Test ICUResourceBundle.getKeys() for a resource item (not a whole bundle/top-level resource).
\r
1032 * This does not take parent bundles into account.
\r
1034 public void TestICUGetKeysForResourceItem() {
\r
1035 String baseName="com/ibm/icu/dev/data/testdata";
\r
1036 UResourceBundle te = UResourceBundle.getBundleInstance(baseName, "te", testLoader);
\r
1037 UResourceBundle tagged_array_in_Root_te = te.get("tagged_array_in_Root_te");
\r
1038 Set<String> keys = setFromEnumeration(tagged_array_in_Root_te.getKeys());
\r
1039 assertTrue("tagged_array_in_Root_te.getKeys().contains(tag0)", keys.contains("tag0"));
\r
1040 assertTrue("tagged_array_in_Root_te.getKeys().contains(tag1)", keys.contains("tag1"));
\r
1041 assertFalse("tagged_array_in_Root_te.getKeys().contains(tag7)", keys.contains("tag7"));
\r
1042 assertFalse("tagged_array_in_Root_te.getKeys().contains(tag12)", keys.contains("tag12"));
\r
1043 UResourceBundle array_in_Root_te = te.get("array_in_Root_te");
\r
1044 assertFalse("array_in_Root_te.getKeys().hasMoreElements()", array_in_Root_te.getKeys().hasMoreElements());
\r
1045 UResourceBundle string_in_Root_te = te.get("string_in_Root_te");
\r
1046 assertFalse("string_in_Root_te.getKeys().hasMoreElements()", string_in_Root_te.getKeys().hasMoreElements());
\r
1050 * UResouceBundle should be able to load a resource bundle even if
\r
1051 * a similarly named class (only case differences) exists in the
\r
1052 * same package. See Ticket#6844
\r
1054 public void TestT6844() {
\r
1056 UResourceBundle rb1
\r
1057 = UResourceBundle.getBundleInstance("com.ibm.icu.dev.data.resources.TestMessages");
\r
1058 assertEquals("bundleContainer in TestMessages", "TestMessages.class", rb1.getString("bundleContainer"));
\r
1060 UResourceBundle rb2
\r
1061 = UResourceBundle.getBundleInstance("com.ibm.icu.dev.data.resources.testmessages");
\r
1062 assertEquals("bundleContainer in testmessages", "testmessages.properties", rb2.getString("bundleContainer"));
\r
1063 } catch (Throwable t) {
\r
1064 errln(t.getMessage());
\r