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