OSDN Git Service

Fix problems with OldSimpleDateFormatTest.java.
authorJesse Wilson <jessewilson@google.com>
Wed, 29 Sep 2010 21:17:23 +0000 (14:17 -0700)
committerJesse Wilson <jessewilson@google.com>
Wed, 29 Sep 2010 22:10:19 +0000 (15:10 -0700)
This changes the single 'y' documentation to point out our behavior.
http://b/2788874

It also isolates two other failures for week-in-year and timezone
formatting.
http://b/1613709 Minimal days in first week is broken
http://b/3049014 Custom SimpleTimeZones not honored by SimpleDateFormat

Change-Id: Ic632bf70076fe0dfd376317379dc363269d5b2df

luni/src/main/java/java/text/SimpleDateFormat.java
luni/src/test/java/libcore/java/text/OldSimpleDateFormatTest.java

index a46ec3c..e1ff700 100644 (file)
@@ -94,10 +94,13 @@ import libcore.icu.TimeZones;
  *
  * <li><b>Number</b> the count is the minimum number of digits. Shorter values are
  * zero-padded to this width, longer values overflow this width.
- * Years are handled specially: {@code yy} truncates to 2 digits. So {@code yyyy} might
- * give {@code 2010}, but {@code yy} would give {@code 10}.
- * Fractional seconds are also handled specially: they're
- * zero-padded on the <i>right</i>.
+ *
+ * <p>Years are handled specially: {@code yy} truncates to the last 2 digits, but any
+ * other number of consecutive {@code y}s does not truncate. So where {@code yyyy} or
+ * {@code y} might give {@code 2010}, {@code yy} would give {@code 10}.
+ *
+ * <p>Fractional seconds are also handled specially: they're zero-padded on the
+ * <i>right</i>.
  *
  * <li><b>Text/Number</b>: if the count is 3 or more, use text; otherwise use a number.
  * So {@code MM} might give {@code 07} while {@code MMM} gives {@code July}.
