Tests for Calendar APIs from OpenJDK8u60
This covers:
- GregorianCalendar.{from,to}ZonedDateTime()
- Calendar.toInstant()
- Calendar.getAvailableCalendarTypes()
Notes:
- The supported calendar types may change in a follow-up CL
(bug 35138741). Currently, only "gregory" is listed even
though Calendar.Builder.build() still attempts to use
"buddhist" (this inconsistency is a bug).
- Calendar.Builder is not covered by these tests
Test: CalendarTest, GregorianCalendarTest
Bug: 34923731
Bug: 29935305
Change-Id: Icfa87f465941e63f11f4ef6c35f82cdbcfd313fb
diff --git a/luni/src/test/java/libcore/java/util/CalendarTest.java b/luni/src/test/java/libcore/java/util/CalendarTest.java
index e1775a9..7d3b124 100644
--- a/luni/src/test/java/libcore/java/util/CalendarTest.java
+++ b/luni/src/test/java/libcore/java/util/CalendarTest.java
@@ -16,10 +16,15 @@
package libcore.java.util;
+import java.time.Instant;
+import java.util.Arrays;
import java.util.Calendar;
+import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
+import java.util.HashSet;
import java.util.Locale;
+import java.util.Set;
import java.util.TimeZone;
import libcore.util.SerializationTester;
@@ -157,6 +162,18 @@
assertEquals(minute, calendar.get(Calendar.MINUTE));
}
+ public void testToInstant() {
+ TimeZone timeZone = TimeZone.getTimeZone("Europe/Paris");
+ Calendar calendar = new GregorianCalendar(timeZone);
+ calendar.clear();
+ calendar.set(2007, Calendar.DECEMBER, 3, 10, 15, 30);
+ Instant instant = calendar.toInstant();
+ assertEquals(calendar.getTime().toInstant(), instant);
+ assertEquals(Instant.ofEpochMilli(calendar.getTimeInMillis()), instant);
+ // GMT is one hour earlier than Europe/Paris, hence hour of day is 9 rather than 10
+ assertEquals(instant, Instant.parse("2007-12-03T09:15:30Z"));
+ }
+
// http://b/5179775
public void testCalendarSerialization() {
String s = "aced00057372001b6a6176612e7574696c2e477265676f7269616e43616c656e6461728f3dd7d6e"
@@ -314,6 +331,13 @@
assertEquals(1,calendar.get(Calendar.HOUR_OF_DAY));
}
+ public void testGetAvailableCalendarTypes() {
+ // Guards against unintentional change; for intentional changes,
+ // update this test.
+ Set<String> expected = Collections.singleton("gregory");
+ assertEquals(expected, Calendar.getAvailableCalendarTypes());
+ }
+
public void testGetWeekYear() {
try {
new FakeCalendar().getWeekYear();
diff --git a/luni/src/test/java/libcore/java/util/GregorianCalendarTest.java b/luni/src/test/java/libcore/java/util/GregorianCalendarTest.java
index 3fa4dab..750b8db 100644
--- a/luni/src/test/java/libcore/java/util/GregorianCalendarTest.java
+++ b/luni/src/test/java/libcore/java/util/GregorianCalendarTest.java
@@ -16,6 +16,7 @@
package libcore.java.util;
+import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@@ -283,6 +284,28 @@
assertEquals(52, cal.getWeeksInWeekYear());
}
+ public void test_fromZonedDateTime() {
+ ZonedDateTime zdt = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
+ GregorianCalendar calendar = GregorianCalendar.from(zdt);
+ TimeZone timeZone = TimeZone.getTimeZone("Europe/Paris");
+ assertEquals(timeZone, calendar.getTimeZone());
+ assertEquals(2007, calendar.get(Calendar.YEAR));
+ assertEquals(Calendar.DECEMBER, calendar.get(Calendar.MONTH));
+ assertEquals(3, calendar.get(Calendar.DAY_OF_MONTH));
+ assertEquals(10, calendar.get(Calendar.HOUR_OF_DAY));
+ assertEquals(15, calendar.get(Calendar.MINUTE));
+ assertEquals(3600 * 1000, calendar.getTimeZone().getRawOffset()); // in milliseconds
+ }
+
+ public void test_toZonedDateTime() {
+ TimeZone timeZone = TimeZone.getTimeZone("Europe/Paris");
+ GregorianCalendar calendar = new GregorianCalendar(timeZone);
+ calendar.set(2007, Calendar.DECEMBER, 3, 10, 15, 30);
+ calendar.set(Calendar.MILLISECOND, 0);
+ ZonedDateTime zdt = calendar.toZonedDateTime();
+ assertEquals(ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]"), zdt);
+ }
+
private long getDstLosAngeles2014(TimeZone timeZone) {
GregorianCalendar cal = new GregorianCalendar(timeZone, Locale.ENGLISH);
cal.set(Calendar.MILLISECOND, 0);