-//##header\r
-/**\r
- *******************************************************************************\r
- * Copyright (C) 2001-2009, International Business Machines Corporation and *\r
- * others. All Rights Reserved. *\r
- *******************************************************************************\r
- */\r
-package com.ibm.icu.dev.test.util;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.InputStream;\r
-import java.io.InputStreamReader;\r
-import java.net.URL;\r
-import java.net.URLConnection;\r
-import java.net.JarURLConnection;\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//##import com.ibm.icu.impl.ByteBuffer;\r
-//#else\r
-import java.nio.ByteBuffer;\r
-//#endif\r
-import java.util.MissingResourceException;\r
-import java.util.Enumeration;\r
-import java.util.jar.JarEntry;\r
-\r
-import com.ibm.icu.dev.test.TestFmwk;\r
-import com.ibm.icu.impl.ICUResourceBundle;\r
-import com.ibm.icu.impl.Utility;\r
-import com.ibm.icu.text.UTF16;\r
-import com.ibm.icu.util.Holiday;\r
-import com.ibm.icu.util.ULocale;\r
-import com.ibm.icu.util.UResourceBundle;\r
-import com.ibm.icu.util.UResourceTypeMismatchException;\r
-\r
-\r
-public final class ICUResourceBundleTest extends TestFmwk {\r
- private static final ClassLoader testLoader = ICUResourceBundleTest.class.getClassLoader();\r
-\r
- public static void main(String args[]) throws Exception {\r
- ICUResourceBundleTest test = new ICUResourceBundleTest();\r
- test.run(args);\r
-\r
- }\r
- public void TestGetResources(){\r
- try{\r
- // It does not work well in eclipse plug-in test because of class loader configuration??\r
- // For now, specify resource path explicitly in this test case\r
- //Enumeration en = testLoader.getResources("META-INF");\r
- Enumeration en = testLoader.getResources("com.ibm.icu.dev.data");\r
- for(;en.hasMoreElements();) {\r
- URL url = (URL)en.nextElement();\r
- if (url == null) {\r
- warnln("could not load resource data");\r
- return;\r
- }\r
- URLConnection c = url.openConnection();\r
-\r
- if (c instanceof JarURLConnection) {\r
- JarURLConnection jc = (JarURLConnection)c;\r
- JarEntry je = jc.getJarEntry();\r
- logln("jar entry: " + je.toString()); \r
- } else {\r
- InputStream is = c.getInputStream();\r
- logln("input stream:");\r
- InputStreamReader r = new InputStreamReader(is);\r
- BufferedReader br = new BufferedReader(r);\r
- String line = null;\r
- int n = 0;\r
- while ((line = br.readLine()) != null) {\r
- logln(" " + ++n + ": " + line);\r
- }\r
- }\r
- }\r
- }catch(SecurityException ex) {\r
- warnln("could not load resource data: " + ex);\r
- ex.printStackTrace();\r
- }catch(NullPointerException ex) {\r
- // thrown by ibm 1.4.2 windows jvm security manager\r
- warnln("could not load resource data: " + ex);\r
- }catch(Exception ex){\r
- ex.printStackTrace();\r
- errln("Unexpected exception: "+ ex);\r
- }\r
- }\r
- public void TestResourceBundleWrapper(){\r
- UResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", "da_DK");\r
- Object o = bundle.getObject("holidays");\r
- if(o instanceof Holiday[] ){\r
- logln("wrapper mechanism works for Weekend data");\r
- }else{\r
- errln("Did not get the expected output for Weekend data");\r
- }\r
-\r
- bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "bogus");\r
- if(bundle instanceof UResourceBundle && bundle.getULocale().equals("en_US")){\r
- logln("wrapper mechanism works for bogus locale");\r
- }else{\r
- errln("wrapper mechanism failed for bogus locale.");\r
- }\r
-\r
- try{\r
- bundle = UResourceBundle.getBundleInstance("bogus", "bogus");\r
- if(bundle!=null){\r
- errln("Did not get the expected exception");\r
- }\r
- }catch(MissingResourceException ex){\r
- logln("got the expected exception");\r
- }\r
-\r
-\r
- }\r
- public void TestJB3879(){\r
- // this tests tests loading of root bundle when a resource bundle\r
- // for the default locale is requested\r
- try {\r
- UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", ULocale.getDefault().toString(), testLoader);\r
- if(bundle==null){\r
- errln("could not create the resource bundle");\r
- }\r
- }\r
- catch (MissingResourceException ex) {\r
- warnln("could not load test data: " + ex.getMessage());\r
- }\r
- }\r
- public void TestOpen(){\r
- UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "en_US_POSIX");\r
-\r
- if(bundle==null){\r
- errln("could not create the resource bundle");\r
- }\r
-\r
- UResourceBundle obj = bundle.get("NumberPatterns");\r
-\r
- int size = obj.getSize();\r
- int type = obj.getType();\r
- if(type == UResourceBundle.ARRAY){\r
- UResourceBundle sub;\r
- for(int i=0; i<size; i++){\r
- sub = obj.get(i);\r
- String temp =sub.getString();\r
- if(temp.length()==0){\r
- errln("Failed to get the items from NumberPatterns array in bundle: "+\r
- bundle.getULocale().getBaseName());\r
- }\r
- //System.out.println("\""+prettify(temp)+"\"");\r
- }\r
-\r
- }\r
- String[] strings = bundle.getStringArray("NumberPatterns");\r
- if(size!=strings.length){\r
- errln("Failed to get the items from NumberPatterns array in bundle: "+\r
- bundle.getULocale().getBaseName());\r
- }\r
- {\r
- obj = bundle.get("NumberElements");\r
-\r
- size = obj.getSize();\r
- type = obj.getType();\r
- if(type == UResourceBundle.ARRAY){\r
- UResourceBundle sub;\r
- for(int i=0; i<size; i++){\r
- sub = obj.get(i);\r
- String temp =sub.getString();\r
- if(temp.length()==0){\r
- errln("Failed to get the items from NumberPatterns array in bundle: "+\r
- bundle.getULocale().getBaseName());\r
- }\r
- // System.out.println("\""+prettify(temp)+"\"");\r
- }\r
-\r
- }\r
- }\r
- if(bundle==null){\r
- errln("could not create the resource bundle");\r
- }\r
- bundle = (UResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "en_US_POSIX");\r
- if(bundle==null){\r
- errln("could not load the stream");\r
- }\r
- bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zzz_ZZ_very_very_very_long_bogus_bundle");\r
- if(!bundle.getULocale().equals(ULocale.getDefault())){\r
- errln("UResourceBundle did not load the default bundle when bundle was not found. Default: " + ULocale.getDefault() + \r
- ", Bundle locale: " + bundle.getULocale());\r
- }\r
-\r
-\r
- }\r
-\r
- public void TestBasicTypes(){\r
- UResourceBundle bundle = null;\r
- try {\r
- bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes", testLoader);\r
- }\r
- catch (MissingResourceException e) {\r
- warnln("could not load test data: " + e.getMessage());\r
- return;\r
- }\r
- {\r
- String expected = "abc\u0000def";\r
- UResourceBundle sub = bundle.get("zerotest");\r
- if(!expected.equals(sub.getString())){\r
- errln("Did not get the expected string for key zerotest in bundle testtypes");\r
- }\r
- sub = bundle.get("emptyexplicitstring");\r
- expected ="";\r
- if(!expected.equals(sub.getString())){\r
- errln("Did not get the expected string for key emptyexplicitstring in bundle testtypes");\r
- }\r
- sub = bundle.get("emptystring");\r
- expected ="";\r
- if(!expected.equals(sub.getString())){\r
- errln("Did not get the expected string for key emptystring in bundle testtypes");\r
- }\r
- }\r
- {\r
- int expected = 123;\r
- UResourceBundle sub = bundle.get("onehundredtwentythree");\r
- if(expected!=sub.getInt()){\r
- errln("Did not get the expected int value for key onehundredtwentythree in bundle testtypes");\r
- }\r
- sub = bundle.get("emptyint");\r
- expected=0;\r
- if(expected!=sub.getInt()){\r
- errln("Did not get the expected int value for key emptyint in bundle testtypes");\r
- }\r
- }\r
- {\r
- int expected = 1;\r
- UResourceBundle sub = bundle.get("one");\r
- if(expected!=sub.getInt()){\r
- errln("Did not get the expected int value for key one in bundle testtypes");\r
- }\r
- }\r
- {\r
- int expected = -1;\r
- UResourceBundle sub = bundle.get("minusone");\r
- int got = sub.getInt();\r
- if(expected!=got){\r
- errln("Did not get the expected int value for key minusone in bundle testtypes");\r
- }\r
- expected = 0xFFFFFFF;\r
- got = sub.getUInt();\r
- if(expected!=got){\r
- errln("Did not get the expected int value for key minusone in bundle testtypes");\r
- }\r
- }\r
- {\r
- int expected = 1;\r
- UResourceBundle sub = bundle.get("plusone");\r
- if(expected!=sub.getInt()){\r
- errln("Did not get the expected int value for key minusone in bundle testtypes");\r
- }\r
-\r
- }\r
- {\r
- int[] expected = new int[]{ 1, 2, 3, -3, 4, 5, 6, 7 } ;\r
- UResourceBundle sub = bundle.get("integerarray");\r
- if(!Utility.arrayEquals(expected,sub.getIntVector())){\r
- errln("Did not get the expected int vector value for key integerarray in bundle testtypes");\r
- }\r
- sub = bundle.get("emptyintv");\r
- expected = new int[0];\r
- if(!Utility.arrayEquals(expected,sub.getIntVector())){\r
- errln("Did not get the expected int vector value for key emptyintv in bundle testtypes");\r
- }\r
-\r
- }\r
- {\r
- UResourceBundle sub = bundle.get("binarytest");\r
- ByteBuffer got = sub.getBinary();\r
- if(got.remaining()!=15){\r
- errln("Did not get the expected length for the binary ByteBuffer");\r
- }\r
- for(int i=0; i< got.remaining(); i++){\r
- byte b = got.get();\r
- if(b!=i){\r
- errln("Did not get the expected value for binary buffer at index: "+i);\r
- }\r
- }\r
- sub = bundle.get("emptybin");\r
- got = sub.getBinary();\r
- if(got.remaining()!=0){\r
- errln("Did not get the expected length for the emptybin ByteBuffer");\r
- }\r
-\r
- }\r
- {\r
- UResourceBundle sub = bundle.get("emptyarray");\r
- String key = sub.getKey();\r
- if(!key.equals("emptyarray")){\r
- errln("Did not get the expected key for emptytable item");\r
- }\r
- if(sub.getSize()!=0){\r
- errln("Did not get the expected length for emptytable item");\r
- }\r
- }\r
- {\r
- UResourceBundle sub = bundle.get("menu");\r
- String key = sub.getKey();\r
- if(!key.equals("menu")){\r
- errln("Did not get the expected key for menu item");\r
- }\r
- UResourceBundle sub1 = sub.get("file");\r
- key = sub1.getKey();\r
- if(!key.equals("file")){\r
- errln("Did not get the expected key for file item");\r
- }\r
- UResourceBundle sub2 = sub1.get("open");\r
- key = sub2.getKey();\r
- if(!key.equals("open")){\r
- errln("Did not get the expected key for file item");\r
- }\r
- String value = sub2.getString();\r
- if(!value.equals("Open")){\r
- errln("Did not get the expected value for key for oen item");\r
- }\r
-\r
- sub = bundle.get("emptytable");\r
- key = sub.getKey();\r
- if(!key.equals("emptytable")){\r
- errln("Did not get the expected key for emptytable item");\r
- }\r
- if(sub.getSize()!=0){\r
- errln("Did not get the expected length for emptytable item");\r
- }\r
- sub = bundle.get("menu").get("file");\r
- int size = sub.getSize();\r
- String expected;\r
- for(int i=0; i<size; i++){\r
- sub1 = sub.get(i);\r
-\r
- switch(i){\r
- case 0:\r
- expected = "exit";\r
- break;\r
- case 1:\r
- expected = "open";\r
- break;\r
- case 2:\r
- expected = "save";\r
- break;\r
- default:\r
- expected ="";\r
- }\r
- String got = sub1.getKey();\r
- if(!expected.equals(got)){\r
- errln("Did not get the expected key at index"+i+". Expected: "+expected+" Got: "+got);\r
- }else{\r
- logln("Got the expected key at index: "+i);\r
- }\r
- }\r
- }\r
-\r
- }\r
- private static final class TestCase{\r
- String key;\r
- int value;\r
- TestCase(String key, int value){\r
- this.key = key;\r
- this.value = value;\r
- }\r
- }\r
- public void TestTable32(){\r
- TestCase[] arr = new TestCase[]{\r
- new TestCase ( "ooooooooooooooooo", 0 ),\r
- new TestCase ( "oooooooooooooooo1", 1 ),\r
- new TestCase ( "ooooooooooooooo1o", 2 ),\r
- new TestCase ( "oo11ooo1ooo11111o", 25150 ),\r
- new TestCase ( "oo11ooo1ooo111111", 25151 ),\r
- new TestCase ( "o1111111111111111", 65535 ),\r
- new TestCase ( "1oooooooooooooooo", 65536 ),\r
- new TestCase ( "1ooooooo11o11ooo1", 65969 ),\r
- new TestCase ( "1ooooooo11o11oo1o", 65970 ),\r
- new TestCase ( "1ooooooo111oo1111", 65999 )\r
- };\r
- UResourceBundle bundle = null;\r
- try {\r
- bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testtable32", testLoader);\r
- }\r
- catch (MissingResourceException ex) {\r
- warnln("could not load resource data: " + ex.getMessage());\r
- return;\r
- }\r
-\r
- if(bundle.getType()!= UResourceBundle.TABLE){\r
- errln("Could not get the correct type for bundle testtable32");\r
- }\r
- int size =bundle.getSize();\r
- if(size!=66000){\r
- errln("Could not get the correct size for bundle testtable32");\r
- }\r
- for(int i =0; i<size; i++){\r
- UResourceBundle item = bundle.get(i);\r
- String key = item.getKey();\r
- int parsedNumber = parseTable32Key(key);\r
- int number=-1;\r
- switch(item.getType()){\r
- case UResourceBundle.STRING:\r
- String value = item.getString();\r
- number = UTF16.charAt(value,0);\r
- break;\r
- case UResourceBundle.INT:\r
- number = item.getInt();\r
- break;\r
- default:\r
- errln("Got unexpected resource type in testtable32");\r
-\r
- }\r
- if(number!=parsedNumber){\r
- errln("Did not get expected value in testtypes32 for key"+\r
- key+". Expected: "+parsedNumber+" Got:"+number);\r
- }\r
-\r
- }\r
- for(int i=0;i<arr.length; i++){\r
- String expected = arr[i].key;\r
- UResourceBundle item = bundle.get(expected);\r
- int number=0;\r
- String key = item.getKey();\r
- int parsedNumber = parseTable32Key(key);\r
- if(!key.equals(expected)){\r
- errln("Did not get the expected key. Expected: "+expected+" Got:"+key);\r
- }\r
- switch(item.getType()){\r
- case UResourceBundle.STRING:\r
- String value = item.getString();\r
- number = UTF16.charAt(value,0);\r
- break;\r
- case UResourceBundle.INT:\r
- number = item.getInt();\r
- break;\r
- default:\r
- errln("Got unexpected resource type in testtable32");\r
- }\r
-\r
- if(number!=parsedNumber){\r
- errln("Did not get expected value in testtypes32 for key"+\r
- key+". Expected: "+parsedNumber+" Got:"+number);\r
- }\r
- }\r
- }\r
- private static int parseTable32Key(String key) {\r
- int number;\r
- char c;\r
-\r
- number=0;\r
- for(int i=0; i<key.length(); i++){\r
- c = key.charAt(i);\r
- number<<=1;\r
- if(c=='1') {\r
- number|=1;\r
- }\r
- }\r
- return number;\r
- }\r
-\r
- public void TestAliases(){\r
-/*\r
- String simpleAlias = "Open";\r
-\r
- UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases", testLoader);\r
- if (rb == null) {\r
- warnln("could not load testaliases data");\r
- return;\r
- }\r
- UResourceBundle sub = rb.get("simplealias");\r
- String s1 = sub.getString("simplealias");\r
- if(s1.equals(simpleAlias)){\r
- logln("Alias mechanism works for simplealias");\r
- }else{\r
- errln("Did not get the expected output for simplealias");\r
- }\r
- {\r
- try{\r
- rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);\r
- sub = rb.get("nonexisting");\r
- errln("Did not get the expected exception for nonexisting");\r
- }catch(MissingResourceException ex){\r
- logln("Alias mechanism works for nonexisting alias");\r
- }\r
- }\r
- {\r
- rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);\r
- sub = rb.get("referencingalias");\r
- s1 = sub.getString();\r
- if(s1.equals("Hani")){\r
- logln("Alias mechanism works for referencingalias");\r
- }else{\r
- errln("Did not get the expected output for referencingalias");\r
- }\r
- }\r
- {\r
- rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);\r
- sub = rb.get("boundaries");\r
- String word = sub.getString("word");\r
-\r
- if(word.equals("word_ja.brk")){\r
- logln("Got the expected output for boundaries/word");\r
- }else{\r
- errln("Did not get the expected type for boundaries/word");\r
- }\r
-\r
- }\r
- {\r
- UResourceBundle rb1 = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);\r
- if(rb1!=rb){\r
- errln("Caching of the resource bundle failed");\r
- }else{\r
- logln("Caching of resource bundle passed");\r
- }\r
- sub = rb1.get("testGetStringByKeyAliasing" );\r
-\r
- s1 = sub.get("KeyAlias0PST").getString();\r
- if(s1.equals("America/Los_Angeles")){\r
- logln("Alias mechanism works for KeyAlias0PST");\r
- }else{\r
- errln("Did not get the expected output for KeyAlias0PST");\r
- }\r
-\r
- s1 = sub.getString("KeyAlias1PacificStandardTime");\r
- if(s1.equals("Pacific Standard Time")){\r
- logln("Alias mechanism works for KeyAlias1PacificStandardTime");\r
- }else{\r
- errln("Did not get the expected output for KeyAlias1PacificStandardTime");\r
- }\r
- s1 = sub.getString("KeyAlias2PDT");\r
- if(s1.equals("PDT")){\r
- logln("Alias mechanism works for KeyAlias2PDT");\r
- }else{\r
- errln("Did not get the expected output for KeyAlias2PDT");\r
- }\r
-\r
- s1 = sub.getString("KeyAlias3LosAngeles");\r
- if(s1.equals("Los Angeles")){\r
- logln("Alias mechanism works for KeyAlias3LosAngeles. Got: "+s1);\r
- }else{\r
- errln("Did not get the expected output for KeyAlias3LosAngeles. Got: "+s1);\r
- }\r
- }\r
- {\r
- sub = rb.get("testGetStringByIndexAliasing" );\r
- s1 = sub.getString(0);\r
- if(s1.equals("America/Los_Angeles")){\r
- logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: "+s1);\r
- }else{\r
- errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: "+s1);\r
- }\r
- s1 = sub.getString(1);\r
- if(s1.equals("Pacific Standard Time")){\r
- logln("Alias mechanism works for testGetStringByIndexAliasing/1");\r
- }else{\r
- errln("Did not get the expected output for testGetStringByIndexAliasing/1");\r
- }\r
- s1 = sub.getString(2);\r
- if(s1.equals("PDT")){\r
- logln("Alias mechanism works for testGetStringByIndexAliasing/2");\r
- }else{\r
- errln("Did not get the expected output for testGetStringByIndexAliasing/2");\r
- }\r
-\r
- s1 = sub.getString(3);\r
- if(s1.equals("Los Angeles")){\r
- logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: "+s1);\r
- }else{\r
- errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: "+s1);\r
- }\r
- }\r
- {\r
- sub = rb.get("testAliasToTree" );\r
- \r
- ByteBuffer buf = sub.get("standard").get("%%CollationBin").getBinary();\r
- if(buf==null){\r
- errln("Did not get the expected output for %%CollationBin");\r
- }\r
- }\r
- // should not get an exception\r
- rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_RBNF_BASE_NAME,"fr_BE");\r
- String str = rb.getString("SpelloutRules");\r
- if(str !=null && str.length()>0){\r
- logln("Alias mechanism works");\r
- }else{\r
- errln("Alias mechanism failed for fr_BE SpelloutRules");\r
- }\r
- rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW");\r
- UResourceBundle b = (UResourceBundle) rb.getObject("collations");\r
- if(b != null){\r
- if(b.get(0).getKey().equals( "default")){\r
- logln("Alias mechanism works");\r
- }else{\r
- errln("Alias mechanism failed for zh_TW collations");\r
- }\r
- }else{\r
- errln("Did not get the expected object for collations");\r
- }\r
-*/\r
- }\r
- public void TestAlias(){\r
- logln("Testing %%ALIAS");\r
- UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"iw_IL");\r
- UResourceBundle b = rb.get("NumberPatterns");\r
- if(b != null){\r
- if(b.getSize()>0){\r
- logln("%%ALIAS mechanism works");\r
- }else{\r
- errln("%%ALIAS mechanism failed for iw_IL collations");\r
- }\r
- }else{\r
- errln("%%ALIAS mechanism failed for iw_IL");\r
- }\r
- }\r
- public void TestXPathAlias(){\r
- UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader);\r
- UResourceBundle b = rb.get("aliasClient");\r
- String result = b.getString();\r
- String expResult= "correct"; \r
-\r
- if(!result.equals(expResult)){\r
- errln("Did not get the expected result for XPath style alias");\r
- }\r
- try{\r
- UResourceBundle c = rb.get("rootAliasClient");\r
- result = c.getString();\r
- expResult = "correct"; \r
- if(!result.equals(expResult)){\r
- errln("Did not get the expected result for XPath style alias for rootAliasClient");\r
- }\r
- }catch( MissingResourceException ex){\r
- errln("Could not get rootAliasClient");\r
- }\r
- }\r
- public void TestCircularAliases(){\r
-// Aliases no longer supported\r
-// try{\r
-// UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);\r
-// UResourceBundle sub = rb.get("aaa");\r
-// String s1 = sub.getString();\r
-// if(s1!=null){\r
-// errln("Did not get the expected exception");\r
-// }\r
-// }catch(IllegalArgumentException ex){\r
-// logln("got expected exception for circular references");\r
-// }\r
-// catch (MissingResourceException ex) {\r
-// warnln("could not load resource data: " + ex.getMessage());\r
-// }\r
- }\r
-\r
- public void TestGetWithFallback(){\r
- /*\r
- UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");\r
- String key = bundle.getStringWithFallback("Keys/collation");\r
- if(!key.equals("COLLATION")){\r
- errln("Did not get the expected result from getStringWithFallback method.");\r
- }\r
- String type = bundle.getStringWithFallback("Types/collation/direct");\r
- if(!type.equals("DIRECT")){\r
- errln("Did not get the expected result form getStringWithFallback method.");\r
- }\r
- */\r
- ICUResourceBundle bundle = null;\r
- String key = null;\r
- try{\r
- bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,ULocale.canonicalize("de__PHONEBOOK"));\r
-\r
- if(!bundle.getULocale().equals("de")){\r
- errln("did not get the expected bundle");\r
- }\r
- key = bundle.getStringWithFallback("collations/collation/default");\r
- if(!key.equals("phonebook")){\r
- errln("Did not get the expected result from getStringWithFallback method.");\r
- }\r
-\r
- }catch(MissingResourceException ex){\r
- logln("got the expected exception");\r
- }\r
-\r
-\r
- bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"fr_FR");\r
- key = bundle.getStringWithFallback("collations/default");\r
- if(!key.equals("standard")){\r
- errln("Did not get the expected result from getStringWithFallback method.");\r
- }\r
- bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"fr_FR");\r
- ICUResourceBundle b1 = bundle.getWithFallback("calendar");\r
- String defaultCal = b1.getStringWithFallback("default");\r
- if(!defaultCal.equals("gregorian")){\r
- errln("Did not get the expected default calendar string: Expected: gregorian, Got: "+defaultCal);\r
- }\r
- ICUResourceBundle b2 = b1.getWithFallback(defaultCal);\r
- ICUResourceBundle b3 = b2.getWithFallback("monthNames");\r
- String defaultContext = b3.getStringWithFallback("default");\r
- ICUResourceBundle b4 = b3.getWithFallback(defaultContext);\r
- String defaultWidth = b4.getStringWithFallback("default");\r
- ICUResourceBundle b5 = b4.getWithFallback(defaultWidth);\r
- if(b5.getSize()!=12){\r
- errln("Did not get the expected size for the default monthNames");\r
- }\r
- }\r
-\r
- private static final String COLLATION_RESNAME = "collations";\r
- private static final String COLLATION_KEYWORD = "collation";\r
- private static final String DEFAULT_NAME = "default";\r
- private static final String STANDARD_NAME = "standard";\r
- private static final String CALENDAR_RESNAME = "calendar";\r
- private static final String CALENDAR_KEYWORD = "calendar";\r
-\r
- public void TestKeywordValues(){\r
- String kwVals[];\r
- boolean foundStandard = false;\r
- int n;\r
-\r
- logln("Testing getting collation values:");\r
- kwVals = ICUResourceBundle.getKeywordValues(ICUResourceBundle.ICU_COLLATION_BASE_NAME,COLLATION_RESNAME);\r
- for(n=0;n<kwVals.length;n++) {\r
- logln(new Integer(n).toString() + ": " + kwVals[n]);\r
- if(DEFAULT_NAME.equals(kwVals[n])) {\r
- errln("getKeywordValues for collation returned 'default' in the list.");\r
- } else if(STANDARD_NAME.equals(kwVals[n])) {\r
- if(foundStandard == false) {\r
- foundStandard = true;\r
- logln("found 'standard'");\r
- } else {\r
- errln("Error - 'standard' is in the keyword list twice!");\r
- }\r
- }\r
- }\r
-\r
- if(foundStandard == false) {\r
- errln("Error - 'standard' was not in the collation tree as a keyword.");\r
- } else {\r
- logln("'standard' was found as a collation keyword.");\r
- }\r
- }\r
-\r
- public void TestLocaleDisplayNames() {\r
- ULocale[] locales = ULocale.getAvailableLocales();\r
- for (int i = 0; i < locales.length; ++i) {\r
- if (!hasLocalizedCountryFor(ULocale.ENGLISH, locales[i])){\r
- errln("Could not get localized country for "+ locales[i]);\r
- }\r
- if(!hasLocalizedLanguageFor(ULocale.ENGLISH, locales[i])){\r
- errln("Could not get localized language for "+ locales[i]);\r
- }\r
- if(!hasLocalizedCountryFor(locales[i], locales[i])){\r
- errln("Could not get localized country for "+ locales[i]);\r
- hasLocalizedCountryFor(locales[i], locales[i]);\r
- }\r
- if(!hasLocalizedLanguageFor(locales[i], locales[i])){\r
- errln("Could not get localized language for "+ locales[i]);\r
- }\r
-\r
- logln(locales[i] + "\t" + locales[i].getDisplayName(ULocale.ENGLISH) + "\t" + locales[i].getDisplayName(locales[i]));\r
- }\r
- }\r
-\r
- private static boolean hasLocalizedLanguageFor(ULocale locale, ULocale otherLocale) {\r
- String lang = otherLocale.getLanguage();\r
- String localizedVersion = otherLocale.getDisplayLanguage(locale);\r
- return !lang.equals(localizedVersion);\r
- }\r
-\r
- private static boolean hasLocalizedCountryFor(ULocale locale, ULocale otherLocale) {\r
- String country = otherLocale.getCountry();\r
- if (country.equals("")) return true;\r
- String localizedVersion = otherLocale.getDisplayCountry(locale);\r
- return !country.equals(localizedVersion);\r
- }\r
-\r
- public void TestFunctionalEquivalent(){\r
- String[] collCases = {\r
- // avail locale equiv\r
- "f", "de_US_CALIFORNIA", "de",\r
- "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */\r
- "t", "zh_Hant_TW@collation=stroke", "zh@collation=stroke",\r
- "f", "de_CN@collation=pinyin", "de",\r
- "t", "zh@collation=pinyin", "zh",\r
- "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */\r
- "t", "zh_Hans_CN@collation=pinyin", "zh",\r
- "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */\r
- "t", "zh_Hant_HK@collation=pinyin", "zh",\r
- "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */\r
- "t", "zh_Hant_HK@collation=stroke", "zh@collation=stroke",\r
- "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */\r
- "t", "zh_Hant_HK", "zh@collation=stroke",\r
- "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */\r
- "t", "zh_Hant_MO", "zh@collation=stroke",\r
- "f", "zh_TW_STROKE", "zh@collation=stroke",\r
- "f", "zh_TW_STROKE@collation=big5han", "zh@collation=big5han",\r
- "f", "de_CN@calendar=japanese", "de",\r
- "t", "de@calendar=japanese", "de",\r
- "f", "zh_TW@collation=big5han", "zh@collation=big5han", /* alias of zh_Hant_TW */\r
- "t", "zh_Hant_TW@collation=big5han", "zh@collation=big5han",\r
- "f", "zh_TW@collation=gb2312han", "zh@collation=gb2312han", /* alias of zh_Hant_TW */\r
- "t", "zh_Hant_TW@collation=gb2312han", "zh@collation=gb2312han",\r
- "f", "zh_CN@collation=big5han", "zh@collation=big5han", /* alias of zh_Hans_CN */\r
- "t", "zh_Hans_CN@collation=big5han", "zh@collation=big5han",\r
- "f", "zh_CN@collation=gb2312han", "zh@collation=gb2312han", /* alias of zh_Hans_CN */\r
- "t", "zh_Hans_CN@collation=gb2312han", "zh@collation=gb2312han",\r
- "t", "zh@collation=big5han", "zh@collation=big5han",\r
- "t", "zh@collation=gb2312han", "zh@collation=gb2312han",\r
- "t", "hi_IN@collation=direct", "hi@collation=direct",\r
- "t", "hi@collation=standard", "hi",\r
- "t", "hi@collation=direct", "hi@collation=direct",\r
- "f", "hi_AU@collation=direct;currency=CHF;calendar=buddhist", "hi@collation=direct",\r
- "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi",\r
- "t", "de_DE@collation=pinyin", "de", /* bug 4582 tests */\r
- "f", "de_DE_BONN@collation=pinyin", "de",\r
- "t", "nl", "root",\r
- "t", "nl_NL", "root",\r
- "f", "nl_NL_EEXT", "root",\r
- "t", "nl@collation=stroke", "root",\r
- "t", "nl_NL@collation=stroke", "root",\r
- "f", "nl_NL_EEXT@collation=stroke", "root",\r
- };\r
-\r
- String[] calCases = {\r
- // avail locale equiv\r
- "t", "en_US_POSIX", "en_US@calendar=gregorian",\r
- "f", "ja_JP_TOKYO", "ja_JP@calendar=gregorian",\r
- "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",\r
- "t", "sr@calendar=gregorian", "sr@calendar=gregorian",\r
- "t", "en", "en@calendar=gregorian",\r
- "t", "th_TH", "th@calendar=buddhist",\r
- "t", "th_TH@calendar=gregorian", "th_TH@calendar=gregorian",\r
- "f", "th_TH_Bangkok", "th@calendar=buddhist",\r
- };\r
-\r
- logln("Testing functional equivalents for collation...");\r
- getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_COLLATION_BASE_NAME,\r
- COLLATION_RESNAME, COLLATION_KEYWORD, true, collCases);\r
-\r
- logln("Testing functional equivalents for calenadr...");\r
- getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_BASE_NAME,\r
- CALENDAR_RESNAME, CALENDAR_KEYWORD, false, calCases);\r
-\r
- logln("Testing error conditions:");\r
- try {\r
- ICUResourceBundle.getFunctionalEquivalent(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "calendar",\r
- "calendar", new ULocale("ar_EG@calendar=islamic"), new boolean[1], true);\r
- errln("Err: expected MissingResourceException");\r
- } catch ( MissingResourceException t ) {\r
- logln("expected MissingResourceException caught (PASS): " + t.toString());\r
- }\r
- }\r
-\r
- private void getFunctionalEquivalentTestCases(String path, String resName, String keyword,\r
- boolean truncate, String[] testCases) {\r
- //String F_STR = "f";\r
- String T_STR = "t";\r
- boolean isAvail[] = new boolean[1];\r
-\r
- logln("Testing functional equivalents...");\r
- for(int i = 0; i < testCases.length ;i+=3) {\r
- boolean expectAvail = T_STR.equals(testCases[i+0]);\r
- ULocale inLocale = new ULocale(testCases[i+1]);\r
- ULocale expectLocale = new ULocale(testCases[i+2]);\r
-\r
- logln(new Integer(i/3).toString() + ": " + new Boolean(expectAvail).toString() + "\t\t" +\r
- inLocale.toString() + "\t\t" + expectLocale.toString());\r
-\r
- ULocale equivLocale = ICUResourceBundle.getFunctionalEquivalent(path, resName, keyword, inLocale, isAvail, truncate);\r
- boolean gotAvail = isAvail[0];\r
-\r
- if((gotAvail != expectAvail) || !equivLocale.equals(expectLocale)) {\r
- errln(new Integer(i/3).toString() + ": Error, expected Equiv=" + new Boolean(expectAvail).toString() + "\t\t" +\r
- inLocale.toString() + "\t\t--> " + expectLocale.toString() + ", but got " + new Boolean(gotAvail).toString() + " " +\r
- equivLocale.toString());\r
- }\r
- }\r
- }\r
-\r
- public void TestNorwegian(){\r
- try{\r
- UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "no_NO_NY");\r
- UResourceBundle sub = rb.get("Countries");\r
- String s1 = sub.getString("NO");\r
- if(s1.equals("Noreg")){\r
- logln("got expected output ");\r
- }else{\r
- errln("did not get the expected result");\r
- }\r
- }catch(IllegalArgumentException ex){\r
- errln("Caught an unexpected expected");\r
- }\r
- }\r
- public void TestJB4102(){\r
- try {\r
- ICUResourceBundle root =(ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "root");\r
- ICUResourceBundle t = null; \r
-// AmPmMarkers now exist in root/islamic calendar, so this test is rendered useless.\r
-// try{\r
-// t = root.getWithFallback("calendar/islamic-civil/AmPmMarkers");\r
-// errln("Second resource does not exist. How did it get here?\n");\r
-// }catch(MissingResourceException ex){\r
-// logln("Got the expected exception");\r
-// }\r
- try{\r
- t = root.getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera");\r
- errln("Second resource does not exist. How did it get here?\n");\r
- }catch(MissingResourceException ex){\r
- logln("Got the expected exception");\r
- }\r
- if(t!=null){\r
- errln("t is not null!");\r
- }\r
- } catch (MissingResourceException e) {\r
- warnln("Could not load the locale data: " + e.getMessage());\r
- }\r
- }\r
-\r
- public void TestCLDRStyleAliases() {\r
- String result = null;\r
- String expected = null;\r
- String[]expects = new String[] { "", "a41", "a12", "a03", "ar4" };\r
-\r
- logln("Testing CLDR style aliases......\n");\r
-\r
- UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED",testLoader);\r
- ICUResourceBundle alias = (ICUResourceBundle)rb.get("a");\r
-\r
- for(int i = 1; i < 5 ; i++) {\r
- String resource="a"+i;\r
- UResourceBundle a = (alias).getWithFallback(resource);\r
- result = a.getString();\r
- if(result.equals(expected)) {\r
- errln("CLDR style aliases failed resource with name "+resource+"resource, exp "+expects[i] +" , got " + result); \r
- }\r
- }\r
-\r
- }\r
- private String getLSString(int status){\r
- switch(status){\r
- case ICUResourceBundle.FROM_FALLBACK:\r
- return "FROM_FALLBACK";\r
- case ICUResourceBundle.FROM_DEFAULT:\r
- return "FROM_DEFAULT";\r
- case ICUResourceBundle.FROM_ROOT: \r
- return "FROM_ROOT";\r
- case ICUResourceBundle.FROM_LOCALE: \r
- return "FROM_LOCALE";\r
- default:\r
- return "UNKNOWN";\r
- }\r
- }\r
- public void TestLoadingStatus(){\r
- ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "yi_IL");\r
- int status = bundle.getLoadingStatus();\r
- if(status != ICUResourceBundle.FROM_DEFAULT){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_DEFAULT) \r
- + " Got: " + getLSString(status));\r
- } \r
- bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "eo_DE");\r
- status = bundle.getLoadingStatus();\r
- if(status != ICUResourceBundle.FROM_FALLBACK){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK) \r
- + " Got: " + getLSString(status));\r
- } \r
- \r
- logln("Test to verify loading status of get(String)");\r
- bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "te_IN");\r
- UResourceBundle countries = bundle.get("Countries");\r
- status = ((ICUResourceBundle)countries).getLoadingStatus();\r
- if(status != ICUResourceBundle.FROM_FALLBACK){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK) \r
- + " Got: " + getLSString(status));\r
- }\r
- /*\r
- UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters");\r
- status = auxExemplar.getLoadingStatus();\r
- if(status != UResourceBundle.FROM_ROOT){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT) \r
- + " Got: " + getLSString(status));\r
- } \r
- */\r
- logln("Test to verify loading status of get(int)");\r
- ICUResourceBundle ms = (ICUResourceBundle)bundle.get("MeasurementSystem");\r
- status = ms.getLoadingStatus();\r
- if(status != ICUResourceBundle.FROM_ROOT){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT) \r
- + " Got: " + getLSString(status));\r
- }\r
- \r
- logln("Test to verify loading status of getwithFallback");\r
- bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU",testLoader);\r
- ICUResourceBundle temp = (ICUResourceBundle)bundle.getWithFallback("a/a2");\r
- status = temp.getLoadingStatus();\r
- if(status != ICUResourceBundle.FROM_LOCALE){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_LOCALE) \r
- + " Got: " + getLSString(status));\r
- }\r
- temp = bundle.getWithFallback("a/a1");\r
- status = temp.getLoadingStatus();\r
- if(status != ICUResourceBundle.FROM_FALLBACK){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK) \r
- + " Got: " + getLSString(status));\r
- }\r
- temp = bundle.getWithFallback("a/a4");\r
- status = temp.getLoadingStatus();\r
- if(status != ICUResourceBundle.FROM_ROOT){\r
- errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT) \r
- + " Got: " + getLSString(status));\r
- }\r
- }\r
- public void TestCoverage(){\r
- UResourceBundle bundle;\r
- bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);\r
- if (bundle == null){\r
- errln("UResourceBundle.getBundleInstance(String baseName) failed");\r
- }\r
- bundle = null;\r
- bundle = UResourceBundle.getBundleInstance(ULocale.getDefault());\r
- if (bundle == null){\r
- errln("UResourceBundle.getBundleInstance(ULocale) failed");\r
- return;\r
- } \r
- if (new UResourceTypeMismatchException("coverage") == null){\r
- errln("Create UResourceTypeMismatchException error");\r
- }\r
- class Stub extends UResourceBundle{\r
- public ULocale getULocale() {return ULocale.ROOT;}\r
- protected String getLocaleID() {return null;}\r
- protected String getBaseName() {return null;}\r
- protected UResourceBundle getParent() {return null;}\r
- protected void setLoadingStatus(int newStatus) {}\r
- public Enumeration getKeys() {return null;}\r
- protected Object handleGetObject(String aKey) {return null;}\r
- }\r
- Stub stub = new Stub();\r
- \r
- if (!stub.getLocale().equals(ULocale.ROOT.toLocale())){\r
- errln("UResourceBundle.getLoclae(Locale) should delegate to (ULocale)");\r
- }\r
- }\r
- public void TestJavaULocaleBundleLoading(){\r
- String baseName="com.ibm.icu.dev.data.resources.TestDataElements";\r
- String locName = "en_Latn_US";\r
- UResourceBundle bundle = UResourceBundle.getBundleInstance(baseName, locName, testLoader);\r
- String fromRoot = bundle.getString("from_root");\r
- if(!fromRoot.equals("This data comes from root")){\r
- errln("Did not get the expected string for from_root");\r
- }\r
- String fromEn = bundle.getString("from_en");\r
- if(!fromEn.equals("This data comes from en")){\r
- errln("Did not get the expected string for from_en");\r
- }\r
- String fromEnLatn = bundle.getString("from_en_Latn");\r
- if(!fromEnLatn.equals("This data comes from en_Latn")){\r
- errln("Did not get the expected string for from_en_Latn");\r
- }\r
- String fromEnLatnUs = bundle.getString("from_en_Latn_US");\r
- if(!fromEnLatnUs.equals("This data comes from en_Latn_US")){\r
- errln("Did not get the expected string for from_en_Latn_US");\r
- }\r
- UResourceBundle bundle1 = UResourceBundle.getBundleInstance(baseName, new ULocale(locName), testLoader);\r
- if(!bundle1.equals(bundle)){\r
- errln("Did not get the expected bundle for "+baseName +"."+locName);\r
- }\r
- if(bundle1!=bundle){\r
- errln("Did not load the bundle from cache");\r
- }\r
- \r
- UResourceBundle bundle2 = UResourceBundle.getBundleInstance(baseName, "en_IN", testLoader);\r
- if(!bundle2.getLocale().toString().equals("en")){\r
- errln("Did not get the expected fallback locale. Expected: en Got: "+bundle2.getLocale().toString()); \r
- }\r
- UResourceBundle bundle3 = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader);\r
- if(!bundle3.getLocale().toString().equals("te")){\r
- errln("Did not get the expected fallback locale. Expected: te Got: "+bundle2.getLocale().toString()); \r
- }\r
- // non-existent bundle .. should return default\r
- UResourceBundle defaultBundle = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);\r
- ULocale defaultLocale = ULocale.getDefault();\r
- if(!defaultBundle.getULocale().equals(defaultLocale)){\r
- errln("Did not get the default bundle for non-existent bundle");\r
- }\r
- // non-existent bundle, non-existent default locale\r
- // so return the root bundle.\r
- ULocale.setDefault(ULocale.CANADA_FRENCH);\r
- UResourceBundle root = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);\r
- if(!root.getULocale().toString().equals("")){\r
- errln("Did not get the root bundle for non-existent default bundle for non-existent bundle");\r
- } \r
- //reset the default\r
- ULocale.setDefault(defaultLocale);\r
- Enumeration keys = bundle.getKeys();\r
- int i=0;\r
- while(keys.hasMoreElements()){\r
- logln("key: "+ keys.nextElement());\r
- i++;\r
- }\r
- if(i!=4){\r
- errln("Did not get the expected number of keys: got " + i + ", expected 4");\r
- }\r
- UResourceBundle bundle4 = UResourceBundle.getBundleInstance(baseName,"fr_Latn_FR", testLoader);\r
- if(bundle4==null){\r
- errln("Could not load bundle fr_Latn_FR");\r
- }\r
- }\r
- public void TestAliasFallback(){\r
- try{\r
- ULocale loc = new ULocale("en_US");\r
- ICUResourceBundle b = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc);\r
- ICUResourceBundle b1 = (ICUResourceBundle)b.getWithFallback("calendar/hebrew/monthNames/format/abbreviated");\r
- if(b1!=null){\r
- logln("loaded data for abbreviated month names: "+ b1.getKey()); \r
- }\r
- }catch(MissingResourceException ex){\r
- warnln("Failed to load data for abbreviated month names");\r
- }\r
- }\r
-}\r
-\r
+//##header J2SE15
+/**
+ *******************************************************************************
+ * Copyright (C) 2001-2009, International Business Machines Corporation and *
+ * others. All Rights Reserved. *
+ *******************************************************************************
+ */
+package com.ibm.icu.dev.test.util;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.JarURLConnection;
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//##import com.ibm.icu.impl.ByteBuffer;
+//#else
+import java.nio.ByteBuffer;
+//#endif
+import java.util.MissingResourceException;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+
+import com.ibm.icu.dev.test.TestFmwk;
+import com.ibm.icu.impl.ICUResourceBundle;
+import com.ibm.icu.impl.Utility;
+import com.ibm.icu.text.UTF16;
+import com.ibm.icu.util.Holiday;
+import com.ibm.icu.util.ULocale;
+import com.ibm.icu.util.UResourceBundle;
+import com.ibm.icu.util.UResourceTypeMismatchException;
+
+
+public final class ICUResourceBundleTest extends TestFmwk {
+ private static final ClassLoader testLoader = ICUResourceBundleTest.class.getClassLoader();
+
+ public static void main(String args[]) throws Exception {
+ ICUResourceBundleTest test = new ICUResourceBundleTest();
+ test.run(args);
+
+ }
+ public void TestGetResources(){
+ try{
+ // It does not work well in eclipse plug-in test because of class loader configuration??
+ // For now, specify resource path explicitly in this test case
+ //Enumeration en = testLoader.getResources("META-INF");
+ Enumeration en = testLoader.getResources("com.ibm.icu.dev.data");
+ for(;en.hasMoreElements();) {
+ URL url = (URL)en.nextElement();
+ if (url == null) {
+ warnln("could not load resource data");
+ return;
+ }
+ URLConnection c = url.openConnection();
+
+ if (c instanceof JarURLConnection) {
+ JarURLConnection jc = (JarURLConnection)c;
+ JarEntry je = jc.getJarEntry();
+ logln("jar entry: " + je.toString());
+ } else {
+ InputStream is = c.getInputStream();
+ logln("input stream:");
+ InputStreamReader r = new InputStreamReader(is);
+ BufferedReader br = new BufferedReader(r);
+ String line = null;
+ int n = 0;
+ while ((line = br.readLine()) != null) {
+ logln(" " + ++n + ": " + line);
+ }
+ }
+ }
+ }catch(SecurityException ex) {
+ warnln("could not load resource data: " + ex);
+ ex.printStackTrace();
+ }catch(NullPointerException ex) {
+ // thrown by ibm 1.4.2 windows jvm security manager
+ warnln("could not load resource data: " + ex);
+ }catch(Exception ex){
+ ex.printStackTrace();
+ errln("Unexpected exception: "+ ex);
+ }
+ }
+ public void TestResourceBundleWrapper(){
+ UResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", "da_DK");
+ Object o = bundle.getObject("holidays");
+ if(o instanceof Holiday[] ){
+ logln("wrapper mechanism works for Weekend data");
+ }else{
+ errln("Did not get the expected output for Weekend data");
+ }
+
+ bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "bogus");
+ if(bundle instanceof UResourceBundle && bundle.getULocale().equals("en_US")){
+ logln("wrapper mechanism works for bogus locale");
+ }else{
+ errln("wrapper mechanism failed for bogus locale.");
+ }
+
+ try{
+ bundle = UResourceBundle.getBundleInstance("bogus", "bogus");
+ if(bundle!=null){
+ errln("Did not get the expected exception");
+ }
+ }catch(MissingResourceException ex){
+ logln("got the expected exception");
+ }
+
+
+ }
+ public void TestJB3879(){
+ // this tests tests loading of root bundle when a resource bundle
+ // for the default locale is requested
+ try {
+ UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", ULocale.getDefault().toString(), testLoader);
+ if(bundle==null){
+ errln("could not create the resource bundle");
+ }
+ }
+ catch (MissingResourceException ex) {
+ warnln("could not load test data: " + ex.getMessage());
+ }
+ }
+ public void TestOpen(){
+ UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "en_US_POSIX");
+
+ if(bundle==null){
+ errln("could not create the resource bundle");
+ }
+
+ UResourceBundle obj = bundle.get("NumberPatterns");
+
+ int size = obj.getSize();
+ int type = obj.getType();
+ if(type == UResourceBundle.ARRAY){
+ UResourceBundle sub;
+ for(int i=0; i<size; i++){
+ sub = obj.get(i);
+ String temp =sub.getString();
+ if(temp.length()==0){
+ errln("Failed to get the items from NumberPatterns array in bundle: "+
+ bundle.getULocale().getBaseName());
+ }
+ //System.out.println("\""+prettify(temp)+"\"");
+ }
+
+ }
+ String[] strings = bundle.getStringArray("NumberPatterns");
+ if(size!=strings.length){
+ errln("Failed to get the items from NumberPatterns array in bundle: "+
+ bundle.getULocale().getBaseName());
+ }
+ {
+ obj = bundle.get("NumberElements");
+
+ size = obj.getSize();
+ type = obj.getType();
+ if(type == UResourceBundle.ARRAY){
+ UResourceBundle sub;
+ for(int i=0; i<size; i++){
+ sub = obj.get(i);
+ String temp =sub.getString();
+ if(temp.length()==0){
+ errln("Failed to get the items from NumberPatterns array in bundle: "+
+ bundle.getULocale().getBaseName());
+ }
+ // System.out.println("\""+prettify(temp)+"\"");
+ }
+
+ }
+ }
+ if(bundle==null){
+ errln("could not create the resource bundle");
+ }
+ bundle = (UResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "en_US_POSIX");
+ if(bundle==null){
+ errln("could not load the stream");
+ }
+ bundle = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zzz_ZZ_very_very_very_long_bogus_bundle");
+ if(!bundle.getULocale().equals(ULocale.getDefault())){
+ errln("UResourceBundle did not load the default bundle when bundle was not found. Default: " + ULocale.getDefault() +
+ ", Bundle locale: " + bundle.getULocale());
+ }
+
+
+ }
+
+ public void TestBasicTypes(){
+ UResourceBundle bundle = null;
+ try {
+ bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes", testLoader);
+ }
+ catch (MissingResourceException e) {
+ warnln("could not load test data: " + e.getMessage());
+ return;
+ }
+ {
+ String expected = "abc\u0000def";
+ UResourceBundle sub = bundle.get("zerotest");
+ if(!expected.equals(sub.getString())){
+ errln("Did not get the expected string for key zerotest in bundle testtypes");
+ }
+ sub = bundle.get("emptyexplicitstring");
+ expected ="";
+ if(!expected.equals(sub.getString())){
+ errln("Did not get the expected string for key emptyexplicitstring in bundle testtypes");
+ }
+ sub = bundle.get("emptystring");
+ expected ="";
+ if(!expected.equals(sub.getString())){
+ errln("Did not get the expected string for key emptystring in bundle testtypes");
+ }
+ }
+ {
+ int expected = 123;
+ UResourceBundle sub = bundle.get("onehundredtwentythree");
+ if(expected!=sub.getInt()){
+ errln("Did not get the expected int value for key onehundredtwentythree in bundle testtypes");
+ }
+ sub = bundle.get("emptyint");
+ expected=0;
+ if(expected!=sub.getInt()){
+ errln("Did not get the expected int value for key emptyint in bundle testtypes");
+ }
+ }
+ {
+ int expected = 1;
+ UResourceBundle sub = bundle.get("one");
+ if(expected!=sub.getInt()){
+ errln("Did not get the expected int value for key one in bundle testtypes");
+ }
+ }
+ {
+ int expected = -1;
+ UResourceBundle sub = bundle.get("minusone");
+ int got = sub.getInt();
+ if(expected!=got){
+ errln("Did not get the expected int value for key minusone in bundle testtypes");
+ }
+ expected = 0xFFFFFFF;
+ got = sub.getUInt();
+ if(expected!=got){
+ errln("Did not get the expected int value for key minusone in bundle testtypes");
+ }
+ }
+ {
+ int expected = 1;
+ UResourceBundle sub = bundle.get("plusone");
+ if(expected!=sub.getInt()){
+ errln("Did not get the expected int value for key minusone in bundle testtypes");
+ }
+
+ }
+ {
+ int[] expected = new int[]{ 1, 2, 3, -3, 4, 5, 6, 7 } ;
+ UResourceBundle sub = bundle.get("integerarray");
+ if(!Utility.arrayEquals(expected,sub.getIntVector())){
+ errln("Did not get the expected int vector value for key integerarray in bundle testtypes");
+ }
+ sub = bundle.get("emptyintv");
+ expected = new int[0];
+ if(!Utility.arrayEquals(expected,sub.getIntVector())){
+ errln("Did not get the expected int vector value for key emptyintv in bundle testtypes");
+ }
+
+ }
+ {
+ UResourceBundle sub = bundle.get("binarytest");
+ ByteBuffer got = sub.getBinary();
+ if(got.remaining()!=15){
+ errln("Did not get the expected length for the binary ByteBuffer");
+ }
+ for(int i=0; i< got.remaining(); i++){
+ byte b = got.get();
+ if(b!=i){
+ errln("Did not get the expected value for binary buffer at index: "+i);
+ }
+ }
+ sub = bundle.get("emptybin");
+ got = sub.getBinary();
+ if(got.remaining()!=0){
+ errln("Did not get the expected length for the emptybin ByteBuffer");
+ }
+
+ }
+ {
+ UResourceBundle sub = bundle.get("emptyarray");
+ String key = sub.getKey();
+ if(!key.equals("emptyarray")){
+ errln("Did not get the expected key for emptytable item");
+ }
+ if(sub.getSize()!=0){
+ errln("Did not get the expected length for emptytable item");
+ }
+ }
+ {
+ UResourceBundle sub = bundle.get("menu");
+ String key = sub.getKey();
+ if(!key.equals("menu")){
+ errln("Did not get the expected key for menu item");
+ }
+ UResourceBundle sub1 = sub.get("file");
+ key = sub1.getKey();
+ if(!key.equals("file")){
+ errln("Did not get the expected key for file item");
+ }
+ UResourceBundle sub2 = sub1.get("open");
+ key = sub2.getKey();
+ if(!key.equals("open")){
+ errln("Did not get the expected key for file item");
+ }
+ String value = sub2.getString();
+ if(!value.equals("Open")){
+ errln("Did not get the expected value for key for oen item");
+ }
+
+ sub = bundle.get("emptytable");
+ key = sub.getKey();
+ if(!key.equals("emptytable")){
+ errln("Did not get the expected key for emptytable item");
+ }
+ if(sub.getSize()!=0){
+ errln("Did not get the expected length for emptytable item");
+ }
+ sub = bundle.get("menu").get("file");
+ int size = sub.getSize();
+ String expected;
+ for(int i=0; i<size; i++){
+ sub1 = sub.get(i);
+
+ switch(i){
+ case 0:
+ expected = "exit";
+ break;
+ case 1:
+ expected = "open";
+ break;
+ case 2:
+ expected = "save";
+ break;
+ default:
+ expected ="";
+ }
+ String got = sub1.getKey();
+ if(!expected.equals(got)){
+ errln("Did not get the expected key at index"+i+". Expected: "+expected+" Got: "+got);
+ }else{
+ logln("Got the expected key at index: "+i);
+ }
+ }
+ }
+
+ }
+ private static final class TestCase{
+ String key;
+ int value;
+ TestCase(String key, int value){
+ this.key = key;
+ this.value = value;
+ }
+ }
+ public void TestTable32(){
+ TestCase[] arr = new TestCase[]{
+ new TestCase ( "ooooooooooooooooo", 0 ),
+ new TestCase ( "oooooooooooooooo1", 1 ),
+ new TestCase ( "ooooooooooooooo1o", 2 ),
+ new TestCase ( "oo11ooo1ooo11111o", 25150 ),
+ new TestCase ( "oo11ooo1ooo111111", 25151 ),
+ new TestCase ( "o1111111111111111", 65535 ),
+ new TestCase ( "1oooooooooooooooo", 65536 ),
+ new TestCase ( "1ooooooo11o11ooo1", 65969 ),
+ new TestCase ( "1ooooooo11o11oo1o", 65970 ),
+ new TestCase ( "1ooooooo111oo1111", 65999 )
+ };
+ UResourceBundle bundle = null;
+ try {
+ bundle = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testtable32", testLoader);
+ }
+ catch (MissingResourceException ex) {
+ warnln("could not load resource data: " + ex.getMessage());
+ return;
+ }
+
+ if(bundle.getType()!= UResourceBundle.TABLE){
+ errln("Could not get the correct type for bundle testtable32");
+ }
+ int size =bundle.getSize();
+ if(size!=66000){
+ errln("Could not get the correct size for bundle testtable32");
+ }
+ for(int i =0; i<size; i++){
+ UResourceBundle item = bundle.get(i);
+ String key = item.getKey();
+ int parsedNumber = parseTable32Key(key);
+ int number=-1;
+ switch(item.getType()){
+ case UResourceBundle.STRING:
+ String value = item.getString();
+ number = UTF16.charAt(value,0);
+ break;
+ case UResourceBundle.INT:
+ number = item.getInt();
+ break;
+ default:
+ errln("Got unexpected resource type in testtable32");
+
+ }
+ if(number!=parsedNumber){
+ errln("Did not get expected value in testtypes32 for key"+
+ key+". Expected: "+parsedNumber+" Got:"+number);
+ }
+
+ }
+ for(int i=0;i<arr.length; i++){
+ String expected = arr[i].key;
+ UResourceBundle item = bundle.get(expected);
+ int number=0;
+ String key = item.getKey();
+ int parsedNumber = parseTable32Key(key);
+ if(!key.equals(expected)){
+ errln("Did not get the expected key. Expected: "+expected+" Got:"+key);
+ }
+ switch(item.getType()){
+ case UResourceBundle.STRING:
+ String value = item.getString();
+ number = UTF16.charAt(value,0);
+ break;
+ case UResourceBundle.INT:
+ number = item.getInt();
+ break;
+ default:
+ errln("Got unexpected resource type in testtable32");
+ }
+
+ if(number!=parsedNumber){
+ errln("Did not get expected value in testtypes32 for key"+
+ key+". Expected: "+parsedNumber+" Got:"+number);
+ }
+ }
+ }
+ private static int parseTable32Key(String key) {
+ int number;
+ char c;
+
+ number=0;
+ for(int i=0; i<key.length(); i++){
+ c = key.charAt(i);
+ number<<=1;
+ if(c=='1') {
+ number|=1;
+ }
+ }
+ return number;
+ }
+
+ public void TestAliases(){
+/*
+ String simpleAlias = "Open";
+
+ UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases", testLoader);
+ if (rb == null) {
+ warnln("could not load testaliases data");
+ return;
+ }
+ UResourceBundle sub = rb.get("simplealias");
+ String s1 = sub.getString("simplealias");
+ if(s1.equals(simpleAlias)){
+ logln("Alias mechanism works for simplealias");
+ }else{
+ errln("Did not get the expected output for simplealias");
+ }
+ {
+ try{
+ rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
+ sub = rb.get("nonexisting");
+ errln("Did not get the expected exception for nonexisting");
+ }catch(MissingResourceException ex){
+ logln("Alias mechanism works for nonexisting alias");
+ }
+ }
+ {
+ rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
+ sub = rb.get("referencingalias");
+ s1 = sub.getString();
+ if(s1.equals("Hani")){
+ logln("Alias mechanism works for referencingalias");
+ }else{
+ errln("Did not get the expected output for referencingalias");
+ }
+ }
+ {
+ rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
+ sub = rb.get("boundaries");
+ String word = sub.getString("word");
+
+ if(word.equals("word_ja.brk")){
+ logln("Got the expected output for boundaries/word");
+ }else{
+ errln("Did not get the expected type for boundaries/word");
+ }
+
+ }
+ {
+ UResourceBundle rb1 = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
+ if(rb1!=rb){
+ errln("Caching of the resource bundle failed");
+ }else{
+ logln("Caching of resource bundle passed");
+ }
+ sub = rb1.get("testGetStringByKeyAliasing" );
+
+ s1 = sub.get("KeyAlias0PST").getString();
+ if(s1.equals("America/Los_Angeles")){
+ logln("Alias mechanism works for KeyAlias0PST");
+ }else{
+ errln("Did not get the expected output for KeyAlias0PST");
+ }
+
+ s1 = sub.getString("KeyAlias1PacificStandardTime");
+ if(s1.equals("Pacific Standard Time")){
+ logln("Alias mechanism works for KeyAlias1PacificStandardTime");
+ }else{
+ errln("Did not get the expected output for KeyAlias1PacificStandardTime");
+ }
+ s1 = sub.getString("KeyAlias2PDT");
+ if(s1.equals("PDT")){
+ logln("Alias mechanism works for KeyAlias2PDT");
+ }else{
+ errln("Did not get the expected output for KeyAlias2PDT");
+ }
+
+ s1 = sub.getString("KeyAlias3LosAngeles");
+ if(s1.equals("Los Angeles")){
+ logln("Alias mechanism works for KeyAlias3LosAngeles. Got: "+s1);
+ }else{
+ errln("Did not get the expected output for KeyAlias3LosAngeles. Got: "+s1);
+ }
+ }
+ {
+ sub = rb.get("testGetStringByIndexAliasing" );
+ s1 = sub.getString(0);
+ if(s1.equals("America/Los_Angeles")){
+ logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: "+s1);
+ }else{
+ errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: "+s1);
+ }
+ s1 = sub.getString(1);
+ if(s1.equals("Pacific Standard Time")){
+ logln("Alias mechanism works for testGetStringByIndexAliasing/1");
+ }else{
+ errln("Did not get the expected output for testGetStringByIndexAliasing/1");
+ }
+ s1 = sub.getString(2);
+ if(s1.equals("PDT")){
+ logln("Alias mechanism works for testGetStringByIndexAliasing/2");
+ }else{
+ errln("Did not get the expected output for testGetStringByIndexAliasing/2");
+ }
+
+ s1 = sub.getString(3);
+ if(s1.equals("Los Angeles")){
+ logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: "+s1);
+ }else{
+ errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: "+s1);
+ }
+ }
+ {
+ sub = rb.get("testAliasToTree" );
+
+ ByteBuffer buf = sub.get("standard").get("%%CollationBin").getBinary();
+ if(buf==null){
+ errln("Did not get the expected output for %%CollationBin");
+ }
+ }
+ // should not get an exception
+ rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_RBNF_BASE_NAME,"fr_BE");
+ String str = rb.getString("SpelloutRules");
+ if(str !=null && str.length()>0){
+ logln("Alias mechanism works");
+ }else{
+ errln("Alias mechanism failed for fr_BE SpelloutRules");
+ }
+ rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW");
+ UResourceBundle b = (UResourceBundle) rb.getObject("collations");
+ if(b != null){
+ if(b.get(0).getKey().equals( "default")){
+ logln("Alias mechanism works");
+ }else{
+ errln("Alias mechanism failed for zh_TW collations");
+ }
+ }else{
+ errln("Did not get the expected object for collations");
+ }
+*/
+ }
+ public void TestAlias(){
+ logln("Testing %%ALIAS");
+ UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"iw_IL");
+ UResourceBundle b = rb.get("NumberPatterns");
+ if(b != null){
+ if(b.getSize()>0){
+ logln("%%ALIAS mechanism works");
+ }else{
+ errln("%%ALIAS mechanism failed for iw_IL collations");
+ }
+ }else{
+ errln("%%ALIAS mechanism failed for iw_IL");
+ }
+ }
+ public void TestXPathAlias(){
+ UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader);
+ UResourceBundle b = rb.get("aliasClient");
+ String result = b.getString();
+ String expResult= "correct";
+
+ if(!result.equals(expResult)){
+ errln("Did not get the expected result for XPath style alias");
+ }
+ try{
+ UResourceBundle c = rb.get("rootAliasClient");
+ result = c.getString();
+ expResult = "correct";
+ if(!result.equals(expResult)){
+ errln("Did not get the expected result for XPath style alias for rootAliasClient");
+ }
+ }catch( MissingResourceException ex){
+ errln("Could not get rootAliasClient");
+ }
+ }
+ public void TestCircularAliases(){
+// Aliases no longer supported
+// try{
+// UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader);
+// UResourceBundle sub = rb.get("aaa");
+// String s1 = sub.getString();
+// if(s1!=null){
+// errln("Did not get the expected exception");
+// }
+// }catch(IllegalArgumentException ex){
+// logln("got expected exception for circular references");
+// }
+// catch (MissingResourceException ex) {
+// warnln("could not load resource data: " + ex.getMessage());
+// }
+ }
+
+ public void TestGetWithFallback(){
+ /*
+ UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");
+ String key = bundle.getStringWithFallback("Keys/collation");
+ if(!key.equals("COLLATION")){
+ errln("Did not get the expected result from getStringWithFallback method.");
+ }
+ String type = bundle.getStringWithFallback("Types/collation/direct");
+ if(!type.equals("DIRECT")){
+ errln("Did not get the expected result form getStringWithFallback method.");
+ }
+ */
+ ICUResourceBundle bundle = null;
+ String key = null;
+ try{
+ bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,ULocale.canonicalize("de__PHONEBOOK"));
+
+ if(!bundle.getULocale().equals("de")){
+ errln("did not get the expected bundle");
+ }
+ key = bundle.getStringWithFallback("collations/collation/default");
+ if(!key.equals("phonebook")){
+ errln("Did not get the expected result from getStringWithFallback method.");
+ }
+
+ }catch(MissingResourceException ex){
+ logln("got the expected exception");
+ }
+
+
+ bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"fr_FR");
+ key = bundle.getStringWithFallback("collations/default");
+ if(!key.equals("standard")){
+ errln("Did not get the expected result from getStringWithFallback method.");
+ }
+ bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"fr_FR");
+ ICUResourceBundle b1 = bundle.getWithFallback("calendar");
+ String defaultCal = b1.getStringWithFallback("default");
+ if(!defaultCal.equals("gregorian")){
+ errln("Did not get the expected default calendar string: Expected: gregorian, Got: "+defaultCal);
+ }
+ ICUResourceBundle b2 = b1.getWithFallback(defaultCal);
+ ICUResourceBundle b3 = b2.getWithFallback("monthNames");
+ String defaultContext = b3.getStringWithFallback("default");
+ ICUResourceBundle b4 = b3.getWithFallback(defaultContext);
+ String defaultWidth = b4.getStringWithFallback("default");
+ ICUResourceBundle b5 = b4.getWithFallback(defaultWidth);
+ if(b5.getSize()!=12){
+ errln("Did not get the expected size for the default monthNames");
+ }
+ }
+
+ private static final String COLLATION_RESNAME = "collations";
+ private static final String COLLATION_KEYWORD = "collation";
+ private static final String DEFAULT_NAME = "default";
+ private static final String STANDARD_NAME = "standard";
+ private static final String CALENDAR_RESNAME = "calendar";
+ private static final String CALENDAR_KEYWORD = "calendar";
+
+ public void TestKeywordValues(){
+ String kwVals[];
+ boolean foundStandard = false;
+ int n;
+
+ logln("Testing getting collation values:");
+ kwVals = ICUResourceBundle.getKeywordValues(ICUResourceBundle.ICU_COLLATION_BASE_NAME,COLLATION_RESNAME);
+ for(n=0;n<kwVals.length;n++) {
+ logln(new Integer(n).toString() + ": " + kwVals[n]);
+ if(DEFAULT_NAME.equals(kwVals[n])) {
+ errln("getKeywordValues for collation returned 'default' in the list.");
+ } else if(STANDARD_NAME.equals(kwVals[n])) {
+ if(foundStandard == false) {
+ foundStandard = true;
+ logln("found 'standard'");
+ } else {
+ errln("Error - 'standard' is in the keyword list twice!");
+ }
+ }
+ }
+
+ if(foundStandard == false) {
+ errln("Error - 'standard' was not in the collation tree as a keyword.");
+ } else {
+ logln("'standard' was found as a collation keyword.");
+ }
+ }
+
+ public void TestLocaleDisplayNames() {
+ ULocale[] locales = ULocale.getAvailableLocales();
+ for (int i = 0; i < locales.length; ++i) {
+ if (!hasLocalizedCountryFor(ULocale.ENGLISH, locales[i])){
+ errln("Could not get localized country for "+ locales[i]);
+ }
+ if(!hasLocalizedLanguageFor(ULocale.ENGLISH, locales[i])){
+ errln("Could not get localized language for "+ locales[i]);
+ }
+ if(!hasLocalizedCountryFor(locales[i], locales[i])){
+ errln("Could not get localized country for "+ locales[i]);
+ hasLocalizedCountryFor(locales[i], locales[i]);
+ }
+ if(!hasLocalizedLanguageFor(locales[i], locales[i])){
+ errln("Could not get localized language for "+ locales[i]);
+ }
+
+ logln(locales[i] + "\t" + locales[i].getDisplayName(ULocale.ENGLISH) + "\t" + locales[i].getDisplayName(locales[i]));
+ }
+ }
+
+ private static boolean hasLocalizedLanguageFor(ULocale locale, ULocale otherLocale) {
+ String lang = otherLocale.getLanguage();
+ String localizedVersion = otherLocale.getDisplayLanguage(locale);
+ return !lang.equals(localizedVersion);
+ }
+
+ private static boolean hasLocalizedCountryFor(ULocale locale, ULocale otherLocale) {
+ String country = otherLocale.getCountry();
+ if (country.equals("")) return true;
+ String localizedVersion = otherLocale.getDisplayCountry(locale);
+ return !country.equals(localizedVersion);
+ }
+
+ public void TestFunctionalEquivalent(){
+ String[] collCases = {
+ // avail locale equiv
+ "f", "de_US_CALIFORNIA", "de",
+ "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */
+ "t", "zh_Hant_TW@collation=stroke", "zh@collation=stroke",
+ "f", "de_CN@collation=pinyin", "de",
+ "t", "zh@collation=pinyin", "zh",
+ "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */
+ "t", "zh_Hans_CN@collation=pinyin", "zh",
+ "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */
+ "t", "zh_Hant_HK@collation=pinyin", "zh",
+ "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */
+ "t", "zh_Hant_HK@collation=stroke", "zh@collation=stroke",
+ "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */
+ "t", "zh_Hant_HK", "zh@collation=stroke",
+ "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */
+ "t", "zh_Hant_MO", "zh@collation=stroke",
+ "f", "zh_TW_STROKE", "zh@collation=stroke",
+ "f", "zh_TW_STROKE@collation=big5han", "zh@collation=big5han",
+ "f", "de_CN@calendar=japanese", "de",
+ "t", "de@calendar=japanese", "de",
+ "f", "zh_TW@collation=big5han", "zh@collation=big5han", /* alias of zh_Hant_TW */
+ "t", "zh_Hant_TW@collation=big5han", "zh@collation=big5han",
+ "f", "zh_TW@collation=gb2312han", "zh@collation=gb2312han", /* alias of zh_Hant_TW */
+ "t", "zh_Hant_TW@collation=gb2312han", "zh@collation=gb2312han",
+ "f", "zh_CN@collation=big5han", "zh@collation=big5han", /* alias of zh_Hans_CN */
+ "t", "zh_Hans_CN@collation=big5han", "zh@collation=big5han",
+ "f", "zh_CN@collation=gb2312han", "zh@collation=gb2312han", /* alias of zh_Hans_CN */
+ "t", "zh_Hans_CN@collation=gb2312han", "zh@collation=gb2312han",
+ "t", "zh@collation=big5han", "zh@collation=big5han",
+ "t", "zh@collation=gb2312han", "zh@collation=gb2312han",
+ "t", "hi_IN@collation=direct", "hi@collation=direct",
+ "t", "hi@collation=standard", "hi",
+ "t", "hi@collation=direct", "hi@collation=direct",
+ "f", "hi_AU@collation=direct;currency=CHF;calendar=buddhist", "hi@collation=direct",
+ "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi",
+ "t", "de_DE@collation=pinyin", "de", /* bug 4582 tests */
+ "f", "de_DE_BONN@collation=pinyin", "de",
+ "t", "nl", "root",
+ "t", "nl_NL", "root",
+ "f", "nl_NL_EEXT", "root",
+ "t", "nl@collation=stroke", "root",
+ "t", "nl_NL@collation=stroke", "root",
+ "f", "nl_NL_EEXT@collation=stroke", "root",
+ };
+
+ String[] calCases = {
+ // avail locale equiv
+ "t", "en_US_POSIX", "en_US@calendar=gregorian",
+ "f", "ja_JP_TOKYO", "ja_JP@calendar=gregorian",
+ "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
+ "t", "sr@calendar=gregorian", "sr@calendar=gregorian",
+ "t", "en", "en@calendar=gregorian",
+ "t", "th_TH", "th@calendar=buddhist",
+ "t", "th_TH@calendar=gregorian", "th_TH@calendar=gregorian",
+ "f", "th_TH_Bangkok", "th@calendar=buddhist",
+ };
+
+ logln("Testing functional equivalents for collation...");
+ getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
+ COLLATION_RESNAME, COLLATION_KEYWORD, true, collCases);
+
+ logln("Testing functional equivalents for calenadr...");
+ getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_BASE_NAME,
+ CALENDAR_RESNAME, CALENDAR_KEYWORD, false, calCases);
+
+ logln("Testing error conditions:");
+ try {
+ ICUResourceBundle.getFunctionalEquivalent(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "calendar",
+ "calendar", new ULocale("ar_EG@calendar=islamic"), new boolean[1], true);
+ errln("Err: expected MissingResourceException");
+ } catch ( MissingResourceException t ) {
+ logln("expected MissingResourceException caught (PASS): " + t.toString());
+ }
+ }
+
+ private void getFunctionalEquivalentTestCases(String path, String resName, String keyword,
+ boolean truncate, String[] testCases) {
+ //String F_STR = "f";
+ String T_STR = "t";
+ boolean isAvail[] = new boolean[1];
+
+ logln("Testing functional equivalents...");
+ for(int i = 0; i < testCases.length ;i+=3) {
+ boolean expectAvail = T_STR.equals(testCases[i+0]);
+ ULocale inLocale = new ULocale(testCases[i+1]);
+ ULocale expectLocale = new ULocale(testCases[i+2]);
+
+ logln(new Integer(i/3).toString() + ": " + new Boolean(expectAvail).toString() + "\t\t" +
+ inLocale.toString() + "\t\t" + expectLocale.toString());
+
+ ULocale equivLocale = ICUResourceBundle.getFunctionalEquivalent(path, resName, keyword, inLocale, isAvail, truncate);
+ boolean gotAvail = isAvail[0];
+
+ if((gotAvail != expectAvail) || !equivLocale.equals(expectLocale)) {
+ errln(new Integer(i/3).toString() + ": Error, expected Equiv=" + new Boolean(expectAvail).toString() + "\t\t" +
+ inLocale.toString() + "\t\t--> " + expectLocale.toString() + ", but got " + new Boolean(gotAvail).toString() + " " +
+ equivLocale.toString());
+ }
+ }
+ }
+
+ public void TestNorwegian(){
+ try{
+ UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "no_NO_NY");
+ UResourceBundle sub = rb.get("Countries");
+ String s1 = sub.getString("NO");
+ if(s1.equals("Noreg")){
+ logln("got expected output ");
+ }else{
+ errln("did not get the expected result");
+ }
+ }catch(IllegalArgumentException ex){
+ errln("Caught an unexpected expected");
+ }
+ }
+ public void TestJB4102(){
+ try {
+ ICUResourceBundle root =(ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "root");
+ ICUResourceBundle t = null;
+// AmPmMarkers now exist in root/islamic calendar, so this test is rendered useless.
+// try{
+// t = root.getWithFallback("calendar/islamic-civil/AmPmMarkers");
+// errln("Second resource does not exist. How did it get here?\n");
+// }catch(MissingResourceException ex){
+// logln("Got the expected exception");
+// }
+ try{
+ t = root.getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera");
+ errln("Second resource does not exist. How did it get here?\n");
+ }catch(MissingResourceException ex){
+ logln("Got the expected exception");
+ }
+ if(t!=null){
+ errln("t is not null!");
+ }
+ } catch (MissingResourceException e) {
+ warnln("Could not load the locale data: " + e.getMessage());
+ }
+ }
+
+ public void TestCLDRStyleAliases() {
+ String result = null;
+ String expected = null;
+ String[]expects = new String[] { "", "a41", "a12", "a03", "ar4" };
+
+ logln("Testing CLDR style aliases......\n");
+
+ UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED",testLoader);
+ ICUResourceBundle alias = (ICUResourceBundle)rb.get("a");
+
+ for(int i = 1; i < 5 ; i++) {
+ String resource="a"+i;
+ UResourceBundle a = (alias).getWithFallback(resource);
+ result = a.getString();
+ if(result.equals(expected)) {
+ errln("CLDR style aliases failed resource with name "+resource+"resource, exp "+expects[i] +" , got " + result);
+ }
+ }
+
+ }
+ private String getLSString(int status){
+ switch(status){
+ case ICUResourceBundle.FROM_FALLBACK:
+ return "FROM_FALLBACK";
+ case ICUResourceBundle.FROM_DEFAULT:
+ return "FROM_DEFAULT";
+ case ICUResourceBundle.FROM_ROOT:
+ return "FROM_ROOT";
+ case ICUResourceBundle.FROM_LOCALE:
+ return "FROM_LOCALE";
+ default:
+ return "UNKNOWN";
+ }
+ }
+ public void TestLoadingStatus(){
+ ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "yi_IL");
+ int status = bundle.getLoadingStatus();
+ if(status != ICUResourceBundle.FROM_DEFAULT){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_DEFAULT)
+ + " Got: " + getLSString(status));
+ }
+ bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "eo_DE");
+ status = bundle.getLoadingStatus();
+ if(status != ICUResourceBundle.FROM_FALLBACK){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK)
+ + " Got: " + getLSString(status));
+ }
+
+ logln("Test to verify loading status of get(String)");
+ bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "te_IN");
+ UResourceBundle countries = bundle.get("Countries");
+ status = ((ICUResourceBundle)countries).getLoadingStatus();
+ if(status != ICUResourceBundle.FROM_FALLBACK){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK)
+ + " Got: " + getLSString(status));
+ }
+ /*
+ UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters");
+ status = auxExemplar.getLoadingStatus();
+ if(status != UResourceBundle.FROM_ROOT){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT)
+ + " Got: " + getLSString(status));
+ }
+ */
+ logln("Test to verify loading status of get(int)");
+ ICUResourceBundle ms = (ICUResourceBundle)bundle.get("MeasurementSystem");
+ status = ms.getLoadingStatus();
+ if(status != ICUResourceBundle.FROM_ROOT){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT)
+ + " Got: " + getLSString(status));
+ }
+
+ logln("Test to verify loading status of getwithFallback");
+ bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU",testLoader);
+ ICUResourceBundle temp = (ICUResourceBundle)bundle.getWithFallback("a/a2");
+ status = temp.getLoadingStatus();
+ if(status != ICUResourceBundle.FROM_LOCALE){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_LOCALE)
+ + " Got: " + getLSString(status));
+ }
+ temp = bundle.getWithFallback("a/a1");
+ status = temp.getLoadingStatus();
+ if(status != ICUResourceBundle.FROM_FALLBACK){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK)
+ + " Got: " + getLSString(status));
+ }
+ temp = bundle.getWithFallback("a/a4");
+ status = temp.getLoadingStatus();
+ if(status != ICUResourceBundle.FROM_ROOT){
+ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT)
+ + " Got: " + getLSString(status));
+ }
+ }
+ public void TestCoverage(){
+ UResourceBundle bundle;
+ bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);
+ if (bundle == null){
+ errln("UResourceBundle.getBundleInstance(String baseName) failed");
+ }
+ bundle = null;
+ bundle = UResourceBundle.getBundleInstance(ULocale.getDefault());
+ if (bundle == null){
+ errln("UResourceBundle.getBundleInstance(ULocale) failed");
+ return;
+ }
+ if (new UResourceTypeMismatchException("coverage") == null){
+ errln("Create UResourceTypeMismatchException error");
+ }
+ class Stub extends UResourceBundle{
+ public ULocale getULocale() {return ULocale.ROOT;}
+ protected String getLocaleID() {return null;}
+ protected String getBaseName() {return null;}
+ protected UResourceBundle getParent() {return null;}
+ protected void setLoadingStatus(int newStatus) {}
+ public Enumeration getKeys() {return null;}
+ protected Object handleGetObject(String aKey) {return null;}
+ }
+ Stub stub = new Stub();
+
+ if (!stub.getLocale().equals(ULocale.ROOT.toLocale())){
+ errln("UResourceBundle.getLoclae(Locale) should delegate to (ULocale)");
+ }
+ }
+ public void TestJavaULocaleBundleLoading(){
+ String baseName="com.ibm.icu.dev.data.resources.TestDataElements";
+ String locName = "en_Latn_US";
+ UResourceBundle bundle = UResourceBundle.getBundleInstance(baseName, locName, testLoader);
+ String fromRoot = bundle.getString("from_root");
+ if(!fromRoot.equals("This data comes from root")){
+ errln("Did not get the expected string for from_root");
+ }
+ String fromEn = bundle.getString("from_en");
+ if(!fromEn.equals("This data comes from en")){
+ errln("Did not get the expected string for from_en");
+ }
+ String fromEnLatn = bundle.getString("from_en_Latn");
+ if(!fromEnLatn.equals("This data comes from en_Latn")){
+ errln("Did not get the expected string for from_en_Latn");
+ }
+ String fromEnLatnUs = bundle.getString("from_en_Latn_US");
+ if(!fromEnLatnUs.equals("This data comes from en_Latn_US")){
+ errln("Did not get the expected string for from_en_Latn_US");
+ }
+ UResourceBundle bundle1 = UResourceBundle.getBundleInstance(baseName, new ULocale(locName), testLoader);
+ if(!bundle1.equals(bundle)){
+ errln("Did not get the expected bundle for "+baseName +"."+locName);
+ }
+ if(bundle1!=bundle){
+ errln("Did not load the bundle from cache");
+ }
+
+ UResourceBundle bundle2 = UResourceBundle.getBundleInstance(baseName, "en_IN", testLoader);
+ if(!bundle2.getLocale().toString().equals("en")){
+ errln("Did not get the expected fallback locale. Expected: en Got: "+bundle2.getLocale().toString());
+ }
+ UResourceBundle bundle3 = UResourceBundle.getBundleInstance(baseName, "te_IN", testLoader);
+ if(!bundle3.getLocale().toString().equals("te")){
+ errln("Did not get the expected fallback locale. Expected: te Got: "+bundle2.getLocale().toString());
+ }
+ // non-existent bundle .. should return default
+ UResourceBundle defaultBundle = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
+ ULocale defaultLocale = ULocale.getDefault();
+ if(!defaultBundle.getULocale().equals(defaultLocale)){
+ errln("Did not get the default bundle for non-existent bundle");
+ }
+ // non-existent bundle, non-existent default locale
+ // so return the root bundle.
+ ULocale.setDefault(ULocale.CANADA_FRENCH);
+ UResourceBundle root = UResourceBundle.getBundleInstance(baseName, "hi_IN", testLoader);
+ if(!root.getULocale().toString().equals("")){
+ errln("Did not get the root bundle for non-existent default bundle for non-existent bundle");
+ }
+ //reset the default
+ ULocale.setDefault(defaultLocale);
+ Enumeration keys = bundle.getKeys();
+ int i=0;
+ while(keys.hasMoreElements()){
+ logln("key: "+ keys.nextElement());
+ i++;
+ }
+ if(i!=4){
+ errln("Did not get the expected number of keys: got " + i + ", expected 4");
+ }
+ UResourceBundle bundle4 = UResourceBundle.getBundleInstance(baseName,"fr_Latn_FR", testLoader);
+ if(bundle4==null){
+ errln("Could not load bundle fr_Latn_FR");
+ }
+ }
+ public void TestAliasFallback(){
+ try{
+ ULocale loc = new ULocale("en_US");
+ ICUResourceBundle b = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc);
+ ICUResourceBundle b1 = (ICUResourceBundle)b.getWithFallback("calendar/hebrew/monthNames/format/abbreviated");
+ if(b1!=null){
+ logln("loaded data for abbreviated month names: "+ b1.getKey());
+ }
+ }catch(MissingResourceException ex){
+ warnln("Failed to load data for abbreviated month names");
+ }
+ }
+}
+