1 package test.crispy.impl.rest; 2 3 import java.math.BigDecimal; 4 import java.util.Hashtable; 5 import java.util.Map; 6 import java.util.Vector; 7 8 import junit.framework.TestCase; 9 import net.sf.crispy.impl.rest.ParameterDeserializer; 10 import net.sf.crispy.impl.rest.ParameterSerializer; 11 import test.crispy.example.model.Adresse; 12 import test.crispy.example.model.Kunde; 13 import test.crispy.example.model.Node; 14 15 public class ParameterDeserializerTest extends TestCase { 16 17 public void testConvertString2Map() throws Exception { 18 Map lvMap = ParameterDeserializer.convertString2Map("param0=Hello"); 19 assertNotNull(lvMap.get("param0")); 20 String s[] = (String[]) lvMap.get("param0"); 21 assertEquals(s.length, 1); 22 assertEquals(s[0], "Hello"); 23 } 24 25 public void testConvertString2MapFromList() throws Exception { 26 Map lvMap = ParameterDeserializer.convertString2Map("param0=Peter¶m1=7"); 27 28 assertNotNull(lvMap.get("param0")); 29 String s[] = (String[]) lvMap.get("param0"); 30 assertEquals(s.length, 1); 31 assertEquals(s[0], "Peter"); 32 33 assertNotNull(lvMap.get("param1")); 34 s = (String[]) lvMap.get("param1"); 35 assertEquals(s.length, 1); 36 assertEquals(s[0], "7"); 37 } 38 39 public void testDeserializerSimple() throws Exception { 40 Map lvMap = ParameterDeserializer.convertString2Map("param0=4711"); 41 Object o = new ParameterDeserializer().deserialize(lvMap); 42 assertTrue(o instanceof String); 43 assertEquals(o, "4711"); 44 } 45 46 public void testDeserializerSimpleList() throws Exception { 47 Map lvMap = ParameterDeserializer.convertString2Map("param0=Peter¶m1=7"); 48 Object o = new ParameterDeserializer().deserialize(lvMap); 49 assertTrue(o instanceof Vector); 50 Vector lvVector = (Vector) o; 51 assertEquals(lvVector.size(), 2); 52 assertEquals(lvVector.get(0), "Peter"); 53 assertEquals(lvVector.get(1), "7"); 54 } 55 56 public void testDeserializerSimpleList2() throws Exception { 57 Vector v = new Vector(); 58 v.add("1");v.add("2");v.add("3"); 59 60 String lvString = new ParameterSerializer().serialize(v); 61 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 62 Object o = new ParameterDeserializer().deserialize(lvMap); 63 assertNotNull(o); 64 assertEquals(o.getClass(), Vector.class); 65 Vector lvVector = (Vector) o; 66 assertEquals(lvVector.size(), 3); 67 assertEquals(lvVector.get(0), "1"); 68 assertEquals(lvVector.get(1), "2"); 69 assertEquals(lvVector.get(2), "3"); 70 } 71 72 public void testDeserializerSimpleMap() throws Exception { 73 Map m = new Hashtable(); 74 m.put("1", "A");m.put("2", "BB");m.put("3", "CCC"); 75 String lvString = new ParameterSerializer().serialize(m); 76 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 77 Object o = new ParameterDeserializer().deserialize(lvMap); 78 assertNotNull(o); 79 assertEquals(o.getClass(), Hashtable.class); 80 Map lvMap2 = (Map) o; 81 assertEquals(lvMap2.get("1"), "A"); 82 assertEquals(lvMap2.get("2"), "BB"); 83 assertEquals(lvMap2.get("3"), "CCC"); 84 } 85 86 public void testParameterDeserializerVectorAndComplex() throws Exception { 87 Kunde k = new Kunde("Mario"); 88 k.setGehalt(new BigDecimal(12.34)); 89 90 Vector v = new Vector(); 91 v.add(k); 92 v.add(new Integer(7)); 93 94 String lvString = new ParameterSerializer().serialize(v); 95 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 96 Object o = new ParameterDeserializer().deserialize(lvMap); 97 assertNotNull(o); 98 assertEquals(o.getClass(), Vector.class); 99 Vector v2 = (Vector) o; 100 assertTrue(v2.size() == 2); 101 assertEquals(v2.get(0).getClass(), Kunde.class); 102 Kunde kAfter = (Kunde) v2.get(0); 103 assertEquals(kAfter.getName(), "Mario"); 104 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 105 assertEquals(v2.get(1), "7"); 106 } 107 108 public void testParameterDeserializerMapAndComplex() throws Exception { 109 Map lvMap0 = new Hashtable (); 110 Kunde k = new Kunde("Mario"); 111 k.setGehalt(new BigDecimal(12.34)); 112 lvMap0.put("kunde", k); 113 lvMap0.put("integer", new Integer (7)); 114 115 String lvString = new ParameterSerializer().serialize(lvMap0); 116 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 117 Object o = new ParameterDeserializer().deserialize(lvMap); 118 assertNotNull(o); 119 assertEquals(o.getClass(), Hashtable.class); 120 Map m = (Map) o; 121 assertEquals(m.get("integer"), "7"); 122 Kunde kAfter = (Kunde) m.get("kunde"); 123 assertEquals(kAfter.getName(), "Mario"); 124 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 125 } 126 127 public void testParameterDeserializerComplex() throws Exception { 128 Kunde k = new Kunde("Mario"); 129 k.setGehalt(new BigDecimal(12.34)); 130 131 String lvString = new ParameterSerializer().serialize(k); 132 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 133 Object o = new ParameterDeserializer().deserialize(lvMap); 134 assertNotNull(o); 135 assertEquals(o.getClass(), Kunde.class); 136 Kunde kAfter = (Kunde) o; 137 assertEquals(kAfter.getName(), "Mario"); 138 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 139 } 140 141 public void testParameterDeserializerComplexWithRelation() throws Exception { 142 Kunde k = new Kunde("Mario"); 143 k.setGehalt(new BigDecimal(12.34)); 144 Adresse a = new Adresse(); 145 a.setOrt("Magdeburg"); 146 a.setPlz("39104"); 147 148 Adresse a2 = new Adresse(); 149 a2.setOrt("Cottbus"); 150 a2.setPlz("03044"); 151 152 k.getAdressen().add(a); 153 k.getAdressen().add(a2); 154 155 String lvString = new ParameterSerializer().serialize(k); 156 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 157 Object o = new ParameterDeserializer().deserialize(lvMap); 158 assertNotNull(o); 159 assertEquals(o.getClass(), Kunde.class); 160 Kunde kAfter = (Kunde) o; 161 assertEquals(kAfter.getName(), "Mario"); 162 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 163 assertEquals(kAfter.getAdressen().size(), 2); 164 Adresse aAfter1 = (Adresse) kAfter.getAdressen().get(0); 165 Adresse aAfter2 = (Adresse) kAfter.getAdressen().get(1); 166 assertEquals(aAfter1.getOrt(), "Magdeburg"); 167 assertEquals(aAfter1.getPlz(), "39104"); 168 assertEquals(aAfter2.getOrt(), "Cottbus"); 169 assertEquals(aAfter2.getPlz(), "03044"); 170 } 171 172 public void testParameterDeserializerComplexWithCycle() throws Exception { 173 Kunde k = new Kunde("Mario"); 174 k.setGehalt(new BigDecimal(12.34)); 175 Adresse a = new Adresse(); 176 a.setOrt("Magdeburg"); 177 a.setPlz("39104"); 178 k.getAdressen().add(a); 179 a.setKunde(k); 180 181 String lvString = new ParameterSerializer().serialize(k); 182 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 183 Object o = new ParameterDeserializer().deserialize(lvMap); 184 assertNotNull(o); 185 assertEquals(o.getClass(), Kunde.class); 186 Kunde kAfter = (Kunde) o; 187 assertEquals(kAfter.getName(), "Mario"); 188 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 189 assertEquals(kAfter.getAdressen().size(), 1); 190 Adresse aAfter1 = (Adresse) kAfter.getAdressen().get(0); 191 assertEquals(aAfter1.getOrt(), "Magdeburg"); 192 assertEquals(aAfter1.getPlz(), "39104"); 193 assertEquals(aAfter1.getKunde(), kAfter); 194 } 195 196 public void testParameterDeserializerComplexObjectWithVectorWithSameReferences() throws Exception { 197 Node n1 = new Node("Node1"); 198 Node n2 = new Node("Node2"); 199 Node n3 = new Node("Node3"); 200 Node nA = new Node("NodeA"); 201 Node nB = new Node("NodeB"); 202 203 n1.getChildren().add(n2); 204 n1.getChildren().add(n3); 205 n2.getChildren().add(nA); 206 n2.getChildren().add(nB); 207 n3.getChildren().add(nA); 208 209 String lvString = new ParameterSerializer().serialize(n1); 210 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 211 Object o = new ParameterDeserializer().deserialize(lvMap); 212 assertNotNull(o); 213 assertEquals(o.getClass(), Node.class); 214 Node n = (Node) o; 215 assertEquals(n.getChildren().size(), n1.getChildren().size()); 216 Node n22 = (Node) n.getChildren().get(0); 217 Node n33 = (Node) n.getChildren().get(1); 218 assertEquals(n22.getName(), n2.getName()); 219 assertEquals(n22.getChildren().size(), n2.getChildren().size()); 220 Node nA2 = (Node) n22.getChildren().get(0); 221 Node nB2 = (Node) n22.getChildren().get(1); 222 assertEquals(nA2.getName(), nA.getName()); 223 assertEquals(nB2.getName(), nB.getName()); 224 assertEquals(n33.getName(), n3.getName()); 225 assertEquals(n33.getChildren().size(), n3.getChildren().size()); 226 Node nA3 = (Node) n33.getChildren().get(0); 227 assertEquals(nA2.getName(), nA3.getName()); 228 assertEquals(nA2, nA3); 229 } 230 231 232 public void testParameterDeserializerComplexObjectWithVector() throws Exception { 233 Node n1 = new Node("Node1"); 234 Node n2 = new Node("Node2"); 235 Node n3 = new Node("Node3"); 236 237 n1.getChildren().add(n2); 238 n1.getChildren().add(n3); 239 240 String lvString = new ParameterSerializer().serialize(n1); 241 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 242 Object o = new ParameterDeserializer().deserialize(lvMap); 243 assertNotNull(o); 244 assertEquals(o.getClass(), Node.class); 245 Node n = (Node) o; 246 assertEquals(n.getChildren().size(), 2); 247 Node n22 = (Node) n.getChildren().get(0); 248 Node n33 = (Node) n.getChildren().get(1); 249 assertEquals(n22.getName(), n2.getName()); 250 assertEquals(n22.getChildren().size(), n2.getChildren().size()); 251 assertEquals(n33.getName(), n3.getName()); 252 assertEquals(n33.getChildren().size(), n3.getChildren().size()); 253 } 254 255 public void testParameterDeserializerComplexObjectWithVectorWithCycle() throws Exception { 256 Node n1 = new Node("Node1"); 257 Node n2 = new Node("Node2"); 258 Node n3 = new Node("Node3"); 259 260 n1.getChildren().add(n2); 261 n1.getChildren().add(n3); 262 n2.getChildren().add(n3); 263 n3.getChildren().add(n1); 264 265 String lvString = new ParameterSerializer().serialize(n1); 266 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 267 Object o = new ParameterDeserializer().deserialize(lvMap); 268 assertNotNull(o); 269 assertEquals(o.getClass(), Node.class); 270 Node n11 = (Node) o; 271 assertEquals(n11.getChildren().size(), 2); 272 Node n22 = (Node) n11.getChildren().get(0); 273 Node n33 = (Node) n11.getChildren().get(1); 274 assertEquals(n22.getName(), n2.getName()); 275 assertEquals(n22.getChildren().size(), n2.getChildren().size()); 276 assertEquals(n33.getName(), n3.getName()); 277 assertEquals(n33.getChildren().size(), n3.getChildren().size()); 278 279 Node n223 = (Node) n22.getChildren().get(0); 280 assertEquals(n223, n33); 281 282 Node n331 = (Node) n33.getChildren().get(0); 283 assertEquals(n331.getName(), n11.getName()); 284 assertEquals(n331, n11); 285 } 286 287 public void testParameterDeserializerComplexObjectWithMapWithCycle() throws Exception { 288 Node n1 = new Node("Node1"); 289 Node n2 = new Node("Node2"); 290 Node n3 = new Node("Node3"); 291 292 n1.getChildrenMap().put(n2.getName(), n2); 293 n2.getChildrenMap().put(n3.getName(), n3); 294 n3.getChildrenMap().put(n1.getName(), n1); 295 296 String lvString = new ParameterSerializer().serialize(n1); 297 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 298 Object o = new ParameterDeserializer().deserialize(lvMap); 299 assertNotNull(o); 300 assertEquals(o.getClass(), Node.class); 301 Node n11 = (Node) o; 302 assertEquals(n11.getChildrenMap().size(), 1); 303 Node n22 = (Node) n11.getChildrenMap().get("Node2"); 304 Node n33 = (Node) n22.getChildrenMap().get("Node3"); 305 assertNotNull(n22); 306 assertNotNull(n33); 307 assertEquals(n33.getChildrenMap().get("Node1"), n11); 308 } 309 310 public void testParameterDeserializerComplexObjectWithVector2() throws Exception { 311 Node n1 = new Node("Node1"); 312 Node n2 = new Node("Node2"); 313 Node n3 = new Node("Node3"); 314 Node nA = new Node("NodeA"); 315 Node nB = new Node("NodeB"); 316 317 n1.getChildren().add(n2); 318 n2.getChildren().add(n3); 319 n3.getChildren().add(nA); 320 nA.getChildren().add(nB); 321 nB.getChildren().add(n3); 322 323 String lvString = new ParameterSerializer().serialize(n1); 324 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 325 Object o = new ParameterDeserializer().deserialize(lvMap); 326 assertNotNull(o); 327 assertEquals(o.getClass(), Node.class); 328 Node n11 = (Node) o; 329 assertEquals(n11.getChildren().size(), 1); 330 Node n12 = (Node) n11.getChildren().get(0); 331 assertEquals(n12.getName(), n2.getName()); 332 Node n13 = (Node) n12.getChildren().get(0); 333 assertEquals(n13.getName(), n3.getName()); 334 Node n1A = (Node) n13.getChildren().get(0); 335 assertEquals(n1A.getName(), nA.getName()); 336 Node n1B = (Node) n1A.getChildren().get(0); 337 assertEquals(n1B.getName(), nB.getName()); 338 339 assertEquals(n1B.getChildren().get(0), n13); 340 } 341 342 public void testParameterDeserializerComplexObjectWithVectorSimple() throws Exception { 343 Node n1 = new Node("Node1"); 344 n1.getChildren().add("A"); 345 n1.getChildren().add("BBB"); 346 n1.getChildren().add("CC"); 347 348 String lvString = new ParameterSerializer().serialize(n1); 349 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 350 Object o = new ParameterDeserializer().deserialize(lvMap); 351 assertNotNull(o); 352 assertEquals(o.getClass(), Node.class); 353 Node n11 = (Node) o; 354 assertEquals(n11.getChildren().size(), n1.getChildren().size()); 355 assertEquals(n11.getChildren().get(0), "A"); 356 assertEquals(n11.getChildren().get(1), "BBB"); 357 assertEquals(n11.getChildren().get(2), "CC"); 358 } 359 360 361 public void testParameterDeserializerComplexObjectWithMap() throws Exception { 362 Node n1 = new Node("Node1"); 363 Node n2 = new Node("Node2"); 364 365 n1.getChildrenMap().put(n2.getName(), n2); 366 367 String lvString = new ParameterSerializer().serialize(n1); 368 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 369 Object o = new ParameterDeserializer().deserialize(lvMap); 370 assertNotNull(o); 371 assertEquals(o.getClass(), Node.class); 372 Node n11 = (Node) o; 373 assertEquals(n11.getChildrenMap().size(), n1.getChildrenMap().size()); 374 Node n22 = (Node) n11.getChildrenMap().get(n2.getName()); 375 assertEquals(n22.getName(), n2.getName()); 376 } 377 378 public void testParameterDeserializerComplexObjectWithMap2() throws Exception { 379 Node n1 = new Node("Node1"); 380 Node n2 = new Node("Node2"); 381 Node n3 = new Node("Node3"); 382 383 n1.getChildrenMap().put(n2.getName(), n2); 384 n1.getChildrenMap().put(n3.getName(), n3); 385 386 String lvString = new ParameterSerializer().serialize(n1); 387 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 388 Object o = new ParameterDeserializer().deserialize(lvMap); 389 assertNotNull(o); 390 assertEquals(o.getClass(), Node.class); 391 Node n11 = (Node) o; 392 assertEquals(n11.getChildrenMap().size(), n1.getChildrenMap().size()); 393 Node n22 = (Node) n11.getChildrenMap().get(n2.getName()); 394 assertEquals(n22.getName(), n2.getName()); 395 Node n33 = (Node) n11.getChildrenMap().get(n3.getName()); 396 assertEquals(n33.getName(), n3.getName()); 397 } 398 399 public void testParameterDeserializerComplexObjectWithMapandVectorMix() throws Exception { 400 Node n1 = new Node("Node1"); 401 Node n2 = new Node("Node2"); 402 Node n3 = new Node("Node3"); 403 Node nA = new Node("NodeA"); 404 405 n1.getChildrenMap().put(n2.getName(), n2); 406 n1.getChildrenMap().put(n3.getName(), n3); 407 408 n1.getChildren().add(n2); 409 n1.getChildren().add(n3); 410 n1.getChildren().add(nA); 411 412 String lvString = new ParameterSerializer().serialize(n1); 413 Map lvMap = ParameterDeserializer.convertString2Map(lvString); 414 Object o = new ParameterDeserializer().deserialize(lvMap); 415 assertNotNull(o); 416 assertEquals(o.getClass(), Node.class); 417 Node n11 = (Node) o; 418 assertEquals(n11.getChildrenMap().size(), n1.getChildrenMap().size()); 419 assertEquals(n11.getChildren().size(), n1.getChildren().size()); 420 Node n22 = (Node) n11.getChildrenMap().get(n2.getName()); 421 Node n33 = (Node) n11.getChildrenMap().get(n3.getName()); 422 assertEquals(n22.getName(), n2.getName()); 423 assertEquals(n33.getName(), n3.getName()); 424 425 Node n222 = (Node) n11.getChildren().get(0); 426 Node n333 = (Node) n11.getChildren().get(1); 427 Node nAAA = (Node) n11.getChildren().get(2); 428 assertEquals(n22, n222); 429 assertEquals(n33, n333); 430 assertEquals(nAAA.getName(), nA.getName()); 431 } 432 433 }