/src/test/java/com/alibaba/json/bvt/support/hsf/HSFJSONUtilsTest_0.java

https://github.com/alibaba/fastjson · Java · 232 lines · 191 code · 34 blank · 7 comment · 10 complexity · 526484d138d22d9ed06cf4ae6b38e145 MD5 · raw file

  1. package com.alibaba.json.bvt.support.hsf;
  2. import com.alibaba.fastjson.support.hsf.HSFJSONUtils;
  3. import com.alibaba.fastjson.support.hsf.MethodLocator;
  4. import junit.framework.TestCase;
  5. import java.lang.reflect.Method;
  6. import java.util.List;
  7. public class HSFJSONUtilsTest_0 extends TestCase {
  8. private Method method_f2;
  9. private Method method_f3;
  10. private Method method_f4;
  11. private Method method_f5;
  12. private MethodLocator methodLocator;
  13. protected void setUp() throws Exception {
  14. method_f2 = Service.class.getMethod("f2", String.class, Model.class);
  15. method_f3 = Service.class.getMethod("f3", String.class, List.class);
  16. method_f4 = Service.class.getMethod("f4", List.class);
  17. method_f5 = Service.class.getMethod("f4", User[].class);
  18. methodLocator = new MethodLocator() {
  19. public Method findMethod(String[] types) {
  20. if (types == null) {
  21. return method_f2;
  22. }
  23. if (types.length == 1 && types[0].equals("java.util.List")) {
  24. return method_f4;
  25. }
  26. if (types.length == 1 && types[0].equals("com.alibaba.json.bvt.support.hsf.HSFJSONUtilsTest_0.User[]")) {
  27. return method_f5;
  28. }
  29. if (types[1].equals("java.util.List")) {
  30. return method_f3;
  31. }
  32. return method_f2;
  33. }
  34. };
  35. }
  36. public void test_invoke() throws Exception {
  37. String json = "{ \n" +
  38. " \"argsTypes\" : [ \"java.lang.String\", \"com.alibaba.json.bvt.support.hsf.HSFJSONUtilsTest_0$Model\"],\n" +
  39. " \"argsObjs\" : [ \"abc\", {\"value\":\"xxx\"} ]\n" +
  40. "}";
  41. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  42. assertNotNull(values);
  43. assertEquals(2, values.length);
  44. assertEquals("abc", values[0]);
  45. assertEquals("xxx", ((Model) values[1]).value);
  46. }
  47. public void test_invoke_1() throws Exception {
  48. String json = "{ \n" +
  49. " \"argsObjs\" : [ \"abc\", {\"value\":\"xxx\"} ]\n" +
  50. "}";
  51. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  52. assertNotNull(values);
  53. assertEquals(2, values.length);
  54. assertEquals("abc", values[0]);
  55. assertEquals("xxx", ((Model) values[1]).value);
  56. }
  57. public void test_invoke_null() throws Exception {
  58. String json = "{ \n" +
  59. " \"argsTypes\" : [ \"java.lang.String\", \"com.alibaba.json.bvt.support.hsf.HSFJSONUtilsTest_0$Model\"],\n" +
  60. " \"argsObjs\" : [ null, null ]\n" +
  61. "}";
  62. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  63. assertNotNull(values);
  64. assertEquals(2, values.length);
  65. assertEquals(null, values[0]);
  66. assertEquals(null, values[1]);
  67. }
  68. public void test_invoke_list() throws Exception {
  69. String json = "{ \n" +
  70. " \"argsTypes\" : [ \"java.lang.String\", \"java.util.List\"],\n" +
  71. " \"argsObjs\" : [ \"abc\", [" +
  72. "{" +
  73. " \"value\":\"xxx\"" +
  74. " }] ]\n" +
  75. "}";
  76. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  77. assertNotNull(values);
  78. assertEquals(2, values.length);
  79. assertEquals("abc", values[0]);
  80. List list = (List) values[1];
  81. assertEquals("xxx", ((Model) list.get(0)).value);
  82. }
  83. public void test_invoke_list_f4() throws Exception {
  84. String json = "{\n" +
  85. " \"argsTypes\": [\"java.util.List\"],\n" +
  86. " \n" +
  87. " \"argsObjs\": [\n" +
  88. " [\n" +
  89. " \t\t{\n" +
  90. " \t\t\t\"name\": \"123\",\n" +
  91. " \t\t\t\"id\": 123,\n" +
  92. " \t\t\t\"age\": 123\n" +
  93. " \t\t},\n" +
  94. " \t\t{\n" +
  95. " \t\t\t\"name\": \"123\",\n" +
  96. " \t\t\t\"id\": 123,\n" +
  97. " \t\t\t\"age\": 123\n" +
  98. " \t\t}\n" +
  99. "\t\t]\n" +
  100. " ]\n" +
  101. "}";
  102. // System.out.println(json);
  103. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  104. assertNotNull(values);
  105. assertEquals(1, values.length);
  106. List list = (List) values[0];
  107. assertEquals("123", ((User) list.get(0)).name);
  108. assertEquals("123", ((User) list.get(1)).name);
  109. }
  110. public void test_invoke_list_f5() throws Exception {
  111. String json = " [\n" +
  112. " \t[\"java.util.List\"],\n" +
  113. " [\n" +
  114. " \t\t[{\n" +
  115. " \t\t\t\"name\": \"123\",\n" +
  116. " \t\t\t\"id\": 123,\n" +
  117. " \t\t\t\"age\": 123\n" +
  118. " \t\t},\n" +
  119. " \t\t{\n" +
  120. " \t\t\t\"name\": \"123\",\n" +
  121. " \t\t\t\"id\": 123,\n" +
  122. " \t\t\t\"age\": 123\n" +
  123. " \t\t}]\n" +
  124. " ]\n" +
  125. "]";
  126. System.out.println(json);
  127. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  128. assertNotNull(values);
  129. assertEquals(1, values.length);
  130. List list = (List) values[0];
  131. assertEquals("123", ((User) list.get(0)).name);
  132. assertEquals("123", ((User) list.get(1)).name);
  133. }
  134. public void test_invoke_array() throws Exception {
  135. String json = "{\n" +
  136. " \"argsTypes\": [\"com.alibaba.json.bvt.support.hsf.HSFJSONUtilsTest_0.User[]\"],\n" +
  137. " \n" +
  138. " \"argsObjs\": [\n" +
  139. " [\n" +
  140. " \t\t{\n" +
  141. " \t\t\t\"name\": \"123\",\n" +
  142. " \t\t\t\"id\": 123,\n" +
  143. " \t\t\t\"age\": 123\n" +
  144. " \t\t},\n" +
  145. " \t\t{\n" +
  146. " \t\t\t\"name\": \"123\",\n" +
  147. " \t\t\t\"id\": 123,\n" +
  148. " \t\t\t\"age\": 123\n" +
  149. " \t\t}\n" +
  150. "\t\t]\n" +
  151. " ]\n" +
  152. "}";
  153. // System.out.println(json);
  154. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  155. assertNotNull(values);
  156. assertEquals(1, values.length);
  157. User[] list = (User[]) values[0];
  158. assertEquals("123", ((User) list[0]).name);
  159. assertEquals("123", ((User) list[1]).name);
  160. }
  161. // public void test_perf() throws Exception {
  162. // for (int i = 0; i < 5; ++i) {
  163. // perf(); // 723
  164. // }
  165. // }
  166. void perf() throws Exception {
  167. long start = System.currentTimeMillis();
  168. String json = "{ \n" +
  169. " \"argsTypes\" : [ \"java.lang.String\", \"com.alibaba.json.bvt.support.hsf.HSFJSONUtilsTest_0$Model\"],\n" +
  170. " \"argsObjs\" : [ \"abc\", {\"value\":\"xxx\"} ]\n" +
  171. "}";
  172. for (int i = 0; i < 1000 * 1000; ++i) {
  173. Object[] values = HSFJSONUtils.parseInvocationArguments(json, methodLocator);
  174. }
  175. long millis = System.currentTimeMillis() - start;
  176. System.out.println("millis : " + millis);
  177. }
  178. public static class Service {
  179. public void f1() {
  180. }
  181. public void f2(String name, Model model) {
  182. }
  183. public void f3(String name, List<Model> models) {
  184. }
  185. public void f4( List<User> models) {
  186. }
  187. public void f4( User[] models) {
  188. }
  189. }
  190. public static class Model {
  191. public String value;
  192. }
  193. public static class User {
  194. public String name;
  195. public int id;
  196. public int age;
  197. }
  198. }