test/java/util/PluggableLocale/CalendarDataProviderTest.java

Print this page




  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 }


  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  * unusual week parameter values, WEDNESDAY - first day of week, 7 - minimal
  40  * days in the first week.



  41  *
  42  * A Calendar instance created with ja_JP_kids should use the week parameters
  43  * provided by com.bar.CalendarDataProviderImpl.



  44  */
  45 public class CalendarDataProviderTest {
  46 
  47     public static void main(String[] s) {
  48         new CalendarDataProviderTest().test();
  49     }
  50 
  51     void test() {
  52         Locale kids = new Locale("ja", "JP", "kids"); // test provider's supported locale
  53         Calendar kcal = Calendar.getInstance(kids);
  54         Calendar jcal = Calendar.getInstance(Locale.JAPAN);
  55 
  56         // check the week parameters
  57         checkResult("firstDayOfWeek", kcal.getFirstDayOfWeek(), WEDNESDAY);
  58         checkResult("minimalDaysInFirstWeek", kcal.getMinimalDaysInFirstWeek(), 7);







































  59     }
  60 
  61     private <T> void checkResult(String msg, T got, T expected) {
  62         if (!expected.equals(got)) {
  63             String s = String.format("%s: got='%s', expected='%s'", msg, got, expected);
  64             throw new RuntimeException(s);
  65         }
  66     }
  67 }