Path: blob/master/test/jdk/java/util/Calendar/JapanEraNameCompatTest.java
41149 views
/*1* Copyright (c) 2019, 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* @test25* @bug 821878126* @summary Test the localized names of Japanese era Reiwa from COMPAT provider.27* @modules jdk.localedata28* @run testng/othervm -Djava.locale.providers=COMPAT JapanEraNameCompatTest29*/3031import static java.util.Calendar.*;32import static java.util.Locale.*;3334import java.time.LocalDate;35import java.time.chrono.JapaneseChronology;36import java.time.chrono.JapaneseEra;37import java.time.format.DateTimeFormatter;38import java.time.format.DateTimeParseException;39import java.time.format.TextStyle;40import java.util.Calendar;41import java.util.Locale;4243import org.testng.annotations.DataProvider;44import org.testng.annotations.Test;45import static org.testng.Assert.assertEquals;4647@Test48public class JapanEraNameCompatTest {49static final Calendar c = new Calendar.Builder()50.setCalendarType("japanese")51.setFields(ERA, 5, YEAR, 1, MONTH, MAY, DAY_OF_MONTH, 1)52.build();53static final String EngName = "Reiwa";54static final String CJName = "\u4ee4\u548c";55static final String KoreanName = "\ub808\uc774\uc640";56static final String ArabicName = "\u0631\u064a\u0648\u0627";57static final String ThaiName = "\u0e40\u0e23\u0e27\u0e30";58static final String HindiName = "\u0930\u0947\u0907\u0935\u093e";59static final String RussianName = "\u0420\u044d\u0439\u0432\u0430";60static final String SerbianName = "\u0420\u0435\u0438\u0432\u0430";61static final String SerbLatinName = "Reiva";6263@DataProvider(name="UtilCalendar")64Object[][] dataUtilCalendar() {65return new Object[][] {66//locale, long, short67{ JAPAN, CJName, "R" },68{ KOREAN, KoreanName, "R" },69{ CHINA, CJName, "R" },70{ TAIWAN, CJName, "R" }, // fallback to zh71{ new Locale("ar"), ArabicName, ArabicName },72{ new Locale("th"), ThaiName, "R" },73// hi_IN fallback to root74{ new Locale("hi", "IN"), EngName, "R" }75};76}7778@Test(dataProvider="UtilCalendar")79public void testCalendarEraDisplayName(Locale locale,80String longName, String shortName) {81assertEquals(c.getDisplayName(ERA, LONG, locale), longName);82assertEquals(c.getDisplayName(ERA, SHORT, locale), shortName);83}8485@DataProvider(name="JavaTime")86Object[][] dataJavaTime() {87return new Object[][] {88// locale, full, short89{ JAPAN, CJName, CJName },90{ KOREAN, KoreanName, KoreanName },91{ CHINA, CJName, CJName },92{ TAIWAN, CJName, CJName },93{ new Locale("ar"), ArabicName, ArabicName },94{ new Locale("th"), ThaiName, ThaiName },95{ new Locale("hi", "IN"), HindiName, HindiName },96{ new Locale("ru"), RussianName, RussianName },97{ new Locale("sr"), SerbianName, SerbianName },98{ Locale.forLanguageTag("sr-Latn"), SerbLatinName, SerbLatinName },99{ new Locale("hr"), EngName, EngName },100{ new Locale("in"), EngName, EngName },101{ new Locale("lt"), EngName, EngName },102{ new Locale("nl"), EngName, EngName },103{ new Locale("no"), EngName, "R" },104{ new Locale("sv"), EngName, EngName },105// el fallback to root106{ new Locale("el"), EngName, EngName }107};108}109110@Test(dataProvider="JavaTime")111public void testChronoJapanEraDisplayName(Locale locale,112String fullName, String shortName) {113114JapaneseEra era = JapaneseEra.valueOf("Reiwa");115assertEquals(era.getDisplayName(TextStyle.FULL, locale), fullName);116assertEquals(era.getDisplayName(TextStyle.SHORT, locale), shortName);117assertEquals(era.getDisplayName(TextStyle.NARROW, locale), "R");118}119120@Test121public void testFormatParseEraName() {122LocalDate date = LocalDate.of(2019, 5, 1);123DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd GGGG");124formatter = formatter.withChronology(JapaneseChronology.INSTANCE);125126int num = 0;127for (Locale locale : Calendar.getAvailableLocales()) {128formatter = formatter.withLocale(locale);129try {130LocalDate.parse(date.format(formatter), formatter);131} catch (DateTimeParseException e) {132// If an array is defined for Japanese eras in java.time resource,133// but an era entry is missing, format fallback to English name134// while parse throw DateTimeParseException.135num++;136System.out.println("Missing java.time resource data for locale: " + locale);137}138}139if (num > 0) {140throw new RuntimeException("Missing java.time data for " + num + " locales");141}142}143}144145146