Search in sources :

Example 11 with Builder

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

the class LocaleEnhanceTest method testGetScript.

///
/// Locale API tests.
///
public void testGetScript() {
    // forLanguageTag normalizes case
    Locale locale = Locale.forLanguageTag("und-latn");
    assertEquals("forLanguageTag", "Latn", locale.getScript());
    // Builder normalizes case
    locale = new Builder().setScript("LATN").build();
    assertEquals("builder", "Latn", locale.getScript());
    // empty string is returned, not null, if there is no script
    locale = Locale.forLanguageTag("und");
    assertEquals("script is empty string", "", locale.getScript());
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 12 with Builder

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

the class LocaleEnhanceTest method testCurrentLocales.

/**
     * Ensure that all current locale ids parse.  Use DateFormat as a proxy
     * for all current locale ids.
     */
public void testCurrentLocales() {
    Locale[] locales = java.text.DateFormat.getAvailableLocales();
    Builder builder = new Builder();
    for (Locale target : locales) {
        String tag = target.toLanguageTag();
        // the tag recreates the original locale,
        // except no_NO_NY
        Locale tagResult = Locale.forLanguageTag(tag);
        if (!target.getVariant().equals("NY")) {
            assertEquals("tagResult", target, tagResult);
        }
        // the builder also recreates the original locale,
        // except ja_JP_JP, th_TH_TH and no_NO_NY
        Locale builderResult = builder.setLocale(target).build();
        if (target.getVariant().length() != 2) {
            assertEquals("builderResult", target, builderResult);
        }
    }
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 13 with Builder

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

the class LocaleEnhanceTest method testBuilderSetLanguage.

public void testBuilderSetLanguage() {
    // language is normalized to lower case
    String source = "eN";
    String target = "en";
    String defaulted = "";
    Builder builder = new Builder();
    String result = builder.setLanguage(source).build().getLanguage();
    assertEquals("en", target, result);
    // setting with empty resets
    result = builder.setLanguage(target).setLanguage("").build().getLanguage();
    assertEquals("empty", defaulted, result);
    // setting with null resets too
    result = builder.setLanguage(target).setLanguage(null).build().getLanguage();
    assertEquals("null", defaulted, result);
    // language codes must be 2-8 alpha
    // for forwards compatibility, 4-alpha and 5-8 alpha (registered)
    // languages are accepted syntax
    new BuilderILE("q", "abcdefghi", "13") {

        public void call() {
            b.setLanguage(arg);
        }
    };
    // language code validation is NOT performed, any 2-8-alpha passes
    assertNotNull("2alpha", builder.setLanguage("zz").build());
    assertNotNull("8alpha", builder.setLanguage("abcdefgh").build());
    // three-letter language codes are NOT canonicalized to two-letter
    result = builder.setLanguage("eng").build().getLanguage();
    assertEquals("eng", "eng", result);
}
Also used : Builder(java.util.Locale.Builder)

Example 14 with Builder

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

the class LocaleEnhanceTest method testBuilderSetVariant.

public void testBuilderSetVariant() {
    // Variant case is not normalized in lenient variant mode
    String source = "NewYork";
    String target = source;
    String defaulted = "";
    Builder builder = new Builder();
    String result = builder.setVariant(source).build().getVariant();
    assertEquals("NewYork", target, result);
    result = builder.setVariant("NeWeR_YoRkEr").build().toLanguageTag();
    assertEquals("newer yorker", "und-NeWeR-YoRkEr", result);
    // subtags of variant are NOT reordered
    result = builder.setVariant("zzzzz_yyyyy_xxxxx").build().getVariant();
    assertEquals("zyx", "zzzzz_yyyyy_xxxxx", result);
    // setting to empty resets
    result = builder.setVariant(target).setVariant("").build().getVariant();
    assertEquals("empty", defaulted, result);
    // setting to null also resets
    result = builder.setVariant(target).setVariant(null).build().getVariant();
    assertEquals("null", defaulted, result);
    // ill-formed variants throw IAE
    // digit followed by 3-7 characters, or alpha followed by 4-8 characters.
    new BuilderILE("abcd", "abcdefghi", "1ab", "1abcdefgh") {

        public void call() {
            b.setVariant(arg);
        }
    };
    // 4 characters is ok as long as the first is a digit
    assertEquals("digit+3alpha", "1abc", builder.setVariant("1abc").build().getVariant());
    // all subfields must conform
    new BuilderILE("abcde-fg") {

        public void call() {
            b.setVariant(arg);
        }
    };
}
Also used : Builder(java.util.Locale.Builder)

Example 15 with Builder

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

the class LocaleEnhanceTest method testCreateLocaleMultipleVariants.

/**
     * Test that locale construction works with 'multiple variants'.
     * <p>
     * The string "Newer__Yorker" is treated as three subtags,
     * "Newer", "", and "Yorker", and concatenated into one
     * subtag by omitting empty subtags and joining the remainer
     * with underscores.  So the resulting variant tag is "Newer_Yorker".
     * Note that 'New' and 'York' are invalid BCP47 variant subtags
     * because they are too short.
     */
public void testCreateLocaleMultipleVariants() {
    String[] valids = { "en-Latn-US-Newer-Yorker", "en_US_Newer_Yorker_#Latn", "en-Latn-Newer-Yorker", "en__Newer_Yorker_#Latn", "en-US-Newer-Yorker", "en_US_Newer_Yorker", "en-Newer-Yorker", "en__Newer_Yorker", "und-Latn-US-Newer-Yorker", "_US_Newer_Yorker_#Latn", "und-Latn-Newer-Yorker", "", "und-US-Newer-Yorker", "_US_Newer_Yorker", "und-Newer-Yorker", "" };
    // lenient variant
    Builder builder = new Builder();
    final String idv = "Newer_Yorker";
    for (int i = 0; i < valids.length; i += 2) {
        String tag = valids[i];
        String id = valids[i + 1];
        String idl = (i & 8) == 0 ? l : "";
        String ids = (i & 4) == 0 ? s : "";
        String idc = (i & 2) == 0 ? c : "";
        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)

Aggregations

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