1 package test.crispy.impl.rest;
2
3 import java.math.BigDecimal;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.Hashtable;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Vector;
10
11 import junit.framework.TestCase;
12 import net.sf.crispy.impl.rest.ParameterSerializer;
13 import net.sf.crispy.util.Converter;
14 import test.crispy.example.model.Adresse;
15 import test.crispy.example.model.Kunde;
16 import test.crispy.example.model.Node;
17
18 public class ParameterSerializerTest extends TestCase {
19
20 public void testSimpleString() throws Exception {
21 String lvString = new ParameterSerializer().serialize("Hello");
22 assertEquals(lvString, "param0=Hello");
23
24 String s = "ab cög_>";
25 lvString = new ParameterSerializer().serialize(s);
26 assertEquals(lvString, "param0=" + s);
27 }
28
29 public void testSimpleDate() throws Exception {
30 Date d = new Date();
31 String lvString = new ParameterSerializer().serialize(d);
32 assertEquals(lvString, "param0=" + d);
33 }
34
35 public void testSimpleDigit() throws Exception {
36 double digit = 47.12;
37 String lvString = new ParameterSerializer().serialize(new Double(digit));
38 assertEquals(lvString, "param0=" + digit);
39
40 }
41
42 public void testSimpleMap() throws Exception {
43 Map m = new Hashtable();
44 m.put("1", "A");m.put("2", "BB");m.put("3", "CCC");
45 ParameterSerializer lvSerializer = new ParameterSerializer();
46 String lvString = lvSerializer.serialize(m);
47 assertEquals(lvString, "param0_MAPSIMPLE_3=CCC¶m0_MAPSIMPLE_2=BB¶m0_MAPSIMPLE_1=A");
48 }
49
50 public void testSimpleStringVector() throws Exception {
51 Vector v = new Vector();
52 v.add("A");v.add("B");v.add("CC");
53 ParameterSerializer lvSerializer = new ParameterSerializer();
54 String lvString = lvSerializer.serialize(v);
55 assertEquals(lvString, "param0=A¶m1=B¶m2=CC");
56 }
57
58 public void testParameterSerializerList() throws Exception {
59 List lvList = new ArrayList();
60 lvList.add("Peter");
61 lvList.add(new Integer (7));
62 ParameterSerializer lvSerializer = new ParameterSerializer();
63 String lvString = lvSerializer.serialize(lvList);
64 assertEquals(lvString, "param0=Peter¶m1=7");
65
66 lvList = new ArrayList();
67 lvList.add(new Integer (7));
68 lvList.add("Peter");
69 lvString = lvSerializer.serialize(lvList);
70 assertEquals(lvString, "param0=7¶m1=Peter");
71 }
72
73 public void testSimpleVector() throws Exception {
74 Vector v = new Vector();
75 Date d = new Date();
76 v.add(new Double(5.6));v.add(d);v.add(new Integer(4711));
77 ParameterSerializer lvSerializer = new ParameterSerializer();
78 String lvString = lvSerializer.serialize(v);
79 assertEquals(lvString, "param0=5.6¶m1=" + d + "¶m2=4711");
80 }
81
82 public void testParameterSerializerMapAndComplex() throws Exception {
83 Map lvMap = new Hashtable ();
84 Kunde k = new Kunde("Mario");
85 lvMap.put("kunde", k);
86 lvMap.put("integer", new Integer (7));
87
88 ParameterSerializer lvSerializer = new ParameterSerializer();
89 String lvString = lvSerializer.serialize(lvMap);
90 Map lvMapAfter = lvSerializer.toSortedMap();
91
92 assertNotNull(lvString);
93 assertNotNull(lvMapAfter.get("param0_MAPSIMPLE_integer=7"));
94 assertNotNull(lvMapAfter.get("param0_MAPSIMPLE_kunde=param2"));
95 assertNotNull(lvMapAfter.get("param2_class=test.crispy.example.model.Kunde"));
96 assertNotNull(lvMapAfter.get("param2_name=Mario"));
97 assertNotNull(lvMapAfter.get("param2_~hashCode~=" + Integer.toString(k.hashCode())));
98 }
99
100 public void testParameterSerializerVectorAndComplex() throws Exception {
101 Kunde k = new Kunde("Mario");
102 k.setGehalt(new BigDecimal(12.34));
103
104 Vector v = new Vector();
105 v.add(new Converter().makeSimple(k));
106 v.add(new Integer(7));
107
108 ParameterSerializer lvSerializer = new ParameterSerializer();
109 String lvString = lvSerializer.serialize(v);
110 Map lvMap = lvSerializer.toSortedMap();
111
112 assertNotNull(lvString);
113 assertNotNull(lvMap.get("param0_LIST_1=param1"));
114 assertNotNull(lvMap.get("param0_LIST_1=7"));
115 assertNotNull(lvMap.get("param1_class=test.crispy.example.model.Kunde"));
116 assertNotNull(lvMap.get("param1_gehalt=12.34"));
117 assertNotNull(lvMap.get("param1_name=Mario"));
118 assertNotNull(lvMap.get("param1_~hashCode~=" + Integer.toString(k.hashCode())));
119
120 }
121
122 public void testComplexObjectWithMaps() throws Exception {
123 Node n1 = new Node("Node1");
124 Node n2 = new Node("Node2");
125 Node n3 = new Node("Node3");
126
127 n1.getChildrenMap().put(n2.getName(), n2);
128 n1.getChildrenMap().put(n3.getName(), n3);
129
130 ParameterSerializer lvSerializer = new ParameterSerializer();
131 String lvString = lvSerializer.serialize(n1);
132 Map lvMap = lvSerializer.toSortedMap();
133
134 assertNotNull(lvString);
135 assertNotNull(lvMap.get("param0_MAP_childrenMap=param1"));
136 assertNotNull(lvMap.get("param0_class=test.crispy.example.model.Node"));
137 assertNotNull(lvMap.get("param0_name=Node1"));
138 assertNotNull(lvMap.get("param0_~hashCode~=" + n1.hashCode()));
139 assertNotNull(lvMap.get("param1_MAP_childrenMap_Node2=param3"));
140 assertNotNull(lvMap.get("param1_MAP_childrenMap_Node3=param2"));
141 assertNotNull(lvMap.get("param2_class=test.crispy.example.model.Node"));
142 assertNotNull(lvMap.get("param2_name=Node3"));
143 assertNotNull(lvMap.get("param2_~hashCode~=" + n3.hashCode()));
144 assertNotNull(lvMap.get("param3_class=test.crispy.example.model.Node"));
145 assertNotNull(lvMap.get("param3_name=Node2"));
146 assertNotNull(lvMap.get("param3_~hashCode~=" + n2.hashCode()));
147 }
148
149 public void testComplexObjectWithMapsAndVector() throws Exception {
150 Node n1 = new Node("Node1");
151 Node n2 = new Node("Node2");
152 Node n3 = new Node("Node3");
153
154 n1.getChildrenMap().put(n2.getName(), n2);
155 n1.getChildrenMap().put(n3.getName(), n3);
156
157 n1.getChildren().add(n2);
158 n1.getChildren().add(n3);
159
160 ParameterSerializer lvSerializer = new ParameterSerializer();
161 String lvString = lvSerializer.serialize(n1);
162 Map lvMap = lvSerializer.toSortedMap();
163
164 assertNotNull(lvString);
165 assertNotNull(lvMap.get("param0_LIST_children=param3"));
166 assertNotNull(lvMap.get("param0_LIST_children=param5"));
167 assertNotNull(lvMap.get("param0_MAP_childrenMap=param1"));
168 assertNotNull(lvMap.get("param0_class=test.crispy.example.model.Node"));
169 assertNotNull(lvMap.get("param0_name=Node1"));
170 assertNotNull(lvMap.get("param0_~hashCode~=" + n1.hashCode()));
171 assertNotNull(lvMap.get("param1_MAP_childrenMap_Node3=~hashCode~" + n3.hashCode()));
172 assertNotNull(lvMap.get("param1_MAP_childrenMap_Node2=~hashCode~" + n2.hashCode()));
173 assertNotNull(lvMap.get("param3_class=test.crispy.example.model.Node"));
174 assertNotNull(lvMap.get("param3_name=Node2"));
175 assertNotNull(lvMap.get("param3_~hashCode~=" + n2.hashCode()));
176 assertNotNull(lvMap.get("param5_class=test.crispy.example.model.Node"));
177 assertNotNull(lvMap.get("param5_name=Node3"));
178 assertNotNull(lvMap.get("param5_~hashCode~=" + n3.hashCode()));
179 }
180
181 public void testParameterSerializerMapAndSimpleWithRelation() throws Exception {
182 Kunde k = new Kunde("Mario");
183 k.setGehalt(new BigDecimal(12.34));
184 Adresse a = new Adresse();
185 a.setOrt("Magdebrug");
186 a.setPlz("39104");
187
188 Adresse a2 = new Adresse();
189 a2.setOrt("Cottbus");
190 a2.setPlz("03044");
191
192 k.getAdressen().add(a);
193 k.getAdressen().add(a2);
194
195 ParameterSerializer lvSerializer = new ParameterSerializer();
196 String lvString = lvSerializer.serialize(k);
197 Map lvMap = lvSerializer.toSortedMap();
198
199 assertNotNull(lvString);
200 assertNotNull(lvMap.get("param0_LIST_adressen=param2"));
201 assertNotNull(lvMap.get("param0_LIST_adressen=param4"));
202 assertNotNull(lvMap.get("param0_class=test.crispy.example.model.Kunde"));
203 assertNotNull(lvMap.get("param0_gehalt=12.34"));
204 assertNotNull(lvMap.get("param0_name=Mario"));
205 assertNotNull(lvMap.get("param0_~hashCode~=" + k.hashCode()));
206 assertNotNull(lvMap.get("param2_class=test.crispy.example.model.Adresse"));
207 assertNotNull(lvMap.get("param2_ort=Magdebrug"));
208 assertNotNull(lvMap.get("param2_plz=39104"));
209 assertNotNull(lvMap.get("param2_~hashCode~=" + a.hashCode()));
210 assertNotNull(lvMap.get("param4_class=test.crispy.example.model.Adresse"));
211 assertNotNull(lvMap.get("param4_ort=Cottbus"));
212 assertNotNull(lvMap.get("param4_plz=03044"));
213 assertNotNull(lvMap.get("param4_~hashCode~=" + a2.hashCode()));
214 }
215
216 public void testParameterSerializerComplexWithCycle() throws Exception {
217 Kunde k = new Kunde("Mario");
218 k.setGehalt(new BigDecimal(12.34));
219 Adresse a = new Adresse();
220 a.setOrt("Magdebrug");
221 a.setPlz("39104");
222 k.getAdressen().add(a);
223 a.setKunde(k);
224
225 ParameterSerializer lvSerializer = new ParameterSerializer();
226 String lvString = lvSerializer.serialize(k);
227 Map lvMap = lvSerializer.toSortedMap();
228
229 assertNotNull(lvString);
230 assertNotNull(lvMap.get("param0_LIST_adressen=param2"));
231 assertNotNull(lvMap.get("param0_class=test.crispy.example.model.Kunde"));
232 assertNotNull(lvMap.get("param0_gehalt=12.34"));
233 assertNotNull(lvMap.get("param0_name=Mario"));
234 assertNotNull(lvMap.get("param0_~hashCode~=" + k.hashCode()));
235 assertNotNull(lvMap.get("param2_class=test.crispy.example.model.Adresse"));
236
237 assertNotNull(lvMap.get("param2_kunde=~hashCode~" + k.hashCode()));
238 assertNotNull(lvMap.get("param2_ort=Magdebrug"));
239 assertNotNull(lvMap.get("param2_plz=39104"));
240 assertNotNull(lvMap.get("param2_~hashCode~=" + a.hashCode()));
241 }
242
243
244
245 }