|
| 1 | +package me.chanjar.weixin.common.util.json; |
| 2 | + |
| 3 | +import com.google.gson.stream.JsonReader; |
| 4 | +import com.google.gson.stream.JsonWriter; |
| 5 | +import org.testng.annotations.Test; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.StringReader; |
| 9 | +import java.io.StringWriter; |
| 10 | +import java.util.Date; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | + |
| 14 | +/** |
| 15 | + * WxDateTypeAdapter 的单元测试. |
| 16 | + * |
| 17 | + * @author liyong |
| 18 | + */ |
| 19 | +public class WxDateTypeAdapterTest { |
| 20 | + |
| 21 | + private final WxDateTypeAdapter adapter = new WxDateTypeAdapter(); |
| 22 | + |
| 23 | + private Date read(String json) throws IOException { |
| 24 | + JsonReader reader = new JsonReader(new StringReader(json)); |
| 25 | + reader.setLenient(true); |
| 26 | + return adapter.read(reader); |
| 27 | + } |
| 28 | + |
| 29 | + private String write(Date date) throws IOException { |
| 30 | + StringWriter out = new StringWriter(); |
| 31 | + JsonWriter writer = new JsonWriter(out); |
| 32 | + writer.setLenient(true); |
| 33 | + adapter.write(writer, date); |
| 34 | + return out.toString(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * 秒级时间戳转成毫秒时如果按 int 运算会溢出,导致解析出的时间错误(甚至早于 1970 年)。 |
| 39 | + */ |
| 40 | + @Test |
| 41 | + public void testReadCurrentTimestamp() throws IOException { |
| 42 | + long seconds = 1481013459L; |
| 43 | + Date date = read(String.valueOf(seconds)); |
| 44 | + assertThat(date).isNotNull(); |
| 45 | + assertThat(date.getTime()).isEqualTo(seconds * 1000L); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testReadTimestampAfterYear2038() throws IOException { |
| 50 | + long seconds = 4102444800L; |
| 51 | + Date date = read(String.valueOf(seconds)); |
| 52 | + assertThat(date).isNotNull(); |
| 53 | + assertThat(date.getTime()).isEqualTo(seconds * 1000L); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testReadNull() throws IOException { |
| 58 | + assertThat(read("null")).isNull(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testWrite() throws IOException { |
| 63 | + long seconds = 1481013459L; |
| 64 | + assertThat(write(new Date(seconds * 1000L))).isEqualTo(String.valueOf(seconds)); |
| 65 | + assertThat(write(null)).isEqualTo("null"); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * 序列化与反序列化应当可以互相还原。 |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void testWriteThenRead() throws IOException { |
| 73 | + Date now = new Date(1600000000L * 1000L); |
| 74 | + assertThat(read(write(now))).isEqualTo(now); |
| 75 | + } |
| 76 | +} |
0 commit comments