Search in sources :

Example 16 with Builder

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

the class LocaleEnhanceTest method testBuilderPrivateUseExtension.

public void testBuilderPrivateUseExtension() {
    // normalizes hyphens to underscore, case to lower
    String source = "c-B-a";
    String target = "c-b-a";
    Builder builder = new Builder();
    String result = builder.setExtension(Locale.PRIVATE_USE_EXTENSION, source).build().getExtension(Locale.PRIVATE_USE_EXTENSION);
    assertEquals("abc", target, result);
    // multiple hyphens are ill-formed
    new BuilderILE("a--b") {

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

Example 17 with Builder

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

the class LocaleEnhanceTest method testBuilderAddUnicodeLocaleAttribute.

public void testBuilderAddUnicodeLocaleAttribute() {
    Builder builder = new Builder();
    Locale locale = builder.addUnicodeLocaleAttribute("def").addUnicodeLocaleAttribute("abc").build();
    Set<String> uattrs = locale.getUnicodeLocaleAttributes();
    assertEquals("number of attributes", 2, uattrs.size());
    assertTrue("attribute abc", uattrs.contains("abc"));
    assertTrue("attribute def", uattrs.contains("def"));
    // remove attribute
    locale = builder.removeUnicodeLocaleAttribute("xxx").build();
    assertEquals("remove bogus", 2, uattrs.size());
    // add duplicate
    locale = builder.addUnicodeLocaleAttribute("abc").build();
    assertEquals("add duplicate", 2, uattrs.size());
    // null attribute throws NPE
    new BuilderNPE("null attribute") {

        public void call() {
            b.addUnicodeLocaleAttribute(null);
        }
    };
    // illformed attribute throws IllformedLocaleException
    new BuilderILE("invalid attribute") {

        public void call() {
            b.addUnicodeLocaleAttribute("ca");
        }
    };
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 18 with Builder

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

the class LocaleEnhanceTest method testBuilderSetLocale.

///
/// Builder tests
///
public void testBuilderSetLocale() {
    Builder builder = new Builder();
    Builder lenientBuilder = new Builder();
    String languageTag = "en-Latn-US-NewYork-a-bb-ccc-u-co-japanese-x-y-z";
    String target = "en-Latn-US-NewYork-a-bb-ccc-u-co-japanese-x-y-z";
    Locale locale = Locale.forLanguageTag(languageTag);
    Locale result = lenientBuilder.setLocale(locale).build();
    assertEquals("long tag", target, result.toLanguageTag());
    assertEquals("long tag", locale, result);
    // null is illegal
    new BuilderNPE("locale") {

        public void call() {
            b.setLocale(null);
        }
    };
    // builder canonicalizes the three legacy locales:
    // ja_JP_JP, th_TH_TH, no_NY_NO.
    locale = builder.setLocale(new Locale("ja", "JP", "JP")).build();
    assertEquals("ja_JP_JP languagetag", "ja-JP-u-ca-japanese", locale.toLanguageTag());
    assertEquals("ja_JP_JP variant", "", locale.getVariant());
    locale = builder.setLocale(new Locale("th", "TH", "TH")).build();
    assertEquals("th_TH_TH languagetag", "th-TH-u-nu-thai", locale.toLanguageTag());
    assertEquals("th_TH_TH variant", "", locale.getVariant());
    locale = builder.setLocale(new Locale("no", "NO", "NY")).build();
    assertEquals("no_NO_NY languagetag", "nn-NO", locale.toLanguageTag());
    assertEquals("no_NO_NY language", "nn", locale.getLanguage());
    assertEquals("no_NO_NY variant", "", locale.getVariant());
    // non-canonical, non-legacy locales are invalid
    new BuilderILE("123_4567_89") {

        public void call() {
            b.setLocale(new Locale("123", "4567", "89"));
        }
    };
}
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