]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/util/ICUResourceBundleTest.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / util / ICUResourceBundleTest.java
1 //##header J2SE15
2 /**
3  *******************************************************************************
4  * Copyright (C) 2001-2009, International Business Machines Corporation and    *
5  * others. All Rights Reserved.                                                *
6  *******************************************************************************
7  */
8 package com.ibm.icu.dev.test.util;
9
10 import java.io.BufferedReader;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.net.URL;
14 import java.net.URLConnection;
15 import java.net.JarURLConnection;
16 //#if defined(FOUNDATION10) || defined(J2SE13)
17 //##import com.ibm.icu.impl.ByteBuffer;
18 //#else
19 import java.nio.ByteBuffer;
20 //#endif
21 import java.util.MissingResourceException;
22 import java.util.Enumeration;
23 import java.util.jar.JarEntry;
24
25 import com.ibm.icu.dev.test.TestFmwk;
26 import com.ibm.icu.impl.ICUResourceBundle;
27 import com.ibm.icu.impl.Utility;
28 import com.ibm.icu.text.UTF16;
29 import com.ibm.icu.util.Holiday;
30 import com.ibm.icu.util.ULocale;
31 import com.ibm.icu.util.UResourceBundle;
32 import com.ibm.icu.util.UResourceTypeMismatchException;
33
34
35 public final class ICUResourceBundleTest extends TestFmwk {
36     private static final ClassLoader testLoader = ICUResourceBundleTest.class.getClassLoader();
37
38     public static void main(String args[]) throws Exception {
39         ICUResourceBundleTest test = new ICUResourceBundleTest();
40         test.run(args);
41
42     }
43     public void TestGetResources(){
44         try{
45             // It does not work well in eclipse plug-in test because of class loader configuration??
46             // For now, specify resource path explicitly in this test case
47             //Enumeration en = testLoader.getResources("META-INF");
48             Enumeration en = testLoader.getResources("com.ibm.icu.dev.data");
49             for(;en.hasMoreElements();) {
50                 URL url = (URL)en.nextElement();
51                 if (url == null) {
52                     warnln("could not load resource data");
53                     return;
54                 }
55                 URLConnection c = url.openConnection();
56
57                 if (c instanceof JarURLConnection) {
58                     JarURLConnection jc = (JarURLConnection)c;
59                     JarEntry je = jc.getJarEntry();
60                     logln("jar entry: " + je.toString()); 
61                 } else {
62                     InputStream is = c.getInputStream();
63                     logln("input stream:");
64                     InputStreamReader r = new InputStreamReader(is);
65                     BufferedReader br = new BufferedReader(r);
66                     String line = null;
67                     int n = 0;
68                     while ((line = br.readLine()) != null) {
69                         logln("  " + ++n + ": " + line);
70                     }
71                 }
72             }
73         }catch(SecurityException ex) {
74             warnln("could not load resource data: " + ex);
75             ex.printStackTrace();
76     }catch(NullPointerException ex) {
77         // thrown by ibm 1.4.2 windows jvm security manager
78         warnln("could not load resource data: " + ex);
79         }catch(Exception ex){
80         ex.printStackTrace();
81             errln("Unexpected exception: "+ ex);
82         }
83     }
84     public void TestResourceBundleWrapper(){
85         UResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", "da_DK");
86         Object o = bundle.getObject("holidays");
87         if(o instanceof Holiday[] ){
88             logln("wrapper mechanism works for Weekend data");
89         }else{
90             errln("Did not get the expected output for Weekend data");
91         }
92
93         bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "bogus");
94         if(bundle instanceof UResourceBundle && bundle.getULocale().equals("en_US")){
95             logln("wrapper mechanism works for bogus locale");
96         }else{
97             errln("wrapper mechanism failed for bogus locale.");
98         }
99
100         try{
101             bundle = UResourceBundle.getBundleInstance("bogus", "bogus");
102             if(bundle!=null){
103               errln("Did not get the expected exception");
104             }
105         }catch(MissingResourceException ex){
106             logln("got the expected exception");
107         }
108
109
110     }
111     public void TestJB3879(){
112         // this tests tests loading of root bundle when a resource bundle
113         // for the default locale is requested
114         try {
115             UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", ULocale.getDefault().toString(), testLoader);
116             if(bundle==null){
117                 errln("could not create the resource bundle");
118             }
119         }
120         catch (MissingResourceException ex) {
121             warnln("could not load test data: " + ex.getMessage());
122         }
123     }
124     public void TestOpen(){
125         UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "en_US_POSIX");
126
127         if(bundle==null){
128             errln("could not create the resource bundle");
129         }
130
131         UResourceBundle obj =  bundle.get("NumberPatterns");
132
133         int size = obj.getSize();
134         int type = obj.getType();
135         if(type == UResourceBundle.ARRAY){
136             UResourceBundle sub;
137             for(int i=0; i<size; i++){
138                 sub = obj.get(i);
139                 String temp =sub.getString();
140                 if(temp.length()==0){
141                     errln("Failed to get the items from NumberPatterns array in bundle: "+
142                             bundle.getULocale().getBaseName());
143                 }
144                 //System.out.println("\""+prettify(temp)+"\"");
145             }
146
147         }
148         String[] strings = bundle.getStringArray("NumberPatterns");
149         if(size!=strings.length){
150             errln("Failed to get the items from NumberPatterns array in bundle: "+
151                     bundle.getULocale().getBaseName());
152         }
153         {
154             obj =  bundle.get("NumberElements");
155
156             size = obj.getSize();
157             type = obj.getType();
158             if(type == UResourceBundle.ARRAY){
159                 UResourceBundle sub;
160                 for(int i=0; i<size; i++){
161                     sub = obj.get(i);
162                     String temp =sub.getString();
163                     if(temp.length()==0){
164                         errln("Failed to get the items from NumberPatterns array in bundle: "+
165                                 bundle.getULocale().getBaseName());
166                     }
167                    // System.out.println("\""+prettify(temp)+"\"");
168                 }
169
170             }
171         }
172         if(bundle==null){
173             errln("could not create the resource bundle");
174         }
175         bundle = (UResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "en_US_POSIX");
176         if(bundle==null){
177             errln("could not load the stream");
178         }
179         bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zzz_ZZ_very_very_very_long_bogus_bundle");
180         if(!bundle.getULocale().equals(ULocale.getDefault())){
181             errln("UResourceBundle did not load the default bundle when bundle was not found. Default: " + ULocale.getDefault() + 
182                         ", Bundle locale: " + bundle.getULocale());
183         }
184
185
186     }
187
188     public void TestBasicTypes(){
189         UResourceBundle bundle = null;
190         try {
191             bundle = (UResourceBundle)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     public void TestTable32(){
363         TestCase[] arr = new TestCase[]{
364           new TestCase  ( "ooooooooooooooooo", 0 ),
365           new TestCase  ( "oooooooooooooooo1", 1 ),
366           new TestCase  ( "ooooooooooooooo1o", 2 ),
367           new TestCase  ( "oo11ooo1ooo11111o", 25150 ),
368           new TestCase  ( "oo11ooo1ooo111111", 25151 ),
369           new TestCase  ( "o1111111111111111", 65535 ),
370           new TestCase  ( "1oooooooooooooooo", 65536 ),
371           new TestCase  ( "1ooooooo11o11ooo1", 65969 ),
372           new TestCase  ( "1ooooooo11o11oo1o", 65970 ),
373           new TestCase  ( "1ooooooo111oo1111", 65999 )
374         };
375         UResourceBundle bundle = null;
376         try {
377             bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testtable32", testLoader);
378         }
379         catch (MissingResourceException ex) {
380             warnln("could not load resource data: " + ex.getMessage());
381             return;
382         }
383
384         if(bundle.getType()!= UResourceBundle.TABLE){
385             errln("Could not get the correct type for bundle testtable32");
386         }
387         int size =bundle.getSize();
388         if(size!=66000){
389             errln("Could not get the correct size for bundle testtable32");
390         }
391         for(int i =0; i<size; i++){
392             UResourceBundle item = bundle.get(i);
393             String key = item.getKey();
394             int parsedNumber = parseTable32Key(key);
395             int number=-1;
396             switch(item.getType()){
397                 case UResourceBundle.STRING:
398                     String value = item.getString();
399                     number = UTF16.charAt(value,0);
400                     break;
401                 case UResourceBundle.INT:
402                     number = item.getInt();
403                     break;
404                 default:
405                     errln("Got unexpected resource type in testtable32");
406
407             }
408             if(number!=parsedNumber){
409                 errln("Did not get expected value in testtypes32 for key"+
410                       key+". Expected: "+parsedNumber+" Got:"+number);
411             }
412
413         }
414         for(int i=0;i<arr.length; i++){
415             String expected = arr[i].key;
416             UResourceBundle item = bundle.get(expected);
417             int number=0;
418             String key = item.getKey();
419             int parsedNumber = parseTable32Key(key);
420             if(!key.equals(expected)){
421                 errln("Did not get the expected key. Expected: "+expected+" Got:"+key);
422             }
423             switch(item.getType()){
424                 case UResourceBundle.STRING:
425                     String value = item.getString();
426                     number = UTF16.charAt(value,0);
427                     break;
428                  case UResourceBundle.INT:
429                     number = item.getInt();
430                     break;
431                 default:
432                     errln("Got unexpected resource type in testtable32");
433             }
434
435             if(number!=parsedNumber){
436                 errln("Did not get expected value in testtypes32 for key"+
437                       key+". Expected: "+parsedNumber+" Got:"+number);
438             }
439         }
440     }
441     private static int  parseTable32Key(String key) {
442         int number;
443         char c;
444
445         number=0;
446         for(int i=0; i<key.length(); i++){
447             c = key.charAt(i);
448             number<<=1;
449             if(c=='1') {
450                 number|=1;
451             }
452         }
453         return number;
454     }
455
456     public void TestAliases(){
457 /*
458        String simpleAlias   = "Open";
459
460        UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases", testLoader);
461        if (rb == null) {
462            warnln("could not load testaliases data");
463            return;
464        }
465         UResourceBundle sub = rb.get("simplealias");
466         String s1 = sub.getString("simplealias");
467         if(s1.equals(simpleAlias)){
468             logln("Alias mechanism works for simplealias");
469         }else{
470             errln("Did not get the expected output for simplealias");
471         }
472         {
473             try{
474                 rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
475                 sub = rb.get("nonexisting");
476                 errln("Did not get the expected exception for nonexisting");
477             }catch(MissingResourceException ex){
478                 logln("Alias mechanism works for nonexisting alias");
479             }
480         }
481         {
482             rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
483             sub = rb.get("referencingalias");
484             s1 = sub.getString();
485             if(s1.equals("Hani")){
486                 logln("Alias mechanism works for referencingalias");
487             }else{
488                 errln("Did not get the expected output for referencingalias");
489             }
490         }
491         {
492             rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
493             sub = rb.get("boundaries");
494             String word = sub.getString("word");
495
496             if(word.equals("word_ja.brk")){
497                 logln("Got the expected output for boundaries/word");
498             }else{
499                 errln("Did not get the expected type for boundaries/word");
500             }
501
502         }
503         {
504             UResourceBundle rb1 = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
505             if(rb1!=rb){
506                 errln("Caching of the resource bundle failed");
507             }else{
508                 logln("Caching of resource bundle passed");
509             }
510             sub = rb1.get("testGetStringByKeyAliasing" );
511
512             s1 = sub.get("KeyAlias0PST").getString();
513             if(s1.equals("America/Los_Angeles")){
514                 logln("Alias mechanism works for KeyAlias0PST");
515             }else{
516                 errln("Did not get the expected output for KeyAlias0PST");
517             }
518
519             s1 = sub.getString("KeyAlias1PacificStandardTime");
520             if(s1.equals("Pacific Standard Time")){
521                 logln("Alias mechanism works for KeyAlias1PacificStandardTime");
522             }else{
523                 errln("Did not get the expected output for KeyAlias1PacificStandardTime");
524             }
525             s1 = sub.getString("KeyAlias2PDT");
526             if(s1.equals("PDT")){
527                 logln("Alias mechanism works for KeyAlias2PDT");
528             }else{
529                 errln("Did not get the expected output for KeyAlias2PDT");
530             }
531
532             s1 = sub.getString("KeyAlias3LosAngeles");
533             if(s1.equals("Los Angeles")){
534                 logln("Alias mechanism works for KeyAlias3LosAngeles. Got: "+s1);
535             }else{
536                 errln("Did not get the expected output for KeyAlias3LosAngeles. Got: "+s1);
537             }
538         }
539         {
540             sub = rb.get("testGetStringByIndexAliasing" );
541             s1 = sub.getString(0);
542             if(s1.equals("America/Los_Angeles")){
543                 logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: "+s1);
544             }else{
545                 errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: "+s1);
546             }
547             s1 = sub.getString(1);
548             if(s1.equals("Pacific Standard Time")){
549                 logln("Alias mechanism works for testGetStringByIndexAliasing/1");
550             }else{
551                 errln("Did not get the expected output for testGetStringByIndexAliasing/1");
552             }
553             s1 = sub.getString(2);
554             if(s1.equals("PDT")){
555                 logln("Alias mechanism works for testGetStringByIndexAliasing/2");
556             }else{
557                 errln("Did not get the expected output for testGetStringByIndexAliasing/2");
558             }
559
560             s1 = sub.getString(3);
561             if(s1.equals("Los Angeles")){
562                 logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: "+s1);
563             }else{
564                 errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: "+s1);
565             }
566         }
567         {
568             sub = rb.get("testAliasToTree" );
569             
570             ByteBuffer buf = sub.get("standard").get("%%CollationBin").getBinary();
571             if(buf==null){
572                 errln("Did not get the expected output for %%CollationBin");
573             }
574         }
575         // should not get an exception
576         rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_RBNF_BASE_NAME,"fr_BE");
577         String str = rb.getString("SpelloutRules");
578         if(str !=null && str.length()>0){
579             logln("Alias mechanism works");
580         }else{
581             errln("Alias mechanism failed for fr_BE SpelloutRules");
582         }
583         rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW");
584         UResourceBundle b = (UResourceBundle) rb.getObject("collations");
585         if(b != null){
586             if(b.get(0).getKey().equals( "default")){
587                 logln("Alias mechanism works");
588             }else{
589                 errln("Alias mechanism failed for zh_TW collations");
590             }
591         }else{
592             errln("Did not get the expected object for collations");
593         }
594 */
595     }
596     public void TestAlias(){
597         logln("Testing %%ALIAS");
598         UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"iw_IL");
599         UResourceBundle b = rb.get("NumberPatterns");
600         if(b != null){
601             if(b.getSize()>0){
602                 logln("%%ALIAS mechanism works");
603             }else{
604                 errln("%%ALIAS mechanism failed for iw_IL collations");
605             }
606         }else{
607             errln("%%ALIAS mechanism failed for iw_IL");
608         }
609     }
610     public void TestXPathAlias(){
611         UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader);
612         UResourceBundle b = rb.get("aliasClient");
613         String result = b.getString();
614         String expResult= "correct"; 
615
616         if(!result.equals(expResult)){
617             errln("Did not get the expected result for XPath style alias");
618         }
619         try{
620             UResourceBundle c = rb.get("rootAliasClient");
621             result = c.getString();
622             expResult = "correct"; 
623             if(!result.equals(expResult)){
624                 errln("Did not get the expected result for XPath style alias for rootAliasClient");
625             }
626         }catch( MissingResourceException ex){
627             errln("Could not get rootAliasClient");
628         }
629     }
630     public void TestCircularAliases(){
631 // Aliases no longer supported
632 //        try{
633 //            UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
634 //            UResourceBundle sub = rb.get("aaa");
635 //            String s1 = sub.getString();
636 //            if(s1!=null){
637 //                errln("Did not get the expected exception");
638 //            }
639 //        }catch(IllegalArgumentException ex){
640 //            logln("got expected exception for circular references");
641 //        }
642 //        catch (MissingResourceException ex) {
643 //            warnln("could not load resource data: " + ex.getMessage());
644 //        }
645     }
646
647     public void TestGetWithFallback(){
648         /*
649         UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");
650         String key = bundle.getStringWithFallback("Keys/collation");
651         if(!key.equals("COLLATION")){
652             errln("Did not get the expected result from getStringWithFallback method.");
653         }
654         String type = bundle.getStringWithFallback("Types/collation/direct");
655         if(!type.equals("DIRECT")){
656             errln("Did not get the expected result form getStringWithFallback method.");
657         }
658         */
659         ICUResourceBundle bundle = null;
660         String key = null;
661         try{
662             bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,ULocale.canonicalize("de__PHONEBOOK"));
663
664             if(!bundle.getULocale().equals("de")){
665                 errln("did not get the expected bundle");
666             }
667             key = bundle.getStringWithFallback("collations/collation/default");
668             if(!key.equals("phonebook")){
669                 errln("Did not get the expected result from getStringWithFallback method.");
670             }
671
672         }catch(MissingResourceException ex){
673             logln("got the expected exception");
674         }
675
676
677         bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"fr_FR");
678         key = bundle.getStringWithFallback("collations/default");
679         if(!key.equals("standard")){
680             errln("Did not get the expected result from getStringWithFallback method.");
681         }
682         bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"fr_FR");
683         ICUResourceBundle b1 = bundle.getWithFallback("calendar");
684         String defaultCal = b1.getStringWithFallback("default");
685         if(!defaultCal.equals("gregorian")){
686             errln("Did not get the expected default calendar string: Expected: gregorian, Got: "+defaultCal);
687         }
688         ICUResourceBundle b2 = b1.getWithFallback(defaultCal);
689         ICUResourceBundle b3 = b2.getWithFallback("monthNames");
690         String defaultContext = b3.getStringWithFallback("default");
691         ICUResourceBundle b4 = b3.getWithFallback(defaultContext);
692         String defaultWidth  = b4.getStringWithFallback("default");
693         ICUResourceBundle b5 = b4.getWithFallback(defaultWidth);
694         if(b5.getSize()!=12){
695             errln("Did not get the expected size for the default monthNames");
696         }
697     }
698
699     private static final String COLLATION_RESNAME = "collations";
700     private static final String COLLATION_KEYWORD = "collation";
701     private static final String DEFAULT_NAME = "default";
702     private static final String STANDARD_NAME = "standard";
703     private static final String CALENDAR_RESNAME = "calendar";
704     private static final String CALENDAR_KEYWORD = "calendar";
705
706     public void TestKeywordValues(){
707         String kwVals[];
708         boolean foundStandard = false;
709         int n;
710
711         logln("Testing getting collation values:");
712         kwVals = ICUResourceBundle.getKeywordValues(ICUResourceBundle.ICU_COLLATION_BASE_NAME,COLLATION_RESNAME);
713         for(n=0;n<kwVals.length;n++) {
714             logln(new Integer(n).toString() + ": " + kwVals[n]);
715             if(DEFAULT_NAME.equals(kwVals[n])) {
716                 errln("getKeywordValues for collation returned 'default' in the list.");
717             } else if(STANDARD_NAME.equals(kwVals[n])) {
718                 if(foundStandard == false) {
719                     foundStandard = true;
720                     logln("found 'standard'");
721                 } else {
722                     errln("Error - 'standard' is in the keyword list twice!");
723                 }
724             }
725         }
726
727         if(foundStandard == false) {
728             errln("Error - 'standard' was not in the collation tree as a keyword.");
729         } else {
730             logln("'standard' was found as a collation keyword.");
731         }
732     }
733
734     public void TestLocaleDisplayNames() {
735         ULocale[] locales = ULocale.getAvailableLocales();
736         for (int i = 0; i < locales.length; ++i) {
737             if (!hasLocalizedCountryFor(ULocale.ENGLISH, locales[i])){
738                  errln("Could not get localized country for "+ locales[i]);
739             }
740             if(!hasLocalizedLanguageFor(ULocale.ENGLISH, locales[i])){
741                 errln("Could not get localized language for "+ locales[i]);
742             }
743             if(!hasLocalizedCountryFor(locales[i], locales[i])){
744                 errln("Could not get localized country for "+ locales[i]);
745                 hasLocalizedCountryFor(locales[i], locales[i]);
746             }
747             if(!hasLocalizedLanguageFor(locales[i], locales[i])){
748                 errln("Could not get localized language for "+ locales[i]);
749             }
750
751             logln(locales[i] + "\t" + locales[i].getDisplayName(ULocale.ENGLISH) + "\t" + locales[i].getDisplayName(locales[i]));
752         }
753     }
754
755     private static boolean hasLocalizedLanguageFor(ULocale locale, ULocale otherLocale) {
756         String lang = otherLocale.getLanguage();
757         String localizedVersion = otherLocale.getDisplayLanguage(locale);
758         return !lang.equals(localizedVersion);
759     }
760
761     private static boolean hasLocalizedCountryFor(ULocale locale, ULocale otherLocale) {
762         String country = otherLocale.getCountry();
763         if (country.equals("")) return true;
764         String localizedVersion = otherLocale.getDisplayCountry(locale);
765         return !country.equals(localizedVersion);
766     }
767
768     public void TestFunctionalEquivalent(){
769        String[] collCases = {
770        //  avail   locale                               equiv
771            "f",     "de_US_CALIFORNIA",                 "de",
772            "f",     "zh_TW@collation=stroke",           "zh@collation=stroke", /* alias of zh_Hant_TW */
773            "t",     "zh_Hant_TW@collation=stroke",      "zh@collation=stroke",
774            "f",     "de_CN@collation=pinyin",           "de",
775            "t",     "zh@collation=pinyin",              "zh",
776            "f",     "zh_CN@collation=pinyin",           "zh", /* alias of zh_Hans_CN */
777            "t",     "zh_Hans_CN@collation=pinyin",      "zh",
778            "f",     "zh_HK@collation=pinyin",           "zh", /* alias of zh_Hant_HK */
779            "t",     "zh_Hant_HK@collation=pinyin",      "zh",
780            "f",     "zh_HK@collation=stroke",           "zh@collation=stroke", /* alias of zh_Hant_HK */
781            "t",     "zh_Hant_HK@collation=stroke",      "zh@collation=stroke",
782            "f",     "zh_HK",                            "zh@collation=stroke", /* alias of zh_Hant_HK */
783            "t",     "zh_Hant_HK",                       "zh@collation=stroke",
784            "f",     "zh_MO",                            "zh@collation=stroke", /* alias of zh_Hant_MO */
785            "t",     "zh_Hant_MO",                       "zh@collation=stroke",
786            "f",     "zh_TW_STROKE",                     "zh@collation=stroke",
787            "f",     "zh_TW_STROKE@collation=big5han",   "zh@collation=big5han",
788            "f",     "de_CN@calendar=japanese",          "de",
789            "t",     "de@calendar=japanese",             "de",
790            "f",     "zh_TW@collation=big5han",          "zh@collation=big5han", /* alias of zh_Hant_TW */
791            "t",     "zh_Hant_TW@collation=big5han",     "zh@collation=big5han",
792            "f",     "zh_TW@collation=gb2312han",        "zh@collation=gb2312han", /* alias of zh_Hant_TW */
793            "t",     "zh_Hant_TW@collation=gb2312han",   "zh@collation=gb2312han",
794            "f",     "zh_CN@collation=big5han",          "zh@collation=big5han", /* alias of zh_Hans_CN */
795            "t",     "zh_Hans_CN@collation=big5han",     "zh@collation=big5han",
796            "f",     "zh_CN@collation=gb2312han",        "zh@collation=gb2312han", /* alias of zh_Hans_CN */
797            "t",     "zh_Hans_CN@collation=gb2312han",   "zh@collation=gb2312han",
798            "t",     "zh@collation=big5han",             "zh@collation=big5han",
799            "t",     "zh@collation=gb2312han",           "zh@collation=gb2312han",
800            "t",     "hi_IN@collation=direct",           "hi@collation=direct",
801            "t",     "hi@collation=standard",            "hi",
802            "t",     "hi@collation=direct",              "hi@collation=direct",
803            "f",     "hi_AU@collation=direct;currency=CHF;calendar=buddhist",    "hi@collation=direct",
804            "f",     "hi_AU@collation=standard;currency=CHF;calendar=buddhist",  "hi",
805            "t",     "de_DE@collation=pinyin",           "de", /* bug 4582 tests */
806            "f",     "de_DE_BONN@collation=pinyin",      "de",
807            "t",     "nl",                               "root",
808            "t",     "nl_NL",                            "root",
809            "f",     "nl_NL_EEXT",                       "root",
810            "t",     "nl@collation=stroke",              "root",
811            "t",     "nl_NL@collation=stroke",           "root",
812            "f",     "nl_NL_EEXT@collation=stroke",      "root",
813        };
814
815        String[] calCases = {
816        //  avail    locale                              equiv
817            "t",     "en_US_POSIX",                      "en_US@calendar=gregorian",
818            "f",     "ja_JP_TOKYO",                      "ja_JP@calendar=gregorian",
819            "f",     "ja_JP_TOKYO@calendar=japanese",    "ja@calendar=japanese",
820            "t",     "sr@calendar=gregorian",            "sr@calendar=gregorian",
821            "t",     "en",                               "en@calendar=gregorian",
822            "t",     "th_TH",                            "th@calendar=buddhist",
823            "t",     "th_TH@calendar=gregorian",         "th_TH@calendar=gregorian",
824            "f",     "th_TH_Bangkok",                    "th@calendar=buddhist",
825        };
826
827        logln("Testing functional equivalents for collation...");
828        getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
829                COLLATION_RESNAME, COLLATION_KEYWORD, true, collCases);
830
831        logln("Testing functional equivalents for calenadr...");
832        getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_BASE_NAME,
833                CALENDAR_RESNAME, CALENDAR_KEYWORD, false, calCases);
834
835        logln("Testing error conditions:");
836        try {
837            ICUResourceBundle.getFunctionalEquivalent(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "calendar",
838               "calendar", new ULocale("ar_EG@calendar=islamic"), new boolean[1], true);
839            errln("Err: expected MissingResourceException");
840        } catch ( MissingResourceException t ) {
841            logln("expected MissingResourceException caught (PASS): " + t.toString());
842        }
843     }
844
845     private void getFunctionalEquivalentTestCases(String path, String resName, String keyword,
846             boolean truncate, String[] testCases) {
847         //String F_STR = "f";
848         String T_STR = "t";
849         boolean isAvail[] = new boolean[1];
850
851         logln("Testing functional equivalents...");
852         for(int i = 0; i < testCases.length ;i+=3) {
853             boolean expectAvail = T_STR.equals(testCases[i+0]);
854             ULocale inLocale = new ULocale(testCases[i+1]);
855             ULocale expectLocale = new ULocale(testCases[i+2]);
856
857             logln(new Integer(i/3).toString() + ": " + new Boolean(expectAvail).toString() + "\t\t" +
858                     inLocale.toString() + "\t\t" + expectLocale.toString());
859
860             ULocale equivLocale = ICUResourceBundle.getFunctionalEquivalent(path, resName, keyword, inLocale, isAvail, truncate);
861             boolean gotAvail = isAvail[0];
862
863             if((gotAvail != expectAvail) || !equivLocale.equals(expectLocale)) {
864                 errln(new Integer(i/3).toString() + ":  Error, expected  Equiv=" + new Boolean(expectAvail).toString() + "\t\t" +
865                         inLocale.toString() + "\t\t--> " + expectLocale.toString() + ",  but got " + new Boolean(gotAvail).toString() + " " +
866                         equivLocale.toString());
867             }
868         }
869     }
870
871     public void TestNorwegian(){
872         try{
873             UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "no_NO_NY");
874             UResourceBundle sub = rb.get("Countries");
875             String s1 = sub.getString("NO");
876             if(s1.equals("Noreg")){
877                 logln("got expected output ");
878             }else{
879                 errln("did not get the expected result");
880             }
881         }catch(IllegalArgumentException ex){
882             errln("Caught an unexpected expected");
883         }
884     }
885     public void TestJB4102(){
886         try {
887             ICUResourceBundle root =(ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "root");
888             ICUResourceBundle t = null;    
889 // AmPmMarkers now exist in root/islamic calendar, so this test is rendered useless.
890 //          try{
891 //              t = root.getWithFallback("calendar/islamic-civil/AmPmMarkers");
892 //              errln("Second resource does not exist. How did it get here?\n");
893 //          }catch(MissingResourceException ex){
894 //              logln("Got the expected exception");
895 //          }
896             try{
897                 t = root.getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera");
898                 errln("Second resource does not exist. How did it get here?\n");
899             }catch(MissingResourceException ex){
900                 logln("Got the expected exception");
901             }
902             if(t!=null){
903                 errln("t is not null!");
904             }
905         } catch (MissingResourceException e) {
906            warnln("Could not load the locale data: " + e.getMessage());
907         }
908     }
909
910     public void TestCLDRStyleAliases() {
911         String result = null;
912         String expected = null;
913         String[]expects = new String[] { "", "a41", "a12", "a03", "ar4" };
914
915         logln("Testing CLDR style aliases......\n");
916
917         UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED",testLoader);
918         ICUResourceBundle alias = (ICUResourceBundle)rb.get("a");
919
920         for(int i = 1; i < 5 ; i++) {
921           String resource="a"+i;
922           UResourceBundle a = (alias).getWithFallback(resource);
923           result = a.getString();
924           if(result.equals(expected)) {
925               errln("CLDR style aliases failed resource with name "+resource+"resource, exp "+expects[i] +" , got " + result); 
926           }
927         }
928
929     }
930     private String getLSString(int status){
931         switch(status){
932             case ICUResourceBundle.FROM_FALLBACK:
933                 return "FROM_FALLBACK";
934             case ICUResourceBundle.FROM_DEFAULT:
935                 return "FROM_DEFAULT";
936             case ICUResourceBundle.FROM_ROOT: 
937                 return "FROM_ROOT";
938             case ICUResourceBundle.FROM_LOCALE: 
939                 return "FROM_LOCALE";
940             default:
941                 return "UNKNOWN";
942         }
943     }
944     public void TestLoadingStatus(){
945         ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "yi_IL");
946         int status = bundle.getLoadingStatus();
947         if(status != ICUResourceBundle.FROM_DEFAULT){
948             errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_DEFAULT) 
949                     + " Got: " + getLSString(status));
950         }        
951         bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "eo_DE");
952         status = bundle.getLoadingStatus();
953         if(status != ICUResourceBundle.FROM_FALLBACK){
954             errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK) 
955                     + " Got: " + getLSString(status));
956         }        
957         
958         logln("Test to verify loading status of get(String)");
959         bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "te_IN");
960         UResourceBundle countries = bundle.get("Countries");
961         status = ((ICUResourceBundle)countries).getLoadingStatus();
962         if(status != ICUResourceBundle.FROM_FALLBACK){
963             errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK) 
964                     + " Got: " + getLSString(status));
965         }
966         /*
967         UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters");
968         status = auxExemplar.getLoadingStatus();
969         if(status != UResourceBundle.FROM_ROOT){
970             errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT) 
971                     + " Got: " + getLSString(status));
972         } 
973         */
974         logln("Test to verify loading status of get(int)");
975         ICUResourceBundle ms = (ICUResourceBundle)bundle.get("MeasurementSystem");
976         status = ms.getLoadingStatus();
977         if(status != ICUResourceBundle.FROM_ROOT){
978             errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT) 
979                     + " Got: " + getLSString(status));
980         }
981                 
982         logln("Test to verify loading status of getwithFallback");
983         bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU",testLoader);
984         ICUResourceBundle temp = (ICUResourceBundle)bundle.getWithFallback("a/a2");
985         status = temp.getLoadingStatus();
986         if(status != ICUResourceBundle.FROM_LOCALE){
987             errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_LOCALE) 
988                     + " Got: " + getLSString(status));
989         }
990         temp = bundle.getWithFallback("a/a1");
991         status = temp.getLoadingStatus();
992         if(status != ICUResourceBundle.FROM_FALLBACK){
993             errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK) 
994                     + " Got: " + getLSString(status));
995         }
996         temp = bundle.getWithFallback("a/a4");
997         status = temp.getLoadingStatus();
998         if(status != ICUResourceBundle.FROM_ROOT){
999             errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT) 
1000                     + " Got: " + getLSString(status));
1001         }
1002     }
1003     public void TestCoverage(){
1004         UResourceBundle bundle;
1005         bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);
1006         if (bundle == null){
1007             errln("UResourceBundle.getBundleInstance(String baseName) failed");
1008         }
1009         bundle = null;
1010         bundle = UResourceBundle.getBundleInstance(ULocale.getDefault());
1011         if (bundle == null){
1012             errln("UResourceBundle.getBundleInstance(ULocale) failed");
1013             return;
1014         } 
1015         if (new UResourceTypeMismatchException("coverage") == null){
1016             errln("Create UResourceTypeMismatchException error");
1017         }
1018         class Stub extends UResourceBundle{
1019             public ULocale getULocale() {return ULocale.ROOT;}
1020             protected String getLocaleID() {return null;}
1021             protected String getBaseName() {return null;}
1022             protected UResourceBundle getParent() {return null;}
1023             protected void setLoadingStatus(int newStatus) {}
1024             public Enumeration getKeys() {return null;}
1025             protected Object handleGetObject(String aKey) {return null;}
1026         }
1027         Stub stub = new Stub();
1028         
1029         if (!stub.getLocale().equals(ULocale.ROOT.toLocale())){
1030             errln("UResourceBundle.getLoclae(Locale) should delegate to (ULocale)");
1031         }
1032     }
1033     public void TestJavaULocaleBundleLoading(){
1034         String baseName="com.ibm.icu.dev.data.resources.TestDataElements";
1035         String locName = "en_Latn_US";
1036         UResourceBundle bundle = UResourceBundle.getBundleInstance(baseName, locName, testLoader);
1037         String fromRoot = bundle.getString("from_root");
1038         if(!fromRoot.equals("This data comes from root")){
1039             errln("Did not get the expected string for from_root");
1040         }
1041         String fromEn = bundle.getString("from_en");
1042         if(!fromEn.equals("This data comes from en")){
1043             errln("Did not get the expected string for from_en");
1044         }
1045         String fromEnLatn = bundle.getString("from_en_Latn");
1046         if(!fromEnLatn.equals("This data comes from en_Latn")){
1047             errln("Did not get the expected string for from_en_Latn");
1048         }
1049         String fromEnLatnUs = bundle.getString("from_en_Latn_US");
1050         if(!fromEnLatnUs.equals("This data comes from en_Latn_US")){
1051             errln("Did not get the expected string for from_en_Latn_US");
1052         }
1053         UResourceBundle bundle1 = UResourceBundle.getBundleInstance(baseName, new ULocale(locName), testLoader);
1054         if(!bundle1.equals(bundle)){
1055             errln("Did not get the expected bundle for "+baseName +"."+locName);
1056         }
1057         if(bundle1!=bundle){
1058             errln("Did not load the bundle from cache");
1059         }
1060         
1061         UResourceBundle bundle2 = UResourceBundle.getBundleInstance(baseName, "en_IN", testLoader);
1062         if(!bundle2.getLocale().toString().equals("en")){
1063             errln("Did not get the expected fallback locale. Expected: en Got: "+bundle2.getLocale().toString());    
1064         }
1065         UResourceBundle bundle3 = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader);
1066         if(!bundle3.getLocale().toString().equals("te")){
1067             errln("Did not get the expected fallback locale. Expected: te Got: "+bundle2.getLocale().toString());    
1068         }
1069         // non-existent bundle .. should return default
1070         UResourceBundle defaultBundle = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
1071         ULocale defaultLocale = ULocale.getDefault();
1072         if(!defaultBundle.getULocale().equals(defaultLocale)){
1073             errln("Did not get the default bundle for non-existent bundle");
1074         }
1075         // non-existent bundle, non-existent default locale
1076         // so return the root bundle.
1077         ULocale.setDefault(ULocale.CANADA_FRENCH);
1078         UResourceBundle root = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
1079         if(!root.getULocale().toString().equals("")){
1080             errln("Did not get the root bundle for non-existent default bundle for non-existent bundle");
1081         }        
1082         //reset the default
1083         ULocale.setDefault(defaultLocale);
1084         Enumeration keys = bundle.getKeys();
1085         int i=0;
1086         while(keys.hasMoreElements()){
1087             logln("key: "+ keys.nextElement());
1088             i++;
1089         }
1090         if(i!=4){
1091             errln("Did not get the expected number of keys: got " + i + ", expected 4");
1092         }
1093         UResourceBundle bundle4 = UResourceBundle.getBundleInstance(baseName,"fr_Latn_FR", testLoader);
1094         if(bundle4==null){
1095             errln("Could not load bundle fr_Latn_FR");
1096         }
1097     }
1098     public void TestAliasFallback(){
1099         try{
1100             ULocale loc = new ULocale("en_US");
1101             ICUResourceBundle b = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc);
1102             ICUResourceBundle b1 = (ICUResourceBundle)b.getWithFallback("calendar/hebrew/monthNames/format/abbreviated");
1103             if(b1!=null){
1104                 logln("loaded data for abbreviated month names: "+ b1.getKey()); 
1105             }
1106         }catch(MissingResourceException ex){
1107             warnln("Failed to load data for abbreviated month names");
1108         }
1109     }
1110 }
1111