From b23fa14820882076195ca65163604e10fc5b2ce2 Mon Sep 17 00:00:00 2001 From: Roger Riggs Date: Fri, 16 Aug 2013 11:11:00 -0400 Subject: [PATCH] 8022193: java/time/test/java/util/TestFormatter.java failed in th locale Tests corrected to use fixed locale and not dependent on the environment Reviewed-by: sherman --- .../share/classes/java/util/Formatter.java | 2 +- .../time/test/java/util/TestFormatter.java | 95 ++++++++++++++----- 2 files changed, 72 insertions(+), 25 deletions(-) diff --git a/jdk/src/share/classes/java/util/Formatter.java b/jdk/src/share/classes/java/util/Formatter.java index 275e1854e0a..3f30c5ebaae 100644 --- a/jdk/src/share/classes/java/util/Formatter.java +++ b/jdk/src/share/classes/java/util/Formatter.java @@ -4196,7 +4196,7 @@ public final class Formatter implements Closeable, Flushable { case DateTime.CENTURY: // 'C' (00 - 99) case DateTime.YEAR_2: // 'y' (00 - 99) case DateTime.YEAR_4: { // 'Y' (0000 - 9999) - int i = t.get(ChronoField.YEAR); + int i = t.get(ChronoField.YEAR_OF_ERA); int size = 2; switch (c) { case DateTime.CENTURY: diff --git a/jdk/test/java/time/test/java/util/TestFormatter.java b/jdk/test/java/time/test/java/util/TestFormatter.java index 8f3b899b97e..4c3ac4b6039 100644 --- a/jdk/test/java/time/test/java/util/TestFormatter.java +++ b/jdk/test/java/time/test/java/util/TestFormatter.java @@ -22,16 +22,27 @@ */ package test.java.util; +import static org.testng.Assert.assertEquals; + import java.time.Instant; +import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZonedDateTime; import java.time.ZoneId; + +import java.time.chrono.ChronoLocalDate; +import java.time.chrono.ChronoLocalDateTime; +import java.time.chrono.ChronoZonedDateTime; +import java.time.chrono.Chronology; + import java.time.temporal.ChronoField; +import java.time.temporal.TemporalQuery; +import java.time.temporal.TemporalAccessor; import java.util.*; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import static org.testng.Assert.assertEquals; /* @test * @summary Unit test for j.u.Formatter threeten date/time support @@ -57,18 +68,32 @@ public class TestFormatter { private int total = 0; private int failure = 0; - private boolean verbose = true; + private boolean verbose = false; - @Test - public void test () { + @DataProvider(name = "calendarsByLocale") + Object[][] data_calendars() { + return new Object[][] { + {"en_US"}, + {"th_TH"}, + {"ja-JP-u-ca-japanese"}, + }; + } + @Test(dataProvider="calendarsByLocale") + public void test (String calendarLocale) { + failure = 0; int N = 12; //locales = Locale.getAvailableLocales(); Locale[] locales = new Locale[] { Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE}; Random r = new Random(); - ZonedDateTime zdt0 = ZonedDateTime.now(); - ZonedDateTime[] zdts = new ZonedDateTime[] { + + Locale calLocale = Locale.forLanguageTag(calendarLocale); + Chronology chrono = Chronology.ofLocale(calLocale); + ChronoLocalDate now = chrono.dateNow(); + ChronoLocalDateTime ldt0 = now.atTime(LocalTime.now()); + ChronoZonedDateTime zdt0 = ldt0.atZone(ZoneId.systemDefault()); + ChronoZonedDateTime[] zdts = new ChronoZonedDateTime[] { zdt0, zdt0.withZoneSameLocal(ZoneId.of("UTC")), zdt0.withZoneSameLocal(ZoneId.of("GMT")), @@ -76,11 +101,11 @@ public class TestFormatter { }; while (N-- > 0) { - for (ZonedDateTime zdt : zdts) { - zdt = zdt.withDayOfYear(r.nextInt(365) + 1) + for (ChronoZonedDateTime zdt : zdts) { + zdt = zdt.with(ChronoField.DAY_OF_YEAR, (r.nextInt(365) + 1)) .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); Instant instant = zdt.toInstant(); - Calendar cal = Calendar.getInstance(); + Calendar cal = Calendar.getInstance(calLocale); cal.setTimeInMillis(instant.toEpochMilli()); cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone())); for (Locale locale : locales) { @@ -106,8 +131,19 @@ public class TestFormatter { } private String getClassName(Object o) { - Class c = o.getClass(); - return c.getName().substring(c.getPackage().getName().length() + 1); + Class c = o.getClass(); + String clname = c.getName().substring(c.getPackage().getName().length() + 1); + if (o instanceof TemporalAccessor) { + Chronology chrono = ((TemporalAccessor)o).query(TemporalQuery.chronology()); + if (chrono != null) { + clname = clname + "(" + chrono.getId() + ")"; + } + } + if (o instanceof Calendar) { + String type = ((Calendar)o).getCalendarType(); + clname = clname + "(" + type + ")"; + } + return clname; } private String test(String fmtStr, Locale locale, @@ -115,12 +151,12 @@ public class TestFormatter { String out = new Formatter( new StringBuilder(), locale).format(fmtStr, dt).out().toString(); if (verbose) { - System.out.printf("%-18s : %s%n", getClassName(dt), out); + System.out.printf("%-24s : %s%n", getClassName(dt), out); } if (expected != null && !out.equals(expected)) { - System.out.printf("=====>%-18s : %s [ FAILED expected: %s ]%n", + System.out.printf("%-24s actual: %s%n FAILED; expected: %s%n", getClassName(dt), out, expected); - new RuntimeException().printStackTrace(); + new RuntimeException().printStackTrace(System.out); failure++; } total++; @@ -135,24 +171,29 @@ public class TestFormatter { } private void testDate(String fmtStr, Locale locale, - ZonedDateTime zdt, Calendar cal) { + ChronoZonedDateTime zdt, Calendar cal) { printFmtStr(locale, fmtStr); String expected = test(fmtStr, locale, null, cal); test(fmtStr, locale, expected, zdt); - test(fmtStr, locale, expected, zdt.toOffsetDateTime()); test(fmtStr, locale, expected, zdt.toLocalDateTime()); test(fmtStr, locale, expected, zdt.toLocalDate()); + if (zdt instanceof ZonedDateTime) { + test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime()); + } } private void testTime(String fmtStr, Locale locale, - ZonedDateTime zdt, Calendar cal) { + ChronoZonedDateTime zdt, Calendar cal) { printFmtStr(locale, fmtStr); String expected = test(fmtStr, locale, null, cal); test(fmtStr, locale, expected, zdt); - test(fmtStr, locale, expected, zdt.toOffsetDateTime()); test(fmtStr, locale, expected, zdt.toLocalDateTime()); - test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime()); test(fmtStr, locale, expected, zdt.toLocalTime()); + if (zdt instanceof ZonedDateTime) { + OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); + test(fmtStr, locale, expected, odt); + test(fmtStr, locale, expected, odt.toOffsetTime()); + } } private String toZoneIdStr(String expected) { @@ -164,7 +205,7 @@ public class TestFormatter { .replaceAll("GMT|UTC|UT", "Z"); } - private void testZoneId(Locale locale, ZonedDateTime zdt, Calendar cal) { + private void testZoneId(Locale locale, ChronoZonedDateTime zdt, Calendar cal) { String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]"; printFmtStr(locale, fmtStr); String expected = toZoneIdStr(test(fmtStr, locale, null, cal)); @@ -174,8 +215,11 @@ public class TestFormatter { cal0.setTimeInMillis(zdt.toInstant().toEpochMilli()); cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId())); expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0)); - test(fmtStr, locale, expected, zdt.toOffsetDateTime()); - test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime()); + if (zdt instanceof ZonedDateTime) { + OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); + test(fmtStr, locale, expected, odt); + test(fmtStr, locale, expected, odt.toOffsetTime()); + } // datetime + zid fmtStr = "c:[%tc] c:[%1$Tc]"; @@ -185,12 +229,15 @@ public class TestFormatter { } private void testInstant(Locale locale, Instant instant, - ZonedDateTime zdt, Calendar cal) { + ChronoZonedDateTime zdt, Calendar cal) { String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]"; printFmtStr(locale, fmtStr); String expected = test(fmtStr, locale, null, cal); test(fmtStr, locale, expected, instant); test(fmtStr, locale, expected, zdt); - test(fmtStr, locale, expected, zdt.toOffsetDateTime()); + if (zdt instanceof ZonedDateTime) { + OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); + test(fmtStr, locale, expected, odt); + } } }