2702N/A/*
2702N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2702N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2702N/A *
2702N/A * This code is free software; you can redistribute it and/or modify it
2702N/A * under the terms of the GNU General Public License version 2 only, as
2702N/A * published by the Free Software Foundation.
2702N/A *
2702N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2702N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2702N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2702N/A * version 2 for more details (a copy is included in the LICENSE file that
2702N/A * accompanied this code).
2702N/A *
2702N/A * You should have received a copy of the GNU General Public License version
2702N/A * 2 along with this work; if not, write to the Free Software Foundation,
2702N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2702N/A *
2702N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2702N/A * or visit www.oracle.com if you need additional information or have any
2702N/A * questions.
2702N/A */
2702N/A
2702N/A/*
2702N/A * @test
2702N/A * @bug 4267450
2702N/A * @summary Unit test for week date support
2702N/A */
2702N/A
2702N/Aimport java.text.*;
2702N/Aimport java.util.*;
2702N/Aimport static java.util.GregorianCalendar.*;
2702N/A
2702N/Apublic class WeekDateTest {
2702N/A
2702N/A // Week dates are in the ISO numbering for day-of-week.
2702N/A static int[][][] data = {
2702N/A // Calendar year-date, Week year-date
2715N/A {{ 2005, 01, 01}, { 2004, 53, 6}},
2715N/A {{ 2005, 01, 02}, { 2004, 53, 7}},
2715N/A {{ 2005, 12, 31}, { 2005, 52, 6}},
2715N/A {{ 2007, 01, 01}, { 2007, 01, 1}},
2715N/A {{ 2007, 12, 30}, { 2007, 52, 7}},
2715N/A {{ 2007, 12, 31}, { 2008, 01, 1}},
2715N/A {{ 2008, 01, 01}, { 2008, 01, 2}},
2715N/A {{ 2008, 12, 29}, { 2009, 01, 1}},
2715N/A {{ 2008, 12, 31}, { 2009, 01, 3}},
2715N/A {{ 2009, 01, 01}, { 2009, 01, 4}},
2715N/A {{ 2009, 12, 31}, { 2009, 53, 4}},
2715N/A {{ 2010, 01, 03}, { 2009, 53, 7}},
2715N/A {{ 2009, 12, 31}, { 2009, 53, 4}},
2715N/A {{ 2010, 01, 01}, { 2009, 53, 5}},
2715N/A {{ 2010, 01, 02}, { 2009, 53, 6}},
2715N/A {{ 2010, 01, 03}, { 2009, 53, 7}},
2715N/A {{ 2008, 12, 28}, { 2008, 52, 7}},
2715N/A {{ 2008, 12, 29}, { 2009, 01, 1}},
2715N/A {{ 2008, 12, 30}, { 2009, 01, 2}},
2715N/A {{ 2008, 12, 31}, { 2009, 01, 3}},
2715N/A {{ 2009, 01, 01}, { 2009, 01, 4}}
2715N/A };
2715N/A
2715N/A // Data for leniency test
2715N/A static final int[][][] leniencyData = {
2715N/A {{ 2008, 12, 28}, { 2009, 0, 7}},
2715N/A {{ 2008, 12, 21}, { 2009, -1, 7}},
2715N/A {{ 2009, 1, 4}, { 2008, 53, 7}},
2715N/A };
2715N/A
2715N/A static final int[][] invalidData = {
2715N/A { 2010, -1, 1},
2715N/A { 2010, 00, 1},
2715N/A { 2010, 55, 1},
2715N/A { 2010, 03, 0},
2715N/A { 2010, 04, 8},
2715N/A { 2010, 04, 19},
2715N/A { 2010, 05, -1},
2702N/A };
2702N/A
2702N/A public static void main(String[] args) {
2702N/A GregorianCalendar cal = newCalendar();
2702N/A for (int[][] dates : data) {
2702N/A int[] expected = dates[0];
2702N/A int[] weekDate = dates[1];
2702N/A // Convert ISO 8601 day-of-week to Calendar.DAY_OF_WEEK.
2715N/A int dayOfWeek = getCalendarDayOfWeek(weekDate[2]);
2702N/A
2702N/A cal.clear();
2702N/A cal.setWeekDate(weekDate[0], weekDate[1], dayOfWeek);
2702N/A if (cal.get(YEAR) != expected[0]
2702N/A || cal.get(MONTH)+1 != expected[1]
2702N/A || cal.get(DAY_OF_MONTH) != expected[2]) {
2702N/A String s = String.format("got=%4d-%02d-%02d, expected=%4d-%02d-%02d",
2702N/A cal.get(YEAR), cal.get(MONTH)+1, cal.get(DAY_OF_MONTH),
2702N/A expected[0], expected[1], expected[2]);
2702N/A throw new RuntimeException(s);
2702N/A }
2702N/A if (cal.getWeekYear() != weekDate[0]
2702N/A || cal.get(WEEK_OF_YEAR) != weekDate[1]
2702N/A || cal.get(DAY_OF_WEEK) != dayOfWeek) {
2702N/A String s = String.format(
2702N/A "got=%4d-W%02d-%d, expected=%4d-W%02d-%d (not ISO day-of-week)",
2702N/A cal.getWeekYear(), cal.get(WEEK_OF_YEAR), cal.get(DAY_OF_WEEK),
2702N/A weekDate[0], weekDate[1], dayOfWeek);
2702N/A throw new RuntimeException(s);
2702N/A }
2702N/A }
2702N/A
2702N/A // Test getWeeksInWeekYear().
2702N/A // If we avoid the first week of January and the last week of
2702N/A // December, getWeeksInWeekYear() and
2702N/A // getActualMaximum(WEEK_OF_YEAR) values should be the same.
2702N/A for (int year = 2000; year <= 2100; year++) {
2702N/A cal.clear();
2702N/A cal.set(year, JUNE, 1);
2702N/A int n = cal.getWeeksInWeekYear();
2702N/A if (n != cal.getActualMaximum(WEEK_OF_YEAR)) {
2702N/A String s = String.format("getWeeksInWeekYear() = %d, "
2702N/A + "getActualMaximum(WEEK_OF_YEAR) = %d%n",
2702N/A n, cal.getActualMaximum(WEEK_OF_YEAR));
2702N/A throw new RuntimeException(s);
2702N/A }
2715N/A
2702N/A cal.setWeekDate(cal.getWeekYear(), 1, MONDAY);
2702N/A if (cal.getWeeksInWeekYear() != n) {
2702N/A String s = String.format("first day: got %d, expected %d%n",
2702N/A cal.getWeeksInWeekYear(), n);
2702N/A throw new RuntimeException(s);
2702N/A }
2715N/A
2702N/A cal.setWeekDate(cal.getWeekYear(), n, SUNDAY);
2702N/A if (cal.getWeeksInWeekYear() != n) {
2702N/A String s = String.format("last day: got %d, expected %d%n",
2702N/A cal.getWeeksInWeekYear(), n);
2702N/A throw new RuntimeException(s);
2702N/A }
2702N/A }
2715N/A
2715N/A // Test lenient mode with out of range values.
2715N/A for (int[][] dates : leniencyData) {
2715N/A int[] expected = dates[0];
2715N/A int[] weekDate = dates[1];
2715N/A // Convert ISO 8601 day-of-week to Calendar.DAY_OF_WEEK.
2715N/A int dayOfWeek = getCalendarDayOfWeek(weekDate[2]);
2715N/A
2715N/A cal.clear();
2715N/A cal.setWeekDate(weekDate[0], weekDate[1], dayOfWeek);
2715N/A if (cal.get(YEAR) != expected[0]
2715N/A || cal.get(MONTH)+1 != expected[1]
2715N/A || cal.get(DAY_OF_MONTH) != expected[2]) {
2715N/A String s = String.format("got=%4d-%02d-%02d, expected=%4d-%02d-%02d",
2715N/A cal.get(YEAR), cal.get(MONTH)+1, cal.get(DAY_OF_MONTH),
2715N/A expected[0], expected[1], expected[2]);
2715N/A throw new RuntimeException(s);
2715N/A }
2715N/A }
2715N/A
2715N/A // Test non-lenient mode
2715N/A cal.setLenient(false);
2715N/A for (int[] date : invalidData) {
2715N/A cal.clear();
2715N/A try {
2715N/A // Use the raw dayOfWeek value as invalid data
2715N/A cal.setWeekDate(date[0], date[1], date[2]);
2715N/A String s = String.format("didn't throw an IllegalArgumentException with"
2715N/A + " %d, %d, %d",date[0], date[1], date[2]);
2715N/A throw new RuntimeException(s);
2715N/A } catch (IllegalArgumentException e) {
2715N/A // OK
2715N/A }
2715N/A }
2702N/A }
2702N/A
2702N/A private static GregorianCalendar newCalendar() {
2702N/A // Use GMT to avoid any surprises related DST transitions.
2702N/A GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
2702N/A if (!cal.isWeekDateSupported()) {
2702N/A throw new RuntimeException("Week dates not supported");
2702N/A }
2702N/A // Setup the ISO 8601 compatible parameters
2702N/A cal.setFirstDayOfWeek(MONDAY);
2702N/A cal.setMinimalDaysInFirstWeek(4);
2702N/A return cal;
2702N/A }
2715N/A
2715N/A private static int getCalendarDayOfWeek(int isoDayOfWeek) {
2715N/A return (isoDayOfWeek == 7) ? SUNDAY : isoDayOfWeek + 1;
2715N/A }
2702N/A}