Path: blob/master/test/jdk/java/util/Calendar/Builder/BuilderTest.java
41152 views
/*1* Copyright (c) 2013, 2018, 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 474576126* @summary Unit test for Calendar.Builder.27*/2829import java.time.LocalDateTime;30import java.util.*;31import static java.util.Calendar.*;3233public class BuilderTest {34private static final Locale jaJPJP = new Locale("ja", "JP", "JP");35private static final Locale thTH = new Locale("th", "TH");36private static final TimeZone LA = TimeZone.getTimeZone("America/Los_Angeles");37private static final TimeZone TOKYO = TimeZone.getTimeZone("Asia/Tokyo");38private static int error;3940public static void main(String[] args) {41TimeZone tz = TimeZone.getDefault();42Locale loc = Locale.getDefault();43try {44TimeZone.setDefault(TimeZone.getTimeZone("GMT"));45Locale.setDefault(Locale.US);46Calendar.Builder calb;47Calendar cal, expected;4849// set instant50calb = builder();51long time = System.currentTimeMillis();52cal = calb.setInstant(time).build();53expected = new GregorianCalendar();54expected.setTimeInMillis(time);55check(cal, expected);5657calb = builder();58cal = calb.setInstant(new Date(time)).build();59check(cal, expected);6061// set time zone62calb = builder();63cal = calb.setTimeZone(LA).setInstant(time).build();64expected = new GregorianCalendar(LA, Locale.US);65expected.setTimeInMillis(time);66check(cal, expected);6768calb = builder();69cal = calb.setTimeZone(TOKYO).setInstant(time).build();70expected = new GregorianCalendar(TOKYO, Locale.US);71expected.setTimeInMillis(time);72check(cal, expected);7374// set vs. setFields75calb = builder();76cal = calb.set(YEAR, 2013).set(MONTH, JANUARY).set(DAY_OF_MONTH, 31)77.set(HOUR_OF_DAY, 10).set(MINUTE, 20).set(SECOND, 30).set(MILLISECOND, 40).build();78expected = new GregorianCalendar(2013, JANUARY, 31, 10, 20, 30);79expected.set(MILLISECOND, 40);80check(cal, expected);8182calb = builder();83cal = calb.setFields(YEAR, 2013, MONTH, JANUARY, DAY_OF_MONTH, 31,84HOUR_OF_DAY, 10, MINUTE, 20, SECOND, 30, MILLISECOND, 40).build();85check(cal, expected);8687// field resolution88calb = builder();89cal = calb.setFields(YEAR, 2013, MONTH, DECEMBER, DAY_OF_MONTH, 31,90HOUR_OF_DAY, 10, MINUTE, 20, SECOND, 30, MILLISECOND, 40)91.set(DAY_OF_YEAR, 31).build(); // DAY_OF_YEAR wins.92check(cal, expected);9394// setDate/setTimeOfDay95calb = builder();96cal = calb.setDate(2013, JANUARY, 31).setTimeOfDay(10, 20, 30, 40).build();97check(cal, expected);9899// week date (ISO 8601)100calb = builder().setCalendarType("iso8601");101cal = calb.setWeekDate(2013, 1, MONDAY).setTimeOfDay(10, 20, 30).build();102expected = getISO8601();103expected.set(2012, DECEMBER, 31, 10, 20, 30);104check(cal, expected);105106// default YEAR == 1970107cal = builder().setFields(MONTH, JANUARY,108DAY_OF_MONTH, 9).build();109check(cal, new GregorianCalendar(1970, JANUARY, 9));110111// no parameters are given.112calb = builder();113cal = calb.build();114expected = new GregorianCalendar();115expected.clear();116check(cal, expected);117118// Thai Buddhist calendar119calb = builder();120cal = calb.setCalendarType("buddhist").setDate(2556, JANUARY, 31).build();121expected = Calendar.getInstance(thTH);122expected.clear();123expected.set(2556, JANUARY, 31);124check(cal, expected);125// setLocale126calb = builder();127cal = calb.setLocale(thTH).setDate(2556, JANUARY, 31).build();128check(cal, expected);129130// Japanese Imperial calendar131cal = builder().setCalendarType("japanese")132.setFields(YEAR, 1, DAY_OF_YEAR, 1).build();133expected = Calendar.getInstance(jaJPJP);134expected.clear();135if (LocalDateTime.now().isBefore(LocalDateTime.of(2019, 5, 1, 0, 0))) {136expected.set(1, JANUARY, 8);137} else {138expected.set(1, MAY, 1);139}140check(cal, expected);141// setLocale142calb = builder();143cal = calb.setLocale(jaJPJP).setFields(YEAR, 1, DAY_OF_YEAR, 1).build();144check(cal, expected);145146testExceptions();147} finally {148// Restore default Locale and TimeZone149Locale.setDefault(loc);150TimeZone.setDefault(tz);151}152if (error > 0) {153throw new RuntimeException("Failed");154}155}156157private static void testExceptions() {158Calendar.Builder calb;159Calendar cal;160161// NPE162try {163calb = builder().setInstant((Date)null);164noException("setInstant((Date)null)");165} catch (NullPointerException npe) {166}167try {168calb = builder().setCalendarType(null);169noException("setCalendarType(null)");170} catch (NullPointerException npe) {171}172try {173calb = builder().setLocale(null);174noException("setLocale(null)");175} catch (NullPointerException npe) {176}177try {178calb = builder().setTimeZone(null);179noException("setTimeZone(null)");180} catch (NullPointerException npe) {181}182183// IllegalArgumentException184try {185// invalid field index in set186calb = builder().set(100, 2013);187noException("set(100, 2013)");188} catch (IllegalArgumentException e) {189}190try {191// invalid field index in setField192calb = builder().setFields(100, 2013);193noException("setFields(100, 2013)");194} catch (IllegalArgumentException e) {195}196try {197// odd number of arguments198calb = builder().setFields(YEAR, 2013, MONTH);199noException("setFields(YEAR, 2013, MONTH)");200} catch (IllegalArgumentException e) {201}202try {203// unknown calendar type204calb = builder().setCalendarType("foo");205noException("setCalendarType(\"foo\")");206} catch (IllegalArgumentException e) {207}208try {209// invalid week definition parameter210calb = builder().setWeekDefinition(8, 1);211noException("setWeekDefinition(8, 1)");212} catch (IllegalArgumentException e) {213}214try {215// invalid week definition parameter216calb = builder().setWeekDefinition(SUNDAY, 0);217noException("setWeekDefinition(8, 1)");218} catch (IllegalArgumentException e) {219}220221try {222// sets both instant and field parameters223calb = builder().setInstant(new Date()).setDate(2013, JANUARY, 1);224noException("setInstant(new Date()).setDate(2013, JANUARY, 1)");225} catch (IllegalStateException e) {226}227try {228// sets both field parameters and instant229calb = builder().setDate(2013, JANUARY, 1).setInstant(new Date());230noException("setDate(2013, JANUARY, 1).setInstant(new Date())");231} catch (IllegalStateException e) {232}233try {234// sets inconsistent calendar types235calb = builder().setCalendarType("iso8601").setCalendarType("japanese");236noException("setCalendarType(\"iso8601\").setCalendarType(\"japanese\")");237} catch (IllegalStateException e) {238}239240// IllegalArgumentException in build()241calb = nonLenientBuilder().set(MONTH, 100);242checkException(calb, IllegalArgumentException.class);243calb = nonLenientBuilder().setTimeOfDay(23, 59, 70);244checkException(calb, IllegalArgumentException.class);245calb = builder().setCalendarType("japanese").setWeekDate(2013, 1, MONDAY);246checkException(calb, IllegalArgumentException.class);247}248249private static Calendar.Builder builder() {250return new Calendar.Builder();251}252253private static Calendar.Builder nonLenientBuilder() {254return builder().setLenient(false);255}256257private static Calendar getISO8601() {258GregorianCalendar cal = new GregorianCalendar();259cal.setFirstDayOfWeek(MONDAY);260cal.setMinimalDaysInFirstWeek(4);261cal.setGregorianChange(new Date(Long.MIN_VALUE));262cal.clear();263return cal;264}265266private static void check(Calendar cal, Calendar expected) {267if (!cal.equals(expected)) {268error++;269System.err.println("FAILED:");270System.err.println("\t cal = "+cal.getTime());271System.err.println("\texpected = "+expected.getTime());272System.err.printf("\tcal = %s%n\texp = %s%n", cal, expected);273}274}275276private static void checkException(Calendar.Builder calb, Class<? extends Exception> exception) {277try {278Calendar cal = calb.build();279error++;280System.err.println("expected exception: " + exception);281} catch (Exception e) {282if (!e.getClass().equals(exception)) {283error++;284System.err.println("unexpected exception: " + e.getClass() + ", expected: " + exception);285}286}287}288289private static void noException(String msg) {290error++;291System.err.println("no exception with "+msg);292}293}294295296