Path: blob/master/test/jdk/java/util/Calendar/CalendarDataTest.java
41149 views
/*1* Copyright (c) 2017, 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 8190918 8202537 8221432 823127326* @summary Tests for region dependent calendar data, i.e.,27* firstDayOfWeek and minimalDaysInFirstWeek.28* @modules jdk.localedata29* @run main CalendarDataTest30*/3132import java.util.Calendar;33import java.util.List;34import java.util.Locale;35import java.util.Optional;36import java.util.stream.IntStream;3738public class CalendarDataTest {3940// golden data from CLDR41private static final List<List<String>> FIRSTDAYDATA = List.of(42List.of("1", "AG AS AU BD BR BS BT BW BZ CA CN CO DM DO ET GT " +43"GU HK HN ID IL IN JM JP KE KH KR LA MH MM MO MT MX MZ " +44"NI NP PA PE PH PK PR PT PY SA SG SV TH TT TW UM US VE " +45"VI WS YE ZA ZW"),46List.of("2", "001 AD AI AL AM AN AR AT AX AZ BA BE BG BM BN BY " +47"CH CL CM CR CY CZ DE DK EC EE ES FI FJ FO FR GB GE GF GP " +48"GR HR HU IE IS IT KG KZ LB LI LK LT LU LV MC MD ME MK MN MQ " +49"MY NL NO NZ PL RE RO RS RU SE SI SK SM TJ TM TR UA UY UZ " +50"VA VN XK"),51List.of("6", "MV"),52List.of("7", "AE AF BH DJ DZ EG IQ IR JO KW LY OM QA SD SY"));5354private static final List<List<String>> MINDAYSDATA = List.of(55List.of("1", "001 GU UM US VI"),56List.of("4", "AD AN AT AX BE BG CH CZ DE DK EE ES FI FJ FO FR " +57"GB GF GG GI GP GR HU IE IM IS IT JE LI LT LU MC MQ NL NO " +58"PL PT RE RU SE SJ SK SM VA"));5960public static void main(String... args) throws Exception {61// world62Calendar cal = Calendar.getInstance(new Locale("", "001"));63checkResult("001",64cal.getFirstDayOfWeek(),65cal.getMinimalDaysInFirstWeek());6667// two letter country codes68IntStream.range(0x41, 0x5b)69.forEach(c1 -> {70IntStream.range(0x41, 0x5b)71.mapToObj(c2 -> String.valueOf((char)c1) + String.valueOf((char)c2))72.forEach(region -> {73Calendar c = Calendar.getInstance(new Locale("", region));74checkResult(region,75c.getFirstDayOfWeek(),76c.getMinimalDaysInFirstWeek());77});78});79}8081private static void checkResult(String region, int firstDay, int minDays) {82// first day of week83int expected = Integer.parseInt(findEntry(region, FIRSTDAYDATA)84.orElse(findEntry("001", FIRSTDAYDATA).orElse(List.of("1")))85.get(0));86if (firstDay != expected) {87throw new RuntimeException("firstDayOfWeek is incorrect for the region: " +88region + ". Returned: " + firstDay + ", Expected: " + expected);89}9091// minimal days in first week92expected = Integer.parseInt(findEntry(region, MINDAYSDATA)93.orElse(findEntry("001", MINDAYSDATA).orElse(List.of("1")))94.get(0));95if (minDays != expected) {96throw new RuntimeException("minimalDaysInFirstWeek is incorrect for the region: " +97region + ". Returned: " + minDays + ", Expected: " + expected);98}99}100101private static Optional<List<String>> findEntry(String region, List<List<String>> data) {102return data.stream()103.filter(l -> l.get(1).contains(region))104.findAny();105}106}107108109110