/src/test/java/com/alibaba/json/bvt/parser/TypeUtilsTest_interface.java

https://github.com/alibaba/fastjson · Java · 136 lines · 90 code · 43 blank · 3 comment · 0 complexity · 4616994739619045766812ac15cf6d62 MD5 · raw file

  1. package com.alibaba.json.bvt.parser;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import junit.framework.TestCase;
  5. import org.junit.Assert;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.TypeReference;
  8. import com.alibaba.fastjson.annotation.JSONField;
  9. public class TypeUtilsTest_interface extends TestCase {
  10. public void test_castToJavaBean() throws Exception {
  11. VO vo = new VO();
  12. vo.setId(123);
  13. vo.setName("abc");
  14. Assert.assertEquals("{\"ID\":123,\"name\":\"abc\"}", JSON.toJSONString(vo));
  15. }
  16. public void test_parse() throws Exception {
  17. VO vo = JSON.parseObject("{\"xid\":123,\"name\":\"abc\"}", VO.class);
  18. Assert.assertEquals(123, vo.getId());
  19. Assert.assertEquals("abc", vo.getName());
  20. }
  21. public void test_parse_var() throws Exception {
  22. List<?> list = JSON.parseObject("[]", new TypeReference<List<?>>() {
  23. });
  24. Assert.assertNotNull(list);
  25. Assert.assertEquals(0, list.size());
  26. }
  27. public void test_deser() throws Exception {
  28. JSON.parseObject("{\"id\":123}", new TypeReference<X_I>(){});
  29. }
  30. public void test_deser2() throws Exception {
  31. JSON.parseObject("{\"id\":123}", new TypeReference<X_X<Integer>>(){});
  32. }
  33. public void test_deser2_x() throws Exception {
  34. JSON.parseObject("{\"id\":123}", new TypeReference<X_X<?>>(){});
  35. }
  36. public static class X_I extends X<Integer> {
  37. }
  38. public static class X_X<T> extends X<T> {
  39. }
  40. public static class X<T> {
  41. private T id;
  42. public X(){
  43. }
  44. public T getId() {
  45. return id;
  46. }
  47. public void setId(T id) {
  48. this.id = id;
  49. }
  50. }
  51. public static class VO implements IV {
  52. private int id;
  53. private String name;
  54. public int getId() {
  55. return id;
  56. }
  57. public void setId(int id) {
  58. this.id = id;
  59. }
  60. public String getName() {
  61. return name;
  62. }
  63. public void setName(String name) {
  64. this.name = name;
  65. }
  66. public String getName(String xx) {
  67. return null;
  68. }
  69. public String getName(String xx, int v) {
  70. // TODO Auto-generated method stub
  71. return null;
  72. }
  73. @JSONField(deserialize = false)
  74. public void setName(int value) {
  75. // TODO Auto-generated method stub
  76. }
  77. public void setName(int value, int x) {
  78. // TODO Auto-generated method stub
  79. }
  80. }
  81. public static interface IV {
  82. @JSONField(name = "ID")
  83. int getId();
  84. @JSONField(name = "xid")
  85. void setId(int value);
  86. @JSONField(name = "NAME")
  87. String getName(String xx);
  88. @JSONField(name = "NAME")
  89. String getName(String xx, int v);
  90. @JSONField(name = "xid_1")
  91. void setName(int value);
  92. }
  93. }