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