Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/CalendarNameProviderTest.java
47216 views
/*1* Copyright (c) 2012, 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*/22/*23*24*/2526import java.text.*;27import java.util.*;28import static java.util.Calendar.*;29import sun.util.locale.provider.*;30import sun.util.resources.*;31import com.bar.CalendarNameProviderImpl;3233/**34* Test case for CalendarNameProvider.35*36* Test strategy:37* com.bar.CalendarNameProviderImpl supports only ja_JP_kids locale. It returns38* month names only in full-width digits, followed by "gatsu" in Hiragana if39* it's a long style. The standalone styles are used because DateFormatSymbols40* has precedence for the format styles.41*42* Calendar.getDisplayName(s) should be called with kids to get the month43* names provided by com.bar.CalendarNameProviderImpl. Other display names44* should be the same as what a Calendar constructed with ja_JP returns.45*/46public class CalendarNameProviderTest {4748public static void main(String[] s) {49new CalendarNameProviderTest().test();50}5152void test() {53Locale kids = new Locale("ja", "JP", "kids"); // test provider's supported locale54Calendar kcal = Calendar.getInstance(kids);55Calendar jcal = Calendar.getInstance(Locale.JAPAN);5657// check month names and week day names58Map<String, Integer> mapAllStyles = new HashMap<>();59for (int style : new int[] { SHORT_STANDALONE, LONG_STANDALONE }) {60// Check month names provided by com.bar.CalendarNameProviderImpl61Map<String, Integer> map = new HashMap<>();62for (int month = JANUARY; month <= DECEMBER; month++) {63kcal.set(DAY_OF_MONTH, 1);64kcal.set(MONTH, month);65kcal.set(HOUR_OF_DAY, 12); // avoid any standard-daylight transitions...66kcal.getTimeInMillis();67String name = kcal.getDisplayName(MONTH, style, kids);68checkResult("Month name",69name,70CalendarNameProviderImpl.toMonthName(kcal.get(MONTH) + 1, style));7172// Builds the map with name to its integer value.73map.put(name, kcal.get(MONTH));74}75checkResult((style == SHORT_STANDALONE ? "Short" : "Long") + " month names map",76kcal.getDisplayNames(MONTH, style, kids), map);77mapAllStyles.putAll(map);78if (style == LONG_STANDALONE) {79checkResult("Short and long month names map",80kcal.getDisplayNames(MONTH, ALL_STYLES, kids), mapAllStyles);81}8283// Check week names: kcal and jcal should return the same names and maps.84for (int dow = SUNDAY; dow <= SATURDAY; dow++) {85kcal.set(DAY_OF_WEEK, dow);86jcal.setTimeInMillis(kcal.getTimeInMillis());87String name = kcal.getDisplayName(DAY_OF_WEEK, style, kids);88checkResult("Day of week name", name,89jcal.getDisplayName(DAY_OF_WEEK, style, Locale.JAPAN));90}91checkResult("Short day of week names", kcal.getDisplayNames(DAY_OF_WEEK, style, kids),92jcal.getDisplayNames(DAY_OF_WEEK, style, Locale.JAPAN));93}9495}9697private <T> void checkResult(String msg, T got, T expected) {98if (!expected.equals(got)) {99String s = String.format("%s: got='%s', expected='%s'", msg, got, expected);100throw new RuntimeException(s);101}102}103}104105106