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,
  90                 "\u0e21.\u0e04.",
  91                 "\u0e01.\u0e1e.",
  92                 "\u0e21\u0e35.\u0e04.",
  93                 "\u0e40\u0e21.\u0e22.",
  94                 "\u0e1e.\u0e04.",
  95                 "\u0e21\u0e34.\u0e22",  // no last dot
  96                 "\u0e01.\u0e04.",
  97                 "\u0e2a.\u0e04.",
  98                 "\u0e01.\u0e22.",
  99                 "\u0e15.\u0e04.",
 100                 "\u0e1e.\u0e22.",
 101                 "\u0e18.\u0e04.");
 102         testMap(THTH, MONTH, NARROW_STANDALONE,
 103                 "\u0e21.\u0e04.",
 104                 "\u0e01.\u0e1e.",
 105                 "\u0e21\u0e35.\u0e04.",
 106                 "\u0e40\u0e21.\u0e22.",
 107                 "\u0e1e.\u0e04.",
 108                 "\u0e21\u0e34.\u0e22.",
 109                 "\u0e01.\u0e04.",
 110                 "\u0e2a.\u0e04.",
 111                 "\u0e01.\u0e22.",
 112                 "\u0e15.\u0e04.",
 113                 "\u0e1e.\u0e22.",
 114                 "\u0e18.\u0e04.");
 115 
 116         if (errors != 0) {
 117             throw new RuntimeException("test failed");
 118         }
 119     }
 120 
 121     private static void test(Locale locale, int field, String expected, int... data) {
 122         test(locale, field, NARROW_FORMAT, expected, data);
 123     }
 124 
 125     private static void test(Locale locale, int field, int style, String expected, int... fieldValuePairs) {
 126         Calendar cal = Calendar.getInstance(locale);
 127         cal.clear();
 128         for (int i = 0; i < fieldValuePairs.length;) {
 129             int f = fieldValuePairs[i++];
 130             int v = fieldValuePairs[i++];
 131             cal.set(f, v);
 132         }
 133         String got = cal.getDisplayName(field, style, locale);
 134         if (!expected.equals(got)) {
 135             System.err.printf("test: locale=%s, field=%d, value=%d, style=%d, got=\"%s\", expected=\"%s\"%n",
 136                               locale, field, cal.get(field), style, got, expected);
 137             errors++;
 138         }
 139     }
 140 
 141     private static void testMap(Locale locale, int field, int style, String... expected) {
 142         Map<String, Integer> expectedMap = null;
 143         if (expected.length > 0) {
 144             expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE);
 145             int index = 0;
 146             for (int i = 0; i < expected.length; i++) {
 147                 if (expected[i].isEmpty()) {
 148                     index++;
 149                     continue;
 150                 }
 151                 if (expected[i] == RESET_INDEX) {
 152                     index = 0;
 153                     continue;
 154                 }
 155                 expectedMap.put(expected[i], index++);
 156             }
 157         }
 158         Calendar cal = Calendar.getInstance(locale);
 159         Map<String, Integer> got = cal.getDisplayNames(field, style, locale);
 160         if (!(expectedMap == null && got == null)
 161             && !(expectedMap != null && expectedMap.equals(got))) {
 162             System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n",
 163                               locale, field, style, expectedMap, got);
 164             errors++;
 165         }
 166     }
 167 
 168     /**
 169      * Comparator implementation for TreeMap which iterates keys from longest
 170      * to shortest.
 171      */
 172     private static class LengthBasedComparator implements Comparator<String> {
 173         private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();
 174 
 175         private LengthBasedComparator() {
 176         }
 177 
 178         @Override
 179         public int compare(String o1, String o2) {
 180             int n = o2.length() - o1.length();
 181             return (n == 0) ? o1.compareTo(o2) : n;
 182         }
 183     }
 184 }