Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
add testcase for issue 331. #331
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 13, 2016
1 parent 3efe30e commit 832bdfe
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_331.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
}

0 comments on commit 832bdfe

Please sign in to comment.