Search in sources :

Example 1 with IllformedLocaleException

use of android.icu.util.IllformedLocaleException in project j2objc by google.

the class LocaleBuilderTest method TestLocaleBuilder.

@Test
public void TestLocaleBuilder() {
    // "L": +1 = language
    // "S": +1 = script
    // "R": +1 = region
    // "V": +1 = variant
    // "K": +1 = Unicode locale key / +2 = Unicode locale type
    // "A": +1 = Unicode locale attribute
    // "E": +1 = extension letter / +2 = extension value
    // "P": +1 = private use
    // "U": +1 = ULocale
    // "B": +1 = BCP47 language tag
    // "C": Clear all
    // "N": Clear extensions
    // "D": +1 = Unicode locale attribute to be removed
    // "X": indicates an exception must be thrown
    // "T": +1 = expected language tag / +2 = expected locale string
    String[][] TESTCASE = { { "L", "en", "R", "us", "T", "en-US", "en_US" }, { "L", "en", "R", "CA", "L", null, "T", "und-CA", "_CA" }, { "L", "en", "R", "CA", "L", "", "T", "und-CA", "_CA" }, { "L", "en", "R", "FR", "L", "fr", "T", "fr-FR", "fr_FR" }, { "L", "123", "X" }, { "R", "us", "T", "und-US", "_US" }, { "R", "usa", "X" }, { "R", "123", "L", "it", "R", null, "T", "it", "it" }, { "R", "123", "L", "it", "R", "", "T", "it", "it" }, { "R", "123", "L", "en", "T", "en-123", "en_123" }, { "S", "LATN", "L", "DE", "T", "de-Latn", "de_Latn" }, { "L", "De", "S", "latn", "R", "de", "S", "", "T", "de-DE", "de_DE" }, { "L", "De", "S", "latn", "R", "de", "S", null, "T", "de-DE", "de_DE" }, { "S", "latin", "X" }, { "V", "1234", "L", "en", "T", "en-1234", "en__1234" }, { "V", "1234", "L", "en", "V", "5678", "T", "en-5678", "en__5678" }, { "V", "1234", "L", "en", "V", null, "T", "en", "en" }, { "V", "1234", "L", "en", "V", "", "T", "en", "en" }, { "V", "123", "X" }, { "U", "en_US", "T", "en-US", "en_US" }, { "U", "en_US_WIN", "X" }, { "B", "fr-FR-1606nict-u-ca-gregory-x-test", "T", "fr-FR-1606nict-u-ca-gregory-x-test", "fr_FR_1606NICT@calendar=gregorian;x=test" }, { "B", "ab-cde-fghij", "T", "cde-fghij", "cde__FGHIJ" }, { "B", "und-CA", "T", "und-CA", "_CA" }, { "B", "en-US-x-test-lvariant-var", "T", "en-US-x-test-lvariant-var", "en_US_VAR@x=test" }, { "B", "en-US-VAR", "X" }, { "U", "ja_JP@calendar=japanese;currency=JPY", "L", "ko", "T", "ko-JP-u-ca-japanese-cu-jpy", "ko_JP@calendar=japanese;currency=jpy" }, { "U", "ja_JP@calendar=japanese;currency=JPY", "K", "ca", null, "T", "ja-JP-u-cu-jpy", "ja_JP@currency=jpy" }, { "U", "ja_JP@calendar=japanese;currency=JPY", "E", "u", "attr1-ca-gregory", "T", "ja-JP-u-attr1-ca-gregory", "ja_JP@attribute=attr1;calendar=gregorian" }, { "U", "en@colnumeric=yes", "K", "kn", "", "T", "en-u-kn-true", "en@colnumeric=yes" }, { "L", "th", "R", "th", "K", "nu", "thai", "T", "th-TH-u-nu-thai", "th_TH@numbers=thai" }, { "U", "zh_Hans", "R", "sg", "K", "ca", "badcalendar", "X" }, { "U", "zh_Hans", "R", "sg", "K", "cal", "gregory", "X" }, { "E", "z", "ExtZ", "L", "en", "T", "en-z-extz", "en@z=extz" }, { "E", "z", "ExtZ", "L", "en", "E", "z", "", "T", "en", "en" }, { "E", "z", "ExtZ", "L", "en", "E", "z", null, "T", "en", "en" }, { "E", "a", "x", "X" }, { "E", "a", "abc_def", "T", "und-a-abc-def", "@a=abc-def" }, // "yes" to "true", but it will break roundtrip conversion if BCP 47 u extension has "00-yes".
    { "L", "en", "E", "u", "bbb-aaa-00", "T", "en-u-aaa-bbb-00-yes", "en@00=yes;attribute=aaa-bbb" }, { "L", "fr", "R", "FR", "P", "Yoshito-ICU", "T", "fr-FR-x-yoshito-icu", "fr_FR@x=yoshito-icu" }, { "L", "ja", "R", "jp", "K", "ca", "japanese", "T", "ja-JP-u-ca-japanese", "ja_JP@calendar=japanese" }, { "K", "co", "PHONEBK", "K", "ca", "gregory", "L", "De", "T", "de-u-ca-gregory-co-phonebk", "de@calendar=gregorian;collation=phonebook" }, { "E", "o", "OPQR", "E", "a", "aBcD", "T", "und-a-abcd-o-opqr", "@a=abcd;o=opqr" }, { "E", "u", "nu-thai-ca-gregory", "L", "TH", "T", "th-u-ca-gregory-nu-thai", "th@calendar=gregorian;numbers=thai" }, { "L", "en", "K", "tz", "usnyc", "R", "US", "T", "en-US-u-tz-usnyc", "en_US@timezone=America/New_York" }, { "L", "de", "K", "co", "phonebk", "K", "ks", "level1", "K", "kk", "true", "T", "de-u-co-phonebk-kk-true-ks-level1", "de@collation=phonebook;colnormalization=yes;colstrength=primary" }, { "L", "en", "R", "US", "K", "ca", "gregory", "T", "en-US-u-ca-gregory", "en_US@calendar=gregorian" }, { "L", "en", "R", "US", "K", "cal", "gregory", "X" }, { "L", "en", "R", "US", "K", "ca", "gregorian", "X" }, { "L", "en", "R", "US", "K", "kn", "", "T", "en-US-u-kn-true", "en_US@colnumeric=yes" }, { "B", "de-DE-u-co-phonebk", "C", "L", "pt", "T", "pt", "pt" }, { "B", "ja-jp-u-ca-japanese", "N", "T", "ja-JP", "ja_JP" }, { "B", "es-u-def-abc-co-trad", "A", "hij", "D", "def", "T", "es-u-abc-hij-co-trad", "es@attribute=abc-hij;collation=traditional" }, { "B", "es-u-def-abc-co-trad", "A", "hij", "D", "def", "D", "def", "T", "es-u-abc-hij-co-trad", "es@attribute=abc-hij;collation=traditional" }, { "L", "en", "A", "aa", "X" }, { "B", "fr-u-attr1-cu-eur", "D", "attribute1", "X" } };
    Builder bld_st = new Builder();
    for (int tidx = 0; tidx < TESTCASE.length; tidx++) {
        int i = 0;
        String[] expected = null;
        Builder bld = bld_st;
        bld.clear();
        while (true) {
            String method = TESTCASE[tidx][i++];
            try {
                // setters
                if (method.equals("L")) {
                    bld.setLanguage(TESTCASE[tidx][i++]);
                } else if (method.equals("S")) {
                    bld.setScript(TESTCASE[tidx][i++]);
                } else if (method.equals("R")) {
                    bld.setRegion(TESTCASE[tidx][i++]);
                } else if (method.equals("V")) {
                    bld.setVariant(TESTCASE[tidx][i++]);
                } else if (method.equals("K")) {
                    String key = TESTCASE[tidx][i++];
                    String type = TESTCASE[tidx][i++];
                    bld.setUnicodeLocaleKeyword(key, type);
                } else if (method.equals("A")) {
                    bld.addUnicodeLocaleAttribute(TESTCASE[tidx][i++]);
                } else if (method.equals("E")) {
                    String key = TESTCASE[tidx][i++];
                    String value = TESTCASE[tidx][i++];
                    bld.setExtension(key.charAt(0), value);
                } else if (method.equals("P")) {
                    bld.setExtension(ULocale.PRIVATE_USE_EXTENSION, TESTCASE[tidx][i++]);
                } else if (method.equals("U")) {
                    bld.setLocale(new ULocale(TESTCASE[tidx][i++]));
                } else if (method.equals("B")) {
                    bld.setLanguageTag(TESTCASE[tidx][i++]);
                } else // clear / remove
                if (method.equals("C")) {
                    bld.clear();
                } else if (method.equals("N")) {
                    bld.clearExtensions();
                } else if (method.equals("D")) {
                    bld.removeUnicodeLocaleAttribute(TESTCASE[tidx][i++]);
                } else // result
                if (method.equals("X")) {
                    errln("FAIL: No excetion was thrown - test csae: " + Arrays.toString(TESTCASE[tidx]));
                } else if (method.equals("T")) {
                    expected = new String[2];
                    expected[0] = TESTCASE[tidx][i];
                    expected[1] = TESTCASE[tidx][i + 1];
                    break;
                } else {
                    // Unknow test method
                    errln("Unknown test case method: There is an error in the test case data.");
                }
            } catch (IllformedLocaleException e) {
                if (TESTCASE[tidx][i].equals("X")) {
                    // This exception is expected
                    break;
                } else {
                    errln("FAIL: IllformedLocaleException at offset " + i + " in test case: " + Arrays.toString(TESTCASE[tidx]));
                }
            }
        }
        if (expected != null) {
            ULocale loc = bld.build();
            if (!expected[1].equals(loc.toString())) {
                errln("FAIL: Wrong locale ID - " + loc + " for test case: " + Arrays.toString(TESTCASE[tidx]));
            }
            String langtag = loc.toLanguageTag();
            if (!expected[0].equals(langtag)) {
                errln("FAIL: Wrong language tag - " + langtag + " for test case: " + Arrays.toString(TESTCASE[tidx]));
            }
            ULocale loc1 = ULocale.forLanguageTag(langtag);
            if (!loc.equals(loc1)) {
                errln("FAIL: Language tag round trip failed for " + loc);
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Builder(android.icu.util.ULocale.Builder) IllformedLocaleException(android.icu.util.IllformedLocaleException) Test(org.junit.Test)

Example 2 with IllformedLocaleException

use of android.icu.util.IllformedLocaleException in project j2objc by google.

the class ULocaleTest method TestForLanguageTag.

@Test
public void TestForLanguageTag() {
    final Integer NOERROR = Integer.valueOf(-1);
    final Object[][] langtag_to_locale = { { "en", "en", NOERROR }, { "en-us", "en_US", NOERROR }, { "und-us", "_US", NOERROR }, { "und-latn", "_Latn", NOERROR }, { "en-us-posix", "en_US_POSIX", NOERROR }, { "de-de_euro", "de", Integer.valueOf(3) }, { "kok-in", "kok_IN", NOERROR }, { "123", "", Integer.valueOf(0) }, { "en_us", "", Integer.valueOf(0) }, { "en-latn-x", "en_Latn", Integer.valueOf(8) }, { "art-lojban", "jbo", NOERROR }, { "zh-hakka", "hak", NOERROR }, { "zh-cmn-CH", "cmn_CH", NOERROR }, { "xxx-yy", "xxx_YY", NOERROR }, { "fr-234", "fr_234", NOERROR }, { "i-default", "en@x=i-default", NOERROR }, { "i-test", "", Integer.valueOf(0) }, { "ja-jp-jp", "ja_JP", Integer.valueOf(6) }, { "bogus", "bogus", NOERROR }, { "boguslang", "", Integer.valueOf(0) }, { "EN-lATN-us", "en_Latn_US", NOERROR }, { "und-variant-1234", "__VARIANT_1234", NOERROR }, { "und-varzero-var1-vartwo", "__VARZERO", Integer.valueOf(12) }, { "en-u-ca-gregory", "en@calendar=gregorian", NOERROR }, { "en-U-cu-USD", "en@currency=usd", NOERROR }, { "en-us-u-va-posix", "en_US_POSIX", NOERROR }, { "en-us-u-ca-gregory-va-posix", "en_US_POSIX@calendar=gregorian", NOERROR }, { "en-us-posix-u-va-posix", "en_US_POSIX@va=posix", NOERROR }, { "en-us-u-va-posix2", "en_US@va=posix2", NOERROR }, { "en-us-vari1-u-va-posix", "en_US_VARI1@va=posix", NOERROR }, { "ar-x-1-2-3", "ar@x=1-2-3", NOERROR }, { "fr-u-nu-latn-cu-eur", "fr@currency=eur;numbers=latn", NOERROR }, { "de-k-kext-u-co-phonebk-nu-latn", "de@collation=phonebook;k=kext;numbers=latn", NOERROR }, { "ja-u-cu-jpy-ca-jp", "ja@calendar=yes;currency=jpy;jp=yes", NOERROR }, { "en-us-u-tz-usnyc", "en_US@timezone=America/New_York", NOERROR }, { "und-a-abc-def", "@a=abc-def", NOERROR }, { "zh-u-ca-chinese-x-u-ca-chinese", "zh@calendar=chinese;x=u-ca-chinese", NOERROR }, { "fr--FR", "fr", Integer.valueOf(3) }, { "fr-", "fr", Integer.valueOf(3) }, { "x-elmer", "@x=elmer", NOERROR }, { "en-US-u-attr1-attr2-ca-gregory", "en_US@attribute=attr1-attr2;calendar=gregorian", NOERROR }, { "sr-u-kn", "sr@colnumeric=yes", NOERROR }, { "de-u-kn-co-phonebk", "de@collation=phonebook;colnumeric=yes", NOERROR }, { "en-u-attr2-attr1-kn-kb", "en@attribute=attr1-attr2;colbackwards=yes;colnumeric=yes", NOERROR }, { "ja-u-ijkl-efgh-abcd-ca-japanese-xx-yyy-zzz-kn", "ja@attribute=abcd-efgh-ijkl;calendar=japanese;colnumeric=yes;xx=yyy-zzz", NOERROR }, { "de-u-xc-xphonebk-co-phonebk-ca-buddhist-mo-very-lo-extensi-xd-that-de-should-vc-probably-xz-killthebuffer", "de@calendar=buddhist;collation=phonebook;de=should;lo=extensi;mo=very;vc=probably;xc=xphonebk;xd=that;xz=yes", Integer.valueOf(92) }, /* #12761 */
    { "en-a-bar-u-baz", "en@a=bar;attribute=baz", NOERROR }, { "en-a-bar-u-baz-x-u-foo", "en@a=bar;attribute=baz;x=u-foo", NOERROR }, { "en-u-baz", "en@attribute=baz", NOERROR }, { "en-u-baz-ca-islamic-civil", "en@attribute=baz;calendar=islamic-civil", NOERROR }, { "en-a-bar-u-ca-islamic-civil-x-u-foo", "en@a=bar;calendar=islamic-civil;x=u-foo", NOERROR }, { "en-a-bar-u-baz-ca-islamic-civil-x-u-foo", "en@a=bar;attribute=baz;calendar=islamic-civil;x=u-foo", NOERROR } };
    for (int i = 0; i < langtag_to_locale.length; i++) {
        String tag = (String) langtag_to_locale[i][0];
        ULocale expected = new ULocale((String) langtag_to_locale[i][1]);
        ULocale loc = ULocale.forLanguageTag(tag);
        if (!loc.equals(expected)) {
            errln("FAIL: forLanguageTag returned locale [" + loc + "] for language tag [" + tag + "] - expected: [" + expected + "]");
        }
    }
    // Use locale builder to check errors
    for (int i = 0; i < langtag_to_locale.length; i++) {
        String tag = (String) langtag_to_locale[i][0];
        ULocale expected = new ULocale((String) langtag_to_locale[i][1]);
        int errorIdx = ((Integer) langtag_to_locale[i][2]).intValue();
        try {
            Builder bld = new Builder();
            bld.setLanguageTag(tag);
            ULocale loc = bld.build();
            if (!loc.equals(expected)) {
                errln("FAIL: forLanguageTag returned locale [" + loc + "] for language tag [" + tag + "] - expected: [" + expected + "]");
            }
            if (errorIdx != NOERROR.intValue()) {
                errln("FAIL: Builder.setLanguageTag should throw an exception for input tag [" + tag + "]");
            }
        } catch (IllformedLocaleException ifle) {
            if (ifle.getErrorIndex() != errorIdx) {
                errln("FAIL: Builder.setLanguageTag returned error index " + ifle.getErrorIndex() + " for input language tag [" + tag + "] expected: " + errorIdx);
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Builder(android.icu.util.ULocale.Builder) IllformedLocaleException(android.icu.util.IllformedLocaleException) Test(org.junit.Test)

Example 3 with IllformedLocaleException

use of android.icu.util.IllformedLocaleException in project j2objc by google.

the class ULocaleCollationTest method TestIllformedLocale.

@Test
public void TestIllformedLocale() {
    ULocale french = ULocale.FRENCH;
    Collator collator = Collator.getInstance(french);
    LocaleDisplayNames names = LocaleDisplayNames.getInstance(french, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
    for (String malformed : Arrays.asList("en-a", "$", "ü--a", "en--US")) {
        try {
            Set<ULocale> supported = Collections.singleton(new ULocale(malformed));
            names.getUiList(supported, false, collator);
            assertNull("Failed to detect bogus locale «" + malformed + "»", supported);
        } catch (IllformedLocaleException e) {
            logln("Successfully detected ill-formed locale «" + malformed + "»:" + e.getMessage());
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) IllformedLocaleException(android.icu.util.IllformedLocaleException) Collator(android.icu.text.Collator) LocaleDisplayNames(android.icu.text.LocaleDisplayNames) Test(org.junit.Test)

Example 4 with IllformedLocaleException

use of android.icu.util.IllformedLocaleException in project j2objc by google.

the class LocaleBuilderTest method TestSetLocale.

@Test
public void TestSetLocale() {
    ULocale loc = new ULocale("th_TH@calendar=gregorian");
    Builder bld = new Builder();
    try {
        bld.setLocale(loc);
        ULocale loc1 = bld.build();
        if (!loc.equals(loc1)) {
            errln("FAIL: Locale loc1 " + loc1 + " was returned by the builder.  Expected " + loc);
        }
        bld.setLanguage("").setUnicodeLocaleKeyword("ca", "buddhist").setLanguage("TH").setUnicodeLocaleKeyword("ca", "gregory");
        ULocale loc2 = bld.build();
        if (!loc.equals(loc2)) {
            errln("FAIL: Locale loc2 " + loc2 + " was returned by the builder.  Expected " + loc);
        }
    } catch (IllformedLocaleException e) {
        errln("FAIL: IllformedLocaleException: " + e.getMessage());
    }
}
Also used : ULocale(android.icu.util.ULocale) Builder(android.icu.util.ULocale.Builder) IllformedLocaleException(android.icu.util.IllformedLocaleException) Test(org.junit.Test)

Example 5 with IllformedLocaleException

use of android.icu.util.IllformedLocaleException in project j2objc by google.

the class CollationTest method setLocaleCollator.

private void setLocaleCollator() {
    coll = null;
    ULocale locale = null;
    if (fileLine.length() > 9) {
        // "@ locale <langTag>"
        String localeID = fileLine.substring(9);
        try {
            // either locale ID or language tag
            locale = new ULocale(localeID);
        } catch (IllformedLocaleException e) {
            locale = null;
        }
    }
    if (locale == null) {
        logln(fileLine);
        errln("invalid language tag on line " + fileLineNumber);
        return;
    }
    logln("creating a collator for locale ID " + locale.getName());
    try {
        coll = Collator.getInstance(locale);
    } catch (Exception e) {
        errln("unable to create a collator for locale " + locale + " on line " + fileLineNumber + " - " + e);
    }
}
Also used : ULocale(android.icu.util.ULocale) IllformedLocaleException(android.icu.util.IllformedLocaleException) ParseException(java.text.ParseException) IOException(java.io.IOException) IllformedLocaleException(android.icu.util.IllformedLocaleException)

Aggregations

IllformedLocaleException (android.icu.util.IllformedLocaleException)5 ULocale (android.icu.util.ULocale)5 Test (org.junit.Test)4 Builder (android.icu.util.ULocale.Builder)3 Collator (android.icu.text.Collator)1 LocaleDisplayNames (android.icu.text.LocaleDisplayNames)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1