1 /*
   2  * Copyright (c) 2013, 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 8007038
  27  * @summary Verify ArrayIndexOutOfBoundsException is not thrown on
  28  *     on calling localizedDateTime().print() with JapaneseChrono
  29  * @compile -XDignore.symbol.file Bug8007038.java
  30  * @run main Bug8007038
  31  */
  32 
  33 import java.util.*;
  34 import static java.util.Calendar.*;
  35 import sun.util.locale.provider.CalendarDataUtility;
  36 
  37 public class Bug8007038 {
  38     private final static String[] calTypes = {
  39         "gregory",
  40         "buddhist",
  41         "japanese",
  42         "roc",
  43         "islamic",
  44     };
  45     private final static int[][] eraMinMax = {
  46         {GregorianCalendar.BC, GregorianCalendar.AD},
  47         {0, 1},
  48         {0, 4},
  49         {0, 1},
  50         {0, 1},
  51         {0, 1},
  52     };
  53     private final static Locale[] testLocs = {
  54         Locale.ROOT,
  55         Locale.forLanguageTag("ja-JP-u-ca-japanese"),
  56         Locale.forLanguageTag("th-TH"),
  57         Locale.forLanguageTag("th-TH-u-ca-buddhist"),
  58         Locale.forLanguageTag("zh-TW-u-ca-roc"),
  59         Locale.forLanguageTag("ar-EG-u-ca-islamic"),
  60         Locale.forLanguageTag("xx-YY-u-ca-bogus"),
  61     };
  62 
  63     public static void main(String[] args) {
  64         for (int calIdx  = 0; calIdx  < calTypes.length; calIdx++) {
  65             for (int locIdx = 0; locIdx < testLocs.length; locIdx++) {
  66                 // era
  67                 for (int fieldIdx = eraMinMax[calIdx][0]; fieldIdx <= eraMinMax[calIdx][1]; fieldIdx++) {
  68                     checkValueRange(calTypes[calIdx], ERA, fieldIdx, LONG, testLocs[locIdx], true);
  69                 }
  70                 checkValueRange(calTypes[calIdx], ERA, eraMinMax[calIdx][0]-1, LONG,
  71                                 testLocs[locIdx], false);
  72                 checkValueRange(calTypes[calIdx], ERA, eraMinMax[calIdx][1]+1,
  73                                 LONG, testLocs[locIdx], false);
  74 
  75                 // month
  76                 for (int fieldIdx = JANUARY; fieldIdx <= UNDECIMBER ; fieldIdx++) {
  77                     checkValueRange(calTypes[calIdx], MONTH, fieldIdx, LONG, testLocs[locIdx], true);
  78                 }
  79                 checkValueRange(calTypes[calIdx], MONTH, JANUARY-1, LONG, testLocs[locIdx], false);
  80                 checkValueRange(calTypes[calIdx], MONTH, UNDECIMBER+1, LONG, testLocs[locIdx], false);
  81 
  82                 // day-of-week
  83                 for (int fieldIdx = SUNDAY; fieldIdx <= SATURDAY; fieldIdx++) {
  84                     checkValueRange(calTypes[calIdx], DAY_OF_WEEK, fieldIdx, LONG, testLocs[locIdx], true);
  85                 }
  86                 checkValueRange(calTypes[calIdx], DAY_OF_WEEK, SUNDAY-1, LONG, testLocs[locIdx], false);
  87                 checkValueRange(calTypes[calIdx], DAY_OF_WEEK, SATURDAY+1, LONG, testLocs[locIdx], false);
  88 
  89                 // am/pm
  90                 for (int fieldIdx = AM; fieldIdx <= PM; fieldIdx++) {
  91                     checkValueRange(calTypes[calIdx], AM_PM, fieldIdx, LONG, testLocs[locIdx], true);
  92                 }
  93                 checkValueRange(calTypes[calIdx], AM_PM, AM-1, LONG, testLocs[locIdx], false);
  94                 checkValueRange(calTypes[calIdx], AM_PM, PM+1, LONG, testLocs[locIdx], false);
  95             }
  96         }
  97     }
  98 
  99     private static void checkValueRange(String calType, int field, int value, int style, Locale l, boolean isNonNull) {
 100         String ret = CalendarDataUtility.retrieveFieldValueName(calType, field, value, style, l);
 101         System.out.print("retrieveFieldValueName("+calType+", "+field+", "+value+", "+style+", "+l+")");
 102         if ((ret != null) == isNonNull) {
 103             System.out.println(" returned "+ret);
 104         } else {
 105             throw new RuntimeException("The call returned "+ret);
 106         }
 107     }
 108 }