Path: blob/master/test/jdk/java/util/PluggableLocale/CalendarNameProviderTest.java
41149 views
/*1* Copyright (c) 2012, 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* @test25* @bug 8000986 8062588 821040626* @summary CalendarNameProvider tests27* @library providersrc/foobarutils28* providersrc/barprovider29* @build com.foobar.Utils30* com.bar.*31* @run main/othervm -Djava.locale.providers=JRE,SPI CalendarNameProviderTest32*/3334import java.util.Calendar;35import java.util.HashMap;36import java.util.Locale;37import java.util.Map;3839import com.bar.CalendarNameProviderImpl;4041import static java.util.Calendar.ALL_STYLES;42import static java.util.Calendar.DAY_OF_MONTH;43import static java.util.Calendar.DAY_OF_WEEK;44import static java.util.Calendar.DECEMBER;45import static java.util.Calendar.HOUR_OF_DAY;46import static java.util.Calendar.JANUARY;47import static java.util.Calendar.LONG_STANDALONE;48import static java.util.Calendar.MONTH;49import static java.util.Calendar.SATURDAY;50import static java.util.Calendar.SHORT_STANDALONE;51import static java.util.Calendar.SUNDAY;5253/**54* Test case for CalendarNameProvider.55*56* Test strategy:57* com.bar.CalendarNameProviderImpl supports only ja_JP_kids locale. It returns58* month names only in full-width digits, followed by "gatsu" in Hiragana if59* it's a long style. The standalone styles are used because DateFormatSymbols60* has precedence for the format styles.61*62* Calendar.getDisplayName(s) should be called with kids to get the month63* names provided by com.bar.CalendarNameProviderImpl. Other display names64* should be the same as what a Calendar constructed with ja_JP returns.65*/66public class CalendarNameProviderTest {6768public static void main(String[] s) {69new CalendarNameProviderTest().test();70}7172void test() {73Locale kids = new Locale("ja", "JP", "kids"); // test provider's supported locale74Calendar kcal = Calendar.getInstance(kids);75Calendar jcal = Calendar.getInstance(Locale.JAPAN);7677// check month names and week day names78Map<String, Integer> mapAllStyles = new HashMap<>();79for (int style : new int[] { SHORT_STANDALONE, LONG_STANDALONE }) {80// Check month names provided by com.bar.CalendarNameProviderImpl81Map<String, Integer> map = new HashMap<>();82for (int month = JANUARY; month <= DECEMBER; month++) {83kcal.set(DAY_OF_MONTH, 1);84kcal.set(MONTH, month);85kcal.set(HOUR_OF_DAY, 12); // avoid any standard-daylight transitions...86kcal.getTimeInMillis();87String name = kcal.getDisplayName(MONTH, style, kids);88checkResult("Month name",89name,90CalendarNameProviderImpl.toMonthName(kcal.get(MONTH) + 1, style));9192// Builds the map with name to its integer value.93map.put(name, kcal.get(MONTH));94}95checkResult((style == SHORT_STANDALONE ? "Short" : "Long") + " month names map",96kcal.getDisplayNames(MONTH, style, kids), map);97mapAllStyles.putAll(map);98if (style == LONG_STANDALONE) {99checkResult("Short and long month names map",100kcal.getDisplayNames(MONTH, ALL_STYLES, kids), mapAllStyles);101}102103// Check week names: kcal and jcal should return the same names and maps.104for (int dow = SUNDAY; dow <= SATURDAY; dow++) {105kcal.set(DAY_OF_WEEK, dow);106jcal.setTimeInMillis(kcal.getTimeInMillis());107String name = kcal.getDisplayName(DAY_OF_WEEK, style, kids);108checkResult("Day of week name", name,109jcal.getDisplayName(DAY_OF_WEEK, style, Locale.JAPAN));110}111checkResult("Short day of week names", kcal.getDisplayNames(DAY_OF_WEEK, style, kids),112jcal.getDisplayNames(DAY_OF_WEEK, style, Locale.JAPAN));113}114115}116117private <T> void checkResult(String msg, T got, T expected) {118if (!expected.equals(got)) {119String s = String.format("%s: got='%s', expected='%s'", msg, got, expected);120throw new RuntimeException(s);121}122}123}124125