|
22 | 22 | import static org.junit.Assert.fail;
|
23 | 23 |
|
24 | 24 | import androidx.annotation.Nullable;
|
| 25 | +import com.google.firebase.Timestamp; |
25 | 26 | import com.google.firebase.firestore.DocumentId;
|
26 | 27 | import com.google.firebase.firestore.DocumentReference;
|
27 | 28 | import com.google.firebase.firestore.Exclude;
|
28 | 29 | import com.google.firebase.firestore.PropertyName;
|
29 | 30 | import com.google.firebase.firestore.TestUtil;
|
30 | 31 | import com.google.firebase.firestore.ThrowOnExtraProperties;
|
31 | 32 | import java.io.Serializable;
|
| 33 | +import java.time.Instant; |
32 | 34 | import java.util.ArrayList;
|
33 | 35 | import java.util.Arrays;
|
34 | 36 | import java.util.Collection;
|
|
37 | 39 | import java.util.HashMap;
|
38 | 40 | import java.util.List;
|
39 | 41 | import java.util.Map;
|
| 42 | +import java.util.Objects; |
40 | 43 | import java.util.Set;
|
41 | 44 | import org.junit.Test;
|
42 | 45 | import org.robolectric.annotation.Config;
|
@@ -95,6 +98,44 @@ public boolean isValue() {
|
95 | 98 | }
|
96 | 99 | }
|
97 | 100 |
|
| 101 | + private static class TimeBean { |
| 102 | + public Timestamp timestamp; |
| 103 | + public Date date; |
| 104 | + public Instant instant; |
| 105 | + public kotlinx.datetime.Instant instantKt; |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean equals(Object o) { |
| 109 | + if (o == null || getClass() != o.getClass()) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + TimeBean timeBean = (TimeBean) o; |
| 113 | + return Objects.equals(timestamp, timeBean.timestamp) |
| 114 | + && Objects.equals(date, timeBean.date) |
| 115 | + && Objects.equals(instant, timeBean.instant) |
| 116 | + && Objects.equals(instantKt, timeBean.instantKt); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public int hashCode() { |
| 121 | + return Objects.hash(timestamp, date, instant, instantKt); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String toString() { |
| 126 | + return "TimeBean{" |
| 127 | + + "_date=" |
| 128 | + + date |
| 129 | + + ", _timestamp=" |
| 130 | + + timestamp |
| 131 | + + ", _instant=" |
| 132 | + + instant |
| 133 | + + ", _instantKt=" |
| 134 | + + instantKt |
| 135 | + + '}'; |
| 136 | + } |
| 137 | + } |
| 138 | + |
98 | 139 | private static class ShortBean {
|
99 | 140 | private short value;
|
100 | 141 |
|
@@ -1476,6 +1517,48 @@ public void serializeBooleanBean() {
|
1476 | 1517 | assertJson("{'value': true}", serialize(bean));
|
1477 | 1518 | }
|
1478 | 1519 |
|
| 1520 | + @Test |
| 1521 | + public void serializeTimeBean() { |
| 1522 | + TimeBean bean = new TimeBean(); |
| 1523 | + bean.instant = Instant.ofEpochSecond(1234, 5678); |
| 1524 | + bean.timestamp = new Timestamp(bean.instant); |
| 1525 | + bean.date = new Date(1234); |
| 1526 | + bean.instantKt = new kotlinx.datetime.Instant(bean.instant); |
| 1527 | + assertEquals( |
| 1528 | + Map.of( |
| 1529 | + "timestamp", |
| 1530 | + bean.timestamp, |
| 1531 | + "date", |
| 1532 | + bean.date, |
| 1533 | + "instant", |
| 1534 | + bean.timestamp, |
| 1535 | + "instantKt", |
| 1536 | + bean.timestamp), |
| 1537 | + serialize(bean)); |
| 1538 | + } |
| 1539 | + |
| 1540 | + @Test |
| 1541 | + public void deserializeTimeBean() { |
| 1542 | + TimeBean bean = new TimeBean(); |
| 1543 | + bean.instant = Instant.ofEpochSecond(1234, 5678); |
| 1544 | + bean.timestamp = new Timestamp(bean.instant); |
| 1545 | + bean.date = new Date(1234); |
| 1546 | + bean.instantKt = new kotlinx.datetime.Instant(bean.instant); |
| 1547 | + assertEquals( |
| 1548 | + bean, |
| 1549 | + convertToCustomClass( |
| 1550 | + Map.of( |
| 1551 | + "timestamp", |
| 1552 | + bean.timestamp, |
| 1553 | + "date", |
| 1554 | + bean.date, |
| 1555 | + "instant", |
| 1556 | + bean.timestamp, |
| 1557 | + "instantKt", |
| 1558 | + bean.timestamp), |
| 1559 | + TimeBean.class)); |
| 1560 | + } |
| 1561 | + |
1479 | 1562 | @Test
|
1480 | 1563 | public void serializeFloatBean() {
|
1481 | 1564 | FloatBean bean = new FloatBean();
|
|
0 commit comments