6984418: (cal) GregorianCalendar.setWeekDate doesn't check parameter consistency in non-lenient
authorokutsu
Tue, 14 Sep 2010 16:47:28 +0900
changeset 6504 e4fb90405787
parent 6503 600ab3437bb8
child 6505 b1faae5e44a3
6984418: (cal) GregorianCalendar.setWeekDate doesn't check parameter consistency in non-lenient Reviewed-by: peytoia
jdk/src/share/classes/java/util/GregorianCalendar.java
jdk/test/java/util/Calendar/WeekDateTest.java
--- a/jdk/src/share/classes/java/util/GregorianCalendar.java	Mon Sep 13 15:34:24 2010 +0400
+++ b/jdk/src/share/classes/java/util/GregorianCalendar.java	Tue Sep 14 16:47:28 2010 +0900
@@ -2160,20 +2160,22 @@
             gc.complete();
         }
 
+        if (!isLenient() &&
+            (gc.getWeekYear() != weekYear
+             || gc.internalGet(WEEK_OF_YEAR) != weekOfYear
+             || gc.internalGet(DAY_OF_WEEK) != dayOfWeek)) {
+            throw new IllegalArgumentException();
+        }
+
         set(ERA, gc.internalGet(ERA));
         set(YEAR, gc.internalGet(YEAR));
         set(MONTH, gc.internalGet(MONTH));
         set(DAY_OF_MONTH, gc.internalGet(DAY_OF_MONTH));
 
         // to avoid throwing an IllegalArgumentException in
-        // non-lenient, set WEEK_OF_YEAR and DAY_OF_WEEK internally
+        // non-lenient, set WEEK_OF_YEAR internally
         internalSet(WEEK_OF_YEAR, weekOfYear);
-        internalSet(DAY_OF_WEEK, dayOfWeek);
         complete();
-
-        assert getWeekYear() == weekYear;
-        assert get(WEEK_OF_YEAR) == weekOfYear;
-        assert get(DAY_OF_WEEK) == dayOfWeek;
     }
 
     /**
--- a/jdk/test/java/util/Calendar/WeekDateTest.java	Mon Sep 13 15:34:24 2010 +0400
+++ b/jdk/test/java/util/Calendar/WeekDateTest.java	Tue Sep 14 16:47:28 2010 +0900
@@ -36,27 +36,44 @@
     // Week dates are in the ISO numbering for day-of-week.
     static int[][][] data = {
         // Calendar year-date, Week year-date
-        {{ 2005, 01, 01}, {2004, 53, 6}},
-        {{ 2005, 01, 02}, {2004, 53, 7}},
-        {{ 2005, 12, 31}, {2005, 52, 6}},
-        {{ 2007, 01, 01}, {2007, 01, 1}},
-        {{ 2007, 12, 30}, {2007, 52, 7}},
-        {{ 2007, 12, 31}, {2008, 01, 1}},
-        {{ 2008, 01, 01}, {2008, 01, 2}},
-        {{ 2008, 12, 29}, {2009, 01, 1}},
-        {{ 2008, 12, 31}, {2009, 01, 3}},
-        {{ 2009, 01, 01}, {2009, 01, 4}},
-        {{ 2009, 12, 31}, {2009, 53, 4}},
-        {{ 2010, 01, 03}, {2009, 53, 7}},
-        {{ 2009, 12, 31}, {2009, 53, 4}},
-        {{ 2010, 01, 01}, {2009, 53, 5}},
-        {{ 2010, 01, 02}, {2009, 53, 6}},
-        {{ 2010, 01, 03}, {2009, 53, 7}},
-        {{ 2008, 12, 28}, {2008, 52, 7}},
-        {{ 2008, 12, 29}, {2009, 01, 1}},
-        {{ 2008, 12, 30}, {2009, 01, 2}},
-        {{ 2008, 12, 31}, {2009, 01, 3}},
-        {{ 2009, 01, 01}, {2009, 01, 4}}
+        {{ 2005, 01, 01}, { 2004, 53, 6}},
+        {{ 2005, 01, 02}, { 2004, 53, 7}},
+        {{ 2005, 12, 31}, { 2005, 52, 6}},
+        {{ 2007, 01, 01}, { 2007, 01, 1}},
+        {{ 2007, 12, 30}, { 2007, 52, 7}},
+        {{ 2007, 12, 31}, { 2008, 01, 1}},
+        {{ 2008, 01, 01}, { 2008, 01, 2}},
+        {{ 2008, 12, 29}, { 2009, 01, 1}},
+        {{ 2008, 12, 31}, { 2009, 01, 3}},
+        {{ 2009, 01, 01}, { 2009, 01, 4}},
+        {{ 2009, 12, 31}, { 2009, 53, 4}},
+        {{ 2010, 01, 03}, { 2009, 53, 7}},
+        {{ 2009, 12, 31}, { 2009, 53, 4}},
+        {{ 2010, 01, 01}, { 2009, 53, 5}},
+        {{ 2010, 01, 02}, { 2009, 53, 6}},
+        {{ 2010, 01, 03}, { 2009, 53, 7}},
+        {{ 2008, 12, 28}, { 2008, 52, 7}},
+        {{ 2008, 12, 29}, { 2009, 01, 1}},
+        {{ 2008, 12, 30}, { 2009, 01, 2}},
+        {{ 2008, 12, 31}, { 2009, 01, 3}},
+        {{ 2009, 01, 01}, { 2009, 01, 4}}
+    };
+
+    // Data for leniency test
+    static final int[][][] leniencyData = {
+        {{ 2008, 12, 28}, { 2009,  0, 7}},
+        {{ 2008, 12, 21}, { 2009, -1, 7}},
+        {{ 2009,  1,  4}, { 2008, 53, 7}},
+    };
+
+    static final int[][] invalidData = {
+        { 2010, -1,  1},
+        { 2010, 00,  1},
+        { 2010, 55,  1},
+        { 2010, 03,  0},
+        { 2010, 04,  8},
+        { 2010, 04, 19},
+        { 2010, 05, -1},
     };
 
     public static void main(String[] args) {
@@ -65,7 +82,7 @@
             int[] expected = dates[0];
             int[] weekDate = dates[1];
             // Convert ISO 8601 day-of-week to Calendar.DAY_OF_WEEK.
-            int dayOfWeek = weekDate[2] == 7 ? SUNDAY : weekDate[2] + 1;
+            int dayOfWeek = getCalendarDayOfWeek(weekDate[2]);
 
             cal.clear();
             cal.setWeekDate(weekDate[0], weekDate[1], dayOfWeek);
@@ -102,21 +119,55 @@
                                          n, cal.getActualMaximum(WEEK_OF_YEAR));
                 throw new RuntimeException(s);
             }
+
             cal.setWeekDate(cal.getWeekYear(), 1, MONDAY);
-            System.out.println(cal.getTime());
             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);
-            System.out.println(cal.getTime());
             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() {
@@ -130,4 +181,8 @@
         cal.setMinimalDaysInFirstWeek(4);
         return cal;
     }
+
+    private static int getCalendarDayOfWeek(int isoDayOfWeek) {
+        return (isoDayOfWeek == 7) ? SUNDAY : isoDayOfWeek + 1;
+    }
 }