Skip to content

Commit

Permalink
8311663: Additional refactoring of Locale tests to JUnit
Browse files Browse the repository at this point in the history
Reviewed-by: naoto
  • Loading branch information
Justin Lu committed Jul 19, 2023
1 parent aa23fd9 commit 71cac8c
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 390 deletions.
24 changes: 13 additions & 11 deletions test/jdk/java/util/Locale/Bug6989440.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
Expand Down Expand Up @@ -28,7 +28,7 @@
* thread accesses.
* @modules java.base/sun.util.locale.provider
* @compile -XDignore.symbol.file=true Bug6989440.java
* @run main Bug6989440
* @run junit Bug6989440
*/
import java.text.spi.DateFormatProvider;
import java.util.spi.LocaleNameProvider;
Expand All @@ -37,11 +37,17 @@

import sun.util.locale.provider.LocaleServiceProviderPool;

import org.junit.jupiter.api.Test;

public class Bug6989440 {
static volatile boolean failed; // false
static final int THREADS = 50;

public static void main(String[] args) throws Exception {
/* Multiple instances of Locale Service Provider Pool calling
* getAvailableLocales() should not throw ConcurrentModificationException
*/
@Test
public void multiThreadAccessTest() throws Exception {
Thread[] threads = new Thread[THREADS];
for (int i=0; i<threads.length; i++)
threads[i] = new TestThread();
Expand All @@ -58,17 +64,13 @@ static class TestThread extends Thread {
private Class<? extends LocaleServiceProvider> cls;
private static int count;

public TestThread(Class<? extends LocaleServiceProvider> providerClass) {
cls = providerClass;
}

public TestThread() {
int which = count++ % 3;
switch (which) {
case 0 : cls = LocaleNameProvider.class; break;
case 1 : cls = TimeZoneNameProvider.class; break;
case 2 : cls = DateFormatProvider.class; break;
default : throw new AssertionError("Should not reach here");
case 0 -> cls = LocaleNameProvider.class;
case 1 -> cls = TimeZoneNameProvider.class;
case 2 -> cls = DateFormatProvider.class;
default -> throw new AssertionError("Should not reach here");
}
}

Expand Down
131 changes: 58 additions & 73 deletions test/jdk/java/util/Locale/Bug8035133.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,104 +25,90 @@
* @bug 8035133
* @summary Checks that the tags matching the range with quality weight q=0
* e.g. en;q=0 must be elimited and must not be the part of output
* @run junit Bug8035133
*/

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class Bug8035133 {

private static boolean err = false;

public static void main(String[] args) {

// checking Locale.lookup with de-ch;q=0
checkLookup("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
"de-ch, en, fr-ch", "fr-CH");

/* checking Locale.lookup with *;q=0 '*' should be ignored
* in lookup
*/
checkLookup("en;q=0.1, *-ch;q=0.5, *;q=0",
"de-ch, en, fr-ch", "de-CH");

// checking Locale.filter with fr-ch;q=0 in BASIC_FILTERING
checkFilter("en;q=0.1, fr-ch;q=0.0, de-ch;q=0.5",
"de-ch, en, fr-ch", "de-CH, en");

// checking Locale.filter with *;q=0 in BASIC_FILTERING
checkFilter("de-ch;q=0.6, *;q=0", "de-ch, fr-ch", "");

// checking Locale.filter with *;q=0 in BASIC_FILTERING
checkFilter("de-ch;q=0.6, de;q=0", "de-ch", "");

// checking Locale.filter with *;q=0.6, en;q=0 in BASIC_FILTERING
checkFilter("*;q=0.6, en;q=0", "de-ch, hi-in, en", "de-CH, hi-IN");

// checking Locale.filter with de-ch;q=0 in EXTENDED_FILTERING
checkFilter("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
"de-ch, en, fr-ch", "fr-CH, en");

/* checking Locale.filter with *-ch;q=0 in EXTENDED_FILTERING which
* must make filter to return "" empty or no match
*/
checkFilter("de-ch;q=0.5, *-ch;q=0", "de-ch, fr-ch", "");

/* checking Locale.filter with *;q=0 in EXTENDED_FILTERING which
* must make filter to return "" empty or no match
*/
checkFilter("*-ch;q=0.5, *;q=0", "de-ch, fr-ch", "");
import static org.junit.jupiter.api.Assertions.assertEquals;

/* checking Locale.filter with *;q=0.6, *-Latn;q=0 in
* EXTENDED_FILTERING
*/
checkFilter("*;q=0.6, *-Latn;q=0", "de-ch, hi-in, en-Latn",
"de-CH, hi-IN");

if (err) {
throw new RuntimeException("[LocaleMatcher method(s) failed]");
}

}
public class Bug8035133 {

private static void checkLookup(String ranges, String tags,
// Ensure weights with 'q=0' work as expected during lookup
@ParameterizedTest
@MethodSource("lookupProvider")
public void lookupTest(String ranges, String tags,
String expectedLocale) {

List<Locale.LanguageRange> priorityList = Locale.LanguageRange
.parse(ranges);
List<Locale> localeList = generateLocales(tags);
Locale loc = Locale.lookup(priorityList, localeList);
String actualLocale
= loc.toLanguageTag();

if (!actualLocale.equals(expectedLocale)) {
System.err.println("Locale.lookup failed with ranges: " + ranges
+ " Expected: " + expectedLocale
+ " Actual: " + actualLocale);
err = true;
}
String actualLocale = loc.toLanguageTag();
assertEquals(expectedLocale, actualLocale);
}

private static Stream<Arguments> lookupProvider() {
return Stream.of(
// checking Locale.lookup with de-ch;q=0
Arguments.of("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
"de-ch, en, fr-ch", "fr-CH"),
// checking Locale.lookup with *;q=0 '*' should be ignored in lookup
Arguments.of("en;q=0.1, *-ch;q=0.5, *;q=0",
"de-ch, en, fr-ch", "de-CH")
);
}

private static void checkFilter(String ranges, String tags,
// Ensure weights with 'q=0' work as expected during filtering
@ParameterizedTest
@MethodSource("filterProvider")
public void filterTest(String ranges, String tags,
String expectedLocales) {

List<Locale.LanguageRange> priorityList = Locale.LanguageRange
.parse(ranges);
List<Locale> localeList = generateLocales(tags);
String actualLocales = getLocalesAsString(
Locale.filter(priorityList, localeList));
assertEquals(expectedLocales, actualLocales);
}

if (!actualLocales.equals(expectedLocales)) {
System.err.println("Locale.filter failed with ranges: " + ranges
+ " Expected: " + expectedLocales
+ " Actual: " + actualLocales);
err = true;
}

private static Stream<Arguments> filterProvider() {
return Stream.of(
// checking Locale.filter with fr-ch;q=0 in BASIC_FILTERING
Arguments.of("en;q=0.1, fr-ch;q=0.0, de-ch;q=0.5",
"de-ch, en, fr-ch", "de-CH, en"),
// checking Locale.filter with *;q=0 in BASIC_FILTERING
Arguments.of("de-ch;q=0.6, *;q=0", "de-ch, fr-ch", ""),
// checking Locale.filter with *;q=0 in BASIC_FILTERING
Arguments.of("de-ch;q=0.6, de;q=0", "de-ch", ""),
// checking Locale.filter with *;q=0.6, en;q=0 in BASIC_FILTERING
Arguments.of("*;q=0.6, en;q=0", "de-ch, hi-in, en", "de-CH, hi-IN"),
// checking Locale.filter with de-ch;q=0 in EXTENDED_FILTERING
Arguments.of("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
"de-ch, en, fr-ch", "fr-CH, en"),
/* checking Locale.filter with *-ch;q=0 in EXTENDED_FILTERING which
* must make filter to return "" empty or no match
*/
Arguments.of("de-ch;q=0.5, *-ch;q=0", "de-ch, fr-ch", ""),
/* checking Locale.filter with *;q=0 in EXTENDED_FILTERING which
* must make filter to return "" empty or no match
*/
Arguments.of("*-ch;q=0.5, *;q=0", "de-ch, fr-ch", ""),
/* checking Locale.filter with *;q=0.6, *-Latn;q=0 in
* EXTENDED_FILTERING
*/
Arguments.of("*;q=0.6, *-Latn;q=0", "de-ch, hi-in, en-Latn",
"de-CH, hi-IN")
);
}

private static List<Locale> generateLocales(String tags) {
Expand Down Expand Up @@ -155,5 +141,4 @@ private static String getLocalesAsString(List<Locale> locales) {

return sb.toString().trim();
}

}
49 changes: 24 additions & 25 deletions test/jdk/java/util/Locale/Bug8135061.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,7 +26,7 @@
* @bug 8135061
* @summary Checks that the Locale.lookup executes properly without throwing
* any exception for some specific language ranges
* @run main Bug8135061
* @run junit Bug8135061
*/

import java.util.Collection;
Expand All @@ -35,47 +35,46 @@
import java.util.Locale;
import java.util.Locale.LanguageRange;

public class Bug8135061 {
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public static void main(String[] args) {
public class Bug8135061 {

/* lookup should run without throwing any exception and
* return null as the language range does not match with the language
* tag
*/
/**
* Lookup should run without throwing any exception and return null as
* the language range does not match with the language tag.
*/
@Test
public void lookupReturnNullTest() {
List<LanguageRange> ranges = LanguageRange.parse("nv");
Collection<Locale> locales = Collections.singleton(Locale.ENGLISH);

try {
Locale match = Locale.lookup(ranges, locales);
if (match != null) {
throw new RuntimeException("Locale.lookup returned non-null: "
+ match);
}
assertNull(match);
} catch (Exception ex) {
throw new RuntimeException("[Locale.lookup failed on language"
+ " range: " + ranges + " and language tags "
+ locales + "]", ex);
}
}

/* lookup should run without throwing any exception and
* return "nv" as the matching tag
*/
ranges = LanguageRange.parse("i-navajo");
locales = Collections.singleton(Locale.of("nv"));

/**
* Lookup should run without throwing any exception and return "nv"
* as the matching tag.
*/
@Test
public void lookupReturnValueTest() {
List<LanguageRange> ranges = LanguageRange.parse("i-navajo");
Collection<Locale> locales = Collections.singleton(Locale.of("nv"));
try {
Locale match = Locale.lookup(ranges, locales);
if (!match.toLanguageTag().equals("nv")) {
throw new RuntimeException("Locale.lookup returned unexpected"
+ " result: " + match);
}
assertEquals(match.toLanguageTag(), "nv");
} catch (Exception ex) {
throw new RuntimeException("[Locale.lookup failed on language"
+ " range: " + ranges + " and language tags "
+ locales + "]", ex);
}

}

}
Loading

0 comments on commit 71cac8c

Please sign in to comment.