8310234: Refactor Locale tests to use JUnit

Reviewed-by: naoto
This commit is contained in:
Justin Lu 2023-06-23 21:33:49 +00:00
parent 69f3114c41
commit dad7bd9efc
10 changed files with 358 additions and 260 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -20,30 +20,45 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
/* /*
* @test * @test
* @summary Test the implementation * @bug 4122700 8282319
* of Locale.availableLocales() * @summary Verify implementation of getAvailableLocales() and availableLocales()
* @bug 8282319 * @run junit AvailableLocalesTest
* @run junit StreamAvailableLocales
*/ */
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
public class StreamAvailableLocales { import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class AvailableLocalesTest {
/**
* Test that Locale.getAvailableLocales() is non-empty and prints out
* the returned locales - 4122700.
*/
@Test
public void nonEmptyLocalesTest() {
Locale[] systemLocales = Locale.getAvailableLocales();
assertNotEquals(systemLocales.length, 0, "Available locale list is empty!");
System.out.println("Found " + systemLocales.length + " locales:");
printLocales(systemLocales);
}
/** /**
* Test to validate that the methods: Locale.getAvailableLocales() * Test to validate that the methods: Locale.getAvailableLocales()
* and Locale.availableLocales() contain the same underlying elements * and Locale.availableLocales() contain the same underlying elements
*/ */
@Test @Test
public void testStreamEqualsArray() { public void streamEqualsArrayTest() {
Locale[] arrayLocales = Locale.getAvailableLocales(); Locale[] arrayLocales = Locale.getAvailableLocales();
Stream<Locale> streamedLocales = Locale.availableLocales(); Stream<Locale> streamedLocales = Locale.availableLocales();
Locale[] convertedLocales = streamedLocales.toArray(Locale[]::new); Locale[] convertedLocales = streamedLocales.toArray(Locale[]::new);
@ -63,7 +78,7 @@ public class StreamAvailableLocales {
*/ */
@ParameterizedTest @ParameterizedTest
@MethodSource("requiredLocaleProvider") @MethodSource("requiredLocaleProvider")
public void testStreamRequirements(Locale requiredLocale, String localeName) { public void requiredLocalesTest(Locale requiredLocale, String localeName) {
if (Locale.availableLocales().anyMatch(loc -> (loc.equals(requiredLocale)))) { if (Locale.availableLocales().anyMatch(loc -> (loc.equals(requiredLocale)))) {
System.out.printf("$$$ Passed: Stream has %s!%n", localeName); System.out.printf("$$$ Passed: Stream has %s!%n", localeName);
} else { } else {
@ -72,6 +87,32 @@ public class StreamAvailableLocales {
} }
} }
// Helper method to print out all the system locales
private void printLocales(Locale[] systemLocales) {
Locale[] locales = new Locale[systemLocales.length];
for (int i = 0; i < locales.length; i++) {
Locale lowest = null;
for (Locale systemLocale : systemLocales) {
if (i > 0 && locales[i - 1].toString().compareTo(systemLocale.toString()) >= 0)
continue;
if (lowest == null || systemLocale.toString().compareTo(lowest.toString()) < 0)
lowest = systemLocale;
}
locales[i] = lowest;
}
for (Locale locale : locales) {
if (locale.getCountry().length() == 0)
System.out.println(" " + locale.getDisplayLanguage() + ":");
else {
if (locale.getVariant().length() == 0)
System.out.println(" " + locale.getDisplayCountry());
else
System.out.println(" " + locale.getDisplayCountry() + ", "
+ locale.getDisplayVariant());
}
}
}
// Data provider for testStreamRequirements // Data provider for testStreamRequirements
private static Stream<Arguments> requiredLocaleProvider() { private static Stream<Arguments> requiredLocaleProvider() {
return Stream.of( return Stream.of(

View File

@ -1,45 +0,0 @@
/*
* Copyright (c) 2007, 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
@summary Locale constructor should allow language-only argument
@bug 4316602
@author joconner
*/
import java.util.Locale;
public class Bug4316602 {
public static void main(String[] args) throws Exception {
String language = "ja";
Locale aLocale = Locale.of(language);
if (aLocale.toString().equals(language)) {
System.out.println("passed");
} else {
System.out.println("Bug4316602 failed");
throw new Exception("Bug4316602 failed");
}
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -20,30 +20,43 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
/**
@test /*
@summary Locale variant should not be uppercased * @test
@run main Bug4210525 * @bug 4210525
@bug 4210525 * @summary Locale variant should not be case folded
*/ * @run junit CaseCheckVariant
*/
import java.util.Locale; import java.util.Locale;
import java.util.stream.Stream;
public class Bug4210525 { import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public static void main(String[] args) throws Exception { import static org.junit.jupiter.api.Assertions.assertEquals;
String language = "en";
String country = "US";
String variant = "socal";
Locale aLocale = Locale.of(language, country, variant); public class CaseCheckVariant {
static final String LANG = "en";
static final String COUNTRY = "US";
/**
* When a locale is created with a given variant, ensure
* that the variant is not case normalized.
*/
@ParameterizedTest
@MethodSource("variants")
public void variantCaseTest(String variant) {
Locale aLocale = Locale.of(LANG, COUNTRY, variant);
String localeVariant = aLocale.getVariant(); String localeVariant = aLocale.getVariant();
if (localeVariant.equals(variant)) { assertEquals(localeVariant, variant);
System.out.println("passed"); }
} else {
System.out.println("failed"); private static Stream<String> variants() {
throw new Exception("Bug4210525 test failed."); return Stream.of(
} "socal",
"Norcal"
);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,43 +28,65 @@
* java.base/sun.util.resources * java.base/sun.util.resources
* jdk.localedata * jdk.localedata
* @summary Test for checking HourFormat and GmtFormat resources are retrieved from * @summary Test for checking HourFormat and GmtFormat resources are retrieved from
* COMPAT and CLDR Providers. * COMPAT and CLDR Providers.
* @run junit CompareProviderFormats
*/ */
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.stream.Stream;
import sun.util.locale.provider.LocaleProviderAdapter.Type; import sun.util.locale.provider.LocaleProviderAdapter.Type;
import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleProviderAdapter;
public class Bug8154797 { import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class CompareProviderFormats {
static Map<String, String> expectedResourcesMap = new HashMap<>(); static Map<String, String> expectedResourcesMap = new HashMap<>();
static final String GMT_RESOURCE_KEY = "timezone.gmtFormat"; static final String GMT_RESOURCE_KEY = "timezone.gmtFormat";
static final String HMT_RESOURCE_KEY = "timezone.hourFormat"; static final String HMT_RESOURCE_KEY = "timezone.hourFormat";
static final String GMT = "Gmt"; static final String GMT = "Gmt";
static final String HMT = "Hmt"; static final String HMT = "Hmt";
static void generateExpectedValues() { /**
* Fill the expectedResourcesMap with the desired key / values
*/
@BeforeAll
static void populateResourcesMap() {
expectedResourcesMap.put("FR" + GMT, "UTC{0}"); expectedResourcesMap.put("FR" + GMT, "UTC{0}");
expectedResourcesMap.put("FR" + HMT, "+HH:mm;\u2212HH:mm"); expectedResourcesMap.put("FR" + HMT, "+HH:mm;\u2212HH:mm");
expectedResourcesMap.put("FI" + HMT, "+H.mm;-H.mm"); expectedResourcesMap.put("FI" + HMT, "+H.mm;-H.mm");
expectedResourcesMap.put("FI" + GMT, "UTC{0}"); expectedResourcesMap.put("FI" + GMT, "UTC{0}");
/* For root locale, en_US, de_DE, hi_IN, ja_JP,Root locale resources /* For root locale, en_US, de_DE, hi_IN, ja_JP, Root locale resources
* should be returned. * should be returned.
*/ */
expectedResourcesMap.put(GMT, "GMT{0}"); //Root locale resource expectedResourcesMap.put(GMT, "GMT{0}"); // Root locale resource
expectedResourcesMap.put(HMT, "+HH:mm;-HH:mm"); //Root locale resource expectedResourcesMap.put(HMT, "+HH:mm;-HH:mm"); // Root locale resource
} }
static void compareResources(Locale loc) { /**
* For each locale, ensure that the returned resources for gmt and hmt match
* the expected resources for both COMPAT and CLDR
*/
@ParameterizedTest
@MethodSource("localeProvider")
public void compareResourcesTest(Locale loc) {
compareResources(loc);
}
private void compareResources(Locale loc) {
String mapKeyHourFormat = HMT, mapKeyGmtFormat = GMT; String mapKeyHourFormat = HMT, mapKeyGmtFormat = GMT;
ResourceBundle compatBundle, cldrBundle; ResourceBundle compatBundle, cldrBundle;
compatBundle = LocaleProviderAdapter.forJRE().getLocaleResources(loc) compatBundle = LocaleProviderAdapter.forJRE().getLocaleResources(loc)
.getJavaTimeFormatData(); .getJavaTimeFormatData();
cldrBundle = LocaleProviderAdapter.forType(Type.CLDR) cldrBundle = LocaleProviderAdapter.forType(Type.CLDR)
.getLocaleResources(loc).getJavaTimeFormatData(); .getLocaleResources(loc).getJavaTimeFormatData();
if (loc.getCountry() == "FR" || loc.getCountry() == "FI") {
if (loc.getCountry().equals("FR") || loc.getCountry().equals("FI")) {
mapKeyHourFormat = loc.getCountry() + HMT; mapKeyHourFormat = loc.getCountry() + HMT;
mapKeyGmtFormat = loc.getCountry() + GMT; mapKeyGmtFormat = loc.getCountry() + GMT;
} }
@ -77,23 +99,17 @@ public class Bug8154797 {
.equals(cldrBundle.getString(GMT_RESOURCE_KEY)) .equals(cldrBundle.getString(GMT_RESOURCE_KEY))
&& expectedResourcesMap.get(mapKeyHourFormat) && expectedResourcesMap.get(mapKeyHourFormat)
.equals(cldrBundle.getString(HMT_RESOURCE_KEY)))) { .equals(cldrBundle.getString(HMT_RESOURCE_KEY)))) {
throw new RuntimeException("Retrieved resource does not match with " throw new RuntimeException("Retrieved resource does not match with "
+ " expected string for Locale " + compatBundle.getLocale()); + " expected string for Locale " + compatBundle.getLocale());
}
}
public static void main(String args[]) {
Bug8154797.generateExpectedValues();
Locale[] locArr = {Locale.of("hi", "IN"), Locale.UK, Locale.of("fi", "FI"),
Locale.ROOT, Locale.GERMAN, Locale.JAPANESE,
Locale.ENGLISH, Locale.FRANCE};
for (Locale loc : locArr) {
Bug8154797.compareResources(loc);
} }
} }
private static Stream<Locale> localeProvider() {
return Stream.of(
Locale.of("hi", "IN"),
Locale.UK, Locale.of("fi", "FI"),
Locale.ROOT, Locale.GERMAN, Locale.JAPANESE,
Locale.ENGLISH, Locale.FRANCE
);
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,25 +26,27 @@
* @bug 8004240 * @bug 8004240
* @summary Verify that getAdapterPreference returns an unmodifiable list. * @summary Verify that getAdapterPreference returns an unmodifiable list.
* @modules java.base/sun.util.locale.provider * @modules java.base/sun.util.locale.provider
* @compile -XDignore.symbol.file Bug8004240.java * @compile -XDignore.symbol.file GetAdapterPreference.java
* @run main Bug8004240 * @run junit GetAdapterPreference
*/ */
import java.util.List; import java.util.List;
import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleProviderAdapter;
public class Bug8004240 { import org.junit.jupiter.api.Test;
public static void main(String[] args) { import static org.junit.jupiter.api.Assertions.assertThrows;
public class GetAdapterPreference {
/**
* Test that the list returned from getAdapterPreference()
* cannot be modified.
*/
@Test
public void immutableTest() {
List<LocaleProviderAdapter.Type> types = LocaleProviderAdapter.getAdapterPreference(); List<LocaleProviderAdapter.Type> types = LocaleProviderAdapter.getAdapterPreference();
assertThrows(UnsupportedOperationException.class, () -> types.set(0, null),
try { "Trying to modify list returned from LocaleProviderAdapter.getAdapterPreference() did not throw UOE");
types.set(0, null);
} catch (UnsupportedOperationException e) {
// success
return;
}
throw new RuntimeException("LocaleProviderAdapter.getAdapterPrefence() returned a modifiable list.");
} }
} }

View File

@ -0,0 +1,126 @@
/*
* Copyright (c) 2007, 2023, 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 6312358
* @summary Verify that an NPE is thrown by invoking Locale.getInstance() with
* any argument being null.
* @modules java.base/java.util:open
* @run junit GetInstanceCheck
*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.fail;
public class GetInstanceCheck {
static Method getInstanceMethod;
static final String NAME = "getInstance";
/**
* Initialize the non-public Locale.getInstance() method.
*/
@BeforeAll
static void initializeMethod() {
try {
// Locale.getInstance is not directly accessible.
getInstanceMethod = Locale.class.getDeclaredMethod(
NAME, String.class, String.class, String.class
);
getInstanceMethod.setAccessible(true);
} catch (java.lang.NoSuchMethodException exc) {
// The test should fail if we can not test the desired method
fail(String.format("Tried to get the method '%s' which was not found," +
" further testing is not possible, failing test", NAME));
}
}
/**
* Exists as sanity check that Locale.getInstance() will not throw
* an NPE if no arguments are null.
*/
@ParameterizedTest
@MethodSource("passingArguments")
public void noNPETest(String language, String country, String variant)
throws IllegalAccessException {
try {
getInstanceMethod.invoke(null, language, country, variant);
} catch (InvocationTargetException exc) {
// Determine underlying exception
Throwable cause = exc.getCause();
if (exc.getCause() instanceof NullPointerException) {
fail(String.format("%s should not be thrown when no args are null", cause));
} else {
fail(String.format("%s unexpectedly thrown, when no exception should be thrown", cause));
}
}
}
/**
* Make sure the Locale.getInstance() method throws an NPE
* if any given argument is null.
*/
@ParameterizedTest
@MethodSource("failingArguments")
public void throwNPETest(String language, String country, String variant)
throws IllegalAccessException {
try {
getInstanceMethod.invoke(null, language, country, variant);
fail("Should NPE with any argument set to null");
} catch (InvocationTargetException exc) {
// Determine underlying exception
Throwable cause = exc.getCause();
if (cause instanceof NullPointerException) {
System.out.println("NPE successfully thrown");
} else {
fail(cause + " is thrown, when NPE should have been thrown");
}
}
}
private static Stream<Arguments> passingArguments() {
return Stream.of(
Arguments.of("null", "GB", ""),
Arguments.of("en", "null", ""),
Arguments.of("en", "GB", "null")
);
}
private static Stream<Arguments> failingArguments() {
return Stream.of(
Arguments.of(null, "GB", ""),
Arguments.of("en", null, ""),
Arguments.of("en", "GB", null)
);
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2007, 2023, 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 4316602
* @author joconner
* @summary Verify all Locale constructors and of() methods
* @run junit LocaleConstructors
*/
import java.util.Locale;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* This class tests to ensure that the language, language/country, and
* language/country/variant Locale constructors + of() method are all allowed.
*/
public class LocaleConstructors {
static final String LANG = "en";
static final String COUNTRY = "US";
static final String VAR = "socal";
// Test Locale constructor and .of() allow (language) argument(s)
@Test
public void langTest() {
Locale aLocale = Locale.of(LANG);
Locale otherLocale = new Locale(LANG);
assertEquals(aLocale.toString(), LANG);
assertEquals(otherLocale.toString(), LANG);
}
// Test Locale constructor and .of() allow (language, constructor) argument(s)
@Test
public void langCountryTest() {
Locale aLocale = Locale.of(LANG, COUNTRY);
Locale otherLocale = new Locale(LANG, COUNTRY);
assertEquals(aLocale.toString(), String.format("%s_%s",
LANG, COUNTRY));
assertEquals(otherLocale.toString(), String.format("%s_%s",
LANG, COUNTRY));
}
// Test Locale constructor and .of() allow
// (language, constructor, variant) argument(s)
@Test
public void langCountryVariantTest() {
Locale aLocale = Locale.of(LANG, COUNTRY, VAR);
Locale otherLocale = new Locale(LANG, COUNTRY, VAR);
assertEquals(aLocale.toString(), String.format("%s_%s_%s",
LANG, COUNTRY, VAR));
assertEquals(otherLocale.toString(), String.format("%s_%s_%s",
LANG, COUNTRY, VAR));
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -20,20 +20,29 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
/* /*
* @test * @test
* @bug 6277243 * @bug 6277243
* @summary Verify that there is Locale.ROOT constant, and it is equal to Locale("", "", "") * @summary Verify that there is Locale.ROOT constant, and it is equal to Locale("", "", "")
* @run junit RootLocale
*/ */
import java.util.Locale; import java.util.Locale;
public class bug6277243 { import org.junit.jupiter.api.Test;
public static void main(String[] args) throws Exception { import static org.junit.jupiter.api.Assertions.assertEquals;
public class RootLocale {
/**
* Locale.ROOT should exist and match an empty Locale given as
* Locale("", "", "").
*/
@Test
public void rootTest() {
Locale root = Locale.of("", "", ""); Locale root = Locale.of("", "", "");
if (!Locale.ROOT.equals(root)) { assertEquals(Locale.ROOT, root, "Locale.ROOT is not equal to Locale(\"\", \"\", \"\")");
throw new RuntimeException("Locale.ROOT is not equal to Locale(\"\", \"\", \"\")");
}
} }
} }

View File

@ -1,60 +0,0 @@
/*
* Copyright (c) 2007, 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 4122700
* @summary Verify that list of available locales is non-empty, and print the list
*/
import java.util.Locale;
public class bug4122700 {
public static void main(String[] args) throws Exception {
Locale[] systemLocales = Locale.getAvailableLocales();
if (systemLocales.length == 0)
throw new Exception("Available locale list is empty!");
System.out.println("Found " + systemLocales.length + " locales:");
Locale[] locales = new Locale[systemLocales.length];
for (int i = 0; i < locales.length; i++) {
Locale lowest = null;
for (int j = 0; j < systemLocales.length; j++) {
if (i > 0 && locales[i - 1].toString().compareTo(systemLocales[j].toString()) >= 0)
continue;
if (lowest == null || systemLocales[j].toString().compareTo(lowest.toString()) < 0)
lowest = systemLocales[j];
}
locales[i] = lowest;
}
for (int i = 0; i < locales.length; i++) {
if (locales[i].getCountry().length() == 0)
System.out.println(" " + locales[i].getDisplayLanguage() + ":");
else {
if (locales[i].getVariant().length() == 0)
System.out.println(" " + locales[i].getDisplayCountry());
else
System.out.println(" " + locales[i].getDisplayCountry() + ", "
+ locales[i].getDisplayVariant());
}
}
}
}

View File

@ -1,83 +0,0 @@
/*
* Copyright (c) 2007, 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 6312358
* @summary Verify that an NPE is thrown by issueing Locale.getInstance() with
* any argument being null.
* @modules java.base/java.util:open
*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
public class bug6312358 {
public static void main(String[] args) throws Exception {
try {
// Locale.getInstance is not directly accessible.
Method getInstanceMethod = Locale.class.getDeclaredMethod(
"getInstance", String.class, String.class, String.class
);
getInstanceMethod.setAccessible(true);
getInstanceMethod.invoke(null, "null", "GB", "");
try {
getInstanceMethod.invoke(null, null, "GB", "");
throw new RuntimeException("Should NPE with language set to null");
} catch (InvocationTargetException exc) {
Throwable cause = exc.getCause();
if (!(cause instanceof NullPointerException)) {
throw new RuntimeException(cause+" is thrown with language set to null");
}
}
getInstanceMethod.invoke(null, "en", "null", "");
try {
getInstanceMethod.invoke(null, "en", null, "");
throw new RuntimeException("Should NPE with country set to null");
} catch (InvocationTargetException exc) {
Throwable cause = exc.getCause();
if (!(cause instanceof NullPointerException)) {
throw new RuntimeException(cause+" is thrown with country set to null");
}
}
getInstanceMethod.invoke(null, "en", "GB", "null");
try {
getInstanceMethod.invoke(null, "en", "GB", null);
throw new RuntimeException("Should NPE with variant set to null");
} catch (InvocationTargetException exc) {
Throwable cause = exc.getCause();
if (!(cause instanceof NullPointerException)) {
throw new RuntimeException(cause+" is thrown with variant set to null");
}
}
} catch (java.lang.NoSuchMethodException exc) {
// method is not found. consider it as a success
}
}
}