1 /* 2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8000983 8008577 27 * @summary Unit test for narrow names support. This test is locale data-dependent 28 * and assumes that both JRE and CLDR have the same narrow names. 29 * @modules jdk.localedata 30 * @comment Locale providers: JRE,SPI 31 * @run main/othervm -Djava.locale.providers=JRE,SPI NarrowNamesTest JRE,SPI 32 * @comment Locale providers: CLDR 33 * @run main/othervm -Djava.locale.providers=CLDR NarrowNamesTest CLDR 34 */ 35 36 import java.time.LocalDateTime; 37 import java.util.Calendar; 38 import java.util.Comparator; 39 import java.util.Locale; 40 import java.util.Map; 41 import java.util.TreeMap; 42 import static java.util.GregorianCalendar.*; 43 44 public class NarrowNamesTest { 45 private static final Locale US = Locale.US; 46 private static final Locale JAJPJP = new Locale("ja", "JP", "JP"); 47 private static final Locale THTH = new Locale("th", "TH"); 48 49 private static final String RESET_INDEX = "RESET_INDEX"; 50 51 private static int errors = 0; 52 53 private static String providers; 54 55 // This test is locale data-dependent. 56 public static void main(String[] args) { 57 providers = args[0]; 58 59 test(US, ERA, "B", 60 ERA, BC, YEAR, 1); 61 test(US, ERA, "A", 62 ERA, AD, YEAR, 2012); 63 test(US, DAY_OF_WEEK, "S", 64 YEAR, 2012, MONTH, DECEMBER, DAY_OF_MONTH, 23); 65 test(US, AM_PM, "a", 66 HOUR_OF_DAY, 10); 67 test(US, AM_PM, "p", 68 HOUR_OF_DAY, 23); 69 test(JAJPJP, DAY_OF_WEEK, 70 LocalDateTime.now().isBefore(LocalDateTime.of(2019, 5, 1, 0, 0)) ? 71 "\u65e5" : "\u706b", // "Sun" for HEISEI, "Tue" for NEWERA 72 YEAR, 24, MONTH, DECEMBER, DAY_OF_MONTH, 23); 73 test(THTH, MONTH, NARROW_STANDALONE, "\u0e18.\u0e04.", 74 YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5); 75 test(THTH, DAY_OF_WEEK, "\u0e1e", 76 YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5); 77 78 testMap(US, DAY_OF_WEEK, ALL_STYLES, // shouldn't include any narrow names 79 "", // 1-based indexing for DAY_OF_WEEK 80 "Sunday", // Sunday 81 "Monday", // Monday 82 "Tuesday", // Tuesday 83 "Wednesday", // Wednesday 84 "Thursday", // Thursday 85 "Friday", // Friday 86 "Saturday", // Saturday 87 RESET_INDEX, 88 "", // 1-based indexing for DAY_OF_WEEK 89 "Sun", // abb Sunday 90 "Mon", // abb Monday 91 "Tue", // abb Tuesday 92 "Wed", // abb Wednesday 93 "Thu", // abb Thursday 94 "Fri", // abb Friday 95 "Sat" // abb Saturday 96 ); 97 testMap(US, DAY_OF_WEEK, NARROW_FORMAT); // expect null 98 testMap(US, AM_PM, ALL_STYLES, 99 "AM", "PM", 100 RESET_INDEX, 101 "a", "p"); 102 testMap(JAJPJP, DAY_OF_WEEK, NARROW_STANDALONE, 103 "", // 1-based indexing for DAY_OF_WEEK 104 "\u65e5", 105 "\u6708", 106 "\u706b", 107 "\u6c34", 108 "\u6728", 109 "\u91d1", 110 "\u571f"); 111 testMap(JAJPJP, DAY_OF_WEEK, NARROW_FORMAT, 112 "", // 1-based indexing for DAY_OF_WEEK 113 "\u65e5", 114 "\u6708", 115 "\u706b", 116 "\u6c34", 117 "\u6728", 118 "\u91d1", 119 "\u571f"); 120 testMap(THTH, MONTH, NARROW_FORMAT, 121 "\u0e21.\u0e04.", 122 "\u0e01.\u0e1e.", 123 "\u0e21\u0e35.\u0e04.", 124 "\u0e40\u0e21.\u0e22.", 125 "\u0e1e.\u0e04.", 126 (providers.startsWith("CLDR") ? 127 "\u0e21\u0e34.\u0e22." : 128 "\u0e21\u0e34.\u0e22"), // no last dot in JRE 129 "\u0e01.\u0e04.", 130 "\u0e2a.\u0e04.", 131 "\u0e01.\u0e22.", 132 "\u0e15.\u0e04.", 133 "\u0e1e.\u0e22.", 134 "\u0e18.\u0e04."); 135 testMap(THTH, MONTH, NARROW_STANDALONE, 136 "\u0e21.\u0e04.", 137 "\u0e01.\u0e1e.", 138 "\u0e21\u0e35.\u0e04.", 139 "\u0e40\u0e21.\u0e22.", 140 "\u0e1e.\u0e04.", 141 "\u0e21\u0e34.\u0e22.", 142 "\u0e01.\u0e04.", 143 "\u0e2a.\u0e04.", 144 "\u0e01.\u0e22.", 145 "\u0e15.\u0e04.", 146 "\u0e1e.\u0e22.", 147 "\u0e18.\u0e04."); 148 149 if (errors != 0) { 150 throw new RuntimeException("test failed"); 151 } 152 } 153 154 private static void test(Locale locale, int field, String expected, int... data) { 155 test(locale, field, NARROW_FORMAT, expected, data); 156 } 157 158 private static void test(Locale locale, int field, int style, String expected, int... fieldValuePairs) { 159 Calendar cal = Calendar.getInstance(locale); 160 cal.clear(); 161 for (int i = 0; i < fieldValuePairs.length;) { 162 int f = fieldValuePairs[i++]; 163 int v = fieldValuePairs[i++]; 164 cal.set(f, v); 165 } 166 String got = cal.getDisplayName(field, style, locale); 167 if (!expected.equals(got)) { 168 System.err.printf("test: locale=%s, field=%d, value=%d, style=%d, got=\"%s\", expected=\"%s\"%n", 169 locale, field, cal.get(field), style, got, expected); 170 errors++; 171 } 172 } 173 174 private static void testMap(Locale locale, int field, int style, String... expected) { 175 Map<String, Integer> expectedMap = null; 176 if (expected.length > 0) { 177 expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE); 178 int index = 0; 179 for (int i = 0; i < expected.length; i++) { 180 if (expected[i].isEmpty()) { 181 index++; 182 continue; 183 } 184 if (expected[i] == RESET_INDEX) { 185 index = 0; 186 continue; 187 } 188 expectedMap.put(expected[i], index++); 189 } 190 } 191 Calendar cal = Calendar.getInstance(locale); 192 Map<String, Integer> got = cal.getDisplayNames(field, style, locale); 193 if (!(expectedMap == null && got == null) 194 && !(expectedMap != null && expectedMap.equals(got))) { 195 System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n", 196 locale, field, style, expectedMap, got); 197 errors++; 198 } 199 } 200 201 /** 202 * Comparator implementation for TreeMap which iterates keys from longest 203 * to shortest. 204 */ 205 private static class LengthBasedComparator implements Comparator<String> { 206 private static final LengthBasedComparator INSTANCE = new LengthBasedComparator(); 207 208 private LengthBasedComparator() { 209 } 210 211 @Override 212 public int compare(String o1, String o2) { 213 int n = o2.length() - o1.length(); 214 return (n == 0) ? o1.compareTo(o2) : n; 215 } 216 } 217 }