Skip to content

Commit 26ee525

Browse files
committed
Backport 71cac8ce47b69a2b78d54cbceb0f0510e5ea4cdc
1 parent 96100ad commit 26ee525

File tree

10 files changed

+378
-383
lines changed

10 files changed

+378
-383
lines changed

test/jdk/java/util/Locale/Bug6989440.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,7 @@
2828
* thread accesses.
2929
* @modules java.base/sun.util.locale.provider
3030
* @compile -XDignore.symbol.file=true Bug6989440.java
31-
* @run main Bug6989440
31+
* @run junit Bug6989440
3232
*/
3333
import java.text.spi.DateFormatProvider;
3434
import java.util.spi.LocaleNameProvider;
@@ -37,11 +37,17 @@
3737

3838
import sun.util.locale.provider.LocaleServiceProviderPool;
3939

40+
import org.junit.jupiter.api.Test;
41+
4042
public class Bug6989440 {
4143
static volatile boolean failed; // false
4244
static final int THREADS = 50;
4345

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

61-
public TestThread(Class<? extends LocaleServiceProvider> providerClass) {
62-
cls = providerClass;
63-
}
64-
6567
public TestThread() {
6668
int which = count++ % 3;
6769
switch (which) {
68-
case 0 : cls = LocaleNameProvider.class; break;
69-
case 1 : cls = TimeZoneNameProvider.class; break;
70-
case 2 : cls = DateFormatProvider.class; break;
71-
default : throw new AssertionError("Should not reach here");
70+
case 0 -> cls = LocaleNameProvider.class;
71+
case 1 -> cls = TimeZoneNameProvider.class;
72+
case 2 -> cls = DateFormatProvider.class;
73+
default -> throw new AssertionError("Should not reach here");
7274
}
7375
}
7476

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,104 +25,90 @@
2525
* @bug 8035133
2626
* @summary Checks that the tags matching the range with quality weight q=0
2727
* e.g. en;q=0 must be elimited and must not be the part of output
28+
* @run junit Bug8035133
2829
*/
2930

3031
import java.util.ArrayList;
3132
import java.util.Iterator;
3233
import java.util.List;
3334
import java.util.Locale;
35+
import java.util.stream.Stream;
3436

37+
import org.junit.jupiter.params.ParameterizedTest;
38+
import org.junit.jupiter.params.provider.Arguments;
39+
import org.junit.jupiter.params.provider.MethodSource;
3540

36-
public class Bug8035133 {
37-
38-
private static boolean err = false;
39-
40-
public static void main(String[] args) {
41-
42-
// checking Locale.lookup with de-ch;q=0
43-
checkLookup("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
44-
"de-ch, en, fr-ch", "fr-CH");
45-
46-
/* checking Locale.lookup with *;q=0 '*' should be ignored
47-
* in lookup
48-
*/
49-
checkLookup("en;q=0.1, *-ch;q=0.5, *;q=0",
50-
"de-ch, en, fr-ch", "de-CH");
51-
52-
// checking Locale.filter with fr-ch;q=0 in BASIC_FILTERING
53-
checkFilter("en;q=0.1, fr-ch;q=0.0, de-ch;q=0.5",
54-
"de-ch, en, fr-ch", "de-CH, en");
55-
56-
// checking Locale.filter with *;q=0 in BASIC_FILTERING
57-
checkFilter("de-ch;q=0.6, *;q=0", "de-ch, fr-ch", "");
58-
59-
// checking Locale.filter with *;q=0 in BASIC_FILTERING
60-
checkFilter("de-ch;q=0.6, de;q=0", "de-ch", "");
61-
62-
// checking Locale.filter with *;q=0.6, en;q=0 in BASIC_FILTERING
63-
checkFilter("*;q=0.6, en;q=0", "de-ch, hi-in, en", "de-CH, hi-IN");
64-
65-
// checking Locale.filter with de-ch;q=0 in EXTENDED_FILTERING
66-
checkFilter("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
67-
"de-ch, en, fr-ch", "fr-CH, en");
68-
69-
/* checking Locale.filter with *-ch;q=0 in EXTENDED_FILTERING which
70-
* must make filter to return "" empty or no match
71-
*/
72-
checkFilter("de-ch;q=0.5, *-ch;q=0", "de-ch, fr-ch", "");
73-
74-
/* checking Locale.filter with *;q=0 in EXTENDED_FILTERING which
75-
* must make filter to return "" empty or no match
76-
*/
77-
checkFilter("*-ch;q=0.5, *;q=0", "de-ch, fr-ch", "");
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
7842

79-
/* checking Locale.filter with *;q=0.6, *-Latn;q=0 in
80-
* EXTENDED_FILTERING
81-
*/
82-
checkFilter("*;q=0.6, *-Latn;q=0", "de-ch, hi-in, en-Latn",
83-
"de-CH, hi-IN");
84-
85-
if (err) {
86-
throw new RuntimeException("[LocaleMatcher method(s) failed]");
87-
}
88-
89-
}
43+
public class Bug8035133 {
9044

91-
private static void checkLookup(String ranges, String tags,
45+
// Ensure weights with 'q=0' work as expected during lookup
46+
@ParameterizedTest
47+
@MethodSource("lookupProvider")
48+
public void lookupTest(String ranges, String tags,
9249
String expectedLocale) {
9350

9451
List<Locale.LanguageRange> priorityList = Locale.LanguageRange
9552
.parse(ranges);
9653
List<Locale> localeList = generateLocales(tags);
9754
Locale loc = Locale.lookup(priorityList, localeList);
98-
String actualLocale
99-
= loc.toLanguageTag();
100-
101-
if (!actualLocale.equals(expectedLocale)) {
102-
System.err.println("Locale.lookup failed with ranges: " + ranges
103-
+ " Expected: " + expectedLocale
104-
+ " Actual: " + actualLocale);
105-
err = true;
106-
}
55+
String actualLocale = loc.toLanguageTag();
56+
assertEquals(expectedLocale, actualLocale);
57+
}
10758

59+
private static Stream<Arguments> lookupProvider() {
60+
return Stream.of(
61+
// checking Locale.lookup with de-ch;q=0
62+
Arguments.of("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
63+
"de-ch, en, fr-ch", "fr-CH"),
64+
// checking Locale.lookup with *;q=0 '*' should be ignored in lookup
65+
Arguments.of("en;q=0.1, *-ch;q=0.5, *;q=0",
66+
"de-ch, en, fr-ch", "de-CH")
67+
);
10868
}
10969

110-
private static void checkFilter(String ranges, String tags,
70+
// Ensure weights with 'q=0' work as expected during filtering
71+
@ParameterizedTest
72+
@MethodSource("filterProvider")
73+
public void filterTest(String ranges, String tags,
11174
String expectedLocales) {
11275

11376
List<Locale.LanguageRange> priorityList = Locale.LanguageRange
11477
.parse(ranges);
11578
List<Locale> localeList = generateLocales(tags);
11679
String actualLocales = getLocalesAsString(
11780
Locale.filter(priorityList, localeList));
81+
assertEquals(expectedLocales, actualLocales);
82+
}
11883

119-
if (!actualLocales.equals(expectedLocales)) {
120-
System.err.println("Locale.filter failed with ranges: " + ranges
121-
+ " Expected: " + expectedLocales
122-
+ " Actual: " + actualLocales);
123-
err = true;
124-
}
125-
84+
private static Stream<Arguments> filterProvider() {
85+
return Stream.of(
86+
// checking Locale.filter with fr-ch;q=0 in BASIC_FILTERING
87+
Arguments.of("en;q=0.1, fr-ch;q=0.0, de-ch;q=0.5",
88+
"de-ch, en, fr-ch", "de-CH, en"),
89+
// checking Locale.filter with *;q=0 in BASIC_FILTERING
90+
Arguments.of("de-ch;q=0.6, *;q=0", "de-ch, fr-ch", ""),
91+
// checking Locale.filter with *;q=0 in BASIC_FILTERING
92+
Arguments.of("de-ch;q=0.6, de;q=0", "de-ch", ""),
93+
// checking Locale.filter with *;q=0.6, en;q=0 in BASIC_FILTERING
94+
Arguments.of("*;q=0.6, en;q=0", "de-ch, hi-in, en", "de-CH, hi-IN"),
95+
// checking Locale.filter with de-ch;q=0 in EXTENDED_FILTERING
96+
Arguments.of("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
97+
"de-ch, en, fr-ch", "fr-CH, en"),
98+
/* checking Locale.filter with *-ch;q=0 in EXTENDED_FILTERING which
99+
* must make filter to return "" empty or no match
100+
*/
101+
Arguments.of("de-ch;q=0.5, *-ch;q=0", "de-ch, fr-ch", ""),
102+
/* checking Locale.filter with *;q=0 in EXTENDED_FILTERING which
103+
* must make filter to return "" empty or no match
104+
*/
105+
Arguments.of("*-ch;q=0.5, *;q=0", "de-ch, fr-ch", ""),
106+
/* checking Locale.filter with *;q=0.6, *-Latn;q=0 in
107+
* EXTENDED_FILTERING
108+
*/
109+
Arguments.of("*;q=0.6, *-Latn;q=0", "de-ch, hi-in, en-Latn",
110+
"de-CH, hi-IN")
111+
);
126112
}
127113

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

156142
return sb.toString().trim();
157143
}
158-
159144
}
Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@
2626
* @bug 8135061
2727
* @summary Checks that the Locale.lookup executes properly without throwing
2828
* any exception for some specific language ranges
29-
* @run main Bug8135061
29+
* @run junit Bug8135061
3030
*/
3131

3232
import java.util.Collection;
@@ -35,47 +35,46 @@
3535
import java.util.Locale;
3636
import java.util.Locale.LanguageRange;
3737

38-
public class Bug8135061 {
38+
import org.junit.jupiter.api.Test;
39+
40+
import static org.junit.jupiter.api.Assertions.assertEquals;
41+
import static org.junit.jupiter.api.Assertions.assertNull;
3942

40-
public static void main(String[] args) {
43+
public class Bug8135061 {
4144

42-
/* lookup should run without throwing any exception and
43-
* return null as the language range does not match with the language
44-
* tag
45-
*/
45+
/**
46+
* Lookup should run without throwing any exception and return null as
47+
* the language range does not match with the language tag.
48+
*/
49+
@Test
50+
public void lookupReturnNullTest() {
4651
List<LanguageRange> ranges = LanguageRange.parse("nv");
4752
Collection<Locale> locales = Collections.singleton(Locale.ENGLISH);
48-
4953
try {
5054
Locale match = Locale.lookup(ranges, locales);
51-
if (match != null) {
52-
throw new RuntimeException("Locale.lookup returned non-null: "
53-
+ match);
54-
}
55+
assertNull(match);
5556
} catch (Exception ex) {
5657
throw new RuntimeException("[Locale.lookup failed on language"
5758
+ " range: " + ranges + " and language tags "
5859
+ locales + "]", ex);
5960
}
61+
}
6062

61-
/* lookup should run without throwing any exception and
62-
* return "nv" as the matching tag
63-
*/
64-
ranges = LanguageRange.parse("i-navajo");
65-
locales = Collections.singleton(new Locale("nv"));
66-
63+
/**
64+
* Lookup should run without throwing any exception and return "nv"
65+
* as the matching tag.
66+
*/
67+
@Test
68+
public void lookupReturnValueTest() {
69+
List<LanguageRange> ranges = LanguageRange.parse("i-navajo");
70+
Collection<Locale> locales = Collections.singleton(new Locale("nv"));
6771
try {
6872
Locale match = Locale.lookup(ranges, locales);
69-
if (!match.toLanguageTag().equals("nv")) {
70-
throw new RuntimeException("Locale.lookup returned unexpected"
71-
+ " result: " + match);
72-
}
73+
assertEquals(match.toLanguageTag(), "nv");
7374
} catch (Exception ex) {
7475
throw new RuntimeException("[Locale.lookup failed on language"
7576
+ " range: " + ranges + " and language tags "
7677
+ locales + "]", ex);
7778
}
78-
7979
}
80-
8180
}

0 commit comments

Comments
 (0)