You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerializerTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.serialization;
  17. import java.text.DateFormat;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.Comparator;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. import java.util.Map.Entry;
  30. import java.util.Set;
  31. import java.util.TimeZone;
  32. import com.vaadin.annotations.Widgetset;
  33. import com.vaadin.server.VaadinRequest;
  34. import com.vaadin.shared.Connector;
  35. import com.vaadin.shared.ui.ContentMode;
  36. import com.vaadin.tests.components.AbstractReindeerTestUI;
  37. import com.vaadin.tests.util.Log;
  38. import com.vaadin.tests.widgetset.client.ComplexTestBean;
  39. import com.vaadin.tests.widgetset.client.SerializerTestRpc;
  40. import com.vaadin.tests.widgetset.client.SerializerTestState;
  41. import com.vaadin.tests.widgetset.client.SimpleTestBean;
  42. import com.vaadin.tests.widgetset.server.SerializerTestExtension;
  43. import elemental.json.Json;
  44. import elemental.json.JsonString;
  45. import elemental.json.JsonValue;
  46. @Widgetset("com.vaadin.tests.widgetset.TestingWidgetSet")
  47. public class SerializerTest extends AbstractReindeerTestUI {
  48. private Log log = new Log(80);
  49. @Override
  50. protected void setup(VaadinRequest request) {
  51. final SerializerTestExtension testExtension = new SerializerTestExtension();
  52. addExtension(testExtension);
  53. // Don't show row numbers to make it easier to add tests without
  54. // changing all numbers
  55. log.setNumberLogRows(false);
  56. addComponent(log);
  57. final SerializerTestRpc rpc = testExtension
  58. .getRpcProxy(SerializerTestRpc.class);
  59. SerializerTestState state = testExtension.getState();
  60. rpc.sendBeanSubclass(new SimpleTestBean() {
  61. @Override
  62. public int getValue() {
  63. return 42;
  64. }
  65. });
  66. state.simpleTestBean = new SimpleTestBean() {
  67. @Override
  68. public int getValue() {
  69. return 42;
  70. }
  71. };
  72. rpc.sendBoolean(true, Boolean.FALSE,
  73. new boolean[] { true, true, false, true, false, false });
  74. state.booleanValue = true;
  75. state.booleanObjectValue = Boolean.FALSE;
  76. state.booleanArray = new boolean[] { true, true, false, true, false,
  77. false };
  78. rpc.sendByte((byte) 5, Byte.valueOf((byte) -12),
  79. new byte[] { 3, 1, 2 });
  80. state.byteValue = (byte) 5;
  81. state.byteObjectValue = Byte.valueOf((byte) -12);
  82. state.byteArray = new byte[] { 3, 1, 2 };
  83. rpc.sendChar('\u222b', Character.valueOf('å'), "aBcD".toCharArray());
  84. state.charValue = '\u222b';
  85. state.charObjectValue = Character.valueOf('å');
  86. state.charArray = "aBcD".toCharArray();
  87. rpc.sendInt(Integer.MAX_VALUE, Integer.valueOf(0), new int[] { 5, 7 });
  88. state.intValue = Integer.MAX_VALUE;
  89. state.intObjectValue = Integer.valueOf(42);
  90. state.intArray = new int[] { 5, 7 };
  91. rpc.sendLong(577431841358l, Long.valueOf(0),
  92. new long[] { -57841235865l, 57 });
  93. state.longValue = 577431841359l;
  94. state.longObjectValue = Long.valueOf(577431841360l);
  95. state.longArray = new long[] { -57841235865l, 57 };
  96. rpc.sendFloat(3.14159f, Float.valueOf(Math.nextUp(1)),
  97. new float[] { 57, 0, -12 });
  98. state.floatValue = 3.14159f;
  99. state.floatObjectValue = Float.valueOf(Math.nextUp(1));
  100. state.floatArray = new float[] { 57, 0, -12 };
  101. rpc.sendDouble(Math.PI, Double.valueOf(-Math.E),
  102. new double[] { Double.MAX_VALUE, Double.MIN_VALUE });
  103. state.doubleValue = Math.PI;
  104. state.doubleObjectValue = Double.valueOf(-Math.E);
  105. state.doubleArray = new double[] { Double.MAX_VALUE, Double.MIN_VALUE };
  106. rpc.sendString("This is a tesing string ‡", new String[] { "‡", null });
  107. state.string = "This is a tesing string ‡";
  108. state.stringArray = new String[] { null, "‡" };
  109. rpc.sendConnector(this);
  110. state.connector = this;
  111. rpc.sendBean(
  112. new ComplexTestBean(new SimpleTestBean(0),
  113. new SimpleTestBean(1),
  114. Arrays.asList(new SimpleTestBean(3),
  115. new SimpleTestBean(4)),
  116. 5),
  117. new SimpleTestBean(6),
  118. new SimpleTestBean[] { new SimpleTestBean(7) });
  119. state.complexTestBean = new ComplexTestBean(new SimpleTestBean(0),
  120. new SimpleTestBean(1),
  121. Arrays.asList(new SimpleTestBean(3), new SimpleTestBean(4)), 5);
  122. state.simpleTestBean = new SimpleTestBean(6);
  123. state.simpleTestBeanArray = new SimpleTestBean[] {
  124. new SimpleTestBean(7) };
  125. rpc.sendNull("Not null", null);
  126. state.nullString = null;
  127. rpc.sendNestedArray(new int[][] { { 5 }, { 7 } },
  128. new SimpleTestBean[][] {
  129. { new SimpleTestBean(4), new SimpleTestBean(2) } });
  130. state.nestedIntArray = new int[][] { { 5 }, { 7 } };
  131. state.nestedBeanArray = new SimpleTestBean[][] {
  132. { new SimpleTestBean(4), new SimpleTestBean(2) } };
  133. rpc.sendList(Arrays.asList(5, 8, -234),
  134. Arrays.<Connector> asList(this, testExtension), Arrays.asList(
  135. new SimpleTestBean(234), new SimpleTestBean(-568)));
  136. state.intList = Arrays.asList(5, 8, -234);
  137. state.connectorList = Arrays.<Connector> asList(this, testExtension);
  138. state.simpleTestBeanList = Arrays.asList(new SimpleTestBean(234),
  139. new SimpleTestBean(-568));
  140. rpc.sendArrayList(Arrays.asList(new int[] { 1, 2 }, new int[] { 3, 4 }),
  141. Arrays.asList(new Integer[] { 5, 6 }, new Integer[] { 7, 8 }),
  142. Collections.singletonList(
  143. new SimpleTestBean[] { new SimpleTestBean(7) }));
  144. state.primitiveArrayList = Arrays.asList(new int[] { 1, 2 },
  145. new int[] { 3, 4 });
  146. state.objectArrayList = Arrays.asList(new Integer[] { 5, 6 },
  147. new Integer[] { 7, 8 });
  148. state.beanArrayList = Collections
  149. .singletonList(new SimpleTestBean[] { new SimpleTestBean(7) });
  150. // Disabled because of #8861
  151. // rpc.sendListArray(
  152. // new List[] { Arrays.asList(1, 2), Arrays.asList(3, 4) },
  153. // new List[] { Collections.singletonList(new SimpleTestBean(-1)) });
  154. rpc.sendSet(new HashSet<>(Arrays.asList(4, 7, 12)),
  155. Collections.singleton((Connector) this), new HashSet<>(Arrays
  156. .asList(new SimpleTestBean(1), new SimpleTestBean(2))));
  157. state.intSet = new HashSet<>(Arrays.asList(4, 7, 12));
  158. state.connectorSet = Collections.singleton((Connector) this);
  159. state.beanSet = new HashSet<>(
  160. Arrays.asList(new SimpleTestBean(1), new SimpleTestBean(2)));
  161. rpc.sendMap(new HashMap<String, SimpleTestBean>() {
  162. {
  163. put("1", new SimpleTestBean(1));
  164. put("2", new SimpleTestBean(2));
  165. }
  166. }, new HashMap<Connector, SimpleTestBean>() {
  167. {
  168. put(testExtension, new SimpleTestBean(3));
  169. put(getUI(), new SimpleTestBean(4));
  170. }
  171. }, new HashMap<Integer, Connector>() {
  172. {
  173. put(5, testExtension);
  174. put(10, getUI());
  175. }
  176. }, new HashMap<SimpleTestBean, SimpleTestBean>() {
  177. {
  178. put(new SimpleTestBean(5), new SimpleTestBean(-5));
  179. put(new SimpleTestBean(-4), new SimpleTestBean(4));
  180. }
  181. });
  182. state.stringMap = new HashMap<String, SimpleTestBean>() {
  183. {
  184. put("1", new SimpleTestBean(1));
  185. put("2", new SimpleTestBean(2));
  186. }
  187. };
  188. state.connectorMap = new HashMap<Connector, SimpleTestBean>() {
  189. {
  190. put(testExtension, new SimpleTestBean(3));
  191. put(getUI(), new SimpleTestBean(4));
  192. }
  193. };
  194. state.intMap = new HashMap<Integer, Connector>() {
  195. {
  196. put(5, testExtension);
  197. put(10, getUI());
  198. }
  199. };
  200. state.beanMap = new HashMap<SimpleTestBean, SimpleTestBean>() {
  201. {
  202. put(new SimpleTestBean(5), new SimpleTestBean(-5));
  203. put(new SimpleTestBean(-4), new SimpleTestBean(4));
  204. }
  205. };
  206. rpc.sendWrappedGenerics(
  207. new HashMap<Set<SimpleTestBean>, Map<Integer, List<SimpleTestBean>>>() {
  208. {
  209. put(Collections.singleton(new SimpleTestBean(42)),
  210. new HashMap<Integer, List<SimpleTestBean>>() {
  211. {
  212. put(1, Arrays.asList(
  213. new SimpleTestBean(1),
  214. new SimpleTestBean(3)));
  215. }
  216. });
  217. }
  218. });
  219. state.generics = new HashMap<Set<SimpleTestBean>, Map<Integer, List<SimpleTestBean>>>() {
  220. {
  221. put(Collections.singleton(new SimpleTestBean(42)),
  222. new HashMap<Integer, List<SimpleTestBean>>() {
  223. {
  224. put(1, Arrays.asList(new SimpleTestBean(1),
  225. new SimpleTestBean(3)));
  226. }
  227. });
  228. }
  229. };
  230. rpc.sendEnum(ContentMode.TEXT,
  231. new ContentMode[] { ContentMode.PREFORMATTED,
  232. ContentMode.TEXT },
  233. Arrays.asList(ContentMode.HTML, ContentMode.TEXT));
  234. state.contentMode = ContentMode.TEXT;
  235. state.array = new ContentMode[] { ContentMode.PREFORMATTED };
  236. state.list = Arrays.asList(ContentMode.HTML);
  237. rpc.sendDate(new Date(1));
  238. rpc.sendDate(new Date(2013 - 1900, 5 - 1, 31, 11, 12, 13));
  239. rpc.sendDateArray(new Date[] { new Date(2013 - 1900, 1, 1),
  240. new Date(2012 - 1900, 1, 1) });
  241. state.jsonNull = Json.createNull();
  242. state.jsonString = Json.create("a string");
  243. state.jsonBoolean = Json.create(false);
  244. rpc.sendJson(Json.create(true), Json.createNull(), Json.create("JSON"));
  245. state.dateArray = new Date[] { new Date(1), new Date(2) };
  246. state.date1 = new Date(1);
  247. state.date2 = new Date(2013 - 1900, 5 - 1, 31, 11, 12, 13);
  248. testExtension.registerRpc(new SerializerTestRpc() {
  249. @Override
  250. public void sendBoolean(boolean value, Boolean boxedValue,
  251. boolean[] array) {
  252. log.log("sendBoolean: " + value + ", " + boxedValue + ", "
  253. + Arrays.toString(array));
  254. }
  255. @Override
  256. public void sendByte(byte value, Byte boxedValue, byte[] array) {
  257. log.log("sendByte: " + value + ", " + boxedValue + ", "
  258. + Arrays.toString(array));
  259. }
  260. @Override
  261. public void sendChar(char value, Character boxedValue,
  262. char[] array) {
  263. log.log("sendChar: " + value + ", " + boxedValue + ", "
  264. + Arrays.toString(array));
  265. }
  266. @Override
  267. public void sendInt(int value, Integer boxedValue, int[] array) {
  268. log.log("sendInt: " + value + ", " + boxedValue + ", "
  269. + Arrays.toString(array));
  270. }
  271. @Override
  272. public void sendLong(long value, Long boxedValue, long[] array) {
  273. log.log("sendLong: " + value + ", " + boxedValue + ", "
  274. + Arrays.toString(array));
  275. }
  276. @Override
  277. public void sendFloat(float value, Float boxedValue,
  278. float[] array) {
  279. log.log("sendFloat: " + value + ", " + boxedValue + ", "
  280. + Arrays.toString(array));
  281. }
  282. @Override
  283. public void sendDouble(double value, Double boxedValue,
  284. double[] array) {
  285. log.log("sendDouble: " + value + ", " + boxedValue + ", "
  286. + Arrays.toString(array));
  287. }
  288. @Override
  289. public void sendString(String value, String[] array) {
  290. log.log("sendString: " + value + ", " + Arrays.toString(array));
  291. }
  292. @Override
  293. public void sendConnector(Connector connector) {
  294. log.log("sendConnector: " + connector.getClass().getName());
  295. }
  296. @Override
  297. public void sendBean(ComplexTestBean complexBean,
  298. SimpleTestBean simpleBean, SimpleTestBean[] array) {
  299. log.log("sendBean: " + complexBean + ", " + simpleBean + ", "
  300. + Arrays.toString(array));
  301. }
  302. @Override
  303. public void sendNull(String value1, String value2) {
  304. log.log("sendNull: " + value1 + ", " + value2);
  305. }
  306. @Override
  307. public void sendNestedArray(int[][] nestedIntArray,
  308. SimpleTestBean[][] nestedBeanArray) {
  309. log.log("sendNestedArray: "
  310. + Arrays.deepToString(nestedIntArray) + ", "
  311. + Arrays.deepToString(nestedBeanArray));
  312. }
  313. @Override
  314. public void sendList(List<Integer> intList,
  315. List<Connector> connectorList,
  316. List<SimpleTestBean> beanList) {
  317. log.log("sendList: " + intList + ", "
  318. + connectorCollectionToString(connectorList) + ", "
  319. + beanList);
  320. }
  321. private String connectorCollectionToString(
  322. Collection<Connector> collection) {
  323. StringBuilder sb = new StringBuilder();
  324. for (Connector connector : collection) {
  325. if (sb.length() != 0) {
  326. sb.append(", ");
  327. }
  328. sb.append(connector.getClass());
  329. }
  330. String string = sb.toString();
  331. return string;
  332. }
  333. @Override
  334. public void sendArrayList(List<int[]> primitiveArrayList,
  335. List<Integer[]> objectArrayList,
  336. List<SimpleTestBean[]> beanArrayList) {
  337. log.log("sendArrayList: "
  338. + Arrays.deepToString(primitiveArrayList.toArray())
  339. + ", " + Arrays.deepToString(objectArrayList.toArray())
  340. + ", " + Arrays.deepToString(beanArrayList.toArray()));
  341. }
  342. @Override
  343. public void sendListArray(List<Integer>[] objectListArray,
  344. List<SimpleTestBean>[] beanListArray) {
  345. log.log("sendArrayList: " + Arrays.toString(objectListArray)
  346. + ", " + Arrays.toString(beanListArray));
  347. }
  348. @Override
  349. public void sendSet(Set<Integer> intSet,
  350. Set<Connector> connectorSet, Set<SimpleTestBean> beanSet) {
  351. List<Integer> intList = new ArrayList<>(intSet);
  352. Collections.sort(intList);
  353. List<Connector> connectorList = new ArrayList<>(connectorSet);
  354. Collections.sort(connectorList, new ConnectorComparator());
  355. List<SimpleTestBean> beanList = new ArrayList<>(beanSet);
  356. Collections.sort(beanList, new SimpleBeanComparator());
  357. log.log("sendSet: " + intList + ", "
  358. + connectorCollectionToString(connectorList) + ", "
  359. + beanList);
  360. }
  361. @Override
  362. public void sendMap(Map<String, SimpleTestBean> stringMap,
  363. Map<Connector, SimpleTestBean> connectorMap,
  364. Map<Integer, Connector> intMap,
  365. Map<SimpleTestBean, SimpleTestBean> beanMap) {
  366. StringBuilder sb = new StringBuilder();
  367. for (Entry<Connector, SimpleTestBean> entry : connectorMap
  368. .entrySet()) {
  369. if (sb.length() == 0) {
  370. sb.append('[');
  371. } else {
  372. sb.append(", ");
  373. }
  374. sb.append(entry.getKey().getClass().getName());
  375. sb.append('=');
  376. sb.append(entry.getValue());
  377. }
  378. sb.append(']');
  379. String connectorMapString = sb.toString();
  380. sb = new StringBuilder();
  381. for (Entry<Integer, Connector> entry : intMap.entrySet()) {
  382. if (sb.length() == 0) {
  383. sb.append('[');
  384. } else {
  385. sb.append(", ");
  386. }
  387. sb.append(entry.getKey());
  388. sb.append('=');
  389. sb.append(entry.getValue().getClass().getName());
  390. }
  391. sb.append(']');
  392. String intMapString = sb.toString();
  393. log.log("sendMap: " + stringMap + ", " + connectorMapString
  394. + ", " + intMapString + ", " + beanMap);
  395. }
  396. @Override
  397. public void sendWrappedGenerics(
  398. Map<Set<SimpleTestBean>, Map<Integer, List<SimpleTestBean>>> generics) {
  399. log.log("sendWrappedGenerics: " + generics.toString());
  400. }
  401. @Override
  402. public void sendEnum(ContentMode contentMode, ContentMode[] array,
  403. List<ContentMode> list) {
  404. log.log("sendEnum: " + contentMode + ", "
  405. + Arrays.toString(array) + ", " + list);
  406. }
  407. @Override
  408. public void sendBeanSubclass(SimpleTestBean bean) {
  409. log.log("sendBeanSubclass: " + bean.getValue());
  410. }
  411. @Override
  412. public void sendDate(Date date) {
  413. DateFormat format = DateFormat.getDateTimeInstance(
  414. DateFormat.LONG, DateFormat.FULL,
  415. new Locale("en", "fi"));
  416. format.setTimeZone(TimeZone.getTimeZone("UTC"));
  417. log.log("sendDate: " + format.format(date));
  418. }
  419. @Override
  420. public void sendDateArray(Date[] dateArray) {
  421. DateFormat format = DateFormat.getDateTimeInstance(
  422. DateFormat.LONG, DateFormat.FULL,
  423. new Locale("en", "fi"));
  424. format.setTimeZone(TimeZone.getTimeZone("UTC"));
  425. String dates = "";
  426. for (Date date : dateArray) {
  427. dates += " " + format.format(date);
  428. }
  429. log.log("sendDateArray: " + dates);
  430. }
  431. @Override
  432. public void sendJson(JsonValue value1, JsonValue value2,
  433. JsonString string) {
  434. log.log("sendJson: " + value1.toJson() + ", " + value2.toJson()
  435. + ", " + string.toJson());
  436. }
  437. @Override
  438. public void log(String string) {
  439. log.log(string);
  440. }
  441. });
  442. }
  443. @Override
  444. protected String getTestDescription() {
  445. return "Test for lots of different cases of encoding and decoding various data types";
  446. }
  447. @Override
  448. protected Integer getTicketNumber() {
  449. return Integer.valueOf(8655);
  450. }
  451. private static class ConnectorComparator implements Comparator<Connector> {
  452. @Override
  453. public int compare(Connector o1, Connector o2) {
  454. return o1.getConnectorId().compareTo(o2.getConnectorId());
  455. }
  456. }
  457. private static class SimpleBeanComparator
  458. implements Comparator<SimpleTestBean> {
  459. @Override
  460. public int compare(SimpleTestBean o1, SimpleTestBean o2) {
  461. return Integer.valueOf(o1.getValue()).compareTo(o2.getValue());
  462. }
  463. }
  464. }