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  *
  25  */
  26 
  27 import java.text.*;
  28 import java.util.*;
  29 import static java.util.Calendar.*;
  30 import sun.util.locale.provider.*;
  31 import sun.util.resources.*;
  32 import com.bar.CalendarNameProviderImpl;
  33 
  34 /**
  35  * Test case for CalendarNameProvider.
  36  *
  37  * Test strategy:
  38  * com.bar.CalendarNameProviderImpl supports only ja_JP_kids locale. It returns
  39  * month names only in full-width digits, followed by "gatsu" in Hiragana if
  40  * it's a long style. The standalone styles are used because DateFormatSymbols
  41  * has precedence for the format styles.
  42  *
  43  * Calendar.getDisplayName(s) should be called with kids to get the month
  44  * names provided by com.bar.CalendarNameProviderImpl. Other display names
  45  * should be the same as what a Calendar constructed with ja_JP returns.
  46  */
  47 public class CalendarNameProviderTest {
  48 
  49     public static void main(String[] s) {
  50         new CalendarNameProviderTest().test();
  51     }
  52 
  53     void test() {
  54         Locale kids = new Locale("ja", "JP", "kids"); // test provider's supported locale
  55         Calendar kcal = Calendar.getInstance(kids);
  56         Calendar jcal = Calendar.getInstance(Locale.JAPAN);
  57 
  58         // check month names and week day names
  59         Map<String, Integer> mapAllStyles = new HashMap<>();
  60         for (int style : new int[] { SHORT_STANDALONE, LONG_STANDALONE }) {
  61             // Check month names provided by com.bar.CalendarNameProviderImpl
  62             Map<String, Integer> map = new HashMap<>();
  63             for (int month = JANUARY; month <= DECEMBER; month++) {
  64                 kcal.set(DAY_OF_MONTH, 1);
  65                 kcal.set(MONTH, month);
  66                 kcal.set(HOUR_OF_DAY, 12); // avoid any standard-daylight transitions...
  67                 kcal.getTimeInMillis();
  68                 String name = kcal.getDisplayName(MONTH, style, kids);
  69                 checkResult("Month name",
  70                             name,
  71                             CalendarNameProviderImpl.toMonthName(kcal.get(MONTH) + 1, style));
  72 
  73                 // Builds the map with name to its integer value.
  74                 map.put(name, kcal.get(MONTH));
  75             }
  76             checkResult((style == SHORT_STANDALONE ? "Short" : "Long") + " month names map",
  77                         kcal.getDisplayNames(MONTH, style, kids), map);
  78             mapAllStyles.putAll(map);
  79             if (style == LONG_STANDALONE) {
  80                 checkResult("Short and long month names map",
  81                             kcal.getDisplayNames(MONTH, ALL_STYLES, kids), mapAllStyles);
  82             }
  83 
  84             // Check week names: kcal and jcal should return the same names and maps.
  85             for (int dow = SUNDAY; dow <= SATURDAY; dow++) {
  86                 kcal.set(DAY_OF_WEEK, dow);
  87                 jcal.setTimeInMillis(kcal.getTimeInMillis());
  88                 String name = kcal.getDisplayName(DAY_OF_WEEK, style, kids);
  89                 checkResult("Day of week name", name,
  90                                                 jcal.getDisplayName(DAY_OF_WEEK, style, Locale.JAPAN));
  91             }
  92             checkResult("Short day of week names", kcal.getDisplayNames(DAY_OF_WEEK, style, kids),
  93                                                    jcal.getDisplayNames(DAY_OF_WEEK, style, Locale.JAPAN));
  94         }
  95 
  96     }
  97 
  98     private <T> void checkResult(String msg, T got, T expected) {
  99         if (!expected.equals(got)) {
 100             String s = String.format("%s: got='%s', expected='%s'", msg, got, expected);
 101             throw new RuntimeException(s);
 102         }
 103     }
 104 }