/src/test/java/com/alibaba/json/bvt/bug/Bug_for_generic.java

https://github.com/alibaba/fastjson · Java · 316 lines · 211 code · 67 blank · 38 comment · 23 complexity · 140455a71c2a3f3a198f09ce5809129b MD5 · raw file

  1. package com.alibaba.json.bvt.bug;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import com.alibaba.fastjson.parser.ParserConfig;
  6. import junit.framework.TestCase;
  7. import com.alibaba.fastjson.JSON;
  8. public class Bug_for_generic extends TestCase {
  9. protected void setUp() throws Exception {
  10. ParserConfig.global.addAccept("NotifyDetail");
  11. }
  12. public void test() throws Exception {
  13. String json = "{\"@type\":\"com.alibaba.json.bvt.bug.Bug_for_generic$NotifyDetail\",\"args\":[\"61354557\",\"依依\",\"六\"],\"destId\":60721687,\"detailId\":3155063,\"display\":false,\"foundTime\":{\"@type\":\"java.sql.Timestamp\",\"val\":1344530416000},\"hotId\":0,\"srcId\":1000,\"templateId\":482}";
  14. JSON.parseObject(json, NotifyDetail.class);
  15. System.out.println("NotifyDetail对象没问题");
  16. String json2 = "{\"@type\":\"com.alibaba.json.bvt.bug.Bug_for_generic$Pagination\",\"fromIndex\":0,\"list\":[{\"@type\":\"NotifyDetail\",\"args\":[\"61354557\",\"依依\",\"六\"],\"destId\":60721687,\"detailId\":3155063,\"display\":false,\"foundTime\":{\"@type\":\"java.sql.Timestamp\",\"val\":1344530416000},\"hotId\":0,\"srcId\":1000,\"templateId\":482},{\"@type\":\"NotifyDetail\",\"args\":[\"14527269\",\"懒洋洋\",\"///最佳拍档,非常\",\"24472950\"],\"destId\":60721687,\"detailId\":3151609,\"display\":false,\"foundTime\":{\"@type\":\"java.sql.Timestamp\",\"val\":1344354485000},\"hotId\":0,\"srcId\":1000,\"templateId\":40},{\"@type\":\"NotifyDetail\",\"args\":[\"51090218\",\"天之涯\",\"天会黑,人会变。三分\"],\"destId\":60721687,\"detailId\":3149221,\"display\":false,\"foundTime\":{\"@type\":\"java.sql.Timestamp\",\"val\":1344247529000},\"hotId\":0,\"srcId\":1000,\"templateId\":459},{\"@type\":\"NotifyDetail\",\"args\":[\"51687981\",\"摹然回首梦已成年\",\"星星在哪里都很亮的,\"],\"destId\":60721687,\"detailId\":3149173,\"display\":false,\"foundTime\":{\"@type\":\"java.sql.Timestamp\",\"val\":1344247414000},\"hotId\":0,\"srcId\":1000,\"templateId\":459},{\"@type\":\"NotifyDetail\",\"args\":[\"41486427\",\"寒江蓑笠\",\"双休了\"],\"destId\":60721687,\"detailId\":3148148,\"display\":false,\"foundTime\":{\"@type\":\"java.sql.Timestamp\",\"val\":1344244730000},\"hotId\":0,\"srcId\":1000,\"templateId\":459}],\"maxLength\":5,\"nextPage\":2,\"pageIndex\":1,\"prevPage\":1,\"toIndex\":5,\"totalPage\":3,\"totalResult\":13}";
  17. JSON.parseObject(json2, Pagination.class);
  18. }
  19. public static class Pagination<T> implements Serializable {
  20. /**
  21. *
  22. */
  23. private static final long serialVersionUID = 5038839734218582220L;
  24. private int totalResult = 0;
  25. private int totalPage = 1;
  26. private int pageIndex = 1;
  27. private int maxLength = 5;
  28. private int fromIndex = 0;
  29. private int toIndex = 0;
  30. private List<T> list = new ArrayList<T>();
  31. public Pagination(){
  32. }
  33. public void setTotalResult(int totalResult) {
  34. this.totalResult = totalResult;
  35. }
  36. public void setTotalPage(int totalPage) {
  37. this.totalPage = totalPage;
  38. }
  39. public void setPageIndex(int pageIndex) {
  40. this.pageIndex = pageIndex;
  41. }
  42. public void setMaxLength(int maxLength) {
  43. this.maxLength = maxLength;
  44. }
  45. public void setFromIndex(int fromIndex) {
  46. this.fromIndex = fromIndex;
  47. }
  48. public void setToIndex(int toIndex) {
  49. this.toIndex = toIndex;
  50. }
  51. public int getFromIndex() {
  52. return this.fromIndex;
  53. }
  54. public int getToIndex() {
  55. return this.toIndex;
  56. }
  57. public int getNextPage() {
  58. if (this.pageIndex < this.totalPage) {
  59. return this.pageIndex + 1;
  60. } else {
  61. return this.pageIndex;
  62. }
  63. }
  64. public int getPrevPage() {
  65. if (this.pageIndex > 1) {
  66. return this.pageIndex - 1;
  67. }
  68. return this.pageIndex;
  69. }
  70. /**
  71. * @return the currentPage
  72. */
  73. public int getPageIndex() {
  74. return pageIndex;
  75. }
  76. /**
  77. * @return the list
  78. */
  79. public List<T> getList() {
  80. if (list == null) {
  81. return new ArrayList<T>();
  82. }
  83. return new ArrayList<T>(list);
  84. }
  85. /**
  86. * @return the totalPage
  87. */
  88. public int getTotalPage() {
  89. return totalPage;
  90. }
  91. /**
  92. * @return the totalRecord
  93. */
  94. public int getTotalResult() {
  95. return totalResult;
  96. }
  97. public int getMaxLength() {
  98. return maxLength;
  99. }
  100. /**
  101. * @param totalResult
  102. * @param pageIndex
  103. * @param maxLength
  104. */
  105. public Pagination(int totalResult, int pageIndex, int maxLength){
  106. if (maxLength > 0) {
  107. this.maxLength = maxLength;
  108. }
  109. if (totalResult > 0) {
  110. this.totalResult = totalResult;
  111. }
  112. if (pageIndex > 0) {
  113. this.pageIndex = pageIndex;
  114. }
  115. this.totalPage = this.totalResult / this.maxLength;
  116. if (this.totalResult % this.maxLength != 0) {
  117. this.totalPage = this.totalPage + 1;
  118. }
  119. if (this.totalPage == 0) {
  120. this.totalPage = 1;
  121. }
  122. if (this.pageIndex > this.totalPage) {
  123. this.pageIndex = this.totalPage;
  124. }
  125. if (this.pageIndex <= 0) {
  126. this.pageIndex = 1;
  127. }
  128. this.fromIndex = (this.pageIndex - 1) * maxLength;
  129. this.toIndex = this.fromIndex + maxLength;
  130. if (this.toIndex < 0) {
  131. this.toIndex = fromIndex;
  132. }
  133. if (this.toIndex > this.totalResult) {
  134. this.toIndex = this.totalResult;
  135. }
  136. }
  137. /**
  138. * @param datas the datas to set
  139. */
  140. public void setList(List<T> list) {
  141. this.list = list;
  142. }
  143. /*
  144. * (non-Javadoc)
  145. * @see java.lang.Object#toString()
  146. */
  147. @Override
  148. public String toString() {
  149. StringBuffer sb = new StringBuffer();
  150. sb.append("Pagination:\r\n");
  151. sb.append("\tpageIndex\t" + this.pageIndex + "\r\n");
  152. sb.append("\ttotalPage\t" + this.totalPage + "\r\n");
  153. sb.append("\tmaxLength\t" + this.maxLength + "\r\n");
  154. sb.append("\ttotalResult\t" + this.totalResult + "\r\n");
  155. for (T t : list) {
  156. sb.append(t + "\r\n");
  157. }
  158. return sb.toString();
  159. }
  160. }
  161. public static class NotifyDetail implements Serializable {
  162. /**
  163. *
  164. */
  165. private static final long serialVersionUID = 8760630447394929224L;
  166. private int detailId;
  167. private int hotId;
  168. private int templateId;
  169. private int srcId;
  170. private int destId;
  171. private boolean display;
  172. private java.sql.Date foundTime;
  173. private List<String> args = new ArrayList<String>();
  174. public int getDetailId() {
  175. return detailId;
  176. }
  177. public void setDetailId(int detailId) {
  178. this.detailId = detailId;
  179. }
  180. public int getHotId() {
  181. return hotId;
  182. }
  183. public void setHotId(int hotId) {
  184. this.hotId = hotId;
  185. }
  186. public int getTemplateId() {
  187. return templateId;
  188. }
  189. public List<String> getArgs() {
  190. return args;
  191. }
  192. public void setArgs(List<String> args) {
  193. this.args = args;
  194. }
  195. public void setTemplateId(int templateId) {
  196. this.templateId = templateId;
  197. }
  198. public int getSrcId() {
  199. return srcId;
  200. }
  201. public void setSrcId(int srcId) {
  202. this.srcId = srcId;
  203. }
  204. public int getDestId() {
  205. return destId;
  206. }
  207. public void setDestId(int destId) {
  208. this.destId = destId;
  209. }
  210. public boolean isDisplay() {
  211. return display;
  212. }
  213. public void setDisplay(boolean display) {
  214. this.display = display;
  215. }
  216. public java.sql.Date getFoundTime() {
  217. return foundTime;
  218. }
  219. public void setFoundTime(java.sql.Date foundTime) {
  220. this.foundTime = foundTime;
  221. }
  222. /*
  223. * (non-Javadoc)
  224. * @see java.lang.Object#hashCode()
  225. */
  226. @Override
  227. public int hashCode() {
  228. int hasCode = 0;
  229. if (this.detailId != 0) {
  230. hasCode += this.detailId;
  231. }
  232. return hasCode;
  233. }
  234. /*
  235. * (non-Javadoc)
  236. * @see java.lang.Object#equals(java.lang.Object)
  237. */
  238. @Override
  239. public boolean equals(Object obj) {
  240. if (obj == null) {
  241. return false;
  242. }
  243. if (!(obj instanceof NotifyDetail)) {
  244. return false;
  245. }
  246. return this.hashCode() == obj.hashCode();
  247. }
  248. }
  249. }