Path: blob/master/test/jdk/java/util/Calendar/WeekDateTest.java
41149 views
/*1* Copyright (c) 2010, 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 426745026* @summary Unit test for week date support27*/2829import java.text.*;30import java.util.*;31import static java.util.GregorianCalendar.*;3233public class WeekDateTest {3435// Week dates are in the ISO numbering for day-of-week.36static int[][][] data = {37// Calendar year-date, Week year-date38{{ 2005, 01, 01}, { 2004, 53, 6}},39{{ 2005, 01, 02}, { 2004, 53, 7}},40{{ 2005, 12, 31}, { 2005, 52, 6}},41{{ 2007, 01, 01}, { 2007, 01, 1}},42{{ 2007, 12, 30}, { 2007, 52, 7}},43{{ 2007, 12, 31}, { 2008, 01, 1}},44{{ 2008, 01, 01}, { 2008, 01, 2}},45{{ 2008, 12, 29}, { 2009, 01, 1}},46{{ 2008, 12, 31}, { 2009, 01, 3}},47{{ 2009, 01, 01}, { 2009, 01, 4}},48{{ 2009, 12, 31}, { 2009, 53, 4}},49{{ 2010, 01, 03}, { 2009, 53, 7}},50{{ 2009, 12, 31}, { 2009, 53, 4}},51{{ 2010, 01, 01}, { 2009, 53, 5}},52{{ 2010, 01, 02}, { 2009, 53, 6}},53{{ 2010, 01, 03}, { 2009, 53, 7}},54{{ 2008, 12, 28}, { 2008, 52, 7}},55{{ 2008, 12, 29}, { 2009, 01, 1}},56{{ 2008, 12, 30}, { 2009, 01, 2}},57{{ 2008, 12, 31}, { 2009, 01, 3}},58{{ 2009, 01, 01}, { 2009, 01, 4}}59};6061// Data for leniency test62static final int[][][] leniencyData = {63{{ 2008, 12, 28}, { 2009, 0, 7}},64{{ 2008, 12, 21}, { 2009, -1, 7}},65{{ 2009, 1, 4}, { 2008, 53, 7}},66};6768static final int[][] invalidData = {69{ 2010, -1, 1},70{ 2010, 00, 1},71{ 2010, 55, 1},72{ 2010, 03, 0},73{ 2010, 04, 8},74{ 2010, 04, 19},75{ 2010, 05, -1},76};7778public static void main(String[] args) {79GregorianCalendar cal = newCalendar();80for (int[][] dates : data) {81int[] expected = dates[0];82int[] weekDate = dates[1];83// Convert ISO 8601 day-of-week to Calendar.DAY_OF_WEEK.84int dayOfWeek = getCalendarDayOfWeek(weekDate[2]);8586cal.clear();87cal.setWeekDate(weekDate[0], weekDate[1], dayOfWeek);88if (cal.get(YEAR) != expected[0]89|| cal.get(MONTH)+1 != expected[1]90|| cal.get(DAY_OF_MONTH) != expected[2]) {91String s = String.format("got=%4d-%02d-%02d, expected=%4d-%02d-%02d",92cal.get(YEAR), cal.get(MONTH)+1, cal.get(DAY_OF_MONTH),93expected[0], expected[1], expected[2]);94throw new RuntimeException(s);95}96if (cal.getWeekYear() != weekDate[0]97|| cal.get(WEEK_OF_YEAR) != weekDate[1]98|| cal.get(DAY_OF_WEEK) != dayOfWeek) {99String s = String.format(100"got=%4d-W%02d-%d, expected=%4d-W%02d-%d (not ISO day-of-week)",101cal.getWeekYear(), cal.get(WEEK_OF_YEAR), cal.get(DAY_OF_WEEK),102weekDate[0], weekDate[1], dayOfWeek);103throw new RuntimeException(s);104}105}106107// Test getWeeksInWeekYear().108// If we avoid the first week of January and the last week of109// December, getWeeksInWeekYear() and110// getActualMaximum(WEEK_OF_YEAR) values should be the same.111for (int year = 2000; year <= 2100; year++) {112cal.clear();113cal.set(year, JUNE, 1);114int n = cal.getWeeksInWeekYear();115if (n != cal.getActualMaximum(WEEK_OF_YEAR)) {116String s = String.format("getWeeksInWeekYear() = %d, "117+ "getActualMaximum(WEEK_OF_YEAR) = %d%n",118n, cal.getActualMaximum(WEEK_OF_YEAR));119throw new RuntimeException(s);120}121122cal.setWeekDate(cal.getWeekYear(), 1, MONDAY);123if (cal.getWeeksInWeekYear() != n) {124String s = String.format("first day: got %d, expected %d%n",125cal.getWeeksInWeekYear(), n);126throw new RuntimeException(s);127}128129cal.setWeekDate(cal.getWeekYear(), n, SUNDAY);130if (cal.getWeeksInWeekYear() != n) {131String s = String.format("last day: got %d, expected %d%n",132cal.getWeeksInWeekYear(), n);133throw new RuntimeException(s);134}135}136137// Test lenient mode with out of range values.138for (int[][] dates : leniencyData) {139int[] expected = dates[0];140int[] weekDate = dates[1];141// Convert ISO 8601 day-of-week to Calendar.DAY_OF_WEEK.142int dayOfWeek = getCalendarDayOfWeek(weekDate[2]);143144cal.clear();145cal.setWeekDate(weekDate[0], weekDate[1], dayOfWeek);146if (cal.get(YEAR) != expected[0]147|| cal.get(MONTH)+1 != expected[1]148|| cal.get(DAY_OF_MONTH) != expected[2]) {149String s = String.format("got=%4d-%02d-%02d, expected=%4d-%02d-%02d",150cal.get(YEAR), cal.get(MONTH)+1, cal.get(DAY_OF_MONTH),151expected[0], expected[1], expected[2]);152throw new RuntimeException(s);153}154}155156// Test non-lenient mode157cal.setLenient(false);158for (int[] date : invalidData) {159cal.clear();160try {161// Use the raw dayOfWeek value as invalid data162cal.setWeekDate(date[0], date[1], date[2]);163String s = String.format("didn't throw an IllegalArgumentException with"164+ " %d, %d, %d",date[0], date[1], date[2]);165throw new RuntimeException(s);166} catch (IllegalArgumentException e) {167// OK168}169}170}171172private static GregorianCalendar newCalendar() {173// Use GMT to avoid any surprises related DST transitions.174GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));175if (!cal.isWeekDateSupported()) {176throw new RuntimeException("Week dates not supported");177}178// Setup the ISO 8601 compatible parameters179cal.setFirstDayOfWeek(MONDAY);180cal.setMinimalDaysInFirstWeek(4);181return cal;182}183184private static int getCalendarDayOfWeek(int isoDayOfWeek) {185return (isoDayOfWeek == 7) ? SUNDAY : isoDayOfWeek + 1;186}187}188189190