1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
package com.vaadin.terminal.gwt.server;
/*
@VaadinApache2LicenseForJavaFiles@
*/
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelState;
import com.vaadin.terminal.gwt.client.communication.JsonDecoder;
import com.vaadin.terminal.gwt.client.communication.JsonEncoder;
/**
* Tests for {@link JsonCodec}, {@link JsonEncoder}, {@link JsonDecoder}
*
* @author Vaadin Ltd
* @version @VERSION@
* @since 7.0
*
*/
public class JSONSerializerTest extends TestCase {
HashMap<String, AbstractSplitPanelState> stringToStateMap;
HashMap<AbstractSplitPanelState, String> stateToStringMap;
public void testStringToBeanMapSerialization() throws Exception {
Type mapType = getClass().getDeclaredField("stringToStateMap")
.getGenericType();
stringToStateMap = new HashMap<String, AbstractSplitPanelState>();
AbstractSplitPanelState s = new AbstractSplitPanelState();
AbstractSplitPanelState s2 = new AbstractSplitPanelState();
s.setCaption("State 1");
s.setDebugId("foo");
s2.setCaption("State 2");
s2.setDebugId("bar");
stringToStateMap.put("string - state 1", s);
stringToStateMap.put("String - state 2", s2);
Object encodedMap = JsonCodec.encode(stringToStateMap, null, mapType,
null);
ensureDecodedCorrectly(stringToStateMap, encodedMap, mapType);
}
public void testBeanToStringMapSerialization() throws Exception {
Type mapType = getClass().getDeclaredField("stateToStringMap")
.getGenericType();
stateToStringMap = new HashMap<AbstractSplitPanelState, String>();
AbstractSplitPanelState s = new AbstractSplitPanelState();
AbstractSplitPanelState s2 = new AbstractSplitPanelState();
s.setCaption("State 1");
s2.setCaption("State 2");
stateToStringMap.put(s, "string - state 1");
stateToStringMap.put(s2, "String - state 2");
Object encodedMap = JsonCodec.encode(stateToStringMap, null, mapType,
null);
ensureDecodedCorrectly(stateToStringMap, encodedMap, mapType);
}
private void ensureDecodedCorrectly(Object original, Object encoded,
Type type) throws Exception {
Object serverSideDecoded = JsonCodec.decodeInternalOrCustomType(type,
encoded, null);
assertTrue("Server decoded", equals(original, serverSideDecoded));
// Object clientSideDecoded = JsonDecoder.decodeValue(
// (com.google.gwt.json.client.JSONArray) JSONParser
// .parseStrict(encoded.toString()), null, null, null);
// assertTrue("Client decoded",
// equals(original, clientSideDecoded));
}
private boolean equals(Object o1, Object o2) throws Exception {
if (o1 == null) {
return (o2 == null);
}
if (o2 == null) {
return false;
}
if (o1 instanceof Map) {
if (!(o2 instanceof Map)) {
return false;
}
return equalsMap((Map) o1, (Map) o2);
}
if (o1.getClass() != o2.getClass()) {
return false;
}
if (o1 instanceof Collection || o1 instanceof Number
|| o1 instanceof String) {
return o1.equals(o2);
}
return equalsBean(o1, o2);
}
private boolean equalsBean(Object o1, Object o2) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(o1.getClass());
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
String fieldName = JsonCodec.getTransportFieldName(pd);
if (fieldName == null) {
continue;
}
Object c1 = pd.getReadMethod().invoke(o1);
Object c2 = pd.getReadMethod().invoke(o2);
if (!equals(c1, c2)) {
return false;
}
}
return true;
}
private boolean equalsMap(Map o1, Map o2) throws Exception {
for (Object key1 : o1.keySet()) {
Object key2 = key1;
if (!(o2.containsKey(key2))) {
// Try to fins a key that is equal
for (Object k2 : o2.keySet()) {
if (equals(key1, k2)) {
key2 = k2;
break;
}
}
}
if (!equals(o1.get(key1), o2.get(key2))) {
return false;
}
}
return true;
}
}
|