1 /* 2 * Copyright (c) 2012, 2015, 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 import java.util.*; 25 import static java.util.GregorianCalendar.*; 26 27 public class NarrowNamesTest { 28 private static final Locale US = Locale.US; 29 private static final Locale JAJPJP = new Locale("ja", "JP", "JP"); 30 private static final Locale THTH = new Locale("th", "TH"); 31 32 private static final String RESET_INDEX = "RESET_INDEX"; 33 34 private static int errors = 0; 35 36 private static String providers; 37 38 // This test is locale data-dependent. 39 public static void main(String[] args) { 40 providers = args[0]; 41 42 test(US, ERA, "B", 43 ERA, BC, YEAR, 1); 44 test(US, ERA, "A", 45 ERA, AD, YEAR, 2012); 46 test(US, DAY_OF_WEEK, "S", 47 YEAR, 2012, MONTH, DECEMBER, DAY_OF_MONTH, 23); 48 test(US, AM_PM, "a", 49 HOUR_OF_DAY, 10); 50 test(US, AM_PM, "p", 51 HOUR_OF_DAY, 23); 52 test(JAJPJP, DAY_OF_WEEK, "\u65e5", 53 YEAR, 24, MONTH, DECEMBER, DAY_OF_MONTH, 23); 54 test(THTH, MONTH, NARROW_STANDALONE, "\u0e18.\u0e04.", 55 YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5); 56 test(THTH, DAY_OF_WEEK, "\u0e1e", 57 YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5); 58 59 testMap(US, DAY_OF_WEEK, ALL_STYLES, // shouldn't include any narrow names 60 "", // 1-based indexing for DAY_OF_WEEK 61 "Sunday", // Sunday 62 "Monday", // Monday 63 "Tuesday", // Tuesday 64 "Wednesday", // Wednesday 65 "Thursday", // Thursday 66 "Friday", // Friday 67 "Saturday", // Saturday 68 RESET_INDEX, 69 "", // 1-based indexing for DAY_OF_WEEK 70 "Sun", // abb Sunday 71 "Mon", // abb Monday 72 "Tue", // abb Tuesday 73 "Wed", // abb Wednesday 74 "Thu", // abb Thursday 75 "Fri", // abb Friday 76 "Sat" // abb Saturday 77 ); 78 testMap(US, DAY_OF_WEEK, NARROW_FORMAT); // expect null 79 testMap(US, AM_PM, ALL_STYLES, 80 "AM", "PM", 81 RESET_INDEX, 82 "a", "p"); 83 testMap(JAJPJP, DAY_OF_WEEK, NARROW_STANDALONE, 84 "", // 1-based indexing for DAY_OF_WEEK 85 "\u65e5", 86 "\u6708", 87 "\u706b", 88 "\u6c34", 89 "\u6728", 90 "\u91d1", 91 "\u571f"); 92 testMap(JAJPJP, DAY_OF_WEEK, NARROW_FORMAT, 93 "", // 1-based indexing for DAY_OF_WEEK 94 "\u65e5", 95 "\u6708", 96 "\u706b", 97 "\u6c34", 98 "\u6728", 99 "\u91d1", 100 "\u571f"); 101 testMap(THTH, MONTH, NARROW_FORMAT, 102 "\u0e21.\u0e04.", 103 "\u0e01.\u0e1e.", 104 "\u0e21\u0e35.\u0e04.", 105 "\u0e40\u0e21.\u0e22.", 106 "\u0e1e.\u0e04.", 107 (providers.startsWith("CLDR") ? 108 "\u0e21\u0e34.\u0e22." : 109 "\u0e21\u0e34.\u0e22"), // no last dot in JRE 110 "\u0e01.\u0e04.", 111 "\u0e2a.\u0e04.", 112 "\u0e01.\u0e22.", 113 "\u0e15.\u0e04.", 114 "\u0e1e.\u0e22.", 115 "\u0e18.\u0e04."); 116 testMap(THTH, MONTH, NARROW_STANDALONE, 117 "\u0e21.\u0e04.", 118 "\u0e01.\u0e1e.", 119 "\u0e21\u0e35.\u0e04.", 120 "\u0e40\u0e21.\u0e22.", 121 "\u0e1e.\u0e04.", 122 "\u0e21\u0e34.\u0e22.", 123 "\u0e01.\u0e04.", 124 "\u0e2a.\u0e04.", 125 "\u0e01.\u0e22.", 126 "\u0e15.\u0e04.", 127 "\u0e1e.\u0e22.", 128 "\u0e18.\u0e04."); 129 130 if (errors != 0) { 131 throw new RuntimeException("test failed"); 132 } 133 } 134 135 private static void test(Locale locale, int field, String expected, int... data) { 136 test(locale, field, NARROW_FORMAT, expected, data); 137 } 138 139 private static void test(Locale locale, int field, int style, String expected, int... fieldValuePairs) { 140 Calendar cal = Calendar.getInstance(locale); 141 cal.clear(); 142 for (int i = 0; i < fieldValuePairs.length;) { 143 int f = fieldValuePairs[i++]; 144 int v = fieldValuePairs[i++]; 145 cal.set(f, v); 146 } 147 String got = cal.getDisplayName(field, style, locale); 148 if (!expected.equals(got)) { 149 System.err.printf("test: locale=%s, field=%d, value=%d, style=%d, got=\"%s\", expected=\"%s\"%n", 150 locale, field, cal.get(field), style, got, expected); 151 errors++; 152 } 153 } 154 155 private static void testMap(Locale locale, int field, int style, String... expected) { 156 Map<String, Integer> expectedMap = null; 157 if (expected.length > 0) { 158 expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE); 159 int index = 0; 160 for (int i = 0; i < expected.length; i++) { 161 if (expected[i].isEmpty()) { 162 index++; 163 continue; 164 } 165 if (expected[i] == RESET_INDEX) { 166 index = 0; 167 continue; 168 } 169 expectedMap.put(expected[i], index++); 170 } 171 } 172 Calendar cal = Calendar.getInstance(locale); 173 Map<String, Integer> got = cal.getDisplayNames(field, style, locale); 174 if (!(expectedMap == null && got == null) 175 && !(expectedMap != null && expectedMap.equals(got))) { 176 System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n", 177 locale, field, style, expectedMap, got); 178 errors++; 179 } 180 } 181 182 /** 183 * Comparator implementation for TreeMap which iterates keys from longest 184 * to shortest. 185 */ 186 private static class LengthBasedComparator implements Comparator<String> { 187 private static final LengthBasedComparator INSTANCE = new LengthBasedComparator(); 188 189 private LengthBasedComparator() { 190 } 191 192 @Override 193 public int compare(String o1, String o2) { 194 int n = o2.length() - o1.length(); 195 return (n == 0) ? o1.compareTo(o2) : n; 196 } 197 } 198 }