Path: blob/master/test/jdk/java/util/Locale/bcp47u/FormatTests.java
41153 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25* @test26* @bug 8176841 819414827* @summary Tests *Format class deals with Unicode extensions28* correctly.29* @modules jdk.localedata30* @run testng/othervm -Djava.locale.providers=CLDR FormatTests31*/3233import static org.testng.Assert.assertEquals;3435import java.text.DateFormat;36import java.text.NumberFormat;37import java.util.Calendar;38import java.util.Date;39import java.util.Locale;40import java.util.TimeZone;4142import org.testng.annotations.AfterTest;43import org.testng.annotations.BeforeTest;44import org.testng.annotations.DataProvider;45import org.testng.annotations.Test;4647/**48* Test *Format classes with BCP47 U extensions49*/50@Test51public class FormatTests {52private static TimeZone defaultTZ;5354private static final TimeZone ASIATOKYO = TimeZone.getTimeZone("Asia/Tokyo");55private static final TimeZone AMLA = TimeZone.getTimeZone("America/Los_Angeles");5657private static final Locale JPTYO = Locale.forLanguageTag("en-u-tz-jptyo");58private static final Locale JCAL = Locale.forLanguageTag("en-u-ca-japanese");59private static final Locale USLAX = Locale.forLanguageTag("en-u-tz-uslax");6061private static final Locale RG_GB = Locale.forLanguageTag("en-US-u-rg-gbzzzz");62private static final Locale RG_DE = Locale.forLanguageTag("en-US-u-rg-dezzzz");6364private static final Locale NU_DEVA = Locale.forLanguageTag("en-US-u-nu-deva");65private static final Locale NU_SINH = Locale.forLanguageTag("en-US-u-nu-sinh");66private static final Locale NU_ZZZZ = Locale.forLanguageTag("en-US-u-nu-zzzz");6768private static final double testNum = 12345.6789;69private static final String NUM_US = "12,345.6789";70private static final String NUM_DE = "12.345,6789";71private static final String NUM_DEVA = "\u0967\u0968,\u0969\u096a\u096b.\u096c\u096d\u096e\u096f";72private static final String NUM_SINH = "\u0de7\u0de8,\u0de9\u0dea\u0deb.\u0dec\u0ded\u0dee\u0def";7374private static final Date testDate = new Calendar.Builder()75.setCalendarType("gregory")76.setDate(2017, 7, 10)77.setTimeOfDay(15, 15, 0)78.setTimeZone(AMLA)79.build()80.getTime();8182@BeforeTest83public void beforeTest() {84defaultTZ = TimeZone.getDefault();85TimeZone.setDefault(AMLA);86}8788@AfterTest89public void afterTest() {90TimeZone.setDefault(defaultTZ);91}9293@DataProvider(name="dateFormatData")94Object[][] dateFormatData() {95return new Object[][] {96// Locale, Expected calendar, Expected timezone, Expected formatted string9798// -ca99{JCAL, "java.util.JapaneseImperialCalendar", null,100"Thursday, August 10, 29 Heisei at 3:15:00 PM Pacific Daylight Time"101},102103// -tz104{JPTYO, null, ASIATOKYO,105"Friday, August 11, 2017 at 7:15:00 AM Japan Standard Time"106},107{USLAX, null, AMLA,108"Thursday, August 10, 2017 at 3:15:00 PM Pacific Daylight Time"109},110111// -rg112{RG_GB, null, null,113"Thursday, 10 August 2017 at 15:15:00 Pacific Daylight Time"114},115};116}117118@DataProvider(name="numberFormatData")119Object[][] numberFormatData() {120return new Object[][] {121// Locale, number, expected format122123// -nu124{NU_DEVA, testNum, NUM_DEVA},125{NU_SINH, testNum, NUM_SINH},126{NU_ZZZZ, testNum, NUM_US},127128// -rg129{RG_DE, testNum, NUM_DE},130131// -nu & -rg, valid & invalid132{Locale.forLanguageTag("en-US-u-nu-deva-rg-dezzzz"), testNum, NUM_DEVA},133{Locale.forLanguageTag("en-US-u-nu-zzzz-rg-dezzzz"), testNum, NUM_US},134{Locale.forLanguageTag("en-US-u-nu-zzzz-rg-zzzz"), testNum, NUM_US},135};136}137138@Test(dataProvider="dateFormatData")139public void test_DateFormat(Locale locale, String calClass, TimeZone tz,140String formatExpected) throws Exception {141DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);142if (calClass != null) {143try {144Class expected = Class.forName(calClass);145assertEquals(df.getCalendar().getClass(), expected);146} catch (Exception e) {147throw e;148}149}150if (tz != null) {151assertEquals(df.getTimeZone(), tz);152}153String formatted = df.format(testDate);154assertEquals(formatted, formatExpected);155assertEquals(df.parse(formatted), testDate);156}157158@Test(dataProvider="numberFormatData")159public void test_NumberFormat(Locale locale, double num,160String formatExpected) throws Exception {161NumberFormat nf = NumberFormat.getNumberInstance(locale);162nf.setMaximumFractionDigits(4);163String formatted = nf.format(num);164assertEquals(nf.format(num), formatExpected);165assertEquals(nf.parse(formatted), num);166}167}168169170