Search in sources :

Example 6 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testCreateLocaleCanonicalValid.

/**
     * Ensure that Builder builds locales that have the expected
     * tag and java6 ID.  Note the odd cases for the ID.
     */
public void testCreateLocaleCanonicalValid() {
    String[] valids = { "en-Latn-US-NewYork", "en_US_NewYork_#Latn", "en-Latn-US", "en_US_#Latn", // double underscore
    "en-Latn-NewYork", // double underscore
    "en__NewYork_#Latn", // double underscore
    "en-Latn", // double underscore
    "en__#Latn", "en-US-NewYork", "en_US_NewYork", "en-US", "en_US", // double underscore
    "en-NewYork", // double underscore
    "en__NewYork", "en", "en", "und-Latn-US-NewYork", "_US_NewYork_#Latn", "und-Latn-US", "_US_#Latn", // variant only not supported
    "und-Latn-NewYork", // variant only not supported
    "", "und-Latn", "", "und-US-NewYork", "_US_NewYork", "und-US", "_US", // variant only not supported
    "und-NewYork", // variant only not supported
    "", "und", "" };
    Builder builder = new Builder();
    for (int i = 0; i < valids.length; i += 2) {
        String tag = valids[i];
        String id = valids[i + 1];
        String idl = (i & 16) == 0 ? l : "";
        String ids = (i & 8) == 0 ? s : "";
        String idc = (i & 4) == 0 ? c : "";
        String idv = (i & 2) == 0 ? v : "";
        String msg = String.valueOf(i / 2) + ": '" + tag + "' ";
        try {
            Locale l = builder.setLanguage(idl).setScript(ids).setRegion(idc).setVariant(idv).build();
            assertEquals(msg + "language", idl, l.getLanguage());
            assertEquals(msg + "script", ids, l.getScript());
            assertEquals(msg + "country", idc, l.getCountry());
            assertEquals(msg + "variant", idv, l.getVariant());
            assertEquals(msg + "tag", tag, l.toLanguageTag());
            assertEquals(msg + "id", id, l.toString());
        } catch (IllegalArgumentException e) {
            errln(msg + e.getMessage());
        }
    }
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 7 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testBuilderClear.

public void testBuilderClear() {
    String monster = "en-latn-US-NewYork-a-bb-cc-u-co-japanese-x-z-y-x-x";
    Builder builder = new Builder();
    Locale locale = Locale.forLanguageTag(monster);
    String result = builder.setLocale(locale).clear().build().toLanguageTag();
    assertEquals("clear", "und", result);
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 8 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testGetExtension.

public void testGetExtension() {
    // forLanguageTag does NOT normalize to hyphen
    Locale locale = Locale.forLanguageTag("und-a-some_ex-tension");
    assertEquals("some_ex-tension", null, locale.getExtension('a'));
    // regular extension
    locale = new Builder().setExtension('a', "some-ex-tension").build();
    assertEquals("builder", "some-ex-tension", locale.getExtension('a'));
    // returns null if extension is not present
    assertEquals("empty b", null, locale.getExtension('b'));
    // throws exception if extension tag is illegal
    new ExpectIAE() {

        public void call() {
            Locale.forLanguageTag("").getExtension('�');
        }
    };
    // 'x' is not an extension, it's a private use tag, but it's accessed through this API
    locale = Locale.forLanguageTag("x-y-z-blork");
    assertEquals("x", "y-z-blork", locale.getExtension('x'));
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 9 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testBuilderSetRegion.

public void testBuilderSetRegion() {
    // region is normalized to upper case
    String source = "uS";
    String target = "US";
    String defaulted = "";
    Builder builder = new Builder();
    String result = builder.setRegion(source).build().getCountry();
    assertEquals("us", target, result);
    // setting with empty resets
    result = builder.setRegion(target).setRegion("").build().getCountry();
    assertEquals("empty", defaulted, result);
    // setting with null also resets
    result = builder.setRegion(target).setRegion(null).build().getCountry();
    assertEquals("null", defaulted, result);
    // ill-formed region codes throw IAE
    // 2 alpha or 3 numeric
    new BuilderILE("q", "abc", "12", "1234", "a3", "12a") {

        public void call() {
            b.setRegion(arg);
        }
    };
    // region code validation is NOT performed, any 2-alpha or 3-digit passes
    assertEquals("2alpha", "ZZ", builder.setRegion("ZZ").build().getCountry());
    assertEquals("3digit", "000", builder.setRegion("000").build().getCountry());
}
Also used : Builder(java.util.Locale.Builder)

Example 10 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testBuilderSetScript.

public void testBuilderSetScript() {
    // script is normalized to title case
    String source = "lAtN";
    String target = "Latn";
    String defaulted = "";
    Builder builder = new Builder();
    String result = builder.setScript(source).build().getScript();
    assertEquals("script", target, result);
    // setting with empty resets
    result = builder.setScript(target).setScript("").build().getScript();
    assertEquals("empty", defaulted, result);
    // settting with null also resets
    result = builder.setScript(target).setScript(null).build().getScript();
    assertEquals("null", defaulted, result);
    // ill-formed script codes throw IAE
    // must be 4alpha
    new BuilderILE("abc", "abcde", "l3tn") {

        public void call() {
            b.setScript(arg);
        }
    };
    // script code validation is NOT performed, any 4-alpha passes
    assertEquals("4alpha", "Wxyz", builder.setScript("wxyz").build().getScript());
}
Also used : Builder(java.util.Locale.Builder)

Aggregations

Builder (java.util.Locale.Builder)18 Locale (java.util.Locale)12 IllformedLocaleException (java.util.IllformedLocaleException)1