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.

JSONSerializerTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.vaadin.server;
  2. import static org.junit.Assert.assertNull;
  3. import static org.junit.Assert.assertTrue;
  4. import java.lang.reflect.Type;
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.junit.Test;
  9. import com.vaadin.server.JsonCodec.BeanProperty;
  10. import com.vaadin.shared.communication.UidlValue;
  11. import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelState;
  12. import elemental.json.Json;
  13. import elemental.json.JsonArray;
  14. import elemental.json.JsonException;
  15. import elemental.json.JsonValue;
  16. /**
  17. * Tests for {@link JsonCodec}
  18. *
  19. * @author Vaadin Ltd
  20. * @since 7.0
  21. *
  22. */
  23. public class JSONSerializerTest {
  24. Map<String, AbstractSplitPanelState> stringToStateMap;
  25. Map<AbstractSplitPanelState, String> stateToStringMap;
  26. @Test
  27. public void testStringToBeanMapSerialization() throws Exception {
  28. Type mapType = getClass().getDeclaredField("stringToStateMap")
  29. .getGenericType();
  30. stringToStateMap = new HashMap<>();
  31. AbstractSplitPanelState s = new AbstractSplitPanelState();
  32. AbstractSplitPanelState s2 = new AbstractSplitPanelState();
  33. s.caption = "State 1";
  34. s.id = "foo";
  35. s2.caption = "State 2";
  36. s2.id = "bar";
  37. stringToStateMap.put("string - state 1", s);
  38. stringToStateMap.put("String - state 2", s2);
  39. JsonValue encodedMap = JsonCodec
  40. .encode(stringToStateMap, null, mapType, null)
  41. .getEncodedValue();
  42. ensureDecodedCorrectly(stringToStateMap, encodedMap, mapType);
  43. }
  44. @Test
  45. public void testBeanToStringMapSerialization() throws Exception {
  46. Type mapType = getClass().getDeclaredField("stateToStringMap")
  47. .getGenericType();
  48. stateToStringMap = new HashMap<>();
  49. AbstractSplitPanelState s = new AbstractSplitPanelState();
  50. AbstractSplitPanelState s2 = new AbstractSplitPanelState();
  51. s.caption = "State 1";
  52. s2.caption = "State 2";
  53. stateToStringMap.put(s, "string - state 1");
  54. stateToStringMap.put(s2, "String - state 2");
  55. JsonValue encodedMap = JsonCodec
  56. .encode(stateToStringMap, null, mapType, null)
  57. .getEncodedValue();
  58. ensureDecodedCorrectly(stateToStringMap, encodedMap, mapType);
  59. }
  60. @Test
  61. public void testNullLegacyValue() throws JsonException {
  62. JsonArray inputArray = Json.createArray();
  63. inputArray.set(0, "n");
  64. inputArray.set(1, Json.createNull());
  65. UidlValue decodedObject = (UidlValue) JsonCodec
  66. .decodeInternalType(UidlValue.class, true, inputArray, null);
  67. assertNull(decodedObject.getValue());
  68. }
  69. @Test(expected = JsonException.class)
  70. public void testNullTypeOtherValue() {
  71. JsonArray inputArray = Json.createArray();
  72. inputArray.set(0, "n");
  73. inputArray.set(1, "a");
  74. UidlValue decodedObject = (UidlValue) JsonCodec
  75. .decodeInternalType(UidlValue.class, true, inputArray, null);
  76. }
  77. private void ensureDecodedCorrectly(Object original, JsonValue encoded,
  78. Type type) throws Exception {
  79. Object serverSideDecoded = JsonCodec.decodeInternalOrCustomType(type,
  80. encoded, null);
  81. assertTrue("Server decoded", equals(original, serverSideDecoded));
  82. }
  83. private boolean equals(Object o1, Object o2) throws Exception {
  84. if (o1 == null) {
  85. return (o2 == null);
  86. }
  87. if (o2 == null) {
  88. return false;
  89. }
  90. if (o1 instanceof Map) {
  91. if (!(o2 instanceof Map)) {
  92. return false;
  93. }
  94. return equalsMap((Map) o1, (Map) o2);
  95. }
  96. if (o1.getClass() != o2.getClass()) {
  97. return false;
  98. }
  99. if (o1 instanceof Collection || o1 instanceof Number
  100. || o1 instanceof String) {
  101. return o1.equals(o2);
  102. }
  103. return equalsBean(o1, o2);
  104. }
  105. private boolean equalsBean(Object o1, Object o2) throws Exception {
  106. for (BeanProperty property : JsonCodec.getProperties(o1.getClass())) {
  107. Object c1 = property.getValue(o1);
  108. Object c2 = property.getValue(o2);
  109. if (!equals(c1, c2)) {
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. private boolean equalsMap(Map o1, Map o2) throws Exception {
  116. for (Object key1 : o1.keySet()) {
  117. Object key2 = key1;
  118. if (!(o2.containsKey(key2))) {
  119. // Try to fins a key that is equal
  120. for (Object k2 : o2.keySet()) {
  121. if (equals(key1, k2)) {
  122. key2 = k2;
  123. break;
  124. }
  125. }
  126. }
  127. if (!equals(o1.get(key1), o2.get(key2))) {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. }