/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 6875847 6992272 7002320 7015500 7023613 7032820 7033504 7004603 7044019
* @summary test API changes to Locale
* @compile LocaleEnhanceTest.java
*/
}
public LocaleEnhanceTest() {
}
///
/// Generic sanity tests
///
/** A canonical language code. */
private static final String l = "en";
/** A canonical script code.. */
private static final String s = "Latn";
/** A canonical region code. */
private static final String c = "US";
/** A canonical variant code. */
private static final String v = "NewYork";
/**
* Ensure that Builder builds locales that have the expected
* tag and java6 ID. Note the odd cases for the ID.
*/
public void testCreateLocaleCanonicalValid() {
"en-Latn-US-NewYork", "en_US_NewYork_#Latn",
"en-Latn-US", "en_US_#Latn",
"en-Latn-NewYork", "en__NewYork_#Latn", // double underscore
"en-Latn", "en__#Latn", // double underscore
"en-US-NewYork", "en_US_NewYork",
"en-US", "en_US",
"en-NewYork", "en__NewYork", // double underscore
"en", "en",
"und-Latn-US-NewYork", "_US_NewYork_#Latn",
"und-Latn-US", "_US_#Latn",
"und-Latn-NewYork", "", // variant only not supported
"und-Latn", "",
"und-US-NewYork", "_US_NewYork",
"und-US", "_US",
"und-NewYork", "", // variant only not supported
"und", ""
};
try {
.build();
}
catch (IllegalArgumentException e) {
}
}
}
/**
* 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() {
"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", "",
};
try {
.build();
}
catch (IllegalArgumentException e) {
}
}
}
/**
* Ensure that all these invalid formats are not recognized by
* forLanguageTag.
*/
public void testCreateLocaleCanonicalInvalidSeparator() {
// trailing separator
"en_Latn_US_NewYork_",
"en_Latn_US_",
"en_Latn_",
"en_",
"_",
// double separator
"en_Latn_US__NewYork",
"_Latn_US__NewYork",
"en_US__NewYork",
"_US__NewYork",
// are these OK?
// "en_Latn__US_NewYork", // variant is 'US_NewYork'
// "_Latn__US_NewYork", // variant is 'US_NewYork'
// "en__Latn_US_NewYork", // variant is 'Latn_US_NewYork'
// "en__US_NewYork", // variant is 'US_NewYork'
// double separator without language or script
"__US",
"__NewYork",
// triple separator anywhere except within variant
"en___NewYork",
"en_Latn___NewYork",
"_Latn___NewYork",
"___NewYork",
};
}
}
/**
* Ensure that all current locale ids parse. Use DateFormat as a proxy
* for all current locale ids.
*/
public void testCurrentLocales() {
// the tag recreates the original locale,
// except no_NO_NY
}
// the builder also recreates the original locale,
// except ja_JP_JP, th_TH_TH and no_NO_NY
}
}
}
/**
* Ensure that all icu locale ids parse.
*/
new InputStreamReader(
"UTF-8"));
}
}
///
/// Compatibility tests
///
public void testConstructor() {
// all the old weirdness still holds, no new weirdness
// language to lower case, region to upper, variant unchanged
// short
{ "X", "y", "z", "x", "Y" },
// long
{ "xXxXxXxXxXxX", "yYyYyYyYyYyYyYyY", "zZzZzZzZzZzZzZzZ",
"xxxxxxxxxxxx", "YYYYYYYYYYYYYYYY" },
// mapped language ids
{ "he", "IW", "", "iw" },
{ "iw", "IW", "", "iw" },
{ "yi", "DE", "", "ji" },
{ "ji", "DE", "", "ji" },
{ "id", "ID", "", "in" },
{ "in", "ID", "", "in" },
// special variants
{ "ja", "JP", "JP" },
{ "th", "TH", "TH" },
{ "no", "NO", "NY" },
{ "no", "NO", "NY" },
// no canonicalization of 3-letter language codes
{ "eng", "US", "" }
};
}
}
///
/// Locale API tests.
///
public void testGetScript() {
// forLanguageTag normalizes case
// Builder normalizes case
// empty string is returned, not null, if there is no script
}
public void testGetExtension() {
// forLanguageTag does NOT normalize to hyphen
// regular extension
// returns null if extension is not present
// throws exception if extension tag is illegal
// 'x' is not an extension, it's a private use tag, but it's accessed through this API
}
public void testGetExtensionKeys() {
// result is not mutable
try {
errln("expected exception on add to extension key set");
}
catch (UnsupportedOperationException e) {
// ok
}
// returns empty set if no extensions
}
public void testGetUnicodeLocaleAttributes() {
}
public void testGetUnicodeLocaleType() {
// Unicode locale extension key is case insensitive
// if keyword is not present, returns null
// if no locale extension is set, returns null
// typeless keyword
// invalid keys throw exception
new ExpectIAE() { public void call() { Locale.forLanguageTag("").getUnicodeLocaleType("abcdefghi"); }};
// null argument throws exception
}
public void testGetUnicodeLocaleKeys() {
// result is not modifiable
try {
errln("expected exception when add to locale key set");
}
catch (UnsupportedOperationException e) {
// ok
}
}
public void testPrivateUseExtension() {
}
public void testToLanguageTag() {
// lots of normalization to test here
// test locales created using the constructor
// empty locale canonicalizes to 'und'
{ "", "", "", "und" },
// variant alone is not a valid Locale, but has a valid language tag
{ "", "", "NewYork", "und-NewYork" },
// standard valid locales
{ "", "Us", "", "und-US" },
{ "", "US", "NewYork", "und-US-NewYork" },
{ "EN", "", "", "en" },
{ "EN", "", "NewYork", "en-NewYork" },
{ "EN", "US", "", "en-US" },
{ "EN", "US", "NewYork", "en-US-NewYork" },
// underscore in variant will be emitted as multiple variant subtags
{ "en", "US", "Newer_Yorker", "en-US-Newer-Yorker" },
// invalid variant subtags are appended as private use
{ "en", "US", "new_yorker", "en-US-x-lvariant-new-yorker" },
// the first invalid variant subtags and following variant subtags are appended as private use
{ "en", "US", "Windows_XP_Home", "en-US-Windows-x-lvariant-XP-Home" },
// too long variant and following variant subtags disappear
{ "en", "US", "WindowsVista_SP2", "en-US" },
// invalid region subtag disappears
{ "en", "USA", "", "en" },
// invalid language tag disappears
{ "e", "US", "", "und-US" },
// three-letter language tags are not canonicalized
{ "Eng", "", "", "eng" },
// legacy languages canonicalize to modern equivalents
{ "he", "IW", "", "he-IW" },
{ "iw", "IW", "", "he-IW" },
{ "yi", "DE", "", "yi-DE" },
{ "ji", "DE", "", "yi-DE" },
{ "id", "ID", "", "id-ID" },
{ "in", "ID", "", "id-ID" },
// special values are converted on output
{ "ja", "JP", "JP", "ja-JP-u-ca-japanese-x-lvariant-JP" },
{ "th", "TH", "TH", "th-TH-u-nu-thai-x-lvariant-TH" },
{ "no", "NO", "NY", "nn-NO" }
};
}
// test locales created from forLanguageTag
// case is normalized during the round trip
{ "EN-us", "en-US" },
{ "en-Latn-US", "en-Latn-US" },
// reordering Unicode locale extensions
{ "de-u-co-phonebk-ca-gregory", "de-u-ca-gregory-co-phonebk" },
// private use only language tag is preserved (no extra "und")
{ "x-elmer", "x-elmer" },
{ "x-lvariant-JP", "x-lvariant-JP" },
};
}
}
public void testForLanguageTag() {
// forLanguageTag implements the 'Language-Tag' production of
// BCP47, so it handles private use and grandfathered tags,
// unlike locale builder. Tags listed below (except for the
// sample private use tags) come from 4646bis Feb 29, 2009.
// private use tags only
{ "x-abc", "x-abc" },
{ "x-a-b-c", "x-a-b-c" },
{ "x-a-12345678", "x-a-12345678" },
// grandfathered tags with preferred mappings
{ "i-ami", "ami" },
{ "i-bnn", "bnn" },
{ "i-hak", "hak" },
{ "i-klingon", "tlh" },
{ "i-lux", "lb" }, // two-letter tag
{ "i-navajo", "nv" }, // two-letter tag
{ "i-pwn", "pwn" },
{ "i-tao", "tao" },
{ "i-tay", "tay" },
{ "i-tsu", "tsu" },
{ "art-lojban", "jbo" },
{ "no-bok", "nb" },
{ "no-nyn", "nn" },
{ "sgn-BE-FR", "sfb" },
{ "sgn-BE-NL", "vgt" },
{ "sgn-CH-DE", "sgg" },
{ "zh-guoyu", "cmn" },
{ "zh-hakka", "hak" },
{ "zh-min-nan", "nan" },
{ "zh-xiang", "hsn" },
// grandfathered irregular tags, no preferred mappings, drop illegal fields
// from end. If no subtag is mappable, fallback to 'und'
{ "i-default", "en-x-i-default" },
{ "i-enochian", "x-i-enochian" },
{ "i-mingo", "see-x-i-mingo" },
{ "en-GB-oed", "en-GB-x-oed" },
{ "zh-min", "nan-x-zh-min" },
{ "cel-gaulish", "xtg-x-cel-gaulish" },
};
}
// forLanguageTag ignores everything past the first place it encounters
// a syntax error
{ "valid",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-x-y-12345678-z",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-x-y-12345678-z" },
{ "segment of private use tag too long",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-x-y-123456789-z",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-x-y" },
{ "segment of private use tag is empty",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-x-y--12345678-z",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-x-y" },
{ "first segment of private use tag is empty",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-x--y-12345678-z",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def" },
{ "illegal extension tag",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def-\uD800-y-12345678-z",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-def" },
{ "locale subtag with no value",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-x-y-12345678-z",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-bb-x-y-12345678-z" },
{ "locale key subtag invalid",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc-123456789-def-x-y-12345678-z",
"en-US-Newer-Yorker-a-bb-cc-dd-u-aa-abc" },
// locale key subtag invalid in earlier position, all following subtags
// dropped (and so the locale extension dropped as well)
{ "locale key subtag invalid in earlier position",
"en-US-Newer-Yorker-a-bb-cc-dd-u-123456789-abc-bb-def-x-y-12345678-z",
"en-US-Newer-Yorker-a-bb-cc-dd" },
};
try {
}
catch (IllegalArgumentException e) {
}
}
// duplicated extension are just ignored
assertEquals("Unicode extension", "ca-gregory", locale.getExtension(Locale.UNICODE_LOCALE_EXTENSION));
// redundant Unicode locale keys in an extension are ignored
assertEquals("Unicode keywords", "aa-000-bb-001-cc-003", locale.getExtension(Locale.UNICODE_LOCALE_EXTENSION));
assertEquals("Duplicated Unicode locake key followed by an extension", "1234", locale.getExtension('c'));
}
public void testGetDisplayScript() {
}
public void testGetDisplayScriptWithLocale() {
assertEquals("hans DE", "Vereinfachte Chinesische Schrift", hansLocale.getDisplayScript(Locale.GERMANY));
}
public void testGetDisplayName() {
final Locale[] testLocales = {
new Locale("en"),
};
final String[] displayNameEnglish = {
"",
"English",
"English (United States)",
"United States",
"Norwegian (Norway,Nynorsk)",
"Nynorsk",
"Chinese (Simplified Han)",
"Chinese (Traditional Han)",
"Chinese (Simplified Han,China)",
"Simplified Han",
};
final String[] displayNameSimplifiedChinese = {
"",
"\u82f1\u6587",
"\u82f1\u6587 (\u7f8e\u56fd)",
"\u7f8e\u56fd",
"\u632a\u5a01\u6587 (\u632a\u5a01,Nynorsk)",
"Nynorsk",
"\u4e2d\u6587 (\u7b80\u4f53\u4e2d\u6587)",
"\u4e2d\u6587 (\u7e41\u4f53\u4e2d\u6587)",
"\u4e2d\u6587 (\u7b80\u4f53\u4e2d\u6587,\u4e2d\u56fd)",
"\u7b80\u4f53\u4e2d\u6587",
};
}
}
///
/// Builder tests
///
public void testBuilderSetLocale() {
.build();
// null is illegal
new BuilderNPE("locale") {
};
// builder canonicalizes the three legacy locales:
// ja_JP_JP, th_TH_TH, no_NY_NO.
// non-canonical, non-legacy locales are invalid
new BuilderILE("123_4567_89") {
public void call() {
}
};
}
public void testBuilderSetLanguageTag() {
.build()
.toLanguageTag();
// redundant extensions cause a failure
// redundant Unicode locale extension keys within an Unicode locale extension cause a failure
}
public void testBuilderSetLanguage() {
// language is normalized to lower case
.build()
.getLanguage();
// setting with empty resets
.setLanguage("")
.build()
.getLanguage();
// setting with null resets too
.build()
.getLanguage();
// language codes must be 2-8 alpha
// for forwards compatibility, 4-alpha and 5-8 alpha (registered)
// languages are accepted syntax
// language code validation is NOT performed, any 2-8-alpha passes
// three-letter language codes are NOT canonicalized to two-letter
.setLanguage("eng")
.build()
.getLanguage();
}
public void testBuilderSetScript() {
// script is normalized to title case
.build()
.getScript();
// setting with empty resets
.setScript("")
.build()
.getScript();
// settting with null also resets
.build()
.getScript();
// ill-formed script codes throw IAE
// must be 4alpha
// script code validation is NOT performed, any 4-alpha passes
}
public void testBuilderSetRegion() {
// region is normalized to upper case
.build()
.getCountry();
// setting with empty resets
.setRegion("")
.build()
.getCountry();
// setting with null also resets
.build()
.getCountry();
// ill-formed region codes throw IAE
// 2 alpha or 3 numeric
// region code validation is NOT performed, any 2-alpha or 3-digit passes
}
public void testBuilderSetVariant() {
// Variant case is not normalized in lenient variant mode
.build()
.getVariant();
.setVariant("NeWeR_YoRkEr")
.build()
.toLanguageTag();
// subtags of variant are NOT reordered
.setVariant("zzzzz_yyyyy_xxxxx")
.build()
.getVariant();
// setting to empty resets
.setVariant("")
.build()
.getVariant();
// setting to null also resets
.build()
.getVariant();
// 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
// all subfields must conform
}
public void testBuilderSetExtension() {
// upper case characters are normalized to lower case
final char sourceKey = 'a';
.build()
// setting with empty resets
.build()
// setting with null also resets
.build()
// ill-formed extension keys throw IAE
// must be in [0-9a-ZA-Z]
// each segment of value must be 2-8 alphanum
// no multiple hyphens.
// locale extension key has special handling
.build();
// locale extension has same behavior with set locale keyword
.build();
// setting locale extension overrides all previous calls to setLocaleKeyword
.build();
// setting locale keyword extends values already set by the locale extension
.build();
// locale extension subtags are reordered
.clear()
.build()
.toLanguageTag();
// multiple keyword types
.clear()
.build()
.getUnicodeLocaleType("nu");
// redundant locale extensions are ignored
.clear()
.build()
.toLanguageTag();
}
public void testBuilderAddUnicodeLocaleAttribute() {
.addUnicodeLocaleAttribute("def")
.addUnicodeLocaleAttribute("abc")
.build();
// remove attribute
.build();
// add duplicate
.build();
// null attribute throws NPE
// illformed attribute throws IllformedLocaleException
}
public void testBuildersetUnicodeLocaleKeyword() {
// Note: most behavior is tested in testBuilderSetExtension
.build();
// can clear a keyword by setting to null, others remain
.build()
.toLanguageTag();
// locale keyword extension goes when all keywords are gone
.build()
.toLanguageTag();
// locale keywords are ordered independent of order of addition
.build()
.toLanguageTag();
// null keyword throws NPE
// well-formed keywords are two alphanum
// well-formed values are 3-8 alphanum
}
public void testBuilderPrivateUseExtension() {
// normalizes hyphens to underscore, case to lower
.build()
// multiple hyphens are ill-formed
new BuilderILE("a--b") { public void call() { b.setExtension(Locale.PRIVATE_USE_EXTENSION, arg); }};
}
public void testBuilderClear() {
.clear()
.build()
.toLanguageTag();
}
public void testBuilderRemoveUnicodeAttribute() {
// tested in testBuilderAddUnicodeAttribute
}
public void testBuilderBuild() {
// tested in other test methods
}
public void testSerialize() {
final Locale[] testLocales = {
new Locale("en"),
};
try {
// write
// read
} catch (Exception e) {
}
}
}
public void testDeserialize6() {
if (dataDirName == null) {
try {
} catch (URISyntaxException urie) {
}
}
} else {
}
errln("Could not locate the serialized test case data location");
return;
}
if (testfile.isDirectory()) {
continue;
}
continue;
}
} else {
}
// deserialize
{
} catch (Exception e) {
}
}
}
public void testBug7002320() {
// forLanguageTag() and Builder.setLanguageTag(String)
// should add a location extension for following two cases.
//
// is exactly "JP" and no BCP 47 extensions are available, then add
// a Unicode locale extension "ca-japanese".
// "TH" and no BCP 47 extensions are available, then add a Unicode locale
// extension "nu-thai".
//
{"ja-JP-x-lvariant-JP", "ja-JP-u-ca-japanese-x-lvariant-JP"}, // special case 1
{"ja-JP-x-lvariant-JP-XXX"},
{"ja-JP-u-ca-japanese-x-lvariant-JP"},
{"ja-JP-u-ca-gregory-x-lvariant-JP"},
{"ja-JP-u-cu-jpy-x-lvariant-JP"},
{"ja-x-lvariant-JP"},
{"th-TH-x-lvariant-TH", "th-TH-u-nu-thai-x-lvariant-TH"}, // special case 2
{"th-TH-u-nu-thai-x-lvariant-TH"},
{"en-US-x-lvariant-JP"},
};
// forLanguageTag
// setLanguageTag
}
}
public void testBug7023613() {
{"en-Latn", "en__#Latn"},
{"en-u-ca-japanese", "en__#u-ca-japanese"},
};
assertEquals("Empty country field with non-empty script/extension with input: " + in, expected, out);
}
}
/*
* 7033504: (lc) incompatible behavior change for ja_JP_JP and th_TH_TH locales
*/
public void testBug7033504() {
"java.util.JapaneseImperialCalendar");
}
}
}
///
/// utility asserts
///
if (!v) {
}
}
if (v) {
}
}
if (e != null) {
e = "'" + e + "'";
}
if (v != null) {
v = "'" + v + "'";
}
}
}
if (e != null) {
e = "'" + e + "'";
}
}
}
if (o != null) {
}
}
if (o == null) {
}
}
// not currently used, might get rid of exceptions from the API
private abstract class ExceptionTest {
this.exceptionClass = exceptionClass;
}
public void run() {
try {
call();
}
catch (Exception e) {
}
}
}
}
return null;
}
public abstract void call();
}
ExpectNPE() {
super(NullPointerException.class);
run();
}
}
super(NullPointerException.class);
run();
}
return msg;
}
}
ExpectIAE() {
super(IllegalArgumentException.class);
run();
}
}
super(IllformedLocaleException.class);
run();
}
public void run() {
super.run();
}
}
}
}
}