Path: blob/master/test/jdk/java/util/Calendar/Bug8007038.java
41149 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8007038 824778126* @summary Verify ArrayIndexOutOfBoundsException is not thrown on27* on calling localizedDateTime().print() with JapaneseChrono28* @modules java.base/sun.util.locale.provider29* @modules jdk.localedata30* @compile -XDignore.symbol.file Bug8007038.java31* @run main/othervm -Djava.locale.providers=COMPAT Bug8007038 COMPAT32* @run main/othervm -Djava.locale.providers=CLDR Bug8007038 CLDR33*/3435import java.util.*;36import static java.util.Calendar.*;37import sun.util.locale.provider.CalendarDataUtility;3839public class Bug8007038 {40private static final String[] calTypes = {41"gregory",42"buddhist",43"japanese",44"roc",45"islamic",46};47private static final int[][] eraMinMax = {48{GregorianCalendar.BC, GregorianCalendar.AD},49{0, 1},50{0, 5},51{0, 1},52{0, 1},53{0, 1},54};55private static final Locale[] testLocs = {56Locale.ROOT,57Locale.forLanguageTag("ja-JP-u-ca-japanese"),58Locale.forLanguageTag("th-TH"),59Locale.forLanguageTag("th-TH-u-ca-buddhist"),60Locale.forLanguageTag("zh-TW-u-ca-roc"),61Locale.forLanguageTag("ar-EG-u-ca-islamic"),62Locale.forLanguageTag("xx-YY-u-ca-bogus"),63};6465public static void main(String[] args) {66for (int calIdx = 0; calIdx < calTypes.length; calIdx++) {67for (int locIdx = 0; locIdx < testLocs.length; locIdx++) {68// era69for (int fieldIdx = eraMinMax[calIdx][0]; fieldIdx <= eraMinMax[calIdx][1]; fieldIdx++) {70checkValueRange(calTypes[calIdx], ERA, fieldIdx, LONG, testLocs[locIdx], true);71}72checkValueRange(calTypes[calIdx], ERA, eraMinMax[calIdx][0]-1, LONG,73testLocs[locIdx], false);74checkValueRange(calTypes[calIdx], ERA, eraMinMax[calIdx][1]+1,75LONG, testLocs[locIdx], false);7677// month78for (int fieldIdx = JANUARY; fieldIdx <= UNDECIMBER ; fieldIdx++) {79checkValueRange(calTypes[calIdx], MONTH, fieldIdx, LONG, testLocs[locIdx], true);80}81checkValueRange(calTypes[calIdx], MONTH, JANUARY-1, LONG, testLocs[locIdx], false);82checkValueRange(calTypes[calIdx], MONTH, UNDECIMBER+1, LONG, testLocs[locIdx], false);8384// day-of-week85for (int fieldIdx = SUNDAY; fieldIdx <= SATURDAY; fieldIdx++) {86checkValueRange(calTypes[calIdx], DAY_OF_WEEK, fieldIdx, LONG, testLocs[locIdx], true);87}88checkValueRange(calTypes[calIdx], DAY_OF_WEEK, SUNDAY-1, LONG, testLocs[locIdx], false);89checkValueRange(calTypes[calIdx], DAY_OF_WEEK, SATURDAY+1, LONG, testLocs[locIdx], false);9091// am/pm92int lastIndex = args[0].equals("CLDR") ? 11 : PM;93for (int fieldIdx = AM; fieldIdx <= lastIndex; fieldIdx++) {94checkValueRange(calTypes[calIdx], AM_PM, fieldIdx, LONG, testLocs[locIdx], true);95}96checkValueRange(calTypes[calIdx], AM_PM, AM-1, LONG, testLocs[locIdx], false);97checkValueRange(calTypes[calIdx], AM_PM, lastIndex+1, LONG, testLocs[locIdx], false);98}99}100}101102private static void checkValueRange(String calType, int field, int value, int style, Locale l, boolean isNonNull) {103String ret = CalendarDataUtility.retrieveJavaTimeFieldValueName(calType, field, value, style, l);104System.out.print("retrieveFieldValueName("+calType+", "+field+", "+value+", "+style+", "+l+")");105if ((ret != null) == isNonNull) {106System.out.println(" returned "+ret);107} else {108throw new RuntimeException("The call returned "+ret);109}110}111}112113114