提交 6faf2c52 编写于 作者: R rriggs

8022193: java/time/test/java/util/TestFormatter.java failed in th locale.

Summary: Tests corrected to use fixed locale and not dependent on the environment
Reviewed-by: sherman
上级 ce10a009
...@@ -4196,7 +4196,7 @@ public final class Formatter implements Closeable, Flushable { ...@@ -4196,7 +4196,7 @@ public final class Formatter implements Closeable, Flushable {
case DateTime.CENTURY: // 'C' (00 - 99) case DateTime.CENTURY: // 'C' (00 - 99)
case DateTime.YEAR_2: // 'y' (00 - 99) case DateTime.YEAR_2: // 'y' (00 - 99)
case DateTime.YEAR_4: { // 'Y' (0000 - 9999) case DateTime.YEAR_4: { // 'Y' (0000 - 9999)
int i = t.get(ChronoField.YEAR); int i = t.get(ChronoField.YEAR_OF_ERA);
int size = 2; int size = 2;
switch (c) { switch (c) {
case DateTime.CENTURY: case DateTime.CENTURY:
......
...@@ -22,16 +22,27 @@ ...@@ -22,16 +22,27 @@
*/ */
package test.java.util; package test.java.util;
import static org.testng.Assert.assertEquals;
import java.time.Instant; import java.time.Instant;
import java.time.LocalTime;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.ZoneId; 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.ChronoField;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalAccessor;
import java.util.*; import java.util.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
/* @test /* @test
* @summary Unit test for j.u.Formatter threeten date/time support * @summary Unit test for j.u.Formatter threeten date/time support
...@@ -57,18 +68,32 @@ public class TestFormatter { ...@@ -57,18 +68,32 @@ public class TestFormatter {
private int total = 0; private int total = 0;
private int failure = 0; private int failure = 0;
private boolean verbose = true; private boolean verbose = false;
@Test @DataProvider(name = "calendarsByLocale")
public void test () { 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; int N = 12;
//locales = Locale.getAvailableLocales(); //locales = Locale.getAvailableLocales();
Locale[] locales = new Locale[] { Locale[] locales = new Locale[] {
Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE}; Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE};
Random r = new Random(); 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,
zdt0.withZoneSameLocal(ZoneId.of("UTC")), zdt0.withZoneSameLocal(ZoneId.of("UTC")),
zdt0.withZoneSameLocal(ZoneId.of("GMT")), zdt0.withZoneSameLocal(ZoneId.of("GMT")),
...@@ -76,11 +101,11 @@ public class TestFormatter { ...@@ -76,11 +101,11 @@ public class TestFormatter {
}; };
while (N-- > 0) { while (N-- > 0) {
for (ZonedDateTime zdt : zdts) { for (ChronoZonedDateTime<?> zdt : zdts) {
zdt = zdt.withDayOfYear(r.nextInt(365) + 1) zdt = zdt.with(ChronoField.DAY_OF_YEAR, (r.nextInt(365) + 1))
.with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
Instant instant = zdt.toInstant(); Instant instant = zdt.toInstant();
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance(calLocale);
cal.setTimeInMillis(instant.toEpochMilli()); cal.setTimeInMillis(instant.toEpochMilli());
cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone())); cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone()));
for (Locale locale : locales) { for (Locale locale : locales) {
...@@ -106,8 +131,19 @@ public class TestFormatter { ...@@ -106,8 +131,19 @@ public class TestFormatter {
} }
private String getClassName(Object o) { private String getClassName(Object o) {
Class c = o.getClass(); Class<?> c = o.getClass();
return c.getName().substring(c.getPackage().getName().length() + 1); 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, private String test(String fmtStr, Locale locale,
...@@ -115,12 +151,12 @@ public class TestFormatter { ...@@ -115,12 +151,12 @@ public class TestFormatter {
String out = new Formatter( String out = new Formatter(
new StringBuilder(), locale).format(fmtStr, dt).out().toString(); new StringBuilder(), locale).format(fmtStr, dt).out().toString();
if (verbose) { 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)) { 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); getClassName(dt), out, expected);
new RuntimeException().printStackTrace(); new RuntimeException().printStackTrace(System.out);
failure++; failure++;
} }
total++; total++;
...@@ -135,24 +171,29 @@ public class TestFormatter { ...@@ -135,24 +171,29 @@ public class TestFormatter {
} }
private void testDate(String fmtStr, Locale locale, private void testDate(String fmtStr, Locale locale,
ZonedDateTime zdt, Calendar cal) { ChronoZonedDateTime<?> zdt, Calendar cal) {
printFmtStr(locale, fmtStr); printFmtStr(locale, fmtStr);
String expected = test(fmtStr, locale, null, cal); String expected = test(fmtStr, locale, null, cal);
test(fmtStr, locale, expected, zdt); test(fmtStr, locale, expected, zdt);
test(fmtStr, locale, expected, zdt.toOffsetDateTime());
test(fmtStr, locale, expected, zdt.toLocalDateTime()); test(fmtStr, locale, expected, zdt.toLocalDateTime());
test(fmtStr, locale, expected, zdt.toLocalDate()); test(fmtStr, locale, expected, zdt.toLocalDate());
if (zdt instanceof ZonedDateTime) {
test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime());
}
} }
private void testTime(String fmtStr, Locale locale, private void testTime(String fmtStr, Locale locale,
ZonedDateTime zdt, Calendar cal) { ChronoZonedDateTime<?> zdt, Calendar cal) {
printFmtStr(locale, fmtStr); printFmtStr(locale, fmtStr);
String expected = test(fmtStr, locale, null, cal); String expected = test(fmtStr, locale, null, cal);
test(fmtStr, locale, expected, zdt); test(fmtStr, locale, expected, zdt);
test(fmtStr, locale, expected, zdt.toOffsetDateTime());
test(fmtStr, locale, expected, zdt.toLocalDateTime()); test(fmtStr, locale, expected, zdt.toLocalDateTime());
test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime());
test(fmtStr, locale, expected, zdt.toLocalTime()); 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) { private String toZoneIdStr(String expected) {
...@@ -164,7 +205,7 @@ public class TestFormatter { ...@@ -164,7 +205,7 @@ public class TestFormatter {
.replaceAll("GMT|UTC|UT", "Z"); .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]"; String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
printFmtStr(locale, fmtStr); printFmtStr(locale, fmtStr);
String expected = toZoneIdStr(test(fmtStr, locale, null, cal)); String expected = toZoneIdStr(test(fmtStr, locale, null, cal));
...@@ -174,8 +215,11 @@ public class TestFormatter { ...@@ -174,8 +215,11 @@ public class TestFormatter {
cal0.setTimeInMillis(zdt.toInstant().toEpochMilli()); cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId())); cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0)); expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0));
test(fmtStr, locale, expected, zdt.toOffsetDateTime()); if (zdt instanceof ZonedDateTime) {
test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime()); OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
test(fmtStr, locale, expected, odt);
test(fmtStr, locale, expected, odt.toOffsetTime());
}
// datetime + zid // datetime + zid
fmtStr = "c:[%tc] c:[%1$Tc]"; fmtStr = "c:[%tc] c:[%1$Tc]";
...@@ -185,12 +229,15 @@ public class TestFormatter { ...@@ -185,12 +229,15 @@ public class TestFormatter {
} }
private void testInstant(Locale locale, Instant instant, 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]"; String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
printFmtStr(locale, fmtStr); printFmtStr(locale, fmtStr);
String expected = test(fmtStr, locale, null, cal); String expected = test(fmtStr, locale, null, cal);
test(fmtStr, locale, expected, instant); test(fmtStr, locale, expected, instant);
test(fmtStr, locale, expected, zdt); 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);
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册