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