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

https://github.com/flydream/fastjson · Java · 418 lines · 322 code · 96 blank · 0 comment · 4 complexity · bc704fcb7c1f7422bf18d531ac9acce0 MD5 · raw file

  1. package com.alibaba.json.bvt.parser;
  2. import java.lang.reflect.Method;
  3. import java.math.BigDecimal;
  4. import java.math.BigInteger;
  5. import java.util.ArrayList;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import junit.framework.TestCase;
  12. import org.junit.Assert;
  13. import com.alibaba.fastjson.JSON;
  14. import com.alibaba.fastjson.JSONException;
  15. import com.alibaba.fastjson.JSONObject;
  16. import com.alibaba.fastjson.parser.ParserConfig;
  17. import com.alibaba.fastjson.util.TypeUtils;
  18. @SuppressWarnings("rawtypes")
  19. public class TypeUtilsTest extends TestCase {
  20. public void test_0() throws Exception {
  21. HashMap map = new HashMap();
  22. Assert.assertTrue(map == TypeUtils.castToJavaBean(map, Map.class));
  23. }
  24. public void test_1() throws Exception {
  25. JSONObject map = new JSONObject();
  26. Assert.assertTrue(map == TypeUtils.castToJavaBean(map, Map.class));
  27. }
  28. public void test_2() throws Exception {
  29. JSONObject map = new JSONObject();
  30. map.put("id", 1);
  31. map.put("name", "panlei");
  32. User user = TypeUtils.castToJavaBean(map, User.class);
  33. Assert.assertEquals(1L, user.getId());
  34. Assert.assertEquals("panlei", user.getName());
  35. }
  36. public void test_cast_Integer() throws Exception {
  37. JSONObject json = new JSONObject();
  38. json.put("id", 1L);
  39. Assert.assertEquals(new Integer(1), json.getObject("id", int.class));
  40. }
  41. public void test_cast_Integer_2() throws Exception {
  42. JSONObject json = new JSONObject();
  43. json.put("id", 1L);
  44. Assert.assertEquals(new Integer(1), json.getObject("id", Integer.class));
  45. }
  46. public void test_cast_to_long() throws Exception {
  47. JSONObject json = new JSONObject();
  48. json.put("id", 1);
  49. Assert.assertEquals(new Long(1), json.getObject("id", long.class));
  50. }
  51. public void test_cast_to_Long() throws Exception {
  52. JSONObject json = new JSONObject();
  53. json.put("id", 1);
  54. Assert.assertEquals(new Long(1), json.getObject("id", Long.class));
  55. }
  56. public void test_cast_to_short() throws Exception {
  57. JSONObject json = new JSONObject();
  58. json.put("id", 1);
  59. Assert.assertEquals(new Short((short) 1), json.getObject("id", short.class));
  60. }
  61. public void test_cast_to_Short() throws Exception {
  62. JSONObject json = new JSONObject();
  63. json.put("id", 1);
  64. Assert.assertEquals(new Short((short) 1), json.getObject("id", Short.class));
  65. }
  66. public void test_cast_to_byte() throws Exception {
  67. JSONObject json = new JSONObject();
  68. json.put("id", 1);
  69. Assert.assertEquals(new Byte((byte) 1), json.getObject("id", byte.class));
  70. }
  71. public void test_cast_to_Byte() throws Exception {
  72. JSONObject json = new JSONObject();
  73. json.put("id", 1);
  74. Assert.assertEquals(new Byte((byte) 1), json.getObject("id", Byte.class));
  75. }
  76. public void test_cast_to_BigInteger() throws Exception {
  77. JSONObject json = new JSONObject();
  78. json.put("id", 1);
  79. Assert.assertEquals(new BigInteger("1"), json.getObject("id", BigInteger.class));
  80. }
  81. public void test_cast_to_BigDecimal() throws Exception {
  82. JSONObject json = new JSONObject();
  83. json.put("id", 1);
  84. Assert.assertEquals(new BigDecimal("1"), json.getObject("id", BigDecimal.class));
  85. }
  86. public void test_cast_to_boolean() throws Exception {
  87. JSONObject json = new JSONObject();
  88. json.put("id", 1);
  89. Assert.assertEquals(Boolean.TRUE, json.getObject("id", boolean.class));
  90. }
  91. public void test_cast_to_Boolean() throws Exception {
  92. JSONObject json = new JSONObject();
  93. json.put("id", 1);
  94. Assert.assertEquals(Boolean.TRUE, json.getObject("id", Boolean.class));
  95. }
  96. public void test_cast_null() throws Exception {
  97. JSONObject json = new JSONObject();
  98. json.put("id", null);
  99. Assert.assertEquals(null, json.getObject("id", Boolean.class));
  100. }
  101. public void test_cast_to_String() throws Exception {
  102. JSONObject json = new JSONObject();
  103. json.put("id", 1);
  104. Assert.assertEquals("1", json.getObject("id", String.class));
  105. }
  106. public void test_cast_to_Date() throws Exception {
  107. long millis = System.currentTimeMillis();
  108. JSONObject json = new JSONObject();
  109. json.put("date", millis);
  110. Assert.assertEquals(new Date(millis), json.getObject("date", Date.class));
  111. }
  112. public void test_cast_to_SqlDate() throws Exception {
  113. long millis = System.currentTimeMillis();
  114. JSONObject json = new JSONObject();
  115. json.put("date", millis);
  116. Assert.assertEquals(new java.sql.Date(millis), json.getObject("date", java.sql.Date.class));
  117. }
  118. public void test_cast_to_SqlDate_string() throws Exception {
  119. long millis = System.currentTimeMillis();
  120. JSONObject json = new JSONObject();
  121. json.put("date", Long.toString(millis));
  122. Assert.assertEquals(new java.sql.Date(millis), json.getObject("date", java.sql.Date.class));
  123. }
  124. public void test_cast_to_SqlDate_null() throws Exception {
  125. JSONObject json = new JSONObject();
  126. json.put("date", null);
  127. Assert.assertEquals(null, json.getObject("date", java.sql.Date.class));
  128. }
  129. public void test_cast_to_SqlDate_null2() throws Exception {
  130. Assert.assertEquals(null, TypeUtils.castToSqlDate(null));
  131. }
  132. public void test_cast_to_SqlDate_util_Date() throws Exception {
  133. long millis = System.currentTimeMillis();
  134. JSONObject json = new JSONObject();
  135. json.put("date", new Date(millis));
  136. Assert.assertEquals(new java.sql.Date(millis), json.getObject("date", java.sql.Date.class));
  137. }
  138. public void test_cast_to_SqlDate_sql_Date() throws Exception {
  139. long millis = System.currentTimeMillis();
  140. JSONObject json = new JSONObject();
  141. json.put("date", new java.sql.Date(millis));
  142. Assert.assertEquals(new java.sql.Date(millis), json.getObject("date", java.sql.Date.class));
  143. }
  144. public void test_cast_to_SqlDate_sql_Date2() throws Exception {
  145. long millis = System.currentTimeMillis();
  146. java.sql.Date date = new java.sql.Date(millis);
  147. Assert.assertEquals(date, TypeUtils.castToSqlDate(date));
  148. }
  149. public void test_cast_to_SqlDate_calendar() throws Exception {
  150. long millis = System.currentTimeMillis();
  151. Calendar calendar = Calendar.getInstance();
  152. calendar.setTimeInMillis(millis);
  153. JSONObject json = new JSONObject();
  154. json.put("date", calendar);
  155. Assert.assertEquals(new java.sql.Date(millis), json.getObject("date", java.sql.Date.class));
  156. }
  157. public void test_cast_to_SqlDate_error() throws Exception {
  158. JSONObject json = new JSONObject();
  159. json.put("date", 0);
  160. JSONException error = null;
  161. try {
  162. json.getObject("date", java.sql.Date.class);
  163. } catch (JSONException e) {
  164. error = e;
  165. }
  166. Assert.assertNotNull(error);
  167. }
  168. public void test_cast_to_Timestamp() throws Exception {
  169. long millis = System.currentTimeMillis();
  170. JSONObject json = new JSONObject();
  171. json.put("date", millis);
  172. Assert.assertEquals(new java.sql.Timestamp(millis), json.getObject("date", java.sql.Timestamp.class));
  173. }
  174. public void test_cast_to_Timestamp_string() throws Exception {
  175. long millis = System.currentTimeMillis();
  176. JSONObject json = new JSONObject();
  177. json.put("date", Long.toString(millis));
  178. Assert.assertEquals(new java.sql.Timestamp(millis), json.getObject("date", java.sql.Timestamp.class));
  179. }
  180. public void test_cast_to_Timestamp_number() throws Exception {
  181. long millis = System.currentTimeMillis();
  182. JSONObject json = new JSONObject();
  183. json.put("date", new BigDecimal(Long.toString(millis)));
  184. Assert.assertEquals(new java.sql.Timestamp(millis), json.getObject("date", java.sql.Timestamp.class));
  185. }
  186. public void test_cast_to_Timestamp_null() throws Exception {
  187. JSONObject json = new JSONObject();
  188. json.put("date", null);
  189. Assert.assertEquals(null, json.getObject("date", java.sql.Timestamp.class));
  190. }
  191. public void test_cast_to_Timestamp_null2() throws Exception {
  192. Assert.assertEquals(null, TypeUtils.castToTimestamp(null));
  193. }
  194. public void test_cast_to_BigDecimal_same() throws Exception {
  195. BigDecimal value = new BigDecimal("123");
  196. Assert.assertEquals(true, value == TypeUtils.castToBigDecimal(value));
  197. }
  198. public void test_cast_to_BigInteger_same() throws Exception {
  199. BigInteger value = new BigInteger("123");
  200. Assert.assertEquals(true, value == TypeUtils.castToBigInteger(value));
  201. }
  202. public void test_cast_Array() throws Exception {
  203. Assert.assertEquals(Integer[].class, TypeUtils.cast(new ArrayList(), Integer[].class, null).getClass());
  204. }
  205. public void test_cast_to_Timestamp_util_Date() throws Exception {
  206. long millis = System.currentTimeMillis();
  207. JSONObject json = new JSONObject();
  208. json.put("date", new Date(millis));
  209. Assert.assertEquals(new java.sql.Timestamp(millis), json.getObject("date", java.sql.Timestamp.class));
  210. }
  211. public void test_cast_to_Timestamp_sql_Date() throws Exception {
  212. long millis = System.currentTimeMillis();
  213. JSONObject json = new JSONObject();
  214. json.put("date", new java.sql.Date(millis));
  215. Assert.assertEquals(new java.sql.Timestamp(millis), json.getObject("date", java.sql.Timestamp.class));
  216. }
  217. public void test_cast_to_Timestamp_sql_Timestamp() throws Exception {
  218. long millis = System.currentTimeMillis();
  219. java.sql.Timestamp date = new java.sql.Timestamp(millis);
  220. Assert.assertEquals(date, TypeUtils.castToTimestamp(date));
  221. }
  222. public void test_cast_to_Timestamp_calendar() throws Exception {
  223. long millis = System.currentTimeMillis();
  224. Calendar calendar = Calendar.getInstance();
  225. calendar.setTimeInMillis(millis);
  226. JSONObject json = new JSONObject();
  227. json.put("date", calendar);
  228. Assert.assertEquals(new java.sql.Timestamp(millis), json.getObject("date", java.sql.Timestamp.class));
  229. }
  230. public void test_cast_to_Timestamp_error() throws Exception {
  231. JSONObject json = new JSONObject();
  232. json.put("date", 0);
  233. JSONException error = null;
  234. try {
  235. json.getObject("date", java.sql.Timestamp.class);
  236. } catch (JSONException e) {
  237. error = e;
  238. }
  239. Assert.assertNotNull(error);
  240. }
  241. public void test_cast_ab() throws Exception {
  242. B b = new B();
  243. JSONObject json = new JSONObject();
  244. json.put("value", b);
  245. Assert.assertEquals(b, json.getObject("value", A.class));
  246. }
  247. public void test_cast_ab_1() throws Exception {
  248. B b = new B();
  249. JSONObject json = new JSONObject();
  250. json.put("value", b);
  251. Assert.assertEquals(b, json.getObject("value", IA.class));
  252. }
  253. public void test_cast_ab_error() throws Exception {
  254. A a = new A();
  255. JSONObject json = new JSONObject();
  256. json.put("value", a);
  257. JSONException error = null;
  258. try {
  259. json.getObject("value", B.class);
  260. } catch (JSONException e) {
  261. error = e;
  262. }
  263. Assert.assertNotNull(error);
  264. }
  265. public void test_error() throws Exception {
  266. JSONObject json = new JSONObject();
  267. json.put("id", 1);
  268. JSONException error = null;
  269. try {
  270. TypeUtils.castToJavaBean(json, C.class, ParserConfig.getGlobalInstance());
  271. } catch (JSONException e) {
  272. error = e;
  273. }
  274. Assert.assertNotNull(error);
  275. }
  276. public void test_error_2() throws Exception {
  277. JSONObject json = new JSONObject();
  278. json.put("id", 1);
  279. Method method = TypeUtilsTest.class.getMethod("f", List.class);
  280. TypeUtils.cast(json, method.getGenericParameterTypes()[0], ParserConfig.getGlobalInstance());
  281. }
  282. public void test_3() throws Exception {
  283. JSONObject map = new JSONObject();
  284. map.put("id", 1);
  285. map.put("name", "panlei");
  286. User user = JSON.toJavaObject(map, User.class);
  287. Assert.assertEquals(1L, user.getId());
  288. Assert.assertEquals("panlei", user.getName());
  289. }
  290. public static class User {
  291. private long id;
  292. private String name;
  293. public long getId() {
  294. return id;
  295. }
  296. public void setId(long id) {
  297. this.id = id;
  298. }
  299. public String getName() {
  300. return name;
  301. }
  302. public void setName(String name) {
  303. this.name = name;
  304. }
  305. }
  306. public static class A implements IA {
  307. }
  308. public static interface IA {
  309. }
  310. public static class B extends A {
  311. }
  312. public static class C extends B {
  313. public int getId() {
  314. throw new UnsupportedOperationException();
  315. }
  316. public void setId(int id) {
  317. throw new UnsupportedOperationException();
  318. }
  319. }
  320. public static void f(List<?> list) {
  321. }
  322. }