Path: blob/master/test/jdk/java/util/Calendar/BuddhistCalendarTest.java
41149 views
/*1* Copyright (c) 2003, 2016, 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 4817812 4847186 4956227 495647926* @summary Confirm that BuddhistCalendar's add(), roll() and toString() work correctly with Buddhist Era years.27*/2829import java.util.Calendar;30import java.util.GregorianCalendar;31import java.util.Locale;32import static java.util.Calendar.*;3334public class BuddhistCalendarTest {3536private static final Locale THAI_LOCALE = new Locale("th", "TH");3738public static void main(String[] args) {39testAddRoll();40testToString();41testException();42testLeastMax();43}4445/**46* 481781247*/48static void testAddRoll() {49Calendar cal;50int base, year;5152/*53* Test: BuddhistCalendar.add(YEAR)54*/55cal = getBuddhistCalendar();56base = cal.get(YEAR);57cal.add(YEAR, 1);58year = cal.get(YEAR);59check(year, base+1, "add(+YEAR)");6061cal = getBuddhistCalendar();62base = cal.get(YEAR);63cal.add(YEAR, -3);64year = cal.get(YEAR);65check(year, base-3, "add(-YEAR)");6667/*68* Test BuddhistCalendar.add(MONTH)69*/70cal = getBuddhistCalendar();71base = cal.get(YEAR);72cal.set(MONTH, DECEMBER);73cal.add(MONTH, 2);74year = cal.get(YEAR);75check(year, base+1, "add(+MONTH)");7677cal = getBuddhistCalendar();78base = cal.get(YEAR);79cal.set(MONTH, FEBRUARY);80cal.add(MONTH, -4);81year = cal.get(YEAR);82check(year, base-1, "add(-MONTH)");8384/*85* Test BuddhistCalendar.roll(YEAR)86*/87cal = getBuddhistCalendar();88base = cal.get(YEAR);89cal.roll(YEAR, 2);90year = cal.get(YEAR);91check(year, base+2, "roll(+YEAR)");9293cal = getBuddhistCalendar();94base = cal.get(YEAR);95cal.roll(YEAR, -4);96year = cal.get(YEAR);97check(year, base-4, "roll(-YEAR)");9899/*100* Test BuddhistCalendar.roll(WEEK_OF_YEAR)101*/102cal = getBuddhistCalendar();103cal.set(YEAR, 2543); // A.D.2000104cal.set(MONTH, DECEMBER);105cal.set(DATE, 31);106base = cal.get(YEAR);107check(base, 2543, "roll(+WEEK_OF_YEAR)");108cal.roll(WEEK_OF_YEAR, 10);109year = cal.get(YEAR);110check(year, base, "roll(+WEEK_OF_YEAR)");111112cal = getBuddhistCalendar();113cal.set(YEAR, 2543); // A.D.2000114cal.set(MONTH, JANUARY);115cal.set(DATE, 1);116base = cal.get(YEAR);117check(base, 2543, "roll(+WEEK_OF_YEAR)");118cal.roll(WEEK_OF_YEAR, -10);119year = cal.get(YEAR);120check(year, base, "roll(-WEEK_OF_YEAR)");121122/*123* Test Calendar.set(year, month, date)124*/125cal = getBuddhistCalendar();126base = cal.get(YEAR);127cal.set(3001, APRIL, 10);128year = cal.get(YEAR);129check(year, 3001, "set(year, month, date)");130131/*132* Test Calendar.set(year, month, date, hour, minute)133*/134cal = getBuddhistCalendar();135base = cal.get(YEAR);136cal.set(3020, MAY, 20, 9, 10);137year = cal.get(YEAR);138check(year, 3020, "set(year, month, date, hour, minute)");139140/*141* Test Calendar.set(year, month, date, hour, minute, second)142*/143cal = getBuddhistCalendar();144base = cal.get(YEAR);145cal.set(3120, MAY, 20, 9, 10, 52);146year = cal.get(YEAR);147check(year, 3120, "set(year, month, date, hour, minute, second)");148149/*150* Test BuddhistCalendar.getActualMaximum(YEAR);151* set(YEAR)/get(YEAR) in this method doesn't affect the real152* YEAR value because a clone is used with set()&get().153*/154cal = getBuddhistCalendar();155base = cal.get(YEAR);156int limit = cal.getActualMaximum(YEAR);157year = cal.get(YEAR);158check(year, base, "BuddhistCalendar.getActualMaximum(YEAR)");159160/*161* Test BuddhistCalendar.getActualMinimum(YEAR);162* This doesn't call set(YEAR) nor get(YEAR), though.163*/164cal = getBuddhistCalendar();165base = cal.get(YEAR);166limit = cal.getActualMinimum(YEAR);167year = cal.get(YEAR);168check(year, base, "BuddhistCalendar.getActualMinimum(YEAR)");169}170171/**172* 4847186: BuddhistCalendar: toString() returns Gregorian year173*/174static void testToString() {175Calendar cal = getBuddhistCalendar();176int year = cal.get(YEAR);177String s = cal.toString();178String y = s.replaceAll(".+,YEAR=(\\d+),.+", "$1");179if (Integer.parseInt(y) != year) {180throw new RuntimeException("toString(): wrong year value: got " + y181+ ", expected " + year);182}183}184185/**186* 4956479: BuddhistCalendar methods may return wrong values after exception187*/188static void testException() {189Calendar cal = getBuddhistCalendar();190int year = cal.get(YEAR);191boolean exceptionOccurred = false;192try {193cal.add(100, +1); // cause exception194} catch (Exception e) {195exceptionOccurred = true;196}197if (!exceptionOccurred) {198throw new RuntimeException("testException: test case failed: no exception thrown");199}200int year2 = cal.get(YEAR);201if (year2 != year) {202throw new RuntimeException("wrong year value after exception: got " + year2203+ ", expected " + year);204}205}206207/**208* 4956227: getLeastMaximum(WEEK_OF_MONTH) return diff. val. for Greg. and Buddhist Calendar209*/210static void testLeastMax() {211Calendar bc = getBuddhistCalendar();212// Specify THAI_LOCALE to get the same params for WEEK213// calculations (6904680).214Calendar gc = new GregorianCalendar(THAI_LOCALE);215for (int f = 0; f < Calendar.FIELD_COUNT; f++) {216if (f == ERA || f == YEAR) {217continue;218}219int bn = bc.getLeastMaximum(f);220int gn = gc.getLeastMaximum(f);221if (bn != gn) {222throw new RuntimeException("inconsistent Least Max value for " + Koyomi.getFieldName(f)223+ ": Buddhist=" + bn224+ ": Gregorian=" + gn);225}226}227}228229/**230* @return a BuddhistCalendar231*/232static Calendar getBuddhistCalendar() {233return Calendar.getInstance(THAI_LOCALE);234}235236static void check(int got, int expected, String s) {237if (got != expected) {238throw new RuntimeException("Failed: " +239s + ": got:" + got + ", expected:" + expected);240}241}242}243244245