1 16 package net.sf.dozer.util.mapping; 17 18 import java.math.BigInteger ; 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Hashtable ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.SortedSet ; 28 import java.util.StringTokenizer ; 29 import java.util.TreeSet ; 30 31 import net.pmonks.xml.dozer.test.ChildType; 32 import net.sf.dozer.util.mapping.converters.StringAppendCustomConverter; 33 import net.sf.dozer.util.mapping.exception.DozerRuntimeException; 34 import net.sf.dozer.util.mapping.util.CollectionUtils; 35 import net.sf.dozer.util.mapping.vo.AnotherTestObject; 36 import net.sf.dozer.util.mapping.vo.AnotherTestObjectPrime; 37 import net.sf.dozer.util.mapping.vo.ArrayCustConverterObj; 38 import net.sf.dozer.util.mapping.vo.ArrayCustConverterObjPrime; 39 import net.sf.dozer.util.mapping.vo.Car; 40 import net.sf.dozer.util.mapping.vo.Child; 41 import net.sf.dozer.util.mapping.vo.FieldValue; 42 import net.sf.dozer.util.mapping.vo.GetWeatherByZipCodeDocument; 43 import net.sf.dozer.util.mapping.vo.InsideTestObject; 44 import net.sf.dozer.util.mapping.vo.InsideTestObjectPrime; 45 import net.sf.dozer.util.mapping.vo.MetalThingyIF; 46 import net.sf.dozer.util.mapping.vo.PrimitiveArrayObj; 47 import net.sf.dozer.util.mapping.vo.PrimitiveArrayObjPrime; 48 import net.sf.dozer.util.mapping.vo.SimpleObj; 49 import net.sf.dozer.util.mapping.vo.SimpleObjPrime; 50 import net.sf.dozer.util.mapping.vo.SimpleObjPrime2; 51 import net.sf.dozer.util.mapping.vo.TestObject; 52 import net.sf.dozer.util.mapping.vo.TestObjectPrime; 53 import net.sf.dozer.util.mapping.vo.TestObjectPrime2; 54 import net.sf.dozer.util.mapping.vo.GetWeatherByZipCodeDocument.GetWeatherByZipCode; 55 import net.sf.dozer.util.mapping.vo.context.ContextMapping; 56 import net.sf.dozer.util.mapping.vo.context.ContextMappingNested; 57 import net.sf.dozer.util.mapping.vo.context.ContextMappingNestedPrime; 58 import net.sf.dozer.util.mapping.vo.context.ContextMappingPrime; 59 import net.sf.dozer.util.mapping.vo.deep.House; 60 import net.sf.dozer.util.mapping.vo.map.MapToMap; 61 import net.sf.dozer.util.mapping.vo.map.MapToMapPrime; 62 import net.sf.dozer.util.mapping.vo.set.NamesArray; 63 import net.sf.dozer.util.mapping.vo.set.NamesSet; 64 import net.sf.dozer.util.mapping.vo.set.NamesSortedSet; 65 import net.sf.dozer.util.mapping.vo.iface.ApplicationUser; 66 import net.sf.dozer.util.mapping.vo.iface.UpdateMember; 67 import net.sf.dozer.util.mapping.vo.index.Mccoy; 68 import net.sf.dozer.util.mapping.vo.index.MccoyPrime; 69 import net.sf.dozer.util.mapping.vo.isaccessible.Foo; 70 import net.sf.dozer.util.mapping.vo.isaccessible.FooPrime; 71 72 75 public class GranularDozerBeanMapperTest extends DozerTestBase { 76 77 public void testMapToMap() throws Exception { 78 MapperIF mapper = getNewMapper(new String [] { "mapInterfaceMapping.xml", "dozerBeanMapping.xml" }); 79 TestObject to = new TestObject(); 80 to.setOne("one"); 81 TestObject to2 = new TestObject(); 82 to2.setTwo(new Integer (2)); 83 Map map = new HashMap (); 84 map.put("to", to); 85 map.put("to2", to2); 86 MapToMap mtm = new MapToMap(); 87 mtm.setStandardMap(map); 88 89 Map map2 = new HashMap (); 90 map2.put("to", to); 91 map2.put("to2", to2); 92 93 mtm.setStandardMapWithHint(map2); 94 95 MapToMapPrime mtmp = (MapToMapPrime) mapper.map(mtm, MapToMapPrime.class); 96 assertEquals("one", ((TestObject) mtmp.getStandardMap().get("to")).getOne()); 97 assertEquals(2, ((TestObject) mtmp.getStandardMap().get("to2")).getTwo().intValue()); 98 assertEquals("one", ((TestObjectPrime) mtmp.getStandardMapWithHint().get("to")).getOnePrime()); 100 assertEquals(2, ((TestObjectPrime) mtmp.getStandardMapWithHint().get("to2")).getTwoPrime().intValue()); 101 } 102 103 public void testMapToMapExistingDestination() throws Exception { 104 MapperIF mapper = getNewMapper(new String [] { "mapInterfaceMapping.xml", "dozerBeanMapping.xml" }); 105 TestObject to = new TestObject(); 106 to.setOne("one"); 107 TestObject to2 = new TestObject(); 108 to2.setTwo(new Integer (2)); 109 Map map = new HashMap (); 110 map.put("to", to); 111 map.put("to2", to2); 112 MapToMap mtm = new MapToMap(); 113 mtm.setStandardMap(map); 114 115 MapToMapPrime mtmp = new MapToMapPrime(); 118 Map map2 = new Hashtable (); 119 map2.put("toDest", to); 120 mtmp.setStandardMap(map2); 121 122 mapper.map(mtm, mtmp); 123 assertEquals("one", ((TestObject) mtmp.getStandardMap().get("to")).getOne()); 124 assertEquals(2, ((TestObject) mtmp.getStandardMap().get("to2")).getTwo().intValue()); 125 assertEquals("one", ((TestObject) mtmp.getStandardMap().get("toDest")).getOne()); 126 } 127 128 public void testFieldAccessible() throws Exception { 129 MapperIF mapper = getNewMapper(new String [] { "fieldAttributeMapping.xml" }); 130 TestObject to = new TestObject(); 131 to.setFieldAccessible("fieldAccessible"); 132 to.setFieldAccessiblePrimInt(2); 133 InsideTestObject ito = new InsideTestObject(); 134 ito.setLabel("label"); 135 to.setFieldAccessibleComplexType(ito); 136 String [] stringArray = new String [] { "one", "two" }; 137 to.setFieldAccessibleArrayToList(stringArray); 138 TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 139 assertEquals("fieldAccessible", top.fieldAccessible); 140 assertEquals("label", top.fieldAccessibleComplexType.getLabelPrime()); 141 assertEquals(2, top.fieldAccessiblePrimInt); 142 assertEquals("one", top.fieldAccessibleArrayToList.get(0)); 143 assertEquals("two", top.fieldAccessibleArrayToList.get(1)); 144 145 TestObject toDest = (TestObject) mapper.map(top, TestObject.class); 147 assertEquals("fieldAccessible", toDest.getFieldAccessible()); 148 assertEquals("label", toDest.getFieldAccessibleComplexType().getLabel()); 149 assertEquals(2, toDest.getFieldAccessiblePrimInt()); 150 assertEquals("one", toDest.getFieldAccessibleArrayToList()[0]); 151 assertEquals("two", toDest.getFieldAccessibleArrayToList()[1]); 152 } 153 154 public void testOverloadGetSetMethods() throws Exception { 155 MapperIF mapper = getNewMapper(new String [] { "fieldAttributeMapping.xml" }); 156 TestObject to = new TestObject(); 157 Date date = new Date (); 158 to.setOverloadGetField(new Date ()); 159 TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 160 assertEquals(date, top.getOverloadSetField()); 161 162 TestObject toDest = (TestObject) mapper.map(top, TestObject.class); 164 assertEquals(date, toDest.getOverloadGetField()); 165 } 166 167 public void testFieldCreateMethod() throws Exception { 168 MapperIF mapper = getNewMapper(new String [] { "fieldAttributeMapping.xml" }); 169 TestObject to = new TestObject(); 170 InsideTestObject ito = new InsideTestObject(); 171 to.setCreateMethodType(ito); 173 TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 174 assertEquals("myField", top.getCreateMethodType().getMyField()); 175 176 TestObject toDest = (TestObject) mapper.map(top, TestObject.class); 178 assertEquals("testCreateMethod", toDest.getCreateMethodType().getTestCreateMethod()); 179 } 180 181 public void testDeepInterfaceWithHint() throws Exception { 182 MapperIF mapper = getNewMapper(new String [] { "fieldAttributeMapping.xml" }); 183 InsideTestObject ito = new InsideTestObject(); 184 House house = new House(); 185 MetalThingyIF thingy = new Car(); 186 thingy.setName("name"); 187 house.setThingy(thingy); 188 ito.setHouse(house); 189 InsideTestObjectPrime itop = (InsideTestObjectPrime) mapper.map(ito, InsideTestObjectPrime.class); 190 assertEquals("name", itop.getDeepInterfaceString()); 191 192 InsideTestObject dest = (InsideTestObject) mapper.map(itop, InsideTestObject.class); 194 assertEquals("name", ito.getHouse().getThingy().getName()); 195 196 } 197 198 public void testIntegerToString() throws Exception { 199 MapperIF mapper = getNewMapper(new String [] { "fieldAttributeMapping.xml" }); 200 TestObject to = new TestObject(); 201 Integer [] array = new Integer [] { new Integer (1), new Integer (2) }; 202 to.setArrayForLists(array); 203 TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 204 assertEquals("1", top.getListForArray().get(0)); 205 210 } 211 212 public void testMapNull_MappingLevel() throws Exception { 213 MapperIF mapper = getNewMapper(new String [] { "nullFieldMapping.xml" }); 214 AnotherTestObject src = new AnotherTestObject(); 216 src.setField3(null); 217 src.setField4(null); 218 AnotherTestObjectPrime dest = new AnotherTestObjectPrime(); 219 dest.setTo(new TestObject()); 220 dest.setField3("555"); 221 dest.getTo().setOne("4641"); 222 223 mapper.map(src, dest); 225 assertEquals("invalid dest field value", "555", dest.getField3()); 226 assertEquals("invalid dest field value2", "4641", dest.getTo().getOne()); 227 } 228 229 public void testMapNull_MappingLevel2() throws Exception { 230 MapperIF mapper = getNewMapper(new String [] { "nullFieldMapping.xml" }); 231 AnotherTestObjectPrime src = new AnotherTestObjectPrime(); 233 src.setTo(new TestObject()); 234 src.setField3(null); 235 src.getTo().setOne(null); 236 AnotherTestObject dest = new AnotherTestObject(); 237 dest.setField3("555"); 238 dest.setField4("4641"); 239 240 mapper.map(src, dest); 242 assertEquals("invalid dest field value", "555", dest.getField3()); 243 assertEquals("invalid dest field value2", "4641", dest.getField4()); 244 } 245 246 public void testMapEmptyString_MappingLevel() throws Exception { 247 MapperIF mapper = getNewMapper(new String [] { "nullFieldMapping.xml" }); 248 AnotherTestObject src = new AnotherTestObject(); 251 src.setField3(""); 252 src.setField4(""); 253 AnotherTestObjectPrime dest = new AnotherTestObjectPrime(); 254 dest.setTo(new TestObject()); 255 dest.setField3("555"); 256 dest.getTo().setOne("4641"); 257 258 mapper.map(src, dest); 260 assertEquals("invalid dest field value", "555", dest.getField3()); 261 assertEquals("invalid dest field value2", "4641", dest.getTo().getOne()); 262 } 263 264 public void testMapEmptyString_MappingLevel2() throws Exception { 265 MapperIF mapper = getNewMapper(new String [] { "nullFieldMapping.xml" }); 266 AnotherTestObjectPrime src = new AnotherTestObjectPrime(); 268 src.setTo(new TestObject()); 269 src.setField3(""); 270 src.getTo().setOne(""); 271 AnotherTestObject dest = new AnotherTestObject(); 272 dest.setField3("555"); 273 dest.setField4("4641"); 274 275 mapper.map(src, dest); 277 assertEquals("invalid dest field value", "555", dest.getField3()); 278 assertEquals("invalid dest field value2", "4641", dest.getField4()); 279 } 280 281 public void testMapNull_ClassLevel() throws Exception { 282 MapperIF mapper = getNewMapper(new String [] { "nullFieldMapping.xml" }); 283 TestObject src = new TestObject(); 285 src.setOne(null); 286 TestObjectPrime2 dest = new TestObjectPrime2(); 287 dest.setOne("555"); 288 289 mapper.map(src, dest); 291 assertNotNull("dest should not be null", dest.getOne()); 292 assertEquals("invalid dest field value", "555", dest.getOne()); 293 294 TestObjectPrime2 src2 = new TestObjectPrime2(); 296 src2.setOne(null); 297 TestObject dest2 = new TestObject(); 298 dest2.setOne("555"); 299 300 mapper.map(src2, dest2); 302 assertNull("dest should be null", dest2.getOne()); 303 } 304 305 public void testMapEmptyString_ClassLevel() throws Exception { 306 MapperIF mapper = getNewMapper(new String [] { "nullFieldMapping.xml" }); 307 TestObject src = new TestObject(); 310 src.setOne(""); 311 TestObjectPrime2 dest = new TestObjectPrime2(); 312 dest.setOne("555"); 313 314 mapper.map(src, dest); 316 assertNotNull("dest should not be null", dest.getOne()); 317 assertEquals("invalid dest field value", "555", dest.getOne()); 318 319 TestObjectPrime2 src2 = new TestObjectPrime2(); 321 src2.setOne(""); 322 TestObject dest2 = new TestObject(); 323 dest2.setOne("555"); 324 325 mapper.map(src2, dest2); 327 assertNotNull("dest should not be null", dest2.getOne()); 328 assertEquals("dest should be an empty string", "", dest2.getOne()); 329 330 } 331 332 public void testContextMappingWithNestedContext() throws Exception { 333 MapperIF mapper = getNewMapper(new String [] { "contextMapping.xml" }); 334 335 ContextMappingNested cmn = new ContextMappingNested(); 336 cmn.setLoanNo("loanNoNested"); 337 List list = new ArrayList (); 338 list.add(cmn); 339 ContextMapping cm = new ContextMapping(); 340 cm.setLoanNo("loanNo"); 341 cm.setContextList(list); 342 343 ContextMappingPrime cmpA = (ContextMappingPrime) mapper.map(cm, ContextMappingPrime.class, "caseA"); 344 assertNull(cmpA.getLoanNo()); 345 assertNull(((ContextMappingNestedPrime) cmpA.getContextList().get(0)).getLoanNo()); 346 347 ContextMappingPrime cmpB = (ContextMappingPrime) mapper.map(cm, ContextMappingPrime.class, "caseB"); 348 assertEquals("loanNo", cmpB.getLoanNo()); 349 assertEquals("loanNoNested", ((ContextMappingNested) cmpB.getContextList().get(0)).getLoanNo()); 350 351 ContextMappingNestedPrime cmn2 = new ContextMappingNestedPrime(); 352 cmn2.setLoanNo("loanNoNested"); 353 List list2 = new ArrayList (); 354 list2.add(cmn2); 355 ContextMappingPrime prime = new ContextMappingPrime(); 356 prime.setLoanNo("loanNo"); 357 prime.setContextList(list2); 358 359 ContextMapping cmDest = (ContextMapping) mapper.map(prime, ContextMapping.class, "caseA"); 360 assertNull(cmDest.getLoanNo()); 361 assertNull(((ContextMappingNested) cmDest.getContextList().get(0)).getLoanNo()); 362 363 ContextMapping cmpBDest = (ContextMapping) mapper.map(prime, ContextMapping.class, "caseB"); 364 assertEquals("loanNo", cmpBDest.getLoanNo()); 365 assertEquals("loanNoNested", ((ContextMappingNestedPrime) cmpBDest.getContextList().get(0)).getLoanNo()); 366 } 367 368 public void testXmlBeans() throws Exception { 369 MapperIF mapper = getNewMapper(new String [] { "xmlBeansMapping.xml" }); 370 TestObject to = new TestObject(); 372 to.setOne("one"); 373 GetWeatherByZipCodeDocument doc = (GetWeatherByZipCodeDocument) mapper.map(to, GetWeatherByZipCodeDocument.class); 374 assertEquals(to.getOne(), doc.getGetWeatherByZipCode().getZipCode()); 375 376 GetWeatherByZipCodeDocument res = GetWeatherByZipCodeDocument.Factory.newInstance(); 378 GetWeatherByZipCode zipCode = res.addNewGetWeatherByZipCode(); 379 zipCode.setZipCode("one"); 380 TestObject to2 = (TestObject) mapper.map(res, TestObject.class); 381 assertEquals(res.getGetWeatherByZipCode().getZipCode(), to2.getOne()); 382 } 383 384 public void testArrayToSortedSet() { 385 NamesArray from = new NamesArray(); 386 NamesSortedSet to = null; 387 String [] names = { "John", "Bill", "Tony", "Fred", "Bruce" }; 388 389 from.setNames(names); 390 391 to = (NamesSortedSet) mapper.map(from, NamesSortedSet.class); 392 393 assertNotNull(to); 394 assertEquals(names.length, to.getNames().size()); 395 } 396 397 public void testSortedSetToArray() { 398 NamesSortedSet from = new NamesSortedSet(); 399 NamesArray to = null; 400 SortedSet names = new TreeSet (); 401 402 names.add("Jen"); 403 names.add("Sue"); 404 names.add("Sally"); 405 names.add("Jill"); 406 from.setNames(names); 407 408 to = (NamesArray) mapper.map(from, NamesArray.class); 409 410 assertNotNull(to); 411 assertEquals(names.size(), to.getNames().length); 412 } 413 414 public void testSetToSortedSet() { 415 NamesSet from = new NamesSet(); 416 NamesSortedSet to = null; 417 Set names = new HashSet (); 418 419 names.add("Red"); 420 names.add("Blue"); 421 names.add("Green"); 422 from.setNames(names); 423 424 to = (NamesSortedSet) mapper.map(from, NamesSortedSet.class); 425 426 assertNotNull(to); 427 assertEquals(names.size(), to.getNames().size()); 428 } 429 430 public void testSortedSetToSet() { 431 NamesSortedSet from = new NamesSortedSet(); 432 NamesSet to = null; 433 SortedSet names = new TreeSet (); 434 435 names.add("Bone"); 436 names.add("White"); 437 names.add("Beige"); 438 names.add("Ivory"); 439 names.add("Cream"); 440 names.add("Off white"); 441 from.setNames(names); 442 443 to = (NamesSet) mapper.map(from, NamesSet.class); 444 445 assertNotNull(to); 446 assertEquals(names.size(), to.getNames().size()); 447 } 448 449 public void testSetPrivateField() { 450 mapper = super.getNewMapper(new String [] { "isaccessiblemapping.xml" }); 451 Foo src = new Foo(); 452 List list = new ArrayList (); 453 list.add("test1"); 454 list.add("test2"); 455 src.setCategories(list); 456 457 FooPrime dest = (FooPrime) mapper.map(src, FooPrime.class); 458 assertNotNull(dest); 459 } 460 461 public void testStringToIndexedSet_UsingMapSetMethod() { 462 mapper = getNewMapper(new String [] { "indexMapping.xml" }); 463 Mccoy src = new Mccoy(); 464 src.setStringProperty(String.valueOf(System.currentTimeMillis())); 465 466 MccoyPrime dest = (MccoyPrime) mapper.map(src, MccoyPrime.class); 467 Set destSet = dest.getFieldValueObjects(); 468 assertNotNull("dest set should not be null", destSet); 469 assertEquals("dest set should contain 1 entry", 1, destSet.size()); 470 Object entry = destSet.iterator().next(); 471 assertTrue("dest set entry should be instance of FieldValue", entry instanceof FieldValue); 472 assertEquals("invalid value for dest object", src.getStringProperty(), ((FieldValue) entry).getValue()); 473 assertEquals("invalid key for dest object", "stringProperty", ((FieldValue) entry).getKey()); 474 } 475 476 public void testStringToIndexedSet_UsingHint() { 477 mapper = getNewMapper(new String [] { "indexMapping.xml" }); 478 Mccoy src = new Mccoy(); 479 src.setStringProperty(String.valueOf(System.currentTimeMillis())); 480 481 MccoyPrime dest = (MccoyPrime) mapper.map(src, MccoyPrime.class, "usingDestHint"); 482 Set destSet = dest.getFieldValueObjects(); 483 assertNotNull("dest set should not be null", destSet); 484 assertEquals("dest set should contain 1 entry", 1, destSet.size()); 485 Object entry = destSet.iterator().next(); 486 assertTrue("dest set entry should be instance of FieldValue", entry instanceof FieldValue); 487 assertEquals("invalid value for dest object", src.getStringProperty(), ((FieldValue) entry).getValue()); 488 assertNull("FieldValue key field should be null", ((FieldValue) entry).getKey()); 489 490 } 491 492 public void testAllowedExceptions() throws Exception { 493 MapperIF mapper = getNewMapper(new String [] { "allowedExceptionsMapping.xml" }); 494 TestObject to = new TestObject(); 495 to.setThrowAllowedExceptionOnMap("throw me"); 496 try { 497 TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 498 fail("We should have thrown DozerRuntimeException"); 499 } catch (RuntimeException e) { 500 if (e instanceof DozerRuntimeException) { 501 assertTrue(true); 502 } else { 503 fail("This should be an instance of DozerRuntimeException"); 504 } 505 } 506 TestObject to2 = new TestObject(); 507 to2.setThrowNonAllowedExceptionOnMap("do not throw me"); 508 try { 509 TestObjectPrime top = (TestObjectPrime) mapper.map(to2, TestObjectPrime.class); 510 } catch (RuntimeException e) { 511 fail("This should not have been thrown"); 512 } 513 } 514 515 public void testPrimitiveArrayToList() throws Exception { 516 mapper = getNewMapper(new String []{"primitiveArrayToListMapping.xml"}); 517 518 int[] i = new int[] {1,2,3}; 519 PrimitiveArrayObj src = new PrimitiveArrayObj(); 520 src.setField1(i); 521 522 PrimitiveArrayObjPrime dest = (PrimitiveArrayObjPrime) mapper.map(src, PrimitiveArrayObjPrime.class); 523 524 assertNotNull("dest list field should not be null", dest.getField1()); 525 assertEquals("invalid dest field size", i.length, dest.getField1().size()); 526 527 CollectionUtils collUtils = new CollectionUtils(); 528 List srcObjectList = collUtils.convertPrimitiveArrayToList(i); 529 assertEquals("invalid dest field value", srcObjectList, dest.getField1()); 530 } 531 532 public void testPrimitiveArrayToList_UsingHint() throws Exception { 533 mapper = getNewMapper(new String []{"primitiveArrayToListMapping.xml"}); 534 535 int[] srcArray = new int[] {1,2,3}; 536 PrimitiveArrayObj src = new PrimitiveArrayObj(); 537 src.setField1(srcArray); 538 539 PrimitiveArrayObjPrime dest = (PrimitiveArrayObjPrime) mapper.map(src, PrimitiveArrayObjPrime.class, "primitiveToArrayUsingHint"); 540 541 assertNotNull("dest list field should not be null", dest.getField1()); 542 assertEquals("invalid dest field size", srcArray.length, dest.getField1().size()); 543 544 for(int i = 0; i < srcArray.length; i++) { 545 String srcValue = String.valueOf(srcArray[i]); 546 String resultValue = (String ) dest.getField1().get(i); 547 assertEquals("invalid result entry value", srcValue, resultValue); 548 } 549 } 550 551 public void testInterface() throws Exception { 552 mapper = getNewMapper(new String []{"interfaceMapping.xml"}); 553 ApplicationUser user = new ApplicationUser(); 554 user.setSubscriberNumber("123"); 555 556 UpdateMember destObject = (UpdateMember) mapper.map(user, UpdateMember.class); 558 559 assertEquals("invalid value for subsriber #", user.getSubscriberNumber(), destObject.getSubscriberKey().getSubscriberNumber()); 560 561 destObject = new UpdateMember(); 563 564 mapper.map(user, destObject); 566 567 assertNotNull("dest field should not be null", destObject.getSubscriberKey()); 568 assertEquals("invalid value for subsriber #", user.getSubscriberNumber(), destObject.getSubscriberKey().getSubscriberNumber()); 569 } 570 571 public void testFieldCustomConverter() throws Exception { 572 mapper = getNewMapper(new String []{"fieldCustomConverter.xml"}); 573 SimpleObj src = new SimpleObj(); 574 src.setField1(String.valueOf(System.currentTimeMillis())); 575 576 SimpleObjPrime2 dest = (SimpleObjPrime2) mapper.map(src, SimpleObjPrime2.class); 577 578 assertNotNull("dest field1 should not be null", dest.getField1Prime()); 580 StringTokenizer st = new StringTokenizer (dest.getField1Prime(), "-"); 581 assertEquals("dest field1 value should contain a hyphon", 2, st.countTokens()); 582 String token1 = st.nextToken(); 583 assertEquals("1st portion of dest field1 value should equal src field value", src.getField1(), token1); 584 String token2 = st.nextToken(); 585 assertEquals("dest field1 value should have been appended to by the cust converter", 586 StringAppendCustomConverter.APPENDED_VALUE, token2); 587 } 588 589 public void testFieldCustomConverter_NullSrcValue() throws Exception { 590 mapper = getNewMapper(new String []{"fieldCustomConverter.xml"}); 592 SimpleObj src = new SimpleObj(); 593 src.setField1(null); 594 595 SimpleObjPrime2 dest = (SimpleObjPrime2) mapper.map(src, SimpleObjPrime2.class); 596 597 assertNotNull("dest field1 should not be null", dest.getField1Prime()); 599 StringTokenizer st = new StringTokenizer (dest.getField1Prime(), "-"); 600 assertEquals("dest field1 value should contain a hyphon", 2, st.countTokens()); 601 String token1 = st.nextToken(); 602 assertEquals("dest field1 value should contain the explicit null string", "null", token1); 603 String token2 = st.nextToken(); 604 assertEquals("dest field1 value should have been appended to by the cust converter", 605 StringAppendCustomConverter.APPENDED_VALUE, token2); 606 } 607 608 public void testSimpleCustomConverter() throws Exception { 609 mapper = getNewMapper(new String []{"simpleCustomConverter.xml"}); 610 SimpleObj src = new SimpleObj(); 611 src.setField1(String.valueOf(System.currentTimeMillis())); 612 613 SimpleObjPrime2 dest = (SimpleObjPrime2) mapper.map(src, SimpleObjPrime2.class); 614 615 assertNotNull("dest field1 should not be null", dest.getField1Prime()); 617 StringTokenizer st = new StringTokenizer (dest.getField1Prime(), "-"); 618 assertEquals("dest field1 value should contain a hyphon", 2, st.countTokens()); 619 String token1 = st.nextToken(); 620 assertEquals("1st portion of dest field1 value should equal src field value", src.getField1(), token1); 621 String token2 = st.nextToken(); 622 assertEquals("dest field1 value should have been appended to by the cust converter", 623 StringAppendCustomConverter.APPENDED_VALUE, token2); 624 } 625 626 public void testSimpleCustomConverter_NullSrcValue() throws Exception { 627 mapper = getNewMapper(new String []{"simpleCustomConverter.xml"}); 629 SimpleObj src = new SimpleObj(); 630 src.setField1(null); 631 632 SimpleObjPrime2 dest = (SimpleObjPrime2) mapper.map(src, SimpleObjPrime2.class); 633 634 assertNotNull("dest field1 should not be null", dest.getField1Prime()); 636 StringTokenizer st = new StringTokenizer (dest.getField1Prime(), "-"); 637 assertEquals("dest field1 value should contain a hyphon", 2, st.countTokens()); 638 String token1 = st.nextToken(); 639 assertEquals("dest field1 value should contain the explicit null string", "null", token1); 640 String token2 = st.nextToken(); 641 assertEquals("dest field1 value should have been appended to by the cust converter", 642 StringAppendCustomConverter.APPENDED_VALUE, token2); 643 } 644 645 648 public void testInterfaceInheritanceViaXmlBeans_PojoToXmlBean() { 649 mapper = getNewMapper(new String []{"xmlBeansMapping.xml"}); 650 Child pojo = new Child(); 651 652 pojo.setId(BigInteger.valueOf(42)); 653 pojo.setName("Ernie"); 654 pojo.setFu("Fu"); 655 pojo.setBar("Bar"); 656 657 ChildType xmlBean = (ChildType)mapper.map(pojo, ChildType.class); 658 659 assertNotNull("dest obj should not be null", xmlBean); 660 assertNotNull("fu value should not be null", xmlBean.getFu()); 661 assertEquals("invalid fu value", pojo.getFu(), xmlBean.getFu()); 662 assertNotNull("bar field should not be null", xmlBean.getBar()); 663 assertEquals("invalid bar value", pojo.getBar(), xmlBean.getBar()); 664 assertNotNull("name value should not be null", xmlBean.getName()); 665 assertEquals("invalid name value", pojo.getName(), xmlBean.getName()); 666 assertNotNull("id field should not be null", xmlBean.getId()); 667 assertEquals("invalid id value", pojo.getId(), xmlBean.getId()); 668 } 669 670 673 public void testInterfaceInheritanceViaXmlBeans_XmlBeanToPojo() { 674 mapper = getNewMapper(new String []{"xmlBeansMapping.xml"}); 675 ChildType xmlBean = ChildType.Factory.newInstance(); 676 677 xmlBean.setId(BigInteger.valueOf(7236)); 678 xmlBean.setName("Bert"); 679 xmlBean.setFu("Uf"); 680 xmlBean.setBar("Rab"); 681 682 Child pojo = (Child)mapper.map(xmlBean, Child.class); 683 684 assertNotNull("dest obj should not be null", pojo); 685 assertNotNull("fu should not be null", pojo.getFu()); 686 assertEquals("invalid fu value", xmlBean.getFu(), pojo.getFu()); 687 assertNotNull("bar should not be null", pojo.getBar()); 688 assertEquals("invalid bar value", xmlBean.getBar(), pojo.getBar()); 689 assertNotNull("id should not be null", pojo.getId()); 690 assertEquals("invalid id value", xmlBean.getId(), pojo.getId()); 691 assertNotNull("name should not be null", pojo.getName()); 692 assertEquals("invalid name value", xmlBean.getName(), pojo.getName()); 693 } 694 695 public void testArrayToStringCustomConverter() throws Exception { 696 mapper = getNewMapper(new String []{"arrayToStringCustomConverter.xml"}); 698 SimpleObj simple = new SimpleObj(); 699 simple.setField1(String.valueOf(System.currentTimeMillis())); 700 701 ArrayCustConverterObj src = new ArrayCustConverterObj(); 702 src.setField1(new SimpleObj[] {simple}); 703 704 ArrayCustConverterObjPrime dest = (ArrayCustConverterObjPrime) mapper.map(src, ArrayCustConverterObjPrime.class); 705 706 assertNotNull("dest field1 should not be null", dest.getField1Prime()); 708 StringTokenizer st = new StringTokenizer (dest.getField1Prime(), "-"); 709 assertEquals("dest field1 value should contain a hyphon", 2, st.countTokens()); 710 String token1 = st.nextToken(); 711 String token2 = st.nextToken(); 712 assertEquals("dest field1 value should have been appended to by the cust converter", 713 StringAppendCustomConverter.APPENDED_VALUE, token2); 714 } 715 716 public void testCustomFieldMapper() throws Exception { 717 CustomFieldMapperIF customFieldMapper = new TestCustomFieldMapper(); 718 mapper = getNewMapper(null); 719 ((DozerBeanMapper) mapper).setCustomFieldMapper(customFieldMapper); 720 721 String currentTime = String.valueOf(System.currentTimeMillis()); 722 SimpleObj src = new SimpleObj(); 723 src.setField1(currentTime); 724 src.setField6("field6Value" + currentTime); 725 726 SimpleObjPrime dest = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class); 727 728 assertNotNull("dest field1 should not be null", dest.getField1()); 729 assertNotNull("dest field6 should not be null", dest.getField6()); 730 assertEquals("dest field1 should have been set by custom field mapper", TestCustomFieldMapper.FIELD_VALUE, dest.getField1()); 731 assertEquals("dest field6 should NOT have been set by custom field mapper", src.getField6(), dest.getField6()); 732 } 733 734 735 } | Popular Tags |