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

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