@@ -596,10 +599,11 @@ public class SimpleDateFormat extends DateFormat {
             case YEAR_FIELD:
                 dateFormatField = Field.YEAR;
                 int year = calendar.get(Calendar.YEAR);
-                // According to Unicode CLDR TR35(http://unicode.org/reports/tr35/) :
-                // If date pattern is "yy", display the last 2 digits of year.
-                // Otherwise, display the actual year with minimum digit count.
-                // Therefore, if the pattern is "y", the display value for year 1234  is '1234' not '34'.
+                /*
+                 * For 'y' and 'yyy', we're consistent with Unicode and previous releases
+                 * of Android. But this means we're inconsistent with the RI.
+                 *     http://unicode.org/reports/tr35/
+                 */
                 if (count == 2) {
                     appendNumber(buffer, 2, year %= 100);
                 } else {
index a6547d4..a4012a8 100644 (file)
@@ -30,50 +30,28 @@ import java.util.TimeZone;
 
 public class OldSimpleDateFormatTest extends junit.framework.TestCase {
 
-    static SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
+    SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
 
-    static SimpleDateFormat pFormat = new SimpleDateFormat("", Locale.ENGLISH);
+    SimpleDateFormat pFormat = new SimpleDateFormat("", Locale.ENGLISH);
 
-    static class TestFormat extends junit.framework.TestCase {
+    class FormatTester {
         boolean testsFailed = false;
 
-        public TestFormat(String name) {
-            super(name);
-        }
-
-        public void test(String pattern, Calendar cal, String expected,
-                int field) {
+        public void test(String pattern, Calendar cal, String expected, int field) {
             StringBuffer buffer = new StringBuffer();
             FieldPosition position = new FieldPosition(field);
             format.applyPattern(pattern);
             format.format(cal.getTime(), buffer, position);
             String result = buffer.toString();
-            if (!System.getProperty("java.vendor", "None").substring(0, 3)
-                    .equals("Sun")) {
-                assertTrue("Wrong format: \"" + pattern + "\" expected: "
-                        + expected + " result: " + result, result
-                        .equals(expected));
-                assertTrue("Wrong begin position: " + pattern + " expected: "
-                        + expected + " field: " + field, position
-                        .getBeginIndex() == 1);
-                assertTrue("Wrong end position: " + pattern + " expected: "
-                        + expected + " field: " + field,
-                        position.getEndIndex() == result.length());
-            } else {
-                // Print the failure but don't use assert as this
-                // will stop subsequent tests from running
-                if (!result.equals(expected)) {
-                    System.out
-                            .println("Wrong format: \"" + pattern
-                                    + "\" expected: " + expected + " result: "
-                                    + result);
-                    testsFailed = true;
-                }
+            if (!result.equals(expected)) {
+                System.out.println("Wrong format: \"" + pattern
+                                + "\" expected: " + expected + " result: "
+                                + result);
+                testsFailed = true;
             }
         }
 
-        public void parse(String pattern, String input, Date expected,
-                int start, int end) {
+        public void parse(String pattern, String input, Date expected, int start, int end) {
             pFormat.applyPattern(pattern);
             ParsePosition position = new ParsePosition(start);
             Date result = pFormat.parse(input, position);
@@ -240,17 +218,7 @@ public class OldSimpleDateFormatTest extends junit.framework.TestCase {
     }
 
     public void test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition() {
-        // Test for method java.lang.StringBuffer
-        // java.text.SimpleDateFormat.format(java.util.Date,
-        // java.lang.StringBuffer, java.text.FieldPosition)
-
-        // Android doesn't support EST time zone
-        // new Support_SimpleDateFormat(
-        //        "test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition")
-        //        .t_format_with_FieldPosition();
-
-        TestFormat test = new TestFormat(
-                "test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition");
+        FormatTester test = new FormatTester();
 
         Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6);
         test.test(" G", cal, " AD", DateFormat.ERA_FIELD);
@@ -259,18 +227,6 @@ public class OldSimpleDateFormatTest extends junit.framework.TestCase {
         test.test(" G", new GregorianCalendar(-1999, Calendar.JUNE, 2), " BC",
                 DateFormat.ERA_FIELD);
 
-        test.test(" y", cal, " 99", DateFormat.YEAR_FIELD);
-        test.test(" yy", cal, " 99", DateFormat.YEAR_FIELD);
-        test.test(" yy", new GregorianCalendar(2001, Calendar.JUNE, 2), " 01",
-                DateFormat.YEAR_FIELD);
-        test.test(" yy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 00",
-                DateFormat.YEAR_FIELD);
-        test.test(" yyy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 00",
-                DateFormat.YEAR_FIELD);
-        test.test(" yyy", cal, " 99", DateFormat.YEAR_FIELD);
-        test.test(" yyyy", cal, " 1999", DateFormat.YEAR_FIELD);
-        test.test(" yyyyy", cal, " 01999", DateFormat.YEAR_FIELD);
-
         test.test(" M", cal, " 6", DateFormat.MONTH_FIELD);
         test.test(" M", new GregorianCalendar(1999, Calendar.NOVEMBER, 2),
                 " 11", DateFormat.MONTH_FIELD);
@@ -339,16 +295,6 @@ public class OldSimpleDateFormatTest extends junit.framework.TestCase {
         test.test(" FF", cal, " 01", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
         test.test(" FFFF", cal, " 0001", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
 
-        test.test(" w", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
-        test.test(" ww", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
-        test.test(" wwww", cal, " 0023", DateFormat.WEEK_OF_YEAR_FIELD);
-
-        test.test(" W", cal, " 1", DateFormat.WEEK_OF_MONTH_FIELD);
-        test.test(" W", new GregorianCalendar(1999, Calendar.NOVEMBER, 14),
-                " 3", DateFormat.WEEK_OF_MONTH_FIELD);
-        test.test(" WW", cal, " 01", DateFormat.WEEK_OF_MONTH_FIELD);
-        test.test(" WWWW", cal, " 0001", DateFormat.WEEK_OF_MONTH_FIELD);
-
         test.test(" a", cal, " PM", DateFormat.AM_PM_FIELD);
         test.test(" a", new GregorianCalendar(1999, Calendar.NOVEMBER, 14),
                 " AM", DateFormat.AM_PM_FIELD);
@@ -376,16 +322,6 @@ public class OldSimpleDateFormatTest extends junit.framework.TestCase {
         test.test(" KK", cal, " 03", DateFormat.HOUR0_FIELD);
         test.test(" KKKK", cal, " 0003", DateFormat.HOUR0_FIELD);
 
-        format.setTimeZone(new SimpleTimeZone(60000, "ONE MINUTE"));
-        test.test(" z", cal, " GMT+00:01", DateFormat.TIMEZONE_FIELD);
-        test.test(" zzzz", cal, " GMT+00:01", DateFormat.TIMEZONE_FIELD);
-        format.setTimeZone(new SimpleTimeZone(5400000, "ONE HOUR, THIRTY"));
-        test.test(" z", cal, " GMT+01:30", DateFormat.TIMEZONE_FIELD);
-        format
-                .setTimeZone(new SimpleTimeZone(-5400000,
-                        "NEG ONE HOUR, THIRTY"));
-        test.test(" z", cal, " GMT-01:30", DateFormat.TIMEZONE_FIELD);
-
         format.applyPattern("'Mkz''':.@5");
         assertEquals("Wrong output", "Mkz':.@5", format.format(new Date()));
 
@@ -402,18 +338,80 @@ public class OldSimpleDateFormatTest extends junit.framework.TestCase {
             fail("Expected test to throw NPE, not " + ex.getClass().getName());
         }
 
-        format.setTimeZone(TimeZone.getTimeZone("EST"));
+        assertFalse(test.testsFailed);
+    }
+
+    /**
+     * This test assumes Unicode behavior where 'y' and 'yyy' don't truncate,
+     * which means that it will fail on the RI.
+     */
+    public void testFormattingYear() {
+        FormatTester test = new FormatTester();
+
+        Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6);
+        test.test(" y", cal, " 1999", DateFormat.YEAR_FIELD);
+        test.test(" yy", cal, " 99", DateFormat.YEAR_FIELD);
+        test.test(" yy", new GregorianCalendar(2001, Calendar.JUNE, 2), " 01",
+                DateFormat.YEAR_FIELD);
+        test.test(" yy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 00",
+                DateFormat.YEAR_FIELD);
+        test.test(" yyy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 2000",
+                DateFormat.YEAR_FIELD);
+        test.test(" yyy", cal, " 1999", DateFormat.YEAR_FIELD);
+        test.test(" yyyy", cal, " 1999", DateFormat.YEAR_FIELD);
+        test.test(" yyyyy", cal, " 01999", DateFormat.YEAR_FIELD);
+
+        assertFalse(test.testsFailed);
+    }
+
+    public void testFormattingWeekOfYear() {
+        FormatTester test = new FormatTester();
+        Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6);
+        cal.setMinimalDaysInFirstWeek(1);
+        cal.setFirstDayOfWeek(1);
+
+        test.test(" w", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
+        test.test(" ww", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
+        test.test(" wwww", cal, " 0023", DateFormat.WEEK_OF_YEAR_FIELD);
+
+        test.test(" W", cal, " 1", DateFormat.WEEK_OF_MONTH_FIELD);
+        test.test(" W", new GregorianCalendar(1999, Calendar.NOVEMBER, 14),
+                " 3", DateFormat.WEEK_OF_MONTH_FIELD);
+        test.test(" WW", cal, " 01", DateFormat.WEEK_OF_MONTH_FIELD);
+        test.test(" WWWW", cal, " 0001", DateFormat.WEEK_OF_MONTH_FIELD);
+
+        assertFalse(test.testsFailed);
+    }
+
+    public void testFormattingTimezones() {
+        FormatTester test = new FormatTester();
+        Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6);
+
+        format.setTimeZone(new SimpleTimeZone(60000, "ONE MINUTE"));
+        test.test(" z", cal, " GMT+00:01", DateFormat.TIMEZONE_FIELD);
+        test.test(" zzzz", cal, " GMT+00:01", DateFormat.TIMEZONE_FIELD);
+        format.setTimeZone(new SimpleTimeZone(5400000, "ONE HOUR, THIRTY"));
+        test.test(" z", cal, " GMT+01:30", DateFormat.TIMEZONE_FIELD);
+        format.setTimeZone(new SimpleTimeZone(-5400000, "NEG ONE HOUR, THIRTY"));
+        test.test(" z", cal, " GMT-01:30", DateFormat.TIMEZONE_FIELD);
+
+        format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
         test.test(" z", cal, " EDT", DateFormat.TIMEZONE_FIELD);
         Calendar temp2 = new GregorianCalendar(1999, Calendar.JANUARY, 12);
         test.test(" z", temp2, " EST", DateFormat.TIMEZONE_FIELD);
         test.test(" zz", cal, " EDT", DateFormat.TIMEZONE_FIELD);
         test.test(" zzz", cal, " EDT", DateFormat.TIMEZONE_FIELD);
-        test.test(" zzzz", cal, " Eastern Daylight Time",
-                DateFormat.TIMEZONE_FIELD);
-        test.test(" zzzz", temp2, " Eastern Standard Time",
-                DateFormat.TIMEZONE_FIELD);
-       test.test(" zzzzz", cal, " Eastern Daylight Time",
-                DateFormat.TIMEZONE_FIELD);
+        test.test(" zzzz", cal, " Eastern Daylight Time", DateFormat.TIMEZONE_FIELD);
+        test.test(" zzzz", temp2, " Eastern Standard Time", DateFormat.TIMEZONE_FIELD);
+        test.test(" zzzzz", cal, " Eastern Daylight Time", DateFormat.TIMEZONE_FIELD);
+
+        assertFalse(test.testsFailed);
+    }
+
+    public void testDefaultMinimalDaysInFirstWeek() {
+        Locale.setDefault(Locale.US);
+        assertEquals(1, new GregorianCalendar().getMinimalDaysInFirstWeek());
+        assertEquals(1, new GregorianCalendar().getFirstDayOfWeek());
     }
 
     /**
@@ -421,22 +419,15 @@ public class OldSimpleDateFormatTest extends junit.framework.TestCase {
      */
     public void test_timeZoneFormatting() {
         // tests specific to formatting of timezones
-        Date summerDate = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3,
-                6).getTime();
-        Date winterDate = new GregorianCalendar(1999, Calendar.JANUARY, 12)
-                .getTime();
-
-        TestFormat test = new TestFormat(
-                "test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition");
-        test.verifyFormatTimezone("GMT-7", "GMT-07:00, GMT-07:00",
-                "-0700, -0700", summerDate);
-        test.verifyFormatTimezone("GMT-7", "GMT-07:00, GMT-07:00",
-                "-0700, -0700", winterDate);
+        Date summerDate = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6).getTime();
+        Date winterDate = new GregorianCalendar(1999, Calendar.JANUARY, 12).getTime();
 
-        test.verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00",
-                "+1400, +1400", summerDate);
-        test.verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00",
-                "+1400, +1400", winterDate);
+        FormatTester test = new FormatTester();
+        test.verifyFormatTimezone("GMT-7", "GMT-07:00, GMT-07:00", "-0700, -0700", summerDate);
+        test.verifyFormatTimezone("GMT-7", "GMT-07:00, GMT-07:00", "-0700, -0700", winterDate);
+
+        test.verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00", "+1400, +1400", summerDate);
+        test.verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00", "+1400, +1400", winterDate);
 
         test.verifyFormatTimezone("America/Los_Angeles", "PDT, Pacific Daylight Time",
                 "-0700, -0700", summerDate);
@@ -448,6 +439,8 @@ public class OldSimpleDateFormatTest extends junit.framework.TestCase {
                 "-0400, -0400", summerDate);
         test.verifyFormatTimezone("America/Detroit", "EST, Eastern Standard Time",
                 "-0500, -0500", winterDate);
+
+        assertFalse(test.testsFailed);
     }
 
     public void test_parseLjava_lang_StringLjava_text_ParsePosition_2() {