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.CalendarDataProviderImpl;
  33 
  34 /**
  35  * Test case for CalendarDataProvider.
  36  *
  37  * Test strategy:
  38  * com.bar.CalendarDataProviderImpl 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, and also returns unusual week parameter values, WEDNESDAY
  41  * - first day of week, 7 - minimal days in the first week.  The standalone
  42  * styles are used because DateFormatSymbols has precedence for the format
  43  * styles.
  44  *
  45  * A Calendar instance created with ja_JP_kids should use the week parameters
  46  * provided by com.bar.CalendarDataProviderImpl. Calendar.getDisplayName(s)
  47  * should be called with kids to get the month names provided by
  48  * com.bar.CalendarDataProviderImpl. Other display names should be the same as
  49  * what a Calendar constructed with ja_JP returns.
  50  */
  51 public class CalendarDataProviderTest {
  52 
  53     public static void main(String[] s) {
  54         new CalendarDataProviderTest().test();
  55     }
  56 
  57     void test() {
  58         Locale kids = new Locale("ja", "JP", "kids"); // test provider's supported locale
  59         Calendar kcal = Calendar.getInstance(kids);
  60         Calendar jcal = Calendar.getInstance(Locale.JAPAN);
  61 
  62         // check the week parameters
  63         checkResult("firstDayOfWeek", kcal.getFirstDayOfWeek(), WEDNESDAY);
  64         checkResult("minimalDaysInFirstWeek", kcal.getMinimalDaysInFirstWeek(), 7);
  65 
  66         // check month names and week day names
  67         Map<String, Integer> mapAllStyles = new HashMap<>();
  68         for (int style : new int[] { SHORT_STANDALONE, LONG_STANDALONE }) {
  69             // Check month names provided by com.bar.CalendarDataProviderImpl
  70             Map<String, Integer> map = new HashMap<>();
  71             for (int month = JANUARY; month <= DECEMBER; month++) {
  72                 kcal.set(DAY_OF_MONTH, 1);
  73                 kcal.set(MONTH, month);
  74                 kcal.set(HOUR_OF_DAY, 12); // avoid any standard-daylight transitions...
  75                 kcal.getTimeInMillis();
  76                 String name = kcal.getDisplayName(MONTH, style, kids);
  77                 checkResult("Month name",
  78                             name,
  79                             CalendarDataProviderImpl.toMonthName(kcal.get(MONTH) + 1, style));
  80 
  81                 // Builds the map with name to its integer value.
  82                 map.put(name, kcal.get(MONTH));
  83             }
  84             checkResult((style == SHORT_STANDALONE ? "Short" : "Long") + " month names map",
  85                         kcal.getDisplayNames(MONTH, style, kids), map);
  86             mapAllStyles.putAll(map);
  87             if (style == LONG_STANDALONE) {
  88                 checkResult("Short and long month names map",
  89                             kcal.getDisplayNames(MONTH, ALL_STYLES, kids), mapAllStyles);
  90             }
  91 
  92             // Check week names: kcal and jcal should return the same names and maps.
  93             for (int dow = SUNDAY; dow <= SATURDAY; dow++) {
  94                 kcal.set(DAY_OF_WEEK, dow);
  95                 jcal.setTimeInMillis(kcal.getTimeInMillis());
  96                 String name = kcal.getDisplayName(DAY_OF_WEEK, style, kids);
  97                 checkResult("Day of week name", name,
  98                                                 jcal.getDisplayName(DAY_OF_WEEK, style, Locale.JAPAN));
  99             }
 100             checkResult("Short day of week names", kcal.getDisplayNames(DAY_OF_WEEK, style, kids),
 101                                                    jcal.getDisplayNames(DAY_OF_WEEK, style, Locale.JAPAN));
 102         }
 103 
 104     }
 105 
 106     private <T> void checkResult(String msg, T got, T expected) {
 107         if (!expected.equals(got)) {
 108             String s = String.format("%s: got='%s', expected='%s'", msg, got, expected);
 109             throw new RuntimeException(s);
 110         }
 111     }
 112 }