From 03d77b6e629b6ff825a1c8cc3f7cbba7feb98395 Mon Sep 17 00:00:00 2001 From: wenshao Date: Thu, 15 Mar 2018 17:14:03 +0800 Subject: [PATCH] add testcases. --- .../json/bvt/issue_1500/Issue1588.java | 19 +++ .../json/bvt/issue_1600/Issue1665.java | 25 ++++ .../json/bvt/issue_1600/Issue_for_gaorui.java | 130 ++++++++++++++++++ .../json/bvt/issue_1700/Issue1761.java | 13 ++ .../alibaba/json/bvt/kotlin/Issue1483.java | 10 +- .../parser/autoType/AutoTypeTest3_deny.java | 54 ++++++++ .../bvt/serializer/SerializerFeatureTest.java | 5 + 7 files changed, 250 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/issue_1500/Issue1588.java create mode 100644 src/test/java/com/alibaba/json/bvt/issue_1600/Issue1665.java create mode 100644 src/test/java/com/alibaba/json/bvt/issue_1600/Issue_for_gaorui.java create mode 100644 src/test/java/com/alibaba/json/bvt/issue_1700/Issue1761.java create mode 100644 src/test/java/com/alibaba/json/bvt/parser/autoType/AutoTypeTest3_deny.java diff --git a/src/test/java/com/alibaba/json/bvt/issue_1500/Issue1588.java b/src/test/java/com/alibaba/json/bvt/issue_1500/Issue1588.java new file mode 100644 index 0000000000..6f1cf8848b --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_1500/Issue1588.java @@ -0,0 +1,19 @@ +package com.alibaba.json.bvt.issue_1500; + +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; +import junit.framework.TestCase; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Issue1588 extends TestCase { + public void test_for_issue() throws Exception { + String dateString = "2017-11-17 00:00:00"; + Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("test", date); + System.out.println(jsonObject.toJSONString(jsonObject, SerializerFeature.UseISO8601DateFormat)); + System.out.println(JSONObject.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd'T'HH:mm:ssXXX")); + } +} diff --git a/src/test/java/com/alibaba/json/bvt/issue_1600/Issue1665.java b/src/test/java/com/alibaba/json/bvt/issue_1600/Issue1665.java new file mode 100644 index 0000000000..499e89dea8 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_1600/Issue1665.java @@ -0,0 +1,25 @@ +package com.alibaba.json.bvt.issue_1600; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import com.alibaba.fastjson.parser.ParserConfig; +import com.alibaba.fastjson.util.TypeUtils; +import junit.framework.TestCase; + +import java.util.Collection; + +public class Issue1665 extends TestCase { + public void test_for_issue() throws Exception { + TypeReference> typeReference = new TypeReference>() {}; + + + Collection collection = TypeUtils.cast(JSON.parse("[{\"id\":101}]"), typeReference.getType(), ParserConfig.getGlobalInstance()); + assertEquals(1, collection.size()); + Model model = collection.iterator().next(); + assertEquals(101, model.id); + } + + public static class Model { + public int id; + } +} diff --git a/src/test/java/com/alibaba/json/bvt/issue_1600/Issue_for_gaorui.java b/src/test/java/com/alibaba/json/bvt/issue_1600/Issue_for_gaorui.java new file mode 100644 index 0000000000..0d2c375521 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_1600/Issue_for_gaorui.java @@ -0,0 +1,130 @@ +package com.alibaba.json.bvt.issue_1600; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.parser.Feature; +import junit.framework.TestCase; + +public class Issue_for_gaorui extends TestCase { + public void test_for_issue() throws Exception { + String json = "{\"@type\":\"java.util.HashMap\",\"COUPON\":[{\"@type\":\"com.alibaba.json.bvt.issue_1600.Issue_for_gaorui.PromotionTermDetail\",\"activityId\":\"1584034\",\"choose\":true,\"couponId\":1251068987,\"couponType\":\"limitp\",\"match\":true,\"realPrice\":{\"amount\":0.6,\"currency\":\"USD\"}}],\"grayTrade\":\"true\"}"; + + JSON.parseObject(json, Object.class, Feature.SupportAutoType); + } + + public static class PromotionTermDetail { + /** + * 卡券Id + */ + private Long couponId; + /** + * 营销Id + */ + private String promotionId; + /** + * 实际单价 + */ + private Money realPrice; + /** + * 活动Id + */ + private String activityId; + + /** + * 卡券类型 + */ + private String couponType; + + /** + * 是否能够获取到该优惠 + */ + private boolean isMatch = false; + /** + * 是否选择了该优惠 + */ + private boolean isChoose = false; + /** + * 未获取到优惠的原因 + */ + private String reasonForLose; + /** + * 未获取优惠的标识码 + */ + private String codeForLose; + + public Long getCouponId() { + return couponId; + } + + public void setCouponId(Long couponId) { + this.couponId = couponId; + } + + public String getPromotionId() { + return promotionId; + } + + public void setPromotionId(String promotionId) { + this.promotionId = promotionId; + } + + public Money getRealPrice() { + return realPrice; + } + + public void setRealPrice(Money realPrice) { + this.realPrice = realPrice; + } + + public String getActivityId() { + return activityId; + } + + public void setActivityId(String activityId) { + this.activityId = activityId; + } + + public String getCouponType() { + return couponType; + } + + public void setCouponType(String couponType) { + this.couponType = couponType; + } + + public boolean isMatch() { + return isMatch; + } + + public void setMatch(boolean match) { + isMatch = match; + } + + public boolean isChoose() { + return isChoose; + } + + public void setChoose(boolean choose) { + isChoose = choose; + } + + public String getReasonForLose() { + return reasonForLose; + } + + public void setReasonForLose(String reasonForLose) { + this.reasonForLose = reasonForLose; + } + + public String getCodeForLose() { + return codeForLose; + } + + public void setCodeForLose(String codeForLose) { + this.codeForLose = codeForLose; + } + } + + public static class Money { + + } +} diff --git a/src/test/java/com/alibaba/json/bvt/issue_1700/Issue1761.java b/src/test/java/com/alibaba/json/bvt/issue_1700/Issue1761.java new file mode 100644 index 0000000000..09ccce7bf9 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_1700/Issue1761.java @@ -0,0 +1,13 @@ +package com.alibaba.json.bvt.issue_1700; + +import com.alibaba.fastjson.JSONObject; +import junit.framework.TestCase; + +public class Issue1761 extends TestCase { + public void test_for_issue() throws Exception { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("null",""); + double d = jsonObject.getDoubleValue("null"); + assertEquals(d, 0.0D); + } +} diff --git a/src/test/java/com/alibaba/json/bvt/kotlin/Issue1483.java b/src/test/java/com/alibaba/json/bvt/kotlin/Issue1483.java index e7203d380f..e8397cbb4c 100644 --- a/src/test/java/com/alibaba/json/bvt/kotlin/Issue1483.java +++ b/src/test/java/com/alibaba/json/bvt/kotlin/Issue1483.java @@ -21,12 +21,10 @@ public void test_user() throws Exception { Object obj = JSON.parseObject(json, clazz); assertSame(clazz, obj.getClass()); // -// String text = JSON.parseObject(JSON.toJSONString(obj), Feature.OrderedField).toJSONString(); -// if ("{\"age\":99,\"desc\":\"[robohorse\",\"name\":\"xx]\"}".equals(text)) { -// return; -// } -// -// assertEquals("{\"age\":99,\"desc\":\"xx\",\"name\":\"robohorse\"}", text); + for (int i = 0; i < 10; ++i) { + String text = JSON.parseObject(JSON.toJSONString(obj), Feature.OrderedField).toJSONString(); + assertEquals("{\"age\":99,\"desc\":\"xx\",\"name\":\"robohorse\"}", text); + } } public static class ExtClassLoader extends ClassLoader { diff --git a/src/test/java/com/alibaba/json/bvt/parser/autoType/AutoTypeTest3_deny.java b/src/test/java/com/alibaba/json/bvt/parser/autoType/AutoTypeTest3_deny.java new file mode 100644 index 0000000000..cddbf67a76 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/parser/autoType/AutoTypeTest3_deny.java @@ -0,0 +1,54 @@ +package com.alibaba.json.bvt.parser.autoType; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.parser.Feature; +import junit.framework.TestCase; + +public class AutoTypeTest3_deny extends TestCase { + public void test_for_x() throws Exception { + Exception error = null; + try { + String json = "{\"@type\":\"java.util.logging.FileHandler\",\"pattern\":\"xxx.txt\"}"; + Object obj = JSON.parse(json, Feature.SupportAutoType); + } catch (Exception ex) { + error = ex; + } + assertNotNull(error); + } + + public void test_for_jar() throws Exception { + Exception error = null; + try { + String json = "{\"@type\":\"java.util.jar.JarFile.JarFile\",\"name\":\"xxx.txt\"}"; + Object obj = JSON.parse(json, Feature.SupportAutoType); + } catch (Exception ex) { + error = ex; + } + assertNotNull(error); + } + + public void test_for_array() throws Exception { + Exception error = null; + try { + String json = "{\"@type\":\"[java.util.jar.JarFile.JarFile\",\"name\":\"xxx.txt\"}"; + Object obj = JSON.parse(json, Feature.SupportAutoType); + } catch (Exception ex) { + ex.printStackTrace(); + error = ex; + } + assertNotNull(error); + } + + + public void test_for_array_1() throws Exception { + Exception error = null; + try { + String json = "{\"@type\":\"L[java.util.jar.JarFile.JarFile;\",\"name\":\"xxx.txt\"}"; + Object obj = JSON.parse(json, Feature.SupportAutoType); + } catch (Exception ex) { + ex.printStackTrace(); + error = ex; + } + assertNotNull(error); + } +} diff --git a/src/test/java/com/alibaba/json/bvt/serializer/SerializerFeatureTest.java b/src/test/java/com/alibaba/json/bvt/serializer/SerializerFeatureTest.java index 1f970f6533..986d028e53 100755 --- a/src/test/java/com/alibaba/json/bvt/serializer/SerializerFeatureTest.java +++ b/src/test/java/com/alibaba/json/bvt/serializer/SerializerFeatureTest.java @@ -22,4 +22,9 @@ public void test_1 () throws Exception { feature = SerializerFeature.config(feature, SerializerFeature.BrowserSecure, false); Assert.assertEquals(false, SerializerFeature.isEnabled(feature, SerializerFeature.BrowserSecure)); } + + public void test_assert_cnt() throws Exception { + int len = SerializerFeature.values().length; + assertTrue(len <= 32); + } }