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
@@ -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
@@ -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
@@ -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);
}

}

}

5 comments on commit 71cac8c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JesperIRL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/tag jdk-22+7

@openjdk
Copy link

@openjdk openjdk bot commented on 71cac8c Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JesperIRL The tag jdk-22+7 was successfully created.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 71cac8c Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 71cac8c Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch backport-GoeLin-71cac8ce in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 71cac8ce from the openjdk/jdk repository.

The commit being backported was authored by Justin Lu on 19 Jul 2023 and was reviewed by Naoto Sato.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-71cac8ce:backport-GoeLin-71cac8ce
$ git checkout backport-GoeLin-71cac8ce
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-71cac8ce

Please sign in to comment.