diff --git a/src/demo/share/jfc/CodePointIM/com/sun/inputmethods/internal/codepointim/CodePointInputMethodDescriptor.java b/src/demo/share/jfc/CodePointIM/com/sun/inputmethods/internal/codepointim/CodePointInputMethodDescriptor.java index 0e91e1916da..320a1ce7e66 100644 --- a/src/demo/share/jfc/CodePointIM/com/sun/inputmethods/internal/codepointim/CodePointInputMethodDescriptor.java +++ b/src/demo/share/jfc/CodePointIM/com/sun/inputmethods/internal/codepointim/CodePointInputMethodDescriptor.java @@ -75,7 +75,7 @@ public class CodePointInputMethodDescriptor implements InputMethodDescriptor { */ public Locale[] getAvailableLocales() { Locale[] locales = { - new Locale("", "", ""), }; + Locale.ROOT, }; return locales; } diff --git a/src/java.base/share/classes/java/util/Locale.java b/src/java.base/share/classes/java/util/Locale.java index a06e5c4f4b7..0ab6fbad86f 100644 --- a/src/java.base/share/classes/java/util/Locale.java +++ b/src/java.base/share/classes/java/util/Locale.java @@ -230,9 +230,9 @@ import sun.util.locale.provider.TimeZoneNameUtility; * implementations in a Java Runtime Environment might not support any * particular Unicode locale attributes or key/type pairs. * - *
There are several different ways to create a {@code Locale} + *
There are several ways to obtain a {@code Locale} * object. * *
Using {@link Builder} you can construct a {@code Locale} object * that conforms to BCP 47 syntax. * - *
The {@code Locale} class provides three constructors: - *
- *
- * {@link #Locale(String language)}
- * {@link #Locale(String language, String country)}
- * {@link #Locale(String language, String country, String variant)}
- *
- *
- * These constructors allow you to create a {@code Locale} object
- * with language, country and variant, but you cannot specify
- * script or extensions.
- *
* The method {@link #forLanguageTag} creates a {@code Locale} - * object for a well-formed BCP 47 language tag. + *
The method {@link #forLanguageTag} obtains a {@code Locale} + * object for a well-formed BCP 47 language tag. The method + * {@link #of(String, String, String)} and its overloads obtain a + * {@code Locale} object from given {@code language}, {@code country}, + * and/or {@code variant} defined above. * *
The {@code Locale} class provides a number of convenient constants - * that you can use to create {@code Locale} objects for commonly used - * locales. For example, the following creates a {@code Locale} object - * for the United States: - *
- *+ * that you can use to obtain {@code Locale} objects for commonly used + * locales. For example, {@code Locale.US} is the {@code Locale} object + * for the United States. * *- * Locale.US - *- *
Once you've created a {@code Locale} you can query it for information + *
Once you've obtained a {@code Locale} you can query it for information * about itself. Use {@code getCountry} to get the country (or region) * code and {@code getLanguage} to get the language code. * You can use {@code getDisplayCountry} to get the @@ -387,7 +371,7 @@ import sun.util.locale.provider.TimeZoneNameUtility; * *
In order to maintain compatibility with existing usage, Locale's + *
In order to maintain compatibility, Locale's * constructors retain their behavior prior to the Java Runtime * Environment version 1.7. The same is largely true for the * {@code toString} method. Thus Locale objects can continue to @@ -741,6 +725,9 @@ public final class Locale implements Cloneable, Serializable { * see Special Cases for more information. * * + * @deprecated Locale constructors have been deprecated. See + * Obtaining a Locale for other options. + * * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag * up to 8 characters in length. See the {@code Locale} class description about * valid language values. @@ -750,6 +737,7 @@ public final class Locale implements Cloneable, Serializable { * See the {@code Locale} class description for the details. * @throws NullPointerException thrown if any argument is null. */ + @Deprecated(since="19") public Locale(String language, String country, String variant) { if (language == null || country == null || variant == null) { throw new NullPointerException(); @@ -771,6 +759,9 @@ public final class Locale implements Cloneable, Serializable { * any syntactic checks on the input. * * + * @deprecated Locale constructors have been deprecated. See + * Obtaining a Locale for other options. + * * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag * up to 8 characters in length. See the {@code Locale} class description about * valid language values. @@ -778,6 +769,7 @@ public final class Locale implements Cloneable, Serializable { * See the {@code Locale} class description about valid country values. * @throws NullPointerException thrown if either argument is null. */ + @Deprecated(since="19") public Locale(String language, String country) { this(language, country, ""); } @@ -794,16 +786,96 @@ public final class Locale implements Cloneable, Serializable { * any syntactic checks on the input. * * + * @deprecated Locale constructors have been deprecated. See + * Obtaining a Locale for other options. + * * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag * up to 8 characters in length. See the {@code Locale} class description about * valid language values. * @throws NullPointerException thrown if argument is null. * @since 1.4 */ + @Deprecated(since="19") public Locale(String language) { this(language, "", ""); } + /** + * Obtains a locale from language, country and variant. + * This method normalizes the language value to lowercase and + * the country value to uppercase. + * @implNote + *
* Note: The {@code Locale} class also supports other codes for * country (region), such as 3-letter numeric UN M.49 area codes. * Therefore, the list returned by this method does not contain ALL valid - * codes that can be used to create Locales. + * codes that can be used to obtain Locales. *
* Note that this method does not return obsolete 2-letter country codes. * ISO3166-3 codes which designate country codes for those obsolete codes, @@ -1178,7 +1250,7 @@ public final class Locale implements Cloneable, Serializable { /** * Returns a list of all 2-letter language codes defined in ISO 639. - * Can be used to create Locales. + * Can be used to obtain Locales. *
* Note: *
Note: Although the language tag created by this + *
Note: Although the language tag obtained by this * method is well-formed (satisfies the syntax requirements * defined by the IETF BCP 47 specification), it is not * necessarily a valid BCP 47 language tag. For example, *
- * new Locale("xx", "YY").toLanguageTag();
+ * Locale.forLanguageTag("xx-YY").toLanguageTag();
*
* will return "xx-YY", but the language subtag "xx" and the
* region subtag "YY" are invalid because they are not registered
@@ -2510,7 +2582,7 @@ public final class Locale implements Cloneable, Serializable {
* from values configured by the setters. Unlike the {@code Locale}
* constructors, the {@code Builder} checks if a value configured by a
* setter satisfies the syntax requirements defined by the {@code Locale}
- * class. A {@code Locale} object created by a {@code Builder} is
+ * class. A {@code Locale} object obtained from a {@code Builder} is
* well-formed and can be transformed to a well-formed IETF BCP 47 language tag
* without losing information.
*
@@ -2521,11 +2593,11 @@ public final class Locale implements Cloneable, Serializable {
* {@code IllformedLocaleException} for a variant that does not satisfy
* this restriction. If it is necessary to support such a variant, use a
* Locale constructor. However, keep in mind that a {@code Locale}
- * object created this way might lose the variant information when
+ * object obtained this way might lose the variant information when
* transformed to a BCP 47 language tag.
*
- * The following example shows how to create a {@code Locale} object - * with the {@code Builder}. + *
The following example shows how to obtain a {@code Locale} object + * using a {@code Builder}. *
*
* Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
@@ -2658,7 +2730,7 @@ public final class Locale implements Cloneable, Serializable {
* The typical region value is a two-letter ISO 3166 code or a
* three-digit UN M.49 area code.
*
- *
The country value in the {@code Locale} created by the
+ *
The country value in the {@code Locale} obtained from a
* {@code Builder} is always normalized to upper case.
*
* @param region the region
@@ -2831,7 +2903,7 @@ public final class Locale implements Cloneable, Serializable {
}
/**
- * Returns an instance of {@code Locale} created from the fields set
+ * Returns an instance of {@code Locale} obtained from the fields set
* on this builder.
*
*
This applies the conversions listed in {@link Locale#forLanguageTag}
diff --git a/src/java.base/share/classes/sun/util/locale/provider/JRELocaleConstants.java b/src/java.base/share/classes/sun/util/locale/provider/JRELocaleConstants.java
index ea511c5bbc6..f54f8d5b68e 100644
--- a/src/java.base/share/classes/sun/util/locale/provider/JRELocaleConstants.java
+++ b/src/java.base/share/classes/sun/util/locale/provider/JRELocaleConstants.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -35,10 +35,10 @@ import java.util.Locale;
* @author Masayoshi Okutsu
*/
public class JRELocaleConstants {
- public static final Locale JA_JP_JP = new Locale("ja", "JP", "JP");
- public static final Locale NO_NO_NY = new Locale("no", "NO", "NY");
- public static final Locale TH_TH = new Locale("th", "TH");
- public static final Locale TH_TH_TH = new Locale("th", "TH", "TH");
+ public static final Locale JA_JP_JP = Locale.of("ja", "JP", "JP");
+ public static final Locale NO_NO_NY = Locale.of("no", "NO", "NY");
+ public static final Locale TH_TH = Locale.of("th", "TH");
+ public static final Locale TH_TH_TH = Locale.of("th", "TH", "TH");
private JRELocaleConstants() {
}
diff --git a/src/java.base/share/classes/sun/util/locale/provider/LocaleServiceProviderPool.java b/src/java.base/share/classes/sun/util/locale/provider/LocaleServiceProviderPool.java
index 18bac98d941..0cfbf24ad83 100644
--- a/src/java.base/share/classes/sun/util/locale/provider/LocaleServiceProviderPool.java
+++ b/src/java.base/share/classes/sun/util/locale/provider/LocaleServiceProviderPool.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -369,7 +369,7 @@ public final class LocaleServiceProviderPool {
"A locale(" + locale + ") has non-empty extensions, but has illformed fields.");
// Fallback - script field will be lost.
- lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant());
+ lookupLocale = Locale.of(locale.getLanguage(), locale.getCountry(), locale.getVariant());
}
}
return lookupLocale;
diff --git a/src/java.base/share/classes/sun/util/resources/LocaleData.java b/src/java.base/share/classes/sun/util/resources/LocaleData.java
index 4240352e831..7d9af41811c 100644
--- a/src/java.base/share/classes/sun/util/resources/LocaleData.java
+++ b/src/java.base/share/classes/sun/util/resources/LocaleData.java
@@ -246,7 +246,7 @@ public class LocaleData {
private static final LocaleDataStrategy INSTANCE = new LocaleDataStrategy();
// TODO: avoid hard-coded Locales
private static final Set JAVA_BASE_LOCALES
- = Set.of(Locale.ROOT, Locale.ENGLISH, Locale.US, new Locale("en", "US", "POSIX"));
+ = Set.of(Locale.ROOT, Locale.ENGLISH, Locale.US, Locale.of("en", "US", "POSIX"));
private LocaleDataStrategy() {
}
diff --git a/src/java.desktop/share/classes/sun/awt/SunToolkit.java b/src/java.desktop/share/classes/sun/awt/SunToolkit.java
index 380a5e4db01..1e144c373e4 100644
--- a/src/java.desktop/share/classes/sun/awt/SunToolkit.java
+++ b/src/java.desktop/share/classes/sun/awt/SunToolkit.java
@@ -1181,7 +1181,7 @@ public abstract class SunToolkit extends Toolkit
variant = AccessController.doPrivileged(
new GetPropertyAction("user.variant", ""));
}
- startupLocale = new Locale(language, country, variant);
+ startupLocale = Locale.of(language, country, variant);
}
return startupLocale;
}
diff --git a/src/java.desktop/share/classes/sun/awt/im/ExecutableInputMethodManager.java b/src/java.desktop/share/classes/sun/awt/im/ExecutableInputMethodManager.java
index eb8f6368c20..5b32879b598 100644
--- a/src/java.desktop/share/classes/sun/awt/im/ExecutableInputMethodManager.java
+++ b/src/java.desktop/share/classes/sun/awt/im/ExecutableInputMethodManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -375,7 +375,7 @@ class ExecutableInputMethodManager extends InputMethodManager
variant = localeString.substring(postIndex + 1);
}
}
- Locale locale = new Locale(language, country, variant);
+ Locale locale = Locale.of(language, country, variant);
locator = locator.deriveLocator(locale);
}
@@ -550,8 +550,8 @@ class ExecutableInputMethodManager extends InputMethodManager
if (preferredLocale.equals(Locale.KOREA)) {
preferredLocale = Locale.KOREAN;
}
- if (preferredLocale.equals(new Locale("th", "TH"))) {
- preferredLocale = new Locale("th");
+ if (preferredLocale.equals(Locale.of("th", "TH"))) {
+ preferredLocale = Locale.of("th");
}
// obtain node
@@ -623,10 +623,10 @@ class ExecutableInputMethodManager extends InputMethodManager
advertised = Locale.KOREAN;
}
} else if (locale.getLanguage().equals("th")) {
- if (locator.isLocaleAvailable(new Locale("th", "TH"))) {
- advertised = new Locale("th", "TH");
- } else if (locator.isLocaleAvailable(new Locale("th"))) {
- advertised = new Locale("th");
+ if (locator.isLocaleAvailable(Locale.of("th", "TH"))) {
+ advertised = Locale.of("th", "TH");
+ } else if (locator.isLocaleAvailable(Locale.of("th"))) {
+ advertised = Locale.of("th");
}
}
diff --git a/src/java.desktop/share/classes/sun/font/SunFontManager.java b/src/java.desktop/share/classes/sun/font/SunFontManager.java
index 4f36498f6e2..3395e343e2e 100644
--- a/src/java.desktop/share/classes/sun/font/SunFontManager.java
+++ b/src/java.desktop/share/classes/sun/font/SunFontManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -3466,7 +3466,7 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
String language = System.getProperty("user.language", "en");
String country = System.getProperty("user.country","");
String variant = System.getProperty("user.variant","");
- return new Locale(language, country, variant);
+ return Locale.of(language, country, variant);
}
});
}
diff --git a/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/I18n.java b/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/I18n.java
index 9b928794c6e..d2d63fc85ff 100644
--- a/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/I18n.java
+++ b/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/I18n.java
@@ -160,7 +160,7 @@ public final class I18n {
I18n.resourceBundle =
ResourceBundle.getBundle(
Constants.exceptionMessagesResourceBundleBase,
- new Locale(languageCode, countryCode)
+ Locale.of(languageCode, countryCode)
);
alreadyInitialized = true;
}
diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/CollatorFactoryBase.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/CollatorFactoryBase.java
index ce509409620..1cfd9693901 100644
--- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/CollatorFactoryBase.java
+++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/CollatorFactoryBase.java
@@ -38,7 +38,7 @@ public class CollatorFactoryBase implements CollatorFactory {
}
public Collator getCollator(String lang, String country) {
- return Collator.getInstance(new Locale(lang, country));
+ return Collator.getInstance(Locale.of(lang, country));
}
public Collator getCollator(Locale locale) {
diff --git a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/datatype/XMLGregorianCalendarImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/datatype/XMLGregorianCalendarImpl.java
index 46582fefd1b..fbb758def47 100644
--- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/datatype/XMLGregorianCalendarImpl.java
+++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/datatype/XMLGregorianCalendarImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -2425,12 +2425,12 @@ public class XMLGregorianCalendarImpl
if (lang != null) {
if (country != null) {
if (variant != null) {
- locale = new Locale(lang, country, variant);
+ locale = Locale.of(lang, country, variant);
} else {
- locale = new Locale(lang, country);
+ locale = Locale.of(lang, country);
}
} else {
- locale = new Locale(lang);
+ locale = Locale.of(lang);
}
}
if (locale == null) {
diff --git a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/LocaleUtility.java b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/LocaleUtility.java
index d63e26aafd4..e08a619dd61 100644
--- a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/LocaleUtility.java
+++ b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/LocaleUtility.java
@@ -78,7 +78,7 @@ public class LocaleUtility {
variant = EMPTY_STRING;
}
- return new Locale(language, country, variant );
+ return Locale.of(language, country, variant);
}
diff --git a/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java b/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java
index c5678b8b750..760db484b97 100644
--- a/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java
+++ b/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -275,7 +275,7 @@ public class SecuritySupport {
return ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
- return ResourceBundle.getBundle(bundle, new Locale("en", "US"));
+ return ResourceBundle.getBundle(bundle, Locale.US);
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java
index 623b7f2917b..fae1cba139f 100644
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -141,9 +141,9 @@ public final class IncludeLocalesPlugin extends AbstractPlugin implements Resour
private static final String jaJPJPTag = "ja-JP-JP";
private static final String noNONYTag = "no-NO-NY";
private static final String thTHTHTag = "th-TH-TH";
- private static final Locale jaJPJP = new Locale("ja", "JP", "JP");
- private static final Locale noNONY = new Locale("no", "NO", "NY");
- private static final Locale thTHTH = new Locale("th", "TH", "TH");
+ private static final Locale jaJPJP = Locale.of("ja", "JP", "JP");
+ private static final Locale noNONY = Locale.of("no", "NO", "NY");
+ private static final Locale thTHTH = Locale.of("th", "TH", "TH");
public IncludeLocalesPlugin() {
super("include-locales");
diff --git a/test/jdk/java/util/Locale/TestOf.java b/test/jdk/java/util/Locale/TestOf.java
new file mode 100644
index 00000000000..c923da832bf
--- /dev/null
+++ b/test/jdk/java/util/Locale/TestOf.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * 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 8282819
+ * @summary Unit tests for Locale.of() method. Those tests check the equality
+ * of obtained objects with ones that are gotten from other means with both
+ * well-formed and ill-formed arguments. Also checks the possible NPEs
+ * for error cases.
+ * @run testng TestOf
+ */
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertThrows;
+
+import java.util.Locale;
+
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+@SuppressWarnings("deprecation")
+@Test
+public class TestOf {
+
+ @DataProvider
+ public Object[][] data_1Arg() {
+ return new Object[][]{
+ // well-formed
+ {Locale.ENGLISH, "en"},
+ {Locale.JAPANESE, "ja"},
+
+ // ill-formed
+ {Locale.ROOT, ""},
+ {new Locale("a"), "a"},
+ {new Locale("xxxxxxxxxx"), "xxxxxxxxxx"},
+ };
+ }
+
+ @DataProvider
+ public Object[][] data_2Args() {
+ return new Object[][]{
+ // well-formed
+ {Locale.US, "en", "US"},
+ {Locale.JAPAN, "ja", "JP"},
+
+ // ill-formed
+ {new Locale("", "US"), "", "US"},
+ {new Locale("a", "b"), "a", "b"},
+ {new Locale("xxxxxxxxxx", "yyyyyyyyyy"), "xxxxxxxxxx", "yyyyyyyyyy"},
+ };
+ }
+
+ @DataProvider
+ public Object[][] data_3Args() {
+ return new Object[][]{
+ // well-formed
+ {Locale.forLanguageTag("en-US-POSIX"), "en", "US", "POSIX"},
+ {Locale.forLanguageTag("ja-JP-POSIX"), "ja", "JP", "POSIX"},
+
+ // ill-formed
+ {new Locale("", "", "POSIX"), "", "", "POSIX"},
+ {new Locale("a", "b", "c"), "a", "b", "c"},
+ {new Locale("xxxxxxxxxx", "yyyyyyyyyy", "zzzzzzzzzz"),
+ "xxxxxxxxxx", "yyyyyyyyyy", "zzzzzzzzzz"},
+ {new Locale("ja", "JP", "JP"), "ja", "JP", "JP"},
+ {new Locale("th", "TH", "TH"), "th", "TH", "TH"},
+ {new Locale("no", "NO", "NY"), "no", "NO", "NY"},
+ };
+ }
+
+ @Test (dataProvider = "data_1Arg")
+ public void test_1Arg(Locale expected, String lang) {
+ assertEquals(Locale.of(lang), expected);
+ }
+
+ @Test (dataProvider = "data_2Args")
+ public void test_2Args(Locale expected, String lang, String ctry) {
+ assertEquals(Locale.of(lang, ctry), expected);
+ }
+
+ @Test (dataProvider = "data_3Args")
+ public void test_3Args(Locale expected, String lang, String ctry, String vrnt) {
+ assertEquals(Locale.of(lang, ctry, vrnt), expected);
+ }
+
+ @Test
+ public void test_NPE() {
+ assertThrows(NullPointerException.class, () -> Locale.of(null));
+ assertThrows(NullPointerException.class, () -> Locale.of("", null));
+ assertThrows(NullPointerException.class, () -> Locale.of("", "", null));
+ }
+}