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

https://github.com/flydream/fastjson · Java · 578 lines · 499 code · 64 blank · 15 comment · 0 complexity · feb2744de13fec0ce368019d7bfe6722 MD5 · raw file

  1. /*
  2. * Copyright 1999-2101 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.json.bvt.parser;
  17. import static com.alibaba.fastjson.util.TypeUtils.castToBigDecimal;
  18. import static com.alibaba.fastjson.util.TypeUtils.castToBigInteger;
  19. import static com.alibaba.fastjson.util.TypeUtils.castToBoolean;
  20. import static com.alibaba.fastjson.util.TypeUtils.castToByte;
  21. import static com.alibaba.fastjson.util.TypeUtils.castToDate;
  22. import static com.alibaba.fastjson.util.TypeUtils.castToDouble;
  23. import static com.alibaba.fastjson.util.TypeUtils.castToFloat;
  24. import static com.alibaba.fastjson.util.TypeUtils.castToInt;
  25. import static com.alibaba.fastjson.util.TypeUtils.castToLong;
  26. import static com.alibaba.fastjson.util.TypeUtils.castToShort;
  27. import static com.alibaba.fastjson.util.TypeUtils.castToString;
  28. import java.io.Reader;
  29. import java.lang.reflect.Method;
  30. import java.lang.reflect.Type;
  31. import java.math.BigDecimal;
  32. import java.math.BigInteger;
  33. import java.util.Calendar;
  34. import java.util.Collection;
  35. import java.util.Date;
  36. import java.util.List;
  37. import java.util.Map;
  38. import junit.framework.Assert;
  39. import junit.framework.TestCase;
  40. import com.alibaba.fastjson.JSON;
  41. import com.alibaba.fastjson.parser.DefaultExtJSONParser;
  42. import com.alibaba.fastjson.parser.Feature;
  43. import com.alibaba.fastjson.util.TypeUtils;
  44. public class DefaultExtJSONParserTest extends TestCase {
  45. public void test_parseObject() {
  46. User user = new User();
  47. user.setName("校长");
  48. user.setAge(3);
  49. user.setSalary(new BigDecimal("123456789.0123"));
  50. String jsonString = JSON.toJSONString(user);
  51. System.out.println(jsonString);
  52. JSON.parseObject(jsonString);
  53. DefaultExtJSONParser parser = new DefaultExtJSONParser(jsonString);
  54. User user1 = new User();
  55. parser.parseObject(user1);
  56. Assert.assertEquals(user.getAge(), user1.getAge());
  57. Assert.assertEquals(user.getName(), user1.getName());
  58. Assert.assertEquals(user.getSalary(), user1.getSalary());
  59. }
  60. public void testCastCalendar() throws Exception {
  61. Calendar c = Calendar.getInstance();
  62. Date d = TypeUtils.castToDate(c);
  63. Assert.assertEquals(c.getTime(), d);
  64. }
  65. public void testCast() throws Exception {
  66. new TypeUtils();
  67. DefaultExtJSONParser parser = new DefaultExtJSONParser("");
  68. Assert.assertNull(castToByte(null));
  69. Assert.assertNull(castToShort(null));
  70. Assert.assertNull(castToInt(null));
  71. Assert.assertNull(castToLong(null));
  72. Assert.assertNull(castToBigInteger(null));
  73. Assert.assertNull(castToBigDecimal(null));
  74. Assert.assertNull(castToFloat(null));
  75. Assert.assertNull(castToDouble(null));
  76. Assert.assertNull(castToBoolean(null));
  77. Assert.assertNull(castToDate(null));
  78. Assert.assertNull(castToString(null));
  79. Assert.assertEquals(12, castToByte("12").intValue());
  80. Assert.assertEquals(1234, castToShort("1234").intValue());
  81. Assert.assertEquals(1234, castToInt("1234").intValue());
  82. Assert.assertEquals(1234, castToLong("1234").intValue());
  83. Assert.assertEquals(1234, castToBigInteger("1234").intValue());
  84. Assert.assertEquals(1234, castToBigDecimal("1234").intValue());
  85. Assert.assertEquals(1234, castToFloat("1234").intValue());
  86. Assert.assertEquals(1234, castToDouble("1234").intValue());
  87. Assert.assertEquals(12, castToByte(12).intValue());
  88. Assert.assertEquals(1234, castToShort(1234).intValue());
  89. Assert.assertEquals(1234, castToInt(1234).intValue());
  90. Assert.assertEquals(1234, castToLong(1234).intValue());
  91. Assert.assertEquals(1234, castToBigInteger(1234).intValue());
  92. Assert.assertEquals(1234, castToBigDecimal(1234).intValue());
  93. Assert.assertEquals(1234, castToFloat(1234).intValue());
  94. Assert.assertEquals(1234, castToDouble(1234).intValue());
  95. Assert.assertEquals(Boolean.TRUE, castToBoolean(true));
  96. Assert.assertEquals(Boolean.FALSE, castToBoolean(false));
  97. Assert.assertEquals(Boolean.TRUE, castToBoolean(1));
  98. Assert.assertEquals(Boolean.FALSE, castToBoolean(0));
  99. Assert.assertEquals(Boolean.TRUE, castToBoolean("true"));
  100. Assert.assertEquals(Boolean.FALSE, castToBoolean("false"));
  101. long time = System.currentTimeMillis();
  102. Assert.assertEquals(time, castToDate(new Date(time)).getTime());
  103. Assert.assertEquals(time, castToDate(time).getTime());
  104. Assert.assertEquals(time, castToDate(Long.toString(time)).getTime());
  105. Assert.assertEquals("true", castToString("true"));
  106. Assert.assertEquals("true", castToString(true));
  107. Assert.assertEquals("123", castToString(123));
  108. Assert.assertEquals(new BigDecimal("2"), castToBigDecimal("2"));
  109. Assert.assertEquals(new BigDecimal("2"), castToBigDecimal(new BigInteger("2")));
  110. }
  111. public void test_casterror2() {
  112. DefaultExtJSONParser parser = new DefaultExtJSONParser("");
  113. {
  114. Exception error = null;
  115. try {
  116. castToByte(new Object());
  117. } catch (Exception ex) {
  118. error = ex;
  119. }
  120. Assert.assertNotNull(error);
  121. }
  122. {
  123. Exception error = null;
  124. try {
  125. castToShort(new Object());
  126. } catch (Exception ex) {
  127. error = ex;
  128. }
  129. Assert.assertNotNull(error);
  130. }
  131. {
  132. Exception error = null;
  133. try {
  134. castToInt(new Object());
  135. } catch (Exception ex) {
  136. error = ex;
  137. }
  138. Assert.assertNotNull(error);
  139. }
  140. {
  141. Exception error = null;
  142. try {
  143. castToLong(new Object());
  144. } catch (Exception ex) {
  145. error = ex;
  146. }
  147. Assert.assertNotNull(error);
  148. }
  149. {
  150. Exception error = null;
  151. try {
  152. castToFloat(new Object());
  153. } catch (Exception ex) {
  154. error = ex;
  155. }
  156. Assert.assertNotNull(error);
  157. }
  158. {
  159. Exception error = null;
  160. try {
  161. castToDouble(new Object());
  162. } catch (Exception ex) {
  163. error = ex;
  164. }
  165. Assert.assertNotNull(error);
  166. }
  167. {
  168. Exception error = null;
  169. try {
  170. castToBigInteger(new Object());
  171. } catch (Exception ex) {
  172. error = ex;
  173. }
  174. Assert.assertNotNull(error);
  175. }
  176. {
  177. Exception error = null;
  178. try {
  179. castToBigDecimal(new Object());
  180. } catch (Exception ex) {
  181. error = ex;
  182. }
  183. Assert.assertNotNull(error);
  184. }
  185. {
  186. Exception error = null;
  187. try {
  188. castToDate(new Object());
  189. } catch (Exception ex) {
  190. error = ex;
  191. }
  192. Assert.assertNotNull(error);
  193. }
  194. {
  195. Exception error = null;
  196. try {
  197. castToBoolean(new Object());
  198. } catch (Exception ex) {
  199. error = ex;
  200. }
  201. Assert.assertNotNull(error);
  202. }
  203. }
  204. public void test_casterror() {
  205. DefaultExtJSONParser parser = new DefaultExtJSONParser("");
  206. {
  207. Exception error = null;
  208. try {
  209. castToByte("xx");
  210. } catch (Exception ex) {
  211. error = ex;
  212. }
  213. Assert.assertNotNull(error);
  214. }
  215. {
  216. Exception error = null;
  217. try {
  218. castToShort("xx");
  219. } catch (Exception ex) {
  220. error = ex;
  221. }
  222. Assert.assertNotNull(error);
  223. }
  224. {
  225. Exception error = null;
  226. try {
  227. castToInt("xx");
  228. } catch (Exception ex) {
  229. error = ex;
  230. }
  231. Assert.assertNotNull(error);
  232. }
  233. {
  234. Exception error = null;
  235. try {
  236. castToLong("xx");
  237. } catch (Exception ex) {
  238. error = ex;
  239. }
  240. Assert.assertNotNull(error);
  241. }
  242. {
  243. Exception error = null;
  244. try {
  245. castToFloat("xx");
  246. } catch (Exception ex) {
  247. error = ex;
  248. }
  249. Assert.assertNotNull(error);
  250. }
  251. {
  252. Exception error = null;
  253. try {
  254. castToDouble("xx");
  255. } catch (Exception ex) {
  256. error = ex;
  257. }
  258. Assert.assertNotNull(error);
  259. }
  260. {
  261. Exception error = null;
  262. try {
  263. castToBigInteger("xx");
  264. } catch (Exception ex) {
  265. error = ex;
  266. }
  267. Assert.assertNotNull(error);
  268. }
  269. {
  270. Exception error = null;
  271. try {
  272. castToBigDecimal("xx");
  273. } catch (Exception ex) {
  274. error = ex;
  275. }
  276. Assert.assertNotNull(error);
  277. }
  278. {
  279. Exception error = null;
  280. try {
  281. castToDate("xx");
  282. } catch (Exception ex) {
  283. error = ex;
  284. }
  285. Assert.assertNotNull(error);
  286. }
  287. {
  288. Exception error = null;
  289. try {
  290. castToBoolean("xx");
  291. } catch (Exception ex) {
  292. error = ex;
  293. }
  294. Assert.assertNotNull(error);
  295. }
  296. }
  297. @SuppressWarnings("rawtypes")
  298. public void test_parseArrayWithType() throws Exception {
  299. Method method = DefaultExtJSONParserTest.class.getMethod("f", Collection.class, Collection.class, Collection.class, Collection.class, Collection.class,
  300. Collection.class, Collection.class);
  301. Type[] types = method.getGenericParameterTypes();
  302. {
  303. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  304. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  305. Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[0])).get(0) instanceof Map);
  306. }
  307. {
  308. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  309. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  310. Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[1])).get(0) instanceof User);
  311. }
  312. {
  313. Exception error = null;
  314. try {
  315. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  316. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  317. parser.parseArrayWithType(types[2]);
  318. ;
  319. } catch (Exception ex) {
  320. error = ex;
  321. }
  322. Assert.assertNotNull(error);
  323. }
  324. {
  325. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  326. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  327. Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[3])).get(0) instanceof User);
  328. }
  329. {
  330. Exception error = null;
  331. try {
  332. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  333. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  334. parser.parseArrayWithType(types[4]);
  335. ;
  336. } catch (Exception ex) {
  337. error = ex;
  338. }
  339. Assert.assertNotNull(error);
  340. }
  341. {
  342. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  343. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  344. Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[5])).get(0) instanceof User);
  345. }
  346. {
  347. Exception error = null;
  348. try {
  349. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  350. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  351. parser.parseArrayWithType(types[6]);
  352. ;
  353. } catch (Exception ex) {
  354. error = ex;
  355. }
  356. Assert.assertNotNull(error);
  357. }
  358. }
  359. public void test_parseArrayWithType_error_1() throws Exception {
  360. Method method = DefaultExtJSONParserTest.class.getMethod("f", Collection.class, Collection.class, Collection.class, Collection.class, Collection.class,
  361. Collection.class, Collection.class);
  362. Type[] types = method.getGenericParameterTypes();
  363. Exception error = null;
  364. try {
  365. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
  366. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  367. parser.parseArrayWithType(types[6]);
  368. ;
  369. } catch (Exception ex) {
  370. error = ex;
  371. }
  372. Assert.assertNotNull(error);
  373. }
  374. public static <T extends Object & Comparable<? super T>, T1 extends User> void f(Collection<?> p0, Collection<? extends User> p1,
  375. Collection<? super User> p2, Collection<User> p3, Collection<T> p4,
  376. Collection<T1> p5, Collection<T[]> p6) {
  377. }
  378. public void test_not_match() throws Exception {
  379. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123, \"kxxx\":33}]";
  380. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  381. Assert.assertEquals(true, (parser.parseArray(User.class).get(0) instanceof User));
  382. }
  383. public void test_not_match_error() throws Exception {
  384. Exception error = null;
  385. try {
  386. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123, \"kxxx\":33}]";
  387. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  388. parser.config(Feature.IgnoreNotMatch, false);
  389. Assert.assertEquals(true, (parser.parseArray(User.class).get(0) instanceof User));
  390. } catch (Exception ex) {
  391. error = ex;
  392. }
  393. Assert.assertNotNull(error);
  394. }
  395. public void test_error() throws Exception {
  396. {
  397. Exception error = null;
  398. try {
  399. String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123]";
  400. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  401. parser.parseArray(User.class);
  402. } catch (Exception ex) {
  403. error = ex;
  404. }
  405. Assert.assertNotNull(error);
  406. }
  407. {
  408. Exception error = null;
  409. try {
  410. String text = "{\"reader\":3}";
  411. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  412. parser.parseObject(ErrorObject.class);
  413. } catch (Exception ex) {
  414. error = ex;
  415. }
  416. Assert.assertNotNull(error);
  417. }
  418. {
  419. Exception error = null;
  420. try {
  421. String text = "{\"name\":3}";
  422. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  423. parser.parseObject(ErrorObject2.class);
  424. } catch (Exception ex) {
  425. error = ex;
  426. }
  427. Assert.assertNotNull(error);
  428. }
  429. }
  430. public static class ErrorObject {
  431. private Reader reader;
  432. public Reader getReader() {
  433. return reader;
  434. }
  435. public void setReader(Reader reader) {
  436. this.reader = reader;
  437. }
  438. }
  439. public static class ErrorObject2 {
  440. private String name;
  441. public String getName() {
  442. return name;
  443. }
  444. public void setName(String name) {
  445. throw new UnsupportedOperationException();
  446. }
  447. }
  448. public void test_error2() throws Exception {
  449. {
  450. Exception error = null;
  451. try {
  452. String text = "{}";
  453. DefaultExtJSONParser parser = new DefaultExtJSONParser(text);
  454. parser.parseArray(User.class);
  455. } catch (Exception ex) {
  456. error = ex;
  457. }
  458. Assert.assertNotNull(error);
  459. }
  460. }
  461. public static class User {
  462. private String name;
  463. private int age;
  464. private BigDecimal salary;
  465. private Date birthdate;
  466. private boolean old;
  467. public boolean isOld() {
  468. return old;
  469. }
  470. public void setOld(boolean old) {
  471. this.old = old;
  472. }
  473. public Date getBirthdate() {
  474. return birthdate;
  475. }
  476. public void setBirthdate(Date birthdate) {
  477. this.birthdate = birthdate;
  478. }
  479. public String getName() {
  480. return name;
  481. }
  482. public void setName(String name) {
  483. this.name = name;
  484. }
  485. public int getAge() {
  486. return age;
  487. }
  488. public void setAge(int age) {
  489. this.age = age;
  490. }
  491. public void setage(int age) {
  492. throw new UnsupportedOperationException();
  493. }
  494. public void set(int age) {
  495. throw new UnsupportedOperationException();
  496. }
  497. public void get(int age) {
  498. throw new UnsupportedOperationException();
  499. }
  500. public void is(int age) {
  501. throw new UnsupportedOperationException();
  502. }
  503. public BigDecimal getSalary() {
  504. return salary;
  505. }
  506. public void setSalary(BigDecimal salary) {
  507. this.salary = salary;
  508. }
  509. public static void setFF() {
  510. }
  511. void setXX() {
  512. }
  513. }
  514. }