Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Locale/bcp47u/FormatTests.java
41153 views
1
/*
2
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
*
26
* @test
27
* @bug 8176841 8194148
28
* @summary Tests *Format class deals with Unicode extensions
29
* correctly.
30
* @modules jdk.localedata
31
* @run testng/othervm -Djava.locale.providers=CLDR FormatTests
32
*/
33
34
import static org.testng.Assert.assertEquals;
35
36
import java.text.DateFormat;
37
import java.text.NumberFormat;
38
import java.util.Calendar;
39
import java.util.Date;
40
import java.util.Locale;
41
import java.util.TimeZone;
42
43
import org.testng.annotations.AfterTest;
44
import org.testng.annotations.BeforeTest;
45
import org.testng.annotations.DataProvider;
46
import org.testng.annotations.Test;
47
48
/**
49
* Test *Format classes with BCP47 U extensions
50
*/
51
@Test
52
public class FormatTests {
53
private static TimeZone defaultTZ;
54
55
private static final TimeZone ASIATOKYO = TimeZone.getTimeZone("Asia/Tokyo");
56
private static final TimeZone AMLA = TimeZone.getTimeZone("America/Los_Angeles");
57
58
private static final Locale JPTYO = Locale.forLanguageTag("en-u-tz-jptyo");
59
private static final Locale JCAL = Locale.forLanguageTag("en-u-ca-japanese");
60
private static final Locale USLAX = Locale.forLanguageTag("en-u-tz-uslax");
61
62
private static final Locale RG_GB = Locale.forLanguageTag("en-US-u-rg-gbzzzz");
63
private static final Locale RG_DE = Locale.forLanguageTag("en-US-u-rg-dezzzz");
64
65
private static final Locale NU_DEVA = Locale.forLanguageTag("en-US-u-nu-deva");
66
private static final Locale NU_SINH = Locale.forLanguageTag("en-US-u-nu-sinh");
67
private static final Locale NU_ZZZZ = Locale.forLanguageTag("en-US-u-nu-zzzz");
68
69
private static final double testNum = 12345.6789;
70
private static final String NUM_US = "12,345.6789";
71
private static final String NUM_DE = "12.345,6789";
72
private static final String NUM_DEVA = "\u0967\u0968,\u0969\u096a\u096b.\u096c\u096d\u096e\u096f";
73
private static final String NUM_SINH = "\u0de7\u0de8,\u0de9\u0dea\u0deb.\u0dec\u0ded\u0dee\u0def";
74
75
private static final Date testDate = new Calendar.Builder()
76
.setCalendarType("gregory")
77
.setDate(2017, 7, 10)
78
.setTimeOfDay(15, 15, 0)
79
.setTimeZone(AMLA)
80
.build()
81
.getTime();
82
83
@BeforeTest
84
public void beforeTest() {
85
defaultTZ = TimeZone.getDefault();
86
TimeZone.setDefault(AMLA);
87
}
88
89
@AfterTest
90
public void afterTest() {
91
TimeZone.setDefault(defaultTZ);
92
}
93
94
@DataProvider(name="dateFormatData")
95
Object[][] dateFormatData() {
96
return new Object[][] {
97
// Locale, Expected calendar, Expected timezone, Expected formatted string
98
99
// -ca
100
{JCAL, "java.util.JapaneseImperialCalendar", null,
101
"Thursday, August 10, 29 Heisei at 3:15:00 PM Pacific Daylight Time"
102
},
103
104
// -tz
105
{JPTYO, null, ASIATOKYO,
106
"Friday, August 11, 2017 at 7:15:00 AM Japan Standard Time"
107
},
108
{USLAX, null, AMLA,
109
"Thursday, August 10, 2017 at 3:15:00 PM Pacific Daylight Time"
110
},
111
112
// -rg
113
{RG_GB, null, null,
114
"Thursday, 10 August 2017 at 15:15:00 Pacific Daylight Time"
115
},
116
};
117
}
118
119
@DataProvider(name="numberFormatData")
120
Object[][] numberFormatData() {
121
return new Object[][] {
122
// Locale, number, expected format
123
124
// -nu
125
{NU_DEVA, testNum, NUM_DEVA},
126
{NU_SINH, testNum, NUM_SINH},
127
{NU_ZZZZ, testNum, NUM_US},
128
129
// -rg
130
{RG_DE, testNum, NUM_DE},
131
132
// -nu & -rg, valid & invalid
133
{Locale.forLanguageTag("en-US-u-nu-deva-rg-dezzzz"), testNum, NUM_DEVA},
134
{Locale.forLanguageTag("en-US-u-nu-zzzz-rg-dezzzz"), testNum, NUM_US},
135
{Locale.forLanguageTag("en-US-u-nu-zzzz-rg-zzzz"), testNum, NUM_US},
136
};
137
}
138
139
@Test(dataProvider="dateFormatData")
140
public void test_DateFormat(Locale locale, String calClass, TimeZone tz,
141
String formatExpected) throws Exception {
142
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
143
if (calClass != null) {
144
try {
145
Class expected = Class.forName(calClass);
146
assertEquals(df.getCalendar().getClass(), expected);
147
} catch (Exception e) {
148
throw e;
149
}
150
}
151
if (tz != null) {
152
assertEquals(df.getTimeZone(), tz);
153
}
154
String formatted = df.format(testDate);
155
assertEquals(formatted, formatExpected);
156
assertEquals(df.parse(formatted), testDate);
157
}
158
159
@Test(dataProvider="numberFormatData")
160
public void test_NumberFormat(Locale locale, double num,
161
String formatExpected) throws Exception {
162
NumberFormat nf = NumberFormat.getNumberInstance(locale);
163
nf.setMaximumFractionDigits(4);
164
String formatted = nf.format(num);
165
assertEquals(nf.format(num), formatExpected);
166
assertEquals(nf.parse(formatted), num);
167
}
168
}
169
170