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