Path: blob/master/test/jdk/java/util/Calendar/NarrowNamesTest.java
41149 views
/*1* Copyright (c) 2012, 2021, 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 8000983 8008577 8247781 826210826* @summary Unit test for narrow names support. This test is locale data-dependent27* and assumes that both COMPAT and CLDR have the same narrow names if not28* explicitly specified.29* @modules jdk.localedata30* @comment Locale providers: COMPAT,SPI31* @run main/othervm -Djava.locale.providers=COMPAT,SPI NarrowNamesTest COMPAT,SPI32* @comment Locale providers: CLDR33* @run main/othervm -Djava.locale.providers=CLDR NarrowNamesTest CLDR34*/3536import java.time.LocalDateTime;37import java.util.Calendar;38import java.util.Comparator;39import java.util.Locale;40import java.util.Map;41import java.util.TreeMap;42import static java.util.GregorianCalendar.*;4344public class NarrowNamesTest {45private static final Locale US = Locale.US;46private static final Locale JAJPJP = new Locale("ja", "JP", "JP");47private static final Locale THTH = new Locale("th", "TH");4849private static final String RESET_INDEX = "RESET_INDEX";5051private static int errors = 0;5253// This test is locale data-dependent.54public static void main(String[] args) {55String providers = args[0];5657test(US, ERA, "B",58ERA, BC, YEAR, 1);59test(US, ERA, "A",60ERA, AD, YEAR, 2012);61test(US, DAY_OF_WEEK, "S",62YEAR, 2012, MONTH, DECEMBER, DAY_OF_MONTH, 23);63test(US, AM_PM, "a",64HOUR_OF_DAY, 10);65test(US, AM_PM, "p",66HOUR_OF_DAY, 23);67test(JAJPJP, DAY_OF_WEEK,68LocalDateTime.now().isBefore(LocalDateTime.of(2019, 5, 1, 0, 0)) ?69"\u65e5" : "\u706b", // "Sun" for HEISEI, "Tue" for REIWA70YEAR, 24, MONTH, DECEMBER, DAY_OF_MONTH, 23);71test(THTH, MONTH, NARROW_STANDALONE, "\u0e18.\u0e04.",72YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5);73test(THTH, DAY_OF_WEEK, "\u0e1e",74YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5);7576testMap(US, DAY_OF_WEEK, ALL_STYLES, // shouldn't include any narrow names77"", // 1-based indexing for DAY_OF_WEEK78"Sunday", // Sunday79"Monday", // Monday80"Tuesday", // Tuesday81"Wednesday", // Wednesday82"Thursday", // Thursday83"Friday", // Friday84"Saturday", // Saturday85RESET_INDEX,86"", // 1-based indexing for DAY_OF_WEEK87"Sun", // abb Sunday88"Mon", // abb Monday89"Tue", // abb Tuesday90"Wed", // abb Wednesday91"Thu", // abb Thursday92"Fri", // abb Friday93"Sat" // abb Saturday94);95testMap(US, DAY_OF_WEEK, NARROW_FORMAT); // expect null96testMap(US, AM_PM, ALL_STYLES,97"AM", "PM",98RESET_INDEX,99"a", "p");100testMap(JAJPJP, DAY_OF_WEEK, NARROW_STANDALONE,101"", // 1-based indexing for DAY_OF_WEEK102"\u65e5",103"\u6708",104"\u706b",105"\u6c34",106"\u6728",107"\u91d1",108"\u571f");109testMap(JAJPJP, DAY_OF_WEEK, NARROW_FORMAT,110"", // 1-based indexing for DAY_OF_WEEK111"\u65e5",112"\u6708",113"\u706b",114"\u6c34",115"\u6728",116"\u91d1",117"\u571f");118testMap(THTH, MONTH, NARROW_FORMAT,119"\u0e21.\u0e04.",120"\u0e01.\u0e1e.",121"\u0e21\u0e35.\u0e04.",122"\u0e40\u0e21.\u0e22.",123"\u0e1e.\u0e04.",124(providers.startsWith("CLDR") ?125"\u0e21\u0e34.\u0e22." :126"\u0e21\u0e34.\u0e22"), // no last dot in JRE127"\u0e01.\u0e04.",128"\u0e2a.\u0e04.",129"\u0e01.\u0e22.",130"\u0e15.\u0e04.",131"\u0e1e.\u0e22.",132"\u0e18.\u0e04.");133testMap(THTH, MONTH, NARROW_STANDALONE,134"\u0e21.\u0e04.",135"\u0e01.\u0e1e.",136"\u0e21\u0e35.\u0e04.",137"\u0e40\u0e21.\u0e22.",138"\u0e1e.\u0e04.",139"\u0e21\u0e34.\u0e22.",140"\u0e01.\u0e04.",141"\u0e2a.\u0e04.",142"\u0e01.\u0e22.",143"\u0e15.\u0e04.",144"\u0e1e.\u0e22.",145"\u0e18.\u0e04.");146147if (errors != 0) {148throw new RuntimeException("test failed");149}150}151152private static void test(Locale locale, int field, String expected, int... data) {153test(locale, field, NARROW_FORMAT, expected, data);154}155156private static void test(Locale locale, int field, int style, String expected, int... fieldValuePairs) {157Calendar cal = Calendar.getInstance(locale);158cal.clear();159for (int i = 0; i < fieldValuePairs.length;) {160int f = fieldValuePairs[i++];161int v = fieldValuePairs[i++];162cal.set(f, v);163}164String got = cal.getDisplayName(field, style, locale);165if (!expected.equals(got)) {166System.err.printf("test: locale=%s, field=%d, value=%d, style=%d, got=\"%s\", expected=\"%s\"%n",167locale, field, cal.get(field), style, got, expected);168errors++;169}170}171172private static void testMap(Locale locale, int field, int style, String... expected) {173Map<String, Integer> expectedMap = null;174if (expected.length > 0) {175expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE);176int index = 0;177for (String s : expected) {178if (s.isEmpty()) {179index++;180continue;181}182if (s == RESET_INDEX) {183index = 0;184continue;185}186expectedMap.put(s, index++);187}188}189Calendar cal = Calendar.getInstance(locale);190Map<String, Integer> got = cal.getDisplayNames(field, style, locale);191if (!(expectedMap == null && got == null)192&& !(expectedMap != null && expectedMap.equals(got))) {193System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n",194locale, field, style, expectedMap, got);195errors++;196}197}198199/**200* Comparator implementation for TreeMap which iterates keys from longest201* to shortest.202*/203private static class LengthBasedComparator implements Comparator<String> {204private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();205206private LengthBasedComparator() {207}208209@Override210public int compare(String o1, String o2) {211int n = o2.length() - o1.length();212return (n == 0) ? o1.compareTo(o2) : n;213}214}215}216217218