Skip to content

Commit d66da41

Browse files
committed
8310234: Refactor Locale tests to use JUnit
Reviewed-by: rschmelter Backport-of: dad7bd9efc7ca2c245f7e35267479670f93a53a8
1 parent 4fb0648 commit d66da41

File tree

9 files changed

+298
-249
lines changed

9 files changed

+298
-249
lines changed

test/jdk/java/util/Locale/Bug4316602.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 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
@@ -20,30 +20,43 @@
2020
* or visit www.oracle.com if you need additional information or have any
2121
* questions.
2222
*/
23-
/**
24-
@test
25-
@summary Locale variant should not be uppercased
26-
@run main Bug4210525
27-
@bug 4210525
28-
*/
23+
24+
/*
25+
* @test
26+
* @bug 4210525
27+
* @summary Locale variant should not be case folded
28+
* @run junit CaseCheckVariant
29+
*/
2930

3031
import java.util.Locale;
32+
import java.util.stream.Stream;
3133

32-
public class Bug4210525 {
34+
import org.junit.jupiter.params.ParameterizedTest;
35+
import org.junit.jupiter.params.provider.MethodSource;
3336

34-
public static void main(String[] args) throws Exception {
35-
String language = "en";
36-
String country = "US";
37-
String variant = "socal";
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
3838

39-
Locale aLocale = new Locale(language, country, variant);
39+
public class CaseCheckVariant {
4040

41+
static final String LANG = "en";
42+
static final String COUNTRY = "US";
43+
44+
/**
45+
* When a locale is created with a given variant, ensure
46+
* that the variant is not case normalized.
47+
*/
48+
@ParameterizedTest
49+
@MethodSource("variants")
50+
public void variantCaseTest(String variant) {
51+
Locale aLocale = new Locale(LANG, COUNTRY, variant);
4152
String localeVariant = aLocale.getVariant();
42-
if (localeVariant.equals(variant)) {
43-
System.out.println("passed");
44-
} else {
45-
System.out.println("failed");
46-
throw new Exception("Bug4210525 test failed.");
47-
}
53+
assertEquals(localeVariant, variant);
54+
}
55+
56+
private static Stream<String> variants() {
57+
return Stream.of(
58+
"socal",
59+
"Norcal"
60+
);
4861
}
4962
}

test/jdk/java/util/Locale/Bug8154797.java renamed to test/jdk/java/util/Locale/CompareProviderFormats.java

Lines changed: 39 additions & 23 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
@@ -28,43 +28,65 @@
2828
* java.base/sun.util.resources
2929
* jdk.localedata
3030
* @summary Test for checking HourFormat and GmtFormat resources are retrieved from
31-
* COMPAT and CLDR Providers.
31+
* COMPAT and CLDR Providers.
32+
* @run junit CompareProviderFormats
3233
*/
3334

3435
import java.util.HashMap;
3536
import java.util.Locale;
3637
import java.util.Map;
3738
import java.util.ResourceBundle;
39+
import java.util.stream.Stream;
40+
3841
import sun.util.locale.provider.LocaleProviderAdapter.Type;
3942
import sun.util.locale.provider.LocaleProviderAdapter;
4043

41-
public class Bug8154797 {
44+
import org.junit.jupiter.api.BeforeAll;
45+
import org.junit.jupiter.params.ParameterizedTest;
46+
import org.junit.jupiter.params.provider.MethodSource;
47+
48+
public class CompareProviderFormats {
4249
static Map<String, String> expectedResourcesMap = new HashMap<>();
4350
static final String GMT_RESOURCE_KEY = "timezone.gmtFormat";
4451
static final String HMT_RESOURCE_KEY = "timezone.hourFormat";
4552
static final String GMT = "Gmt";
4653
static final String HMT = "Hmt";
4754

48-
static void generateExpectedValues() {
55+
/**
56+
* Fill the expectedResourcesMap with the desired key / values
57+
*/
58+
@BeforeAll
59+
static void populateResourcesMap() {
4960
expectedResourcesMap.put("FR" + GMT, "UTC{0}");
5061
expectedResourcesMap.put("FR" + HMT, "+HH:mm;\u2212HH:mm");
5162
expectedResourcesMap.put("FI" + HMT, "+H.mm;-H.mm");
5263
expectedResourcesMap.put("FI" + GMT, "UTC{0}");
53-
/* For root locale, en_US, de_DE, hi_IN, ja_JP,Root locale resources
54-
* should be returned.
64+
/* For root locale, en_US, de_DE, hi_IN, ja_JP, Root locale resources
65+
* should be returned.
5566
*/
56-
expectedResourcesMap.put(GMT, "GMT{0}"); //Root locale resource
57-
expectedResourcesMap.put(HMT, "+HH:mm;-HH:mm"); //Root locale resource
67+
expectedResourcesMap.put(GMT, "GMT{0}"); // Root locale resource
68+
expectedResourcesMap.put(HMT, "+HH:mm;-HH:mm"); // Root locale resource
69+
}
70+
71+
/**
72+
* For each locale, ensure that the returned resources for gmt and hmt match
73+
* the expected resources for both COMPAT and CLDR
74+
*/
75+
@ParameterizedTest
76+
@MethodSource("localeProvider")
77+
public void compareResourcesTest(Locale loc) {
78+
compareResources(loc);
5879
}
5980

60-
static void compareResources(Locale loc) {
81+
private void compareResources(Locale loc) {
6182
String mapKeyHourFormat = HMT, mapKeyGmtFormat = GMT;
6283
ResourceBundle compatBundle, cldrBundle;
6384
compatBundle = LocaleProviderAdapter.forJRE().getLocaleResources(loc)
6485
.getJavaTimeFormatData();
6586
cldrBundle = LocaleProviderAdapter.forType(Type.CLDR)
6687
.getLocaleResources(loc).getJavaTimeFormatData();
67-
if (loc.getCountry() == "FR" || loc.getCountry() == "FI") {
88+
89+
if (loc.getCountry().equals("FR") || loc.getCountry().equals("FI")) {
6890
mapKeyHourFormat = loc.getCountry() + HMT;
6991
mapKeyGmtFormat = loc.getCountry() + GMT;
7092
}
@@ -77,23 +99,17 @@ static void compareResources(Locale loc) {
7799
.equals(cldrBundle.getString(GMT_RESOURCE_KEY))
78100
&& expectedResourcesMap.get(mapKeyHourFormat)
79101
.equals(cldrBundle.getString(HMT_RESOURCE_KEY)))) {
80-
81102
throw new RuntimeException("Retrieved resource does not match with "
82103
+ " expected string for Locale " + compatBundle.getLocale());
83-
84104
}
85-
86105
}
87106

88-
public static void main(String args[]) {
89-
Bug8154797.generateExpectedValues();
90-
Locale[] locArr = {new Locale("hi", "IN"), Locale.UK, new Locale("fi", "FI"),
91-
Locale.ROOT, Locale.GERMAN, Locale.JAPANESE,
92-
Locale.ENGLISH, Locale.FRANCE};
93-
for (Locale loc : locArr) {
94-
Bug8154797.compareResources(loc);
95-
}
107+
private static Stream<Locale> localeProvider() {
108+
return Stream.of(
109+
new Locale("hi", "IN"),
110+
Locale.UK, new Locale("fi", "FI"),
111+
Locale.ROOT, Locale.GERMAN, Locale.JAPANESE,
112+
Locale.ENGLISH, Locale.FRANCE
113+
);
96114
}
97-
98115
}
99-

test/jdk/java/util/Locale/Bug8004240.java renamed to test/jdk/java/util/Locale/GetAdapterPreference.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 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,25 +26,27 @@
2626
* @bug 8004240
2727
* @summary Verify that getAdapterPreference returns an unmodifiable list.
2828
* @modules java.base/sun.util.locale.provider
29-
* @compile -XDignore.symbol.file Bug8004240.java
30-
* @run main Bug8004240
29+
* @compile -XDignore.symbol.file GetAdapterPreference.java
30+
* @run junit GetAdapterPreference
3131
*/
3232

3333
import java.util.List;
3434
import sun.util.locale.provider.LocaleProviderAdapter;
3535

36-
public class Bug8004240 {
36+
import org.junit.jupiter.api.Test;
3737

38-
public static void main(String[] args) {
39-
List<LocaleProviderAdapter.Type> types = LocaleProviderAdapter.getAdapterPreference();
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
4039

41-
try {
42-
types.set(0, null);
43-
} catch (UnsupportedOperationException e) {
44-
// success
45-
return;
46-
}
40+
public class GetAdapterPreference {
4741

48-
throw new RuntimeException("LocaleProviderAdapter.getAdapterPrefence() returned a modifiable list.");
42+
/**
43+
* Test that the list returned from getAdapterPreference()
44+
* cannot be modified.
45+
*/
46+
@Test
47+
public void immutableTest() {
48+
List<LocaleProviderAdapter.Type> types = LocaleProviderAdapter.getAdapterPreference();
49+
assertThrows(UnsupportedOperationException.class, () -> types.set(0, null),
50+
"Trying to modify list returned from LocaleProviderAdapter.getAdapterPreference() did not throw UOE");
4951
}
5052
}

0 commit comments

Comments
 (0)