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

https://github.com/alibaba/fastjson · Java · 581 lines · 501 code · 65 blank · 15 comment · 0 complexity · 9f3c7a216e2632bca615ed37f1f65d5d MD5 · raw file

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