From 2fd0c4b033d55da6b20a0b18b026095a0933168f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Wed, 13 Apr 2016 20:04:33 +0800 Subject: [PATCH] add testcase for issue 331. https://github.com/alibaba/fastjson/issues/331 --- .../json/bvt/bug/Bug_for_issue_331.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_331.java diff --git a/src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_331.java b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_331.java new file mode 100644 index 0000000000..e6ee9ac9b2 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_331.java @@ -0,0 +1,66 @@ +package com.alibaba.json.bvt.bug; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import org.junit.Assert; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.JSONSerializer; +import com.alibaba.fastjson.serializer.SerializeWriter; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import junit.framework.TestCase; + +public class Bug_for_issue_331 extends TestCase { + public void test_for_issue() throws Exception { + + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + Date date = format.parse("2015-05-23"); + + Calendar c = Calendar.getInstance(); + c.setTime(date); + + Model original = new Model(); + original.setDate(date); + original.setCalendar(c); + + String json = JSON.toJSONString(original, SerializerFeature.UseISO8601DateFormat); + + System.out.println(json); //V1.2.4 输出{"calendar":"2015-05-23","date":"2015-05-23"} , V1.2.6 输出{"calendar":"2015-05-23+08:00","date":"2015-05-23+08:00"} + + Model actual = JSON.parseObject(json, Model.class); + + Assert.assertNotNull(actual); + Assert.assertNotNull(actual.getDate()); + Assert.assertNotNull(actual.getCalendar()); + + Assert.assertEquals("与序列化前比较不相等", original.getDate(), actual.getDate()); + + Assert.assertEquals("序列化后的Date 和 Calendar 不相等", actual.getDate(), actual.getCalendar().getTime()); + } + + public static class Model { + private Date date; + private Calendar calendar; + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public Calendar getCalendar() { + return calendar; + } + + public void setCalendar(Calendar calendar) { + this.calendar = calendar; + } + + + } +}