0001: //##header
0002: /**
0003: *******************************************************************************
0004: * Copyright (C) 2001-2006, International Business Machines Corporation and *
0005: * others. All Rights Reserved. *
0006: *******************************************************************************
0007: */package com.ibm.icu.dev.test.util;
0008:
0009: import java.io.BufferedReader;
0010: import java.io.File;
0011: import java.io.InputStream;
0012: import java.io.InputStreamReader;
0013: import java.io.IOException;
0014: import java.net.URL;
0015: import java.net.URLConnection;
0016: import java.net.JarURLConnection; //#ifndef FOUNDATION
0017: import java.nio.ByteBuffer; //#else
0018: //##import com.ibm.icu.impl.ByteBuffer;
0019: //#endif
0020: import java.util.MissingResourceException;
0021: import java.util.Enumeration;
0022: import java.util.jar.JarEntry;
0023:
0024: import com.ibm.icu.dev.test.TestFmwk;
0025: import com.ibm.icu.impl.ICUData;
0026: import com.ibm.icu.impl.ICUResourceBundle;
0027: import com.ibm.icu.impl.Utility;
0028: import com.ibm.icu.text.UTF16;
0029: import com.ibm.icu.util.Holiday;
0030: import com.ibm.icu.util.ULocale;
0031: import com.ibm.icu.util.UResourceBundle;
0032: import com.ibm.icu.util.UResourceTypeMismatchException;
0033:
0034: public final class ICUResourceBundleTest extends TestFmwk {
0035: private static final ClassLoader testLoader = ICUResourceBundleTest.class
0036: .getClassLoader();
0037:
0038: public static void main(String args[]) throws Exception {
0039: ICUResourceBundleTest test = new ICUResourceBundleTest();
0040: test.run(args);
0041:
0042: }
0043:
0044: public void TestGetResources() {
0045: try {
0046: // It does not work well in eclipse plug-in test because of class loader configuration??
0047: // For now, specify resource path explicitly in this test case
0048: //Enumeration en = testLoader.getResources("META-INF");
0049: Enumeration en = testLoader
0050: .getResources("com.ibm.icu.dev.data");
0051: for (; en.hasMoreElements();) {
0052: URL url = (URL) en.nextElement();
0053: if (url == null) {
0054: warnln("could not load resource data");
0055: return;
0056: }
0057: URLConnection c = url.openConnection();
0058:
0059: if (c instanceof JarURLConnection) {
0060: JarURLConnection jc = (JarURLConnection) c;
0061: JarEntry je = jc.getJarEntry();
0062: logln("jar entry: " + je.toString());
0063: } else {
0064: InputStream is = c.getInputStream();
0065: logln("input stream:");
0066: InputStreamReader r = new InputStreamReader(is);
0067: BufferedReader br = new BufferedReader(r);
0068: String line = null;
0069: int n = 0;
0070: while ((line = br.readLine()) != null) {
0071: logln(" " + ++n + ": " + line);
0072: }
0073: }
0074: }
0075: } catch (SecurityException ex) {
0076: warnln("could not load resource data: " + ex);
0077: ex.printStackTrace();
0078: } catch (NullPointerException ex) {
0079: // thrown by ibm 1.4.2 windows jvm security manager
0080: warnln("could not load resource data: " + ex);
0081: } catch (Exception ex) {
0082: ex.printStackTrace();
0083: errln("Unexpected exception: " + ex);
0084: }
0085: }
0086:
0087: public void TestResourceBundleWrapper() {
0088: UResourceBundle bundle = UResourceBundle.getBundleInstance(
0089: "com.ibm.icu.impl.data.HolidayBundle", "da_DK");
0090: Object o = bundle.getObject("holidays");
0091: if (o instanceof Holiday[]) {
0092: logln("wrapper mechanism works for Weekend data");
0093: } else {
0094: errln("Did not get the expected output for Weekend data");
0095: }
0096:
0097: bundle = UResourceBundle.getBundleInstance(
0098: ICUResourceBundle.ICU_BASE_NAME, "bogus");
0099: if (bundle instanceof ICUResourceBundle
0100: && bundle.getULocale().equals("en_US")) {
0101: logln("wrapper mechanism works for bogus locale");
0102: } else {
0103: errln("wrapper mechanism failed for bogus locale.");
0104: }
0105:
0106: try {
0107: bundle = UResourceBundle
0108: .getBundleInstance("bogus", "bogus");
0109: if (bundle != null) {
0110: errln("Did not get the expected exception");
0111: }
0112: } catch (MissingResourceException ex) {
0113: logln("got the expected exception");
0114: }
0115:
0116: }
0117:
0118: public void TestJB3879() {
0119: // this tests tests loading of root bundle when a resource bundle
0120: // for the default locale is requested
0121: try {
0122: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
0123: .getBundleInstance("com/ibm/icu/dev/data/testdata",
0124: ULocale.getDefault().toString(), testLoader);
0125: if (bundle == null) {
0126: errln("could not create the resource bundle");
0127: }
0128: } catch (MissingResourceException ex) {
0129: warnln("could not load test data: " + ex.getMessage());
0130: }
0131: }
0132:
0133: public void TestOpen() {
0134: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
0135: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
0136: "en_US_POSIX");
0137:
0138: if (bundle == null) {
0139: errln("could not create the resource bundle");
0140: }
0141:
0142: ICUResourceBundle obj = bundle.get("NumberPatterns");
0143:
0144: int size = obj.getSize();
0145: int type = obj.getType();
0146: if (type == ICUResourceBundle.ARRAY) {
0147: ICUResourceBundle sub;
0148: for (int i = 0; i < size; i++) {
0149: sub = obj.get(i);
0150: String temp = sub.getString();
0151: if (temp.length() == 0) {
0152: errln("Failed to get the items from NumberPatterns array in bundle: "
0153: + bundle.getULocale().getBaseName());
0154: }
0155: //System.out.println("\""+prettify(temp)+"\"");
0156: }
0157:
0158: }
0159: String[] strings = bundle.getStringArray("NumberPatterns");
0160: if (size != strings.length) {
0161: errln("Failed to get the items from NumberPatterns array in bundle: "
0162: + bundle.getULocale().getBaseName());
0163: }
0164: {
0165: obj = bundle.get("NumberElements");
0166:
0167: size = obj.getSize();
0168: type = obj.getType();
0169: if (type == ICUResourceBundle.ARRAY) {
0170: ICUResourceBundle sub;
0171: for (int i = 0; i < size; i++) {
0172: sub = obj.get(i);
0173: String temp = sub.getString();
0174: if (temp.length() == 0) {
0175: errln("Failed to get the items from NumberPatterns array in bundle: "
0176: + bundle.getULocale().getBaseName());
0177: }
0178: // System.out.println("\""+prettify(temp)+"\"");
0179: }
0180:
0181: }
0182: }
0183: if (bundle == null) {
0184: errln("could not create the resource bundle");
0185: }
0186: bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0187: ICUResourceBundle.ICU_COLLATION_BASE_NAME,
0188: "en_US_POSIX");
0189: if (bundle == null) {
0190: errln("could not load the stream");
0191: }
0192: bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0193: ICUResourceBundle.ICU_BASE_NAME,
0194: "my_very_very_very_long_bogus_bundle");
0195: if (!bundle.getULocale().equals(ULocale.getDefault())) {
0196: errln("UResourceBundle did not load the default bundle when bundle was not found");
0197: }
0198:
0199: }
0200:
0201: public void TestBasicTypes() {
0202: ICUResourceBundle bundle = null;
0203: try {
0204: bundle = (ICUResourceBundle) UResourceBundle
0205: .getBundleInstance("com/ibm/icu/dev/data/testdata",
0206: "testtypes", testLoader);
0207: } catch (MissingResourceException e) {
0208: warnln("could not load test data: " + e.getMessage());
0209: return;
0210: }
0211: {
0212: String expected = "abc\u0000def";
0213: ICUResourceBundle sub = bundle.get("zerotest");
0214: if (!expected.equals(sub.getString())) {
0215: errln("Did not get the expected string for key zerotest in bundle testtypes");
0216: }
0217: sub = bundle.get("emptyexplicitstring");
0218: expected = "";
0219: if (!expected.equals(sub.getString())) {
0220: errln("Did not get the expected string for key emptyexplicitstring in bundle testtypes");
0221: }
0222: sub = bundle.get("emptystring");
0223: expected = "";
0224: if (!expected.equals(sub.getString())) {
0225: errln("Did not get the expected string for key emptystring in bundle testtypes");
0226: }
0227: }
0228: {
0229: int expected = 123;
0230: ICUResourceBundle sub = bundle.get("onehundredtwentythree");
0231: if (expected != sub.getInt()) {
0232: errln("Did not get the expected int value for key onehundredtwentythree in bundle testtypes");
0233: }
0234: sub = bundle.get("emptyint");
0235: expected = 0;
0236: if (expected != sub.getInt()) {
0237: errln("Did not get the expected int value for key emptyint in bundle testtypes");
0238: }
0239: }
0240: {
0241: int expected = 1;
0242: ICUResourceBundle sub = bundle.get("one");
0243: if (expected != sub.getInt()) {
0244: errln("Did not get the expected int value for key one in bundle testtypes");
0245: }
0246: }
0247: {
0248: int expected = -1;
0249: ICUResourceBundle sub = bundle.get("minusone");
0250: int got = sub.getInt();
0251: if (expected != got) {
0252: errln("Did not get the expected int value for key minusone in bundle testtypes");
0253: }
0254: expected = 0xFFFFFFF;
0255: got = sub.getUInt();
0256: if (expected != got) {
0257: errln("Did not get the expected int value for key minusone in bundle testtypes");
0258: }
0259: }
0260: {
0261: int expected = 1;
0262: ICUResourceBundle sub = bundle.get("plusone");
0263: if (expected != sub.getInt()) {
0264: errln("Did not get the expected int value for key minusone in bundle testtypes");
0265: }
0266:
0267: }
0268: {
0269: int[] expected = new int[] { 1, 2, 3, -3, 4, 5, 6, 7 };
0270: ICUResourceBundle sub = bundle.get("integerarray");
0271: if (!Utility.arrayEquals(expected, sub.getIntVector())) {
0272: errln("Did not get the expected int vector value for key integerarray in bundle testtypes");
0273: }
0274: sub = bundle.get("emptyintv");
0275: expected = new int[0];
0276: if (!Utility.arrayEquals(expected, sub.getIntVector())) {
0277: errln("Did not get the expected int vector value for key emptyintv in bundle testtypes");
0278: }
0279:
0280: }
0281: {
0282: ICUResourceBundle sub = bundle.get("binarytest");
0283: ByteBuffer got = sub.getBinary();
0284: if (got.remaining() != 15) {
0285: errln("Did not get the expected length for the binary ByteBuffer");
0286: }
0287: for (int i = 0; i < got.remaining(); i++) {
0288: byte b = got.get();
0289: if (b != i) {
0290: errln("Did not get the expected value for binary buffer at index: "
0291: + i);
0292: }
0293: }
0294: sub = bundle.get("emptybin");
0295: got = sub.getBinary();
0296: if (got.remaining() != 0) {
0297: errln("Did not get the expected length for the emptybin ByteBuffer");
0298: }
0299:
0300: }
0301: {
0302: ICUResourceBundle sub = bundle.get("emptyarray");
0303: String key = sub.getKey();
0304: if (!key.equals("emptyarray")) {
0305: errln("Did not get the expected key for emptytable item");
0306: }
0307: if (sub.getSize() != 0) {
0308: errln("Did not get the expected length for emptytable item");
0309: }
0310: }
0311: {
0312: ICUResourceBundle sub = bundle.get("menu");
0313: String key = sub.getKey();
0314: if (!key.equals("menu")) {
0315: errln("Did not get the expected key for menu item");
0316: }
0317: ICUResourceBundle sub1 = sub.get("file");
0318: key = sub1.getKey();
0319: if (!key.equals("file")) {
0320: errln("Did not get the expected key for file item");
0321: }
0322: ICUResourceBundle sub2 = sub1.get("open");
0323: key = sub2.getKey();
0324: if (!key.equals("open")) {
0325: errln("Did not get the expected key for file item");
0326: }
0327: String value = sub2.getString();
0328: if (!value.equals("Open")) {
0329: errln("Did not get the expected value for key for oen item");
0330: }
0331:
0332: sub = bundle.get("emptytable");
0333: key = sub.getKey();
0334: if (!key.equals("emptytable")) {
0335: errln("Did not get the expected key for emptytable item");
0336: }
0337: if (sub.getSize() != 0) {
0338: errln("Did not get the expected length for emptytable item");
0339: }
0340: sub = bundle.get("menu").get("file");
0341: int size = sub.getSize();
0342: String expected;
0343: for (int i = 0; i < size; i++) {
0344: sub1 = sub.get(i);
0345:
0346: switch (i) {
0347: case 0:
0348: expected = "exit";
0349: break;
0350: case 1:
0351: expected = "open";
0352: break;
0353: case 2:
0354: expected = "save";
0355: break;
0356: default:
0357: expected = "";
0358: }
0359: String got = sub1.getKey();
0360: if (!expected.equals(got)) {
0361: errln("Did not get the expected key at index" + i
0362: + ". Expected: " + expected + " Got: "
0363: + got);
0364: } else {
0365: logln("Got the expected key at index: " + i);
0366: }
0367: }
0368: }
0369:
0370: }
0371:
0372: private static final class TestCase {
0373: String key;
0374: int value;
0375:
0376: TestCase(String key, int value) {
0377: this .key = key;
0378: this .value = value;
0379: }
0380: }
0381:
0382: public void TestTable32() {
0383: TestCase[] arr = new TestCase[] {
0384: new TestCase("ooooooooooooooooo", 0),
0385: new TestCase("oooooooooooooooo1", 1),
0386: new TestCase("ooooooooooooooo1o", 2),
0387: new TestCase("oo11ooo1ooo11111o", 25150),
0388: new TestCase("oo11ooo1ooo111111", 25151),
0389: new TestCase("o1111111111111111", 65535),
0390: new TestCase("1oooooooooooooooo", 65536),
0391: new TestCase("1ooooooo11o11ooo1", 65969),
0392: new TestCase("1ooooooo11o11oo1o", 65970),
0393: new TestCase("1ooooooo111oo1111", 65999) };
0394: ICUResourceBundle bundle = null;
0395: try {
0396: bundle = (ICUResourceBundle) UResourceBundle
0397: .getBundleInstance("com/ibm/icu/dev/data/testdata",
0398: "testtable32", testLoader);
0399: } catch (MissingResourceException ex) {
0400: warnln("could not load resource data: " + ex.getMessage());
0401: return;
0402: }
0403:
0404: if (bundle.getType() != ICUResourceBundle.TABLE) {
0405: errln("Could not get the correct type for bundle testtable32");
0406: }
0407: int size = bundle.getSize();
0408: if (size != 66000) {
0409: errln("Could not get the correct size for bundle testtable32");
0410: }
0411: for (int i = 0; i < size; i++) {
0412: ICUResourceBundle item = bundle.get(i);
0413: String key = item.getKey();
0414: int parsedNumber = parseTable32Key(key);
0415: int number = -1;
0416: switch (item.getType()) {
0417: case ICUResourceBundle.STRING:
0418: String value = item.getString();
0419: number = UTF16.charAt(value, 0);
0420: break;
0421: case ICUResourceBundle.INT:
0422: number = item.getInt();
0423: break;
0424: default:
0425: errln("Got unexpected resource type in testtable32");
0426:
0427: }
0428: if (number != parsedNumber) {
0429: errln("Did not get expected value in testtypes32 for key"
0430: + key
0431: + ". Expected: "
0432: + parsedNumber
0433: + " Got:"
0434: + number);
0435: }
0436:
0437: }
0438: for (int i = 0; i < arr.length; i++) {
0439: String expected = arr[i].key;
0440: ICUResourceBundle item = bundle.get(expected);
0441: int number = 0;
0442: String key = item.getKey();
0443: int parsedNumber = parseTable32Key(key);
0444: if (!key.equals(expected)) {
0445: errln("Did not get the expected key. Expected: "
0446: + expected + " Got:" + key);
0447: }
0448: switch (item.getType()) {
0449: case ICUResourceBundle.STRING:
0450: String value = item.getString();
0451: number = UTF16.charAt(value, 0);
0452: break;
0453: case ICUResourceBundle.INT:
0454: number = item.getInt();
0455: break;
0456: default:
0457: errln("Got unexpected resource type in testtable32");
0458: }
0459:
0460: if (number != parsedNumber) {
0461: errln("Did not get expected value in testtypes32 for key"
0462: + key
0463: + ". Expected: "
0464: + parsedNumber
0465: + " Got:"
0466: + number);
0467: }
0468: }
0469: }
0470:
0471: private static int parseTable32Key(String key) {
0472: int number;
0473: char c;
0474:
0475: number = 0;
0476: for (int i = 0; i < key.length(); i++) {
0477: c = key.charAt(i);
0478: number <<= 1;
0479: if (c == '1') {
0480: number |= 1;
0481: }
0482: }
0483: return number;
0484: }
0485:
0486: public void TestAliases() {
0487: String simpleAlias = "Open";
0488:
0489: ICUResourceBundle rb = (ICUResourceBundle) ICUResourceBundle
0490: .createBundle("com/ibm/icu/dev/data/testdata",
0491: "testaliases", testLoader);
0492: if (rb == null) {
0493: warnln("could not load testaliases data");
0494: return;
0495: }
0496: ICUResourceBundle sub = rb.get("simplealias");
0497: String s1 = sub.getString("simplealias");
0498: if (s1.equals(simpleAlias)) {
0499: logln("Alias mechanism works for simplealias");
0500: } else {
0501: errln("Did not get the expected output for simplealias");
0502: }
0503: {
0504: try {
0505: rb = (ICUResourceBundle) UResourceBundle
0506: .getBundleInstance(
0507: "com/ibm/icu/dev/data/testdata",
0508: "testaliases", testLoader);
0509: sub = rb.get("nonexisting");
0510: errln("Did not get the expected exception for nonexisting");
0511: } catch (MissingResourceException ex) {
0512: logln("Alias mechanism works for nonexisting alias");
0513: }
0514: }
0515: {
0516: rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0517: "com/ibm/icu/dev/data/testdata", "testaliases",
0518: testLoader);
0519: sub = rb.get("referencingalias");
0520: s1 = sub.getString();
0521: if (s1.equals("Hani")) {
0522: logln("Alias mechanism works for referencingalias");
0523: } else {
0524: errln("Did not get the expected output for referencingalias");
0525: }
0526: }
0527: {
0528: rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0529: "com/ibm/icu/dev/data/testdata", "testaliases",
0530: testLoader);
0531: sub = rb.get("boundaries");
0532: String word = sub.getString("word");
0533:
0534: if (word.equals("word_ja.brk")) {
0535: logln("Got the expected output for boundaries/word");
0536: } else {
0537: errln("Did not get the expected type for boundaries/word");
0538: }
0539:
0540: }
0541: {
0542: ICUResourceBundle rb1 = (ICUResourceBundle) UResourceBundle
0543: .getBundleInstance("com/ibm/icu/dev/data/testdata",
0544: "testaliases", testLoader);
0545: if (rb1 != rb) {
0546: errln("Caching of the resource bundle failed");
0547: } else {
0548: logln("Caching of resource bundle passed");
0549: }
0550: sub = rb1.get("testGetStringByKeyAliasing");
0551:
0552: s1 = sub.get("KeyAlias0PST").getString();
0553: if (s1.equals("America/Los_Angeles")) {
0554: logln("Alias mechanism works for KeyAlias0PST");
0555: } else {
0556: errln("Did not get the expected output for KeyAlias0PST");
0557: }
0558:
0559: s1 = sub.getString("KeyAlias1PacificStandardTime");
0560: if (s1.equals("Pacific Standard Time")) {
0561: logln("Alias mechanism works for KeyAlias1PacificStandardTime");
0562: } else {
0563: errln("Did not get the expected output for KeyAlias1PacificStandardTime");
0564: }
0565: s1 = sub.getString("KeyAlias2PDT");
0566: if (s1.equals("PDT")) {
0567: logln("Alias mechanism works for KeyAlias2PDT");
0568: } else {
0569: errln("Did not get the expected output for KeyAlias2PDT");
0570: }
0571:
0572: s1 = sub.getString("KeyAlias3LosAngeles");
0573: if (s1.equals("Los Angeles")) {
0574: logln("Alias mechanism works for KeyAlias3LosAngeles. Got: "
0575: + s1);
0576: } else {
0577: errln("Did not get the expected output for KeyAlias3LosAngeles. Got: "
0578: + s1);
0579: }
0580: }
0581: {
0582: sub = rb.get("testGetStringByIndexAliasing");
0583: s1 = sub.getString(0);
0584: if (s1.equals("America/Los_Angeles")) {
0585: logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: "
0586: + s1);
0587: } else {
0588: errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: "
0589: + s1);
0590: }
0591: s1 = sub.getString(1);
0592: if (s1.equals("Pacific Standard Time")) {
0593: logln("Alias mechanism works for testGetStringByIndexAliasing/1");
0594: } else {
0595: errln("Did not get the expected output for testGetStringByIndexAliasing/1");
0596: }
0597: s1 = sub.getString(2);
0598: if (s1.equals("PDT")) {
0599: logln("Alias mechanism works for testGetStringByIndexAliasing/2");
0600: } else {
0601: errln("Did not get the expected output for testGetStringByIndexAliasing/2");
0602: }
0603:
0604: s1 = sub.getString(3);
0605: if (s1.equals("Los Angeles")) {
0606: logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: "
0607: + s1);
0608: } else {
0609: errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: "
0610: + s1);
0611: }
0612: }
0613: {
0614: sub = rb.get("testAliasToTree");
0615:
0616: ByteBuffer buf = sub.get("standard").get("%%CollationBin")
0617: .getBinary();
0618: if (buf == null) {
0619: errln("Did not get the expected output for %%CollationBin");
0620: }
0621: }
0622: // should not get an exception
0623: rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0624: ICUResourceBundle.ICU_RBNF_BASE_NAME, "fr_BE");
0625: String str = rb.getString("SpelloutRules");
0626: if (str != null || str.length() > 0) {
0627: logln("Alias mechanism works");
0628: } else {
0629: errln("Alias mechanism failed for fr_BE SpelloutRules");
0630: }
0631: rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0632: ICUResourceBundle.ICU_COLLATION_BASE_NAME, "zh_TW");
0633: ICUResourceBundle b = (ICUResourceBundle) rb
0634: .getObject("collations");
0635: if (b != null) {
0636: if (b.get(0).getKey().equals("default")) {
0637: logln("Alias mechanism works");
0638: } else {
0639: errln("Alias mechanism failed for zh_TW collations");
0640: }
0641: } else {
0642: errln("Did not get the expected object for collations");
0643: }
0644:
0645: }
0646:
0647: public void TestAlias() {
0648: logln("Testing %%ALIAS");
0649: ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle
0650: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
0651: "iw_IL");
0652: ICUResourceBundle b = rb.get("NumberPatterns");
0653: if (b != null) {
0654: if (b.getSize() > 0) {
0655: logln("%%ALIAS mechanism works");
0656: } else {
0657: errln("%%ALIAS mechanism failed for iw_IL collations");
0658: }
0659: } else {
0660: errln("%%ALIAS mechanism failed for iw_IL");
0661: }
0662: }
0663:
0664: public void TestXPathAlias() {
0665: ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle
0666: .getBundleInstance("com/ibm/icu/dev/data/testdata",
0667: "te_IN", testLoader);
0668: ICUResourceBundle b = rb.get("aliasClient");
0669: String result = b.getString();
0670: String expResult = "correct";
0671:
0672: if (!result.equals(expResult)) {
0673: errln("Did not get the expected result for XPath style alias");
0674: }
0675: try {
0676: ICUResourceBundle c = rb.get("rootAliasClient");
0677: result = c.getString();
0678: expResult = "correct";
0679: if (!result.equals(expResult)) {
0680: errln("Did not get the expected result for XPath style alias for rootAliasClient");
0681: }
0682: } catch (MissingResourceException ex) {
0683: errln("Could not get rootAliasClient");
0684: }
0685: }
0686:
0687: public void TestCircularAliases() {
0688: try {
0689: ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle
0690: .getBundleInstance("com/ibm/icu/dev/data/testdata",
0691: "testaliases", testLoader);
0692: ICUResourceBundle sub = rb.get("aaa");
0693: String s1 = sub.getString();
0694: if (s1 != null) {
0695: errln("Did not get the expected exception");
0696: }
0697: } catch (IllegalArgumentException ex) {
0698: logln("got expected exception for circular references");
0699: } catch (MissingResourceException ex) {
0700: warnln("could not load resource data: " + ex.getMessage());
0701: }
0702: }
0703:
0704: public void TestGetWithFallback() {
0705: /*
0706: ICUResourceBundle bundle =(ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");
0707: String key = bundle.getStringWithFallback("Keys/collation");
0708: if(!key.equals("COLLATION")){
0709: errln("Did not get the expected result from getStringWithFallback method.");
0710: }
0711: String type = bundle.getStringWithFallback("Types/collation/direct");
0712: if(!type.equals("DIRECT")){
0713: errln("Did not get the expected result form getStringWithFallback method.");
0714: }
0715: */
0716: ICUResourceBundle bundle = null;
0717: String key = null;
0718: try {
0719: bundle = (ICUResourceBundle) UResourceBundle
0720: .getBundleInstance(
0721: ICUResourceBundle.ICU_COLLATION_BASE_NAME,
0722: ULocale.canonicalize("de__PHONEBOOK"));
0723:
0724: if (!bundle.getULocale().equals("de")) {
0725: errln("did not get the expected bundle");
0726: }
0727: key = bundle
0728: .getStringWithFallback("collations/collation/default");
0729: if (!key.equals("phonebook")) {
0730: errln("Did not get the expected result from getStringWithFallback method.");
0731: }
0732:
0733: } catch (MissingResourceException ex) {
0734: logln("got the expected exception");
0735: }
0736:
0737: bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0738: ICUResourceBundle.ICU_COLLATION_BASE_NAME, "fr_FR");
0739: key = bundle.getStringWithFallback("collations/default");
0740: if (!key.equals("standard")) {
0741: errln("Did not get the expected result from getStringWithFallback method.");
0742: }
0743: bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
0744: ICUResourceBundle.ICU_BASE_NAME, "fr_FR");
0745: ICUResourceBundle b1 = bundle.getWithFallback("calendar");
0746: String defaultCal = b1.getStringWithFallback("default");
0747: if (!defaultCal.equals("gregorian")) {
0748: errln("Did not get the expected default calendar string: Expected: gregorian, Got: "
0749: + defaultCal);
0750: }
0751: ICUResourceBundle b2 = b1.getWithFallback(defaultCal);
0752: ICUResourceBundle b3 = b2.getWithFallback("monthNames");
0753: String defaultContext = b3.getStringWithFallback("default");
0754: ICUResourceBundle b4 = b3.getWithFallback(defaultContext);
0755: String defaultWidth = b4.getStringWithFallback("default");
0756: ICUResourceBundle b5 = b4.getWithFallback(defaultWidth);
0757: if (b5.getSize() != 12) {
0758: errln("Did not get the expected size for the default monthNames");
0759: }
0760: }
0761:
0762: private static final String COLLATION_RESNAME = "collations";
0763: private static final String COLLATION_KEYWORD = "collation";
0764: private static final String DEFAULT_NAME = "default";
0765: private static final String STANDARD_NAME = "standard";
0766:
0767: public void TestKeywordValues() {
0768: String kwVals[];
0769: boolean foundStandard = false;
0770: int n;
0771:
0772: logln("Testing getting collation values:");
0773: kwVals = ICUResourceBundle.getKeywordValues(
0774: ICUResourceBundle.ICU_COLLATION_BASE_NAME,
0775: COLLATION_RESNAME);
0776: for (n = 0; n < kwVals.length; n++) {
0777: logln(new Integer(n).toString() + ": " + kwVals[n]);
0778: if (DEFAULT_NAME.equals(kwVals[n])) {
0779: errln("getKeywordValues for collation returned 'default' in the list.");
0780: } else if (STANDARD_NAME.equals(kwVals[n])) {
0781: if (foundStandard == false) {
0782: foundStandard = true;
0783: logln("found 'standard'");
0784: } else {
0785: errln("Error - 'standard' is in the keyword list twice!");
0786: }
0787: }
0788: }
0789:
0790: if (foundStandard == false) {
0791: errln("Error - 'standard' was not in the collation tree as a keyword.");
0792: } else {
0793: logln("'standard' was found as a collation keyword.");
0794: }
0795: }
0796:
0797: public void TestLocaleDisplayNames() {
0798: ULocale[] locales = ULocale.getAvailableLocales();
0799: for (int i = 0; i < locales.length; ++i) {
0800: if (!hasLocalizedCountryFor(ULocale.ENGLISH, locales[i])) {
0801: errln("Could not get localized country for "
0802: + locales[i]);
0803: }
0804: if (!hasLocalizedLanguageFor(ULocale.ENGLISH, locales[i])) {
0805: errln("Could not get localized language for "
0806: + locales[i]);
0807: }
0808: if (!hasLocalizedCountryFor(locales[i], locales[i])) {
0809: errln("Could not get localized country for "
0810: + locales[i]);
0811: hasLocalizedCountryFor(locales[i], locales[i]);
0812: }
0813: if (!hasLocalizedLanguageFor(locales[i], locales[i])) {
0814: errln("Could not get localized language for "
0815: + locales[i]);
0816: }
0817:
0818: logln(locales[i] + "\t"
0819: + locales[i].getDisplayName(ULocale.ENGLISH) + "\t"
0820: + locales[i].getDisplayName(locales[i]));
0821: }
0822: }
0823:
0824: private static boolean hasLocalizedLanguageFor(ULocale locale,
0825: ULocale otherLocale) {
0826: String lang = otherLocale.getLanguage();
0827: String localizedVersion = otherLocale
0828: .getDisplayLanguage(locale);
0829: return !lang.equals(localizedVersion);
0830: }
0831:
0832: private static boolean hasLocalizedCountryFor(ULocale locale,
0833: ULocale otherLocale) {
0834: String country = otherLocale.getCountry();
0835: if (country.equals(""))
0836: return true;
0837: String localizedVersion = otherLocale.getDisplayCountry(locale);
0838: return !country.equals(localizedVersion);
0839: }
0840:
0841: public void TestFunctionalEquivalent() {
0842: String[] testCases = {
0843: // avail locale equiv
0844: "f",
0845: "de_US_CALIFORNIA",
0846: "de",
0847: "t",
0848: "zh_TW@collation=stroke",
0849: "zh@collation=stroke",
0850: "f",
0851: "de_CN@collation=pinyin",
0852: "de",
0853: "t",
0854: "zh@collation=pinyin",
0855: "zh",
0856: "t",
0857: "zh_CN@collation=pinyin",
0858: "zh", /* should be 'T' when validSubLocales works */
0859: "t",
0860: "zh_HK@collation=pinyin",
0861: "zh",
0862: "t",
0863: "zh_HK@collation=stroke",
0864: "zh@collation=stroke",
0865: "t",
0866: "zh_HK",
0867: "zh@collation=stroke",
0868: "t",
0869: "zh_MO",
0870: "zh@collation=stroke",
0871: "t",
0872: "zh_TW_STROKE",
0873: "zh@collation=stroke",
0874: "t",
0875: "zh_TW_STROKE@collation=big5han",
0876: "zh@collation=big5han",
0877: "f",
0878: "de_CN@calendar=japanese",
0879: "de",
0880: "t",
0881: "de@calendar=japanese",
0882: "de",
0883: "t",
0884: "zh_TW@collation=big5han",
0885: "zh@collation=big5han",
0886: "t",
0887: "zh_TW@collation=gb2312han",
0888: "zh@collation=gb2312han",
0889: "t",
0890: "zh_CN@collation=big5han",
0891: "zh@collation=big5han",
0892: "t",
0893: "zh_CN@collation=gb2312han",
0894: "zh@collation=gb2312han",
0895: "t",
0896: "zh@collation=big5han",
0897: "zh@collation=big5han",
0898: "t",
0899: "zh@collation=gb2312han",
0900: "zh@collation=gb2312han",
0901: "t",
0902: "hi_IN@collation=direct",
0903: "hi@collation=direct",
0904: "t",
0905: "hi@collation=standard",
0906: "hi",
0907: "t",
0908: "hi@collation=direct",
0909: "hi@collation=direct",
0910: "f",
0911: "hi_AU@collation=direct;currency=CHF;calendar=buddhist",
0912: "hi@collation=direct",
0913: "f",
0914: "hi_AU@collation=standard;currency=CHF;calendar=buddhist",
0915: "hi", "t", "de_DE@collation=pinyin", "de", /* bug 4582 tests */
0916: "f", "de_DE_BONN@collation=pinyin", "de", "t", "nl",
0917: "root", "t", "nl_NL", "root", "f", "nl_NL_EEXT",
0918: "root", "t", "nl@collation=stroke", "root", "t",
0919: "nl_NL@collation=stroke", "root", "f",
0920: "nl_NL_EEXT@collation=stroke", "root", };
0921:
0922: String F_STR = "f";
0923: String T_STR = "t";
0924: boolean isAvail[] = new boolean[1];
0925: int i;
0926:
0927: logln("Testing functional equivalents...");
0928: for (i = 0; i < testCases.length; i += 3) {
0929: boolean expectAvail = T_STR.equals(testCases[i + 0]);
0930: ULocale inLocale = new ULocale(testCases[i + 1]);
0931: ULocale expectLocale = new ULocale(testCases[i + 2]);
0932:
0933: logln(new Integer(i / 3).toString() + ": "
0934: + new Boolean(expectAvail).toString() + "\t\t"
0935: + inLocale.toString() + "\t\t"
0936: + expectLocale.toString());
0937:
0938: ULocale equivLocale = ICUResourceBundle
0939: .getFunctionalEquivalent(
0940: ICUResourceBundle.ICU_COLLATION_BASE_NAME,
0941: COLLATION_RESNAME, COLLATION_KEYWORD,
0942: inLocale, isAvail);
0943: boolean gotAvail = isAvail[0];
0944:
0945: if ((gotAvail != expectAvail)
0946: || !equivLocale.equals(expectLocale)) {
0947: errln(new Integer(i / 3).toString()
0948: + ": Error, expected Equiv="
0949: + new Boolean(expectAvail).toString() + "\t\t"
0950: + inLocale.toString() + "\t\t--> "
0951: + expectLocale.toString() + ", but got "
0952: + new Boolean(gotAvail).toString() + " "
0953: + equivLocale.toString());
0954: }
0955: }
0956:
0957: logln("Testing error conditions:");
0958: try {
0959: ULocale equivLocale = ICUResourceBundle
0960: .getFunctionalEquivalent(
0961: ICUResourceBundle.ICU_COLLATION_BASE_NAME,
0962: "calendar", "calendar", new ULocale(
0963: "ar_EG@calendar=islamic"),
0964: new boolean[1]);
0965: errln("Err: expected MissingResourceException");
0966: } catch (MissingResourceException t) {
0967: logln("expected MissingResourceException caught (PASS): "
0968: + t.toString());
0969: }
0970: }
0971:
0972: public void TestNorwegian() {
0973: try {
0974: ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle
0975: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
0976: "no_NO_NY");
0977: ICUResourceBundle sub = rb.get("Countries");
0978: String s1 = sub.getString("NO");
0979: if (s1.equals("Noreg")) {
0980: logln("got expected output ");
0981: } else {
0982: errln("did not get the expected result");
0983: }
0984: } catch (IllegalArgumentException ex) {
0985: errln("Caught an unexpected expected");
0986: }
0987: }
0988:
0989: public void TestJB4102() {
0990: try {
0991: ICUResourceBundle root = (ICUResourceBundle) ICUResourceBundle
0992: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
0993: "root");
0994: ICUResourceBundle t = null;
0995: try {
0996: t = root
0997: .getWithFallback("calendar/islamic-civil/AmPmMarkers");
0998: errln("Second resource does not exist. How did it get here?\n");
0999: } catch (MissingResourceException ex) {
1000: logln("Got the expected exception");
1001: }
1002: try {
1003: t = root
1004: .getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera");
1005: errln("Second resource does not exist. How did it get here?\n");
1006: } catch (MissingResourceException ex) {
1007: logln("Got the expected exception");
1008: }
1009: if (t != null) {
1010: errln("t is not null!");
1011: }
1012: } catch (MissingResourceException e) {
1013: warnln("Could not load the locale data: " + e.getMessage());
1014: }
1015: }
1016:
1017: public void TestCLDRStyleAliases() {
1018: String result = null;
1019: String expected = null;
1020: String[] expects = new String[] { "", "a41", "a12", "a03",
1021: "ar4" };
1022:
1023: logln("Testing CLDR style aliases......\n");
1024:
1025: ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle
1026: .getBundleInstance("com/ibm/icu/dev/data/testdata",
1027: "te_IN_REVISED", testLoader);
1028: ICUResourceBundle alias = rb.get("a");
1029:
1030: for (int i = 1; i < 5; i++) {
1031: String resource = "a" + i;
1032: ICUResourceBundle a = ((ICUResourceBundle) alias)
1033: .getWithFallback(resource);
1034: result = a.getString();
1035: if (result.equals(expected)) {
1036: errln("CLDR style aliases failed resource with name "
1037: + resource + "resource, exp " + expects[i]
1038: + " , got " + result);
1039: }
1040: }
1041:
1042: }
1043:
1044: private String getLSString(int status) {
1045: switch (status) {
1046: case ICUResourceBundle.FROM_FALLBACK:
1047: return "FROM_FALLBACK";
1048: case ICUResourceBundle.FROM_DEFAULT:
1049: return "FROM_DEFAULT";
1050: case ICUResourceBundle.FROM_ROOT:
1051: return "FROM_ROOT";
1052: case ICUResourceBundle.FROM_LOCALE:
1053: return "FROM_LOCALE";
1054: default:
1055: return "UNKNOWN";
1056: }
1057: }
1058:
1059: public void TestLoadingStatus() {
1060: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
1061: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
1062: "yi_IL");
1063: int status = bundle.getLoadingStatus();
1064: if (status != ICUResourceBundle.FROM_DEFAULT) {
1065: errln("Did not get the expected value for loading status. Expected "
1066: + getLSString(ICUResourceBundle.FROM_DEFAULT)
1067: + " Got: " + getLSString(status));
1068: }
1069: bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
1070: ICUResourceBundle.ICU_BASE_NAME, "eo_DE");
1071: status = bundle.getLoadingStatus();
1072: if (status != ICUResourceBundle.FROM_FALLBACK) {
1073: errln("Did not get the expected value for loading status. Expected "
1074: + getLSString(ICUResourceBundle.FROM_FALLBACK)
1075: + " Got: " + getLSString(status));
1076: }
1077:
1078: logln("Test to verify loading status of get(String)");
1079: bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
1080: ICUResourceBundle.ICU_BASE_NAME, "te_IN");
1081: ICUResourceBundle countries = bundle.get("Countries");
1082: status = countries.getLoadingStatus();
1083: if (status != ICUResourceBundle.FROM_FALLBACK) {
1084: errln("Did not get the expected value for loading status. Expected "
1085: + getLSString(ICUResourceBundle.FROM_FALLBACK)
1086: + " Got: " + getLSString(status));
1087: }
1088: /*
1089: ICUResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters");
1090: status = auxExemplar.getLoadingStatus();
1091: if(status != ICUResourceBundle.FROM_ROOT){
1092: errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT)
1093: + " Got: " + getLSString(status));
1094: }
1095: */
1096: logln("Test to verify loading status of get(int)");
1097: ICUResourceBundle ms = bundle.get("MeasurementSystem");
1098: status = ms.getLoadingStatus();
1099: if (status != ICUResourceBundle.FROM_ROOT) {
1100: errln("Did not get the expected value for loading status. Expected "
1101: + getLSString(ICUResourceBundle.FROM_ROOT)
1102: + " Got: " + getLSString(status));
1103: }
1104:
1105: logln("Test to verify loading status of getwithFallback");
1106: bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
1107: "com/ibm/icu/dev/data/testdata", "sh_YU", testLoader);
1108: ICUResourceBundle temp = bundle.getWithFallback("a/a2");
1109: status = temp.getLoadingStatus();
1110: if (status != ICUResourceBundle.FROM_LOCALE) {
1111: errln("Did not get the expected value for loading status. Expected "
1112: + getLSString(ICUResourceBundle.FROM_LOCALE)
1113: + " Got: " + getLSString(status));
1114: }
1115: temp = bundle.getWithFallback("a/a1");
1116: status = temp.getLoadingStatus();
1117: if (status != ICUResourceBundle.FROM_FALLBACK) {
1118: errln("Did not get the expected value for loading status. Expected "
1119: + getLSString(ICUResourceBundle.FROM_FALLBACK)
1120: + " Got: " + getLSString(status));
1121: }
1122: temp = bundle.getWithFallback("a/a4");
1123: status = temp.getLoadingStatus();
1124: if (status != ICUResourceBundle.FROM_ROOT) {
1125: errln("Did not get the expected value for loading status. Expected "
1126: + getLSString(ICUResourceBundle.FROM_ROOT)
1127: + " Got: " + getLSString(status));
1128: }
1129: }
1130:
1131: public void TestCoverage() {
1132: UResourceBundle bundle;
1133: bundle = UResourceBundle
1134: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);
1135: if (bundle == null) {
1136: errln("UResourceBundle.getBundleInstance(String baseName) failed");
1137: }
1138: bundle = null;
1139: bundle = UResourceBundle
1140: .getBundleInstance(ULocale.getDefault());
1141: if (bundle == null) {
1142: errln("UResourceBundle.getBundleInstance(ULocale) failed");
1143: return;
1144: }
1145: if (new UResourceTypeMismatchException("coverage") == null) {
1146: errln("Create UResourceTypeMismatchException error");
1147: }
1148: class Stub extends UResourceBundle {
1149: public ULocale getULocale() {
1150: return ULocale.ROOT;
1151: }
1152:
1153: protected String getLocaleID() {
1154: return null;
1155: }
1156:
1157: protected String getBaseName() {
1158: return null;
1159: }
1160:
1161: protected UResourceBundle getParent() {
1162: return null;
1163: }
1164:
1165: protected void setLoadingStatus(int newStatus) {
1166: }
1167:
1168: public Enumeration getKeys() {
1169: return null;
1170: }
1171:
1172: protected Object handleGetObject(String key) {
1173: return null;
1174: }
1175: }
1176: Stub stub = new Stub();
1177:
1178: if (!stub.getLocale().equals(ULocale.ROOT.toLocale())) {
1179: errln("UResourceBundle.getLoclae(Locale) should delegate to (ULocale)");
1180: }
1181: }
1182:
1183: public void TestJavaULocaleBundleLoading() {
1184: String baseName = "com.ibm.icu.dev.data.resources.TestDataElements";
1185: String locName = "en_Latn_US";
1186: UResourceBundle bundle = UResourceBundle.getBundleInstance(
1187: baseName, locName, testLoader);
1188: String fromRoot = bundle.getString("from_root");
1189: if (!fromRoot.equals("This data comes from root")) {
1190: errln("Did not get the expected string for from_root");
1191: }
1192: String fromEn = bundle.getString("from_en");
1193: if (!fromEn.equals("This data comes from en")) {
1194: errln("Did not get the expected string for from_en");
1195: }
1196: String fromEnLatn = bundle.getString("from_en_Latn");
1197: if (!fromEnLatn.equals("This data comes from en_Latn")) {
1198: errln("Did not get the expected string for from_en_Latn");
1199: }
1200: String fromEnLatnUs = bundle.getString("from_en_Latn_US");
1201: if (!fromEnLatnUs.equals("This data comes from en_Latn_US")) {
1202: errln("Did not get the expected string for from_en_Latn_US");
1203: }
1204: UResourceBundle bundle1 = UResourceBundle.getBundleInstance(
1205: baseName, new ULocale(locName), testLoader);
1206: if (!bundle1.equals(bundle)) {
1207: errln("Did not get the expected bundle for " + baseName
1208: + "." + locName);
1209: }
1210: if (bundle1 != bundle) {
1211: errln("Did not load the bundle from cache");
1212: }
1213:
1214: UResourceBundle bundle2 = UResourceBundle.getBundleInstance(
1215: baseName, "en_IN", testLoader);
1216: if (!bundle2.getLocale().toString().equals("en")) {
1217: errln("Did not get the expected fallback locale. Expected: en Got: "
1218: + bundle2.getLocale().toString());
1219: }
1220: UResourceBundle bundle3 = UResourceBundle.getBundleInstance(
1221: baseName, "te_IN", testLoader);
1222: if (!bundle3.getLocale().toString().equals("te")) {
1223: errln("Did not get the expected fallback locale. Expected: te Got: "
1224: + bundle2.getLocale().toString());
1225: }
1226: // non-existent bundle .. should return default
1227: UResourceBundle defaultBundle = UResourceBundle
1228: .getBundleInstance(baseName, "hi_IN", testLoader);
1229: ULocale defaultLocale = ULocale.getDefault();
1230: if (!defaultBundle.getULocale().equals(defaultLocale)) {
1231: errln("Did not get the default bundle for non-existent bundle");
1232: }
1233: // non-existent bundle, non-existent default locale
1234: // so return the root bundle.
1235: ULocale.setDefault(ULocale.CANADA_FRENCH);
1236: UResourceBundle root = UResourceBundle.getBundleInstance(
1237: baseName, "hi_IN", testLoader);
1238: if (!root.getULocale().toString().equals("")) {
1239: errln("Did not get the root bundle for non-existent default bundle for non-existent bundle");
1240: }
1241: //reset the default
1242: ULocale.setDefault(defaultLocale);
1243: Enumeration keys = bundle.getKeys();
1244: int i = 0;
1245: while (keys.hasMoreElements()) {
1246: logln("key: " + keys.nextElement());
1247: i++;
1248: }
1249: if (i != 4) {
1250: errln("Did not get the expected number of keys: got " + i
1251: + ", expected 4");
1252: }
1253: UResourceBundle bundle4 = UResourceBundle.getBundleInstance(
1254: baseName, "fr_Latn_FR", testLoader);
1255: if (bundle == null) {
1256: errln("Could not load bundle fr_Latn_FR");
1257: }
1258: }
1259:
1260: public void TestAliasFallback() {
1261: try {
1262: ULocale loc = new ULocale("en_US");
1263: ICUResourceBundle b = (ICUResourceBundle) UResourceBundle
1264: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
1265: loc);
1266: ICUResourceBundle b1 = (ICUResourceBundle) b
1267: .getWithFallback("calendar/hebrew/monthNames/format/abbreviated");
1268: if (b1 != null) {
1269: logln("loaded data for abbreviated month names: "
1270: + b1.getKey());
1271: }
1272: } catch (MissingResourceException ex) {
1273: warnln("Failed to load data for abbreviated month names");
1274: }
1275: }
1276: }
|