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