1 16 package net.sf.dozer.util.mapping; 17 18 import java.text.DateFormat ; 19 import java.text.SimpleDateFormat ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import net.sf.dozer.util.mapping.util.TestDataFactory; 29 import net.sf.dozer.util.mapping.vo.AnotherTestObject; 30 import net.sf.dozer.util.mapping.vo.AnotherTestObjectPrime; 31 import net.sf.dozer.util.mapping.vo.Apple; 32 import net.sf.dozer.util.mapping.vo.Car; 33 import net.sf.dozer.util.mapping.vo.CustomConverterWrapper; 34 import net.sf.dozer.util.mapping.vo.CustomConverterWrapperPrime; 35 import net.sf.dozer.util.mapping.vo.CustomDoubleObject; 36 import net.sf.dozer.util.mapping.vo.CustomDoubleObjectIF; 37 import net.sf.dozer.util.mapping.vo.DehydrateTestObject; 38 import net.sf.dozer.util.mapping.vo.FurtherTestObject; 39 import net.sf.dozer.util.mapping.vo.FurtherTestObjectPrime; 40 import net.sf.dozer.util.mapping.vo.HintedOnly; 41 import net.sf.dozer.util.mapping.vo.HydrateTestObject; 42 import net.sf.dozer.util.mapping.vo.LoopObjectChild; 43 import net.sf.dozer.util.mapping.vo.LoopObjectParent; 44 import net.sf.dozer.util.mapping.vo.LoopObjectParentPrime; 45 import net.sf.dozer.util.mapping.vo.MethodFieldTestObject; 46 import net.sf.dozer.util.mapping.vo.MethodFieldTestObject2; 47 import net.sf.dozer.util.mapping.vo.NoCustomMappingsObject; 48 import net.sf.dozer.util.mapping.vo.NoCustomMappingsObjectPrime; 49 import net.sf.dozer.util.mapping.vo.NoDefaultConstructor; 50 import net.sf.dozer.util.mapping.vo.NoReadMethod; 51 import net.sf.dozer.util.mapping.vo.NoReadMethodPrime; 52 import net.sf.dozer.util.mapping.vo.NoSuperClass; 53 import net.sf.dozer.util.mapping.vo.NoWriteMethod; 54 import net.sf.dozer.util.mapping.vo.NoWriteMethodPrime; 55 import net.sf.dozer.util.mapping.vo.OneWayObject; 56 import net.sf.dozer.util.mapping.vo.OneWayObjectPrime; 57 import net.sf.dozer.util.mapping.vo.Orange; 58 import net.sf.dozer.util.mapping.vo.SubClass; 59 import net.sf.dozer.util.mapping.vo.SubClassPrime; 60 import net.sf.dozer.util.mapping.vo.TestCustomConverterHashMapObject; 61 import net.sf.dozer.util.mapping.vo.TestCustomConverterHashMapPrimeObject; 62 import net.sf.dozer.util.mapping.vo.TestCustomConverterObject; 63 import net.sf.dozer.util.mapping.vo.TestCustomConverterObjectPrime; 64 import net.sf.dozer.util.mapping.vo.TestObject; 65 import net.sf.dozer.util.mapping.vo.TestObjectPrime; 66 import net.sf.dozer.util.mapping.vo.TestReferenceFoo; 67 import net.sf.dozer.util.mapping.vo.TestReferenceFooPrime; 68 import net.sf.dozer.util.mapping.vo.TestReferenceObject; 69 import net.sf.dozer.util.mapping.vo.TestReferencePrimeObject; 70 import net.sf.dozer.util.mapping.vo.TheFirstSubClass; 71 import net.sf.dozer.util.mapping.vo.Van; 72 import net.sf.dozer.util.mapping.vo.Vehicle; 73 import net.sf.dozer.util.mapping.vo.WeirdGetter; 74 import net.sf.dozer.util.mapping.vo.WeirdGetter2; 75 import net.sf.dozer.util.mapping.vo.WeirdGetterPrime; 76 import net.sf.dozer.util.mapping.vo.WeirdGetterPrime2; 77 import net.sf.dozer.util.mapping.vo.deep.DestDeepObj; 78 import net.sf.dozer.util.mapping.vo.deep.HomeDescription; 79 import net.sf.dozer.util.mapping.vo.deep.House; 80 import net.sf.dozer.util.mapping.vo.deep.Person; 81 import net.sf.dozer.util.mapping.vo.deep.Room; 82 import net.sf.dozer.util.mapping.vo.deep.SrcDeepObj; 83 import net.sf.dozer.util.mapping.vo.deep.SrcNestedDeepObj; 84 import net.sf.dozer.util.mapping.vo.inheritance.AnotherSubClass; 85 import net.sf.dozer.util.mapping.vo.inheritance.AnotherSubClassPrime; 86 import net.sf.dozer.util.mapping.vo.inheritance.BaseSubClassCombined; 87 import net.sf.dozer.util.mapping.vo.inheritance.GenericAbstractSuper; 88 import net.sf.dozer.util.mapping.vo.inheritance.GenericIF; 89 import net.sf.dozer.util.mapping.vo.inheritance.S2Class; 90 import net.sf.dozer.util.mapping.vo.inheritance.S2ClassPrime; 91 import net.sf.dozer.util.mapping.vo.inheritance.SClass; 92 import net.sf.dozer.util.mapping.vo.inheritance.SClassPrime; 93 import net.sf.dozer.util.mapping.vo.inheritance.Specific3; 94 import net.sf.dozer.util.mapping.vo.inheritance.SpecificObject; 95 import net.sf.dozer.util.mapping.vo.inheritance.WrapperSpecific; 96 import net.sf.dozer.util.mapping.vo.inheritance.WrapperSpecificPrime; 97 import net.sf.dozer.util.mapping.vo.map.CustomMap; 98 import net.sf.dozer.util.mapping.vo.map.CustomMapIF; 99 import net.sf.dozer.util.mapping.vo.map.MapTestObject; 100 import net.sf.dozer.util.mapping.vo.map.MapTestObjectPrime; 101 import net.sf.dozer.util.mapping.vo.map.MapToProperty; 102 import net.sf.dozer.util.mapping.vo.map.PropertyToMap; 103 import net.sf.dozer.util.mapping.vo.self.Account; 104 import net.sf.dozer.util.mapping.vo.self.SimpleAccount; 105 106 import org.apache.commons.lang.SerializationUtils; 107 108 115 public class MapperTest extends DozerTestBase { 116 private static MapperIF mapper; 117 118 protected void setUp() throws Exception { 119 super.setUp(); 120 if (mapper == null) { 121 mapper = getNewMapper(new String [] { "dozerBeanMapping.xml" }); 122 } 123 } 124 125 public void testNoSourceObject() throws Exception { 126 try { 127 mapper.map(null, TestObjectPrime.class); 128 } catch (MappingException e) { 129 assertEquals("source object must not be null", e.getMessage()); 130 } 131 } 132 133 public void testNoDestinationClass() throws Exception { 134 try { 135 mapper.map(new TestObjectPrime(), null); 136 } catch (MappingException e) { 137 assertEquals("destination class must not be null", e.getMessage()); 138 } 139 } 140 141 public void testNoDefaultConstructor() throws Exception { 142 try { 143 mapper.map("test", NoDefaultConstructor.class); 144 } catch (MappingException e) { 145 146 assertEquals("java.lang.InstantiationException: net.sf.dozer.util.mapping.vo.NoDefaultConstructor", e 147 .getMessage()); 148 149 } 150 } 151 152 public void testNoSourceValueIterateFieldMap() throws Exception { 153 DehydrateTestObject inputDto = new DehydrateTestObject(); 154 HydrateTestObject hto = (HydrateTestObject) mapper.map(inputDto, HydrateTestObject.class); 155 assertEquals(TestDataFactory.getExpectedTestNoSourceValueIterateFieldMapHydrateTestObject(), hto); 156 } 157 158 public void testCustomGetterSetterMap() throws Exception { 159 WeirdGetter inputDto = new WeirdGetter(); 160 inputDto.placeValue("theValue"); 161 inputDto.setWildValue("wild"); 162 163 WeirdGetterPrime prime = (WeirdGetterPrime) mapper.map(inputDto, WeirdGetterPrime.class); 164 assertNull(prime.getWildValue()); assertEquals(inputDto.buildValue(), prime.getValue()); 166 167 inputDto = (WeirdGetter) mapper.map(prime, WeirdGetter.class); 168 assertEquals(inputDto.buildValue(), prime.getValue()); 169 170 WeirdGetterPrime2 inputDto2 = new WeirdGetterPrime2(); 171 inputDto2.placeValue("theValue"); 172 WeirdGetter2 prime2 = (WeirdGetter2) mapper.map(inputDto2, WeirdGetter2.class); 173 assertEquals(inputDto2.buildValue(), prime2.getValue()); 174 175 inputDto2 = (WeirdGetterPrime2) mapper.map(prime2, WeirdGetterPrime2.class); 176 assertEquals(inputDto2.buildValue(), prime2.getValue()); 177 178 } 179 180 public void testIncompatibleMapping() throws Exception { 181 mapper.map(new Vehicle(), TestObject.class); 182 } 184 185 public void testNoClassMappings() throws Exception { 186 NoCustomMappingsObjectPrime dest1 = (NoCustomMappingsObjectPrime) mapper.map(TestDataFactory 188 .getInputTestNoClassMappingsNoCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 189 NoCustomMappingsObject source = (NoCustomMappingsObject) mapper.map(dest1, NoCustomMappingsObject.class); 190 NoCustomMappingsObjectPrime dest3 = (NoCustomMappingsObjectPrime) mapper.map(source, 191 NoCustomMappingsObjectPrime.class); 192 assertEquals(dest1, dest3); 193 194 TestObject to = new TestObject(); 196 to.setNoMappingsObj(TestDataFactory.getInputTestNoClassMappingsNoCustomMappingsObject()); 197 TestObjectPrime dest2 = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 198 TestObject source2 = (TestObject) mapper.map(dest2, TestObject.class); 199 TestObjectPrime dest4 = (TestObjectPrime) mapper.map(source2, TestObjectPrime.class); 200 assertEquals(dest2, dest4); 201 } 202 203 public void testMapField() throws Exception { 204 NoCustomMappingsObjectPrime dest = (NoCustomMappingsObjectPrime) mapper.map(TestDataFactory 205 .getInputTestMapFieldWithMapNoCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 206 207 NoCustomMappingsObject source = (NoCustomMappingsObject) mapper.map(dest, NoCustomMappingsObject.class); 208 NoCustomMappingsObjectPrime dest2 = (NoCustomMappingsObjectPrime) mapper.map(source, 209 NoCustomMappingsObjectPrime.class); 210 211 assertEquals(dest2, dest); 212 213 dest = (NoCustomMappingsObjectPrime) mapper.map(new NoCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 214 source = (NoCustomMappingsObject) mapper.map(dest, NoCustomMappingsObject.class); 215 dest2 = (NoCustomMappingsObjectPrime) mapper.map(source, NoCustomMappingsObjectPrime.class); 216 assertEquals(dest2, dest); 217 218 dest = (NoCustomMappingsObjectPrime) mapper.map(TestDataFactory 220 .getInputTestMapFieldWithEmptyMapNoCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 221 source = (NoCustomMappingsObject) mapper.map(dest, NoCustomMappingsObject.class); 222 dest2 = (NoCustomMappingsObjectPrime) mapper.map(source, NoCustomMappingsObjectPrime.class); 223 assertEquals(dest2, dest); 224 } 225 226 public void testSetField() throws Exception { 227 NoCustomMappingsObjectPrime dest = (NoCustomMappingsObjectPrime) mapper.map(TestDataFactory 229 .getInputTestSetFieldWithSetNoCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 230 NoCustomMappingsObject source = (NoCustomMappingsObject) mapper.map(dest, NoCustomMappingsObject.class); 231 NoCustomMappingsObjectPrime dest2 = (NoCustomMappingsObjectPrime) mapper.map(source, 232 NoCustomMappingsObjectPrime.class); 233 assertEquals(dest2, dest); 234 235 dest = (NoCustomMappingsObjectPrime) mapper.map(new NoCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 237 source = (NoCustomMappingsObject) mapper.map(dest, NoCustomMappingsObject.class); 238 dest2 = (NoCustomMappingsObjectPrime) mapper.map(source, NoCustomMappingsObjectPrime.class); 239 assertEquals(dest2, dest); 240 241 dest = (NoCustomMappingsObjectPrime) mapper.map(TestDataFactory 243 .getInputTestSetFieldWithSetEmptyCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 244 source = (NoCustomMappingsObject) mapper.map(dest, NoCustomMappingsObject.class); 245 dest2 = (NoCustomMappingsObjectPrime) mapper.map(source, NoCustomMappingsObjectPrime.class); 246 assertEquals(dest2, dest); 247 248 dest = (NoCustomMappingsObjectPrime) mapper.map(TestDataFactory 250 .getInputTestSetFieldComplexSetNoCustomMappingsObject(), NoCustomMappingsObjectPrime.class); 251 source = (NoCustomMappingsObject) mapper.map(dest, NoCustomMappingsObject.class); 252 dest2 = (NoCustomMappingsObjectPrime) mapper.map(source, NoCustomMappingsObjectPrime.class); 253 assertEquals(dest2, dest); 254 } 255 256 public void testListField() throws Exception { 257 TestObjectPrime dest = (TestObjectPrime) mapper.map(TestDataFactory.getInputTestListFieldEmptyListTestObject(), 259 TestObjectPrime.class); 260 TestObject source = (TestObject) mapper.map(dest, TestObject.class); 261 TestObjectPrime dest2 = (TestObjectPrime) mapper.map(source, TestObjectPrime.class); 262 assertEquals(dest2, dest); 263 264 dest = (TestObjectPrime) mapper.map(TestDataFactory.getInputTestListFieldArrayListTestObject(), 266 TestObjectPrime.class); 267 source = (TestObject) mapper.map(dest, TestObject.class); 268 dest2 = (TestObjectPrime) mapper.map(source, TestObjectPrime.class); 269 assertEquals(dest2, dest); 270 } 271 272 public void testListUsingDestHint() throws Exception { 273 TestObjectPrime dest = (TestObjectPrime) mapper.map(TestDataFactory.getInputTestListUsingDestHintTestObject(), 274 TestObjectPrime.class); 275 TestObject source = (TestObject) mapper.map(dest, TestObject.class); 276 TestObjectPrime dest2 = (TestObjectPrime) mapper.map(source, TestObjectPrime.class); 277 assertEquals(dest, dest2); 278 } 279 280 public void testExcludeFields() throws Exception { 281 TestObjectPrime prime = (TestObjectPrime) mapper.map(TestDataFactory.getInputGeneralMappingTestObject(), 283 TestObjectPrime.class); 284 assertEquals("excludeMe", prime.getExcludeMe()); 285 assertEquals("excludeMeOneWay", prime.getExcludeMeOneWay()); 286 TestObject to = (TestObject) mapper.map( prime, TestObject.class); 288 assertNull(to.getExcludeMe()); 289 assertEquals("excludeMeOneWay", to.getExcludeMeOneWay()); 290 } 291 292 public void testGeneralMapping() throws Exception { 293 TestObject to = TestDataFactory.getInputGeneralMappingTestObject(); 295 TestObjectPrime prime = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 296 TestObject source = (TestObject) mapper.map(prime, TestObject.class); 298 TestObjectPrime prime2 = (TestObjectPrime) mapper.map(source, TestObjectPrime.class); 299 assertEquals(prime2, prime); 300 } 301 302 public void testMappingNoDestSpecified() throws Exception { 303 House src = TestDataFactory.getHouse(); 305 HomeDescription dest = (HomeDescription) mapper.map(src, HomeDescription.class); 306 House src2 = (House) mapper.map(dest, House.class); 307 HomeDescription dest2 = (HomeDescription) mapper.map(src2, HomeDescription.class); 308 309 long[] prim = { 1, 2, 3, 1, 2, 3 }; 310 dest.setPrim(prim); 312 assertEquals(dest, dest2); 313 314 src = TestDataFactory.getHouse(); 316 House houseClone = (House) SerializationUtils.clone(src); 317 dest = (HomeDescription) mapper.map(src, HomeDescription.class); 318 mapper.map(dest, House.class); 319 assertEquals(houseClone, src); 320 } 321 322 public void testGeneralMappingPassByReference() throws Exception { 323 TestObject to = TestDataFactory.getInputGeneralMappingTestObject(); 325 TestObject toClone = (TestObject) SerializationUtils.clone(to); 326 TestObjectPrime prime = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 327 mapper.map(prime, to); 328 TheFirstSubClass fsc = new TheFirstSubClass(); 330 fsc.setS("s"); 331 toClone.getHintList().add(fsc); 332 toClone.getHintList().add(fsc); 333 toClone.getEqualNamedList().add("1value"); 334 toClone.getEqualNamedList().add("2value"); 335 int[] pa = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 }; 336 int[] intArray = { 1, 1, 1, 1 }; 337 Integer [] integerArray = { new Integer (1), new Integer (1), new Integer (1), new Integer (1) }; 338 toClone.setAnArray(intArray); 339 toClone.setArrayForLists(integerArray); 340 toClone.setPrimArray(pa); 341 toClone.setBlankDate(null); 342 toClone.setBlankStringToLong(null); 343 to.setCopyByReference(null); 347 toClone.setCopyByReference(null); 348 to.setCopyByReferenceDeep(null); 349 toClone.setCopyByReferenceDeep(null); 350 to.setGlobalCopyByReference(null); 351 toClone.setGlobalCopyByReference(null); 352 to.setStringArrayWithNullValue(null); 354 toClone.setStringArrayWithNullValue(null); 355 toClone.setExcludeMeOneWay("excludeMeOneWay"); 356 assertEquals(toClone, to); 357 } 358 359 public void testLongToLongMapping() throws Exception { 360 TestObject source = TestDataFactory.getInputGeneralMappingTestObject(); 362 source.setAnotherLongValue(42); 363 TestObjectPrime prime2 = (TestObjectPrime) mapper.map(source, TestObjectPrime.class); 364 Long value = prime2.getTheLongValue(); 365 assertEquals(value.longValue(), 42); 366 } 367 368 public void testSuperClassMapping() throws Exception { 369 SubClass obj = TestDataFactory.getSubClass(); 370 SubClassPrime objPrime = (SubClassPrime) mapper.map(obj, SubClassPrime.class); 371 SubClass obj2 = (SubClass) mapper.map(objPrime, SubClass.class); 372 SubClassPrime objPrime2 = (SubClassPrime) mapper.map(obj2, SubClassPrime.class); 373 374 assertEquals("" + obj.getCustomConvert().getAttribute().getTheDouble(), obj2.getCustomConvert().getAttribute() 375 .getTheDouble() 376 + ""); 377 378 objPrime.setCustomConvert(null); 380 objPrime2.setCustomConvert(null); 381 objPrime.setSuperFieldToExcludePrime(null); 383 assertEquals(objPrime, objPrime2); 384 385 obj = TestDataFactory.getSubClass(); 387 SubClass subClassClone = (SubClass) SerializationUtils.clone(obj); 388 objPrime = (SubClassPrime) mapper.map(obj, SubClassPrime.class); 389 mapper.map(objPrime, obj); 390 obj.setCustomConvert(null); 391 subClassClone.setCustomConvert(null); 392 TheFirstSubClass fsc = new TheFirstSubClass(); 394 fsc.setS("s"); 395 subClassClone.getTestObject().getHintList().add(fsc); 396 subClassClone.getTestObject().getHintList().add(fsc); 397 subClassClone.getTestObject().getEqualNamedList().add("1value"); 398 subClassClone.getTestObject().getEqualNamedList().add("2value"); 399 int[] pa = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 }; 400 int[] intArray = { 1, 1, 1, 1 }; 401 Integer [] integerArray = { new Integer (1), new Integer (1), new Integer (1), new Integer (1) }; 402 subClassClone.getTestObject().setAnArray(intArray); 403 subClassClone.getTestObject().setArrayForLists(integerArray); 404 subClassClone.getTestObject().setPrimArray(pa); 405 subClassClone.getTestObject().setBlankDate(null); 406 subClassClone.getTestObject().setBlankStringToLong(null); 407 subClassClone.getSuperList().add("one"); 408 subClassClone.getSuperList().add("two"); 409 subClassClone.getSuperList().add("three"); 410 obj.getTestObject().setCopyByReference(null); 414 subClassClone.getTestObject().setCopyByReference(null); 415 obj.getTestObject().setCopyByReferenceDeep(null); 416 subClassClone.getTestObject().setCopyByReferenceDeep(null); 417 obj.getTestObject().setGlobalCopyByReference(null); 418 subClassClone.getTestObject().setGlobalCopyByReference(null); 419 obj.getTestObject().setStringArrayWithNullValue(null); 421 subClassClone.getTestObject().setStringArrayWithNullValue(null); 422 subClassClone.getTestObject().setExcludeMeOneWay("excludeMeOneWay"); 423 assertEquals(subClassClone, obj); 424 } 425 426 public void testSuperClassMapping2() throws Exception { 427 NoSuperClass src = new NoSuperClass(); 429 src.setAttribute("somefieldvalue"); 430 src.setSuperAttribute("someotherfieldvalue"); 431 432 SubClass dest = (SubClass) mapper.map(src, SubClass.class); 433 NoSuperClass src1 = (NoSuperClass) mapper.map(dest, NoSuperClass.class); 434 SubClass dest2 = (SubClass) mapper.map(src1, SubClass.class); 435 436 assertEquals(src, src1); 437 assertEquals(dest, dest2); 438 } 439 440 public void testInheritance() throws Exception { 441 net.sf.dozer.util.mapping.vo.inheritance.SubClass sub = new net.sf.dozer.util.mapping.vo.inheritance.SubClass(); 443 444 sub.setBaseAttribute("base"); 445 sub.setSubAttribute("sub"); 446 447 BaseSubClassCombined combined = (BaseSubClassCombined) mapper.map(sub, BaseSubClassCombined.class); 448 449 assertEquals(sub.getSubAttribute(), combined.getSubAttribute2()); 450 assertEquals(sub.getBaseAttribute(), combined.getBaseAttribute2()); 451 } 452 453 public void testInheritance2() throws Exception { 454 AnotherSubClass asub = new AnotherSubClass(); 456 asub.setBaseAttribute("base"); 457 asub.setSubAttribute("sub"); 458 List list = new ArrayList (); 459 SClass s = new SClass(); 460 s.setBaseSubAttribute("sBase"); 461 s.setSubAttribute("s"); 462 S2Class s2 = new S2Class(); 463 s2.setBaseSubAttribute("s2Base"); 464 s2.setSub2Attribute("s2"); 465 list.add(s2); 466 list.add(s); 467 asub.setSubList(list); 468 469 List list2 = new ArrayList (); 470 SClass sclass = new SClass(); 471 sclass.setBaseSubAttribute("sBase"); 472 sclass.setSubAttribute("s"); 473 S2Class s2class = new S2Class(); 474 s2class.setBaseSubAttribute("s2Base"); 475 s2class.setSub2Attribute("s2"); 476 SClass sclass2 = new SClass(); 477 sclass2.setBaseSubAttribute("sclass2"); 478 sclass2.setSubAttribute("sclass2"); 479 list2.add(s2class); 480 list2.add(sclass); 481 list2.add(sclass2); 482 asub.setListToArray(list2); 483 484 SClass sclassA = new SClass(); 485 SClass sclassB = new SClass(); 486 sclassA.setBaseSubAttribute("sBase"); 487 sclassA.setSubAttribute("s"); 488 sclassB.setBaseSubAttribute("sBase"); 489 sclassB.setSubAttribute("s"); 490 asub.setSClass(sclassA); 491 asub.setSClass2(sclassB); 492 493 AnotherSubClassPrime subPrime = (AnotherSubClassPrime) mapper.map(asub, AnotherSubClassPrime.class); 494 495 assertEquals(asub.getSubAttribute(), subPrime.getSubAttribute2()); 496 assertEquals(asub.getBaseAttribute(), subPrime.getBaseAttribute2()); 497 assertTrue(subPrime.getTheListOfSubClassPrime().get(0) instanceof S2ClassPrime); 498 assertTrue(subPrime.getTheListOfSubClassPrime().get(1) instanceof SClassPrime); 499 assertEquals(s2.getSub2Attribute(), ((S2ClassPrime) subPrime.getTheListOfSubClassPrime().get(0)) 500 .getSub2Attribute2()); 501 assertEquals(s2.getBaseSubAttribute(), ((S2ClassPrime) subPrime.getTheListOfSubClassPrime().get(0)) 502 .getBaseSubAttribute2()); 503 assertEquals(s.getSubAttribute(), ((SClassPrime) subPrime.getTheListOfSubClassPrime().get(1)).getSubAttribute2()); 504 assertEquals(s.getBaseSubAttribute(), ((SClassPrime) subPrime.getTheListOfSubClassPrime().get(1)) 505 .getBaseSubAttribute2()); 506 assertTrue(subPrime.getArrayToList()[0] instanceof S2ClassPrime); 507 assertTrue(subPrime.getArrayToList()[1] instanceof SClassPrime); 508 assertTrue(subPrime.getArrayToList()[2] instanceof SClassPrime); 509 assertEquals(s2class.getSub2Attribute(), ((S2ClassPrime) subPrime.getArrayToList()[0]).getSub2Attribute2()); 510 assertEquals(s2class.getBaseSubAttribute(), ((S2ClassPrime) subPrime.getArrayToList()[0]).getBaseSubAttribute2()); 511 assertEquals(sclass.getSubAttribute(), ((SClassPrime) subPrime.getArrayToList()[1]).getSubAttribute2()); 512 assertEquals(sclass.getBaseSubAttribute(), ((SClassPrime) subPrime.getArrayToList()[1]).getBaseSubAttribute2()); 513 assertEquals(sclass2.getSubAttribute(), ((SClassPrime) subPrime.getArrayToList()[2]).getSubAttribute2()); 514 assertEquals(sclass2.getBaseSubAttribute(), ((SClassPrime) subPrime.getArrayToList()[2]).getBaseSubAttribute2()); 515 assertEquals(asub.getSClass().getSubAttribute(), subPrime.getSClass().getSubAttribute2()); 516 assertEquals(asub.getSClass().getBaseSubAttribute(), subPrime.getSClass().getBaseSubAttribute2()); 517 assertEquals(asub.getSClass2().getSubAttribute(), subPrime.getSClass2().getSubAttribute2()); 518 assertEquals(asub.getSClass2().getBaseSubAttribute(), subPrime.getSClass2().getBaseSubAttribute2()); 519 520 AnotherSubClass sub = (AnotherSubClass) mapper.map(subPrime, AnotherSubClass.class); 522 assertTrue(sub.getSubList().get(0) instanceof S2Class); 523 assertTrue(sub.getSubList().get(1) instanceof SClass); 524 assertEquals(s2.getSub2Attribute(), ((S2Class) sub.getSubList().get(0)).getSub2Attribute()); 525 assertEquals(s2.getBaseSubAttribute(), ((S2Class) sub.getSubList().get(0)).getBaseSubAttribute()); 526 assertEquals(s.getSubAttribute(), ((SClass) sub.getSubList().get(1)).getSubAttribute()); 527 assertEquals(s.getBaseSubAttribute(), ((SClass) sub.getSubList().get(1)).getBaseSubAttribute()); 528 } 529 530 public void testInheritanceWithAbstractClassOrInterfaceAsDestination() throws Exception { 531 SpecificObject so = new SpecificObject(); 532 so.setSuperAttr1("superAttr1"); 533 534 Object obj = mapper.map(so, GenericAbstractSuper.class); 536 assertTrue(obj instanceof Specific3); 537 Specific3 spec3 = (Specific3) obj; 538 assertEquals("superAttr1", spec3.getSuperAttr3()); 539 assertEquals("superAttr1", spec3.getSuperAttr2()); 540 541 obj = mapper.map(so, GenericIF.class); 543 assertTrue(obj instanceof Specific3); 544 spec3 = (Specific3) obj; 545 assertEquals("superAttr1", spec3.getSuperAttr3()); 546 assertEquals("superAttr1", spec3.getSuperAttr2()); 547 548 WrapperSpecific ws = new WrapperSpecific(); 549 SpecificObject so2 = new SpecificObject(); 550 so2.setSuperAttr1("superAttr1"); 551 ws.setSpecificObject(so2); 552 WrapperSpecificPrime wsp = (WrapperSpecificPrime) mapper.map(ws, WrapperSpecificPrime.class); 553 assertTrue(wsp.getSpecificObjectPrime() instanceof Specific3); 554 assertEquals("superAttr1", ((Specific3) wsp.getSpecificObjectPrime()).getSuperAttr3()); 555 assertEquals("superAttr1", ((Specific3) wsp.getSpecificObjectPrime()).getSuperAttr2()); 556 } 557 558 public void testNoWildcards() throws Exception { 559 FurtherTestObjectPrime prime = (FurtherTestObjectPrime) mapper.map(TestDataFactory 561 .getInputTestNoWildcardsFurtherTestObject(), FurtherTestObjectPrime.class); 562 FurtherTestObject source = (FurtherTestObject) mapper.map(prime, FurtherTestObject.class); 563 FurtherTestObjectPrime prime2 = (FurtherTestObjectPrime) mapper.map(source, FurtherTestObjectPrime.class); 564 assertEquals(prime2, prime); 565 } 566 567 public void testHydrateAndMore() throws Exception { 568 HydrateTestObject dest = (HydrateTestObject) mapper.map(TestDataFactory 569 .getInputTestHydrateAndMoreDehydrateTestObject(), HydrateTestObject.class); 570 assertEquals(TestDataFactory.getExpectedTestHydrateAndMoreHydrateTestObject(), dest); 572 573 DehydrateTestObject dhto = (DehydrateTestObject) mapper.map(TestDataFactory 575 .getInputTestHydrateAndMoreHydrateTestObject(), DehydrateTestObject.class); 576 assertEquals(TestDataFactory.getExpectedTestHydrateAndMoreDehydrateTestObject(), dhto); 577 } 578 579 public void testDeepProperties() throws Exception { 580 House src = TestDataFactory.getHouse(); 581 HomeDescription dest = (HomeDescription) mapper.map(src, HomeDescription.class); 582 House src2 = (House) mapper.map(dest, House.class); 583 HomeDescription dest2 = (HomeDescription) mapper.map(src2, HomeDescription.class); 584 585 long[] prim = { 1, 2, 3, 1, 2, 3 }; 586 dest.setPrim(prim); 588 assertEquals(dest, dest2); 589 590 src = TestDataFactory.getHouse(); 592 House houseClone = (House) SerializationUtils.clone(src); 593 dest = (HomeDescription) mapper.map(src, HomeDescription.class); 594 mapper.map(dest, src); 595 int[] prims = { 1, 2, 3, 1, 2, 3 }; 597 houseClone.getOwner().setPrim(prims); 598 Room room1 = new Room(); 600 room1.setName("Living"); 601 Room room2 = new Room(); 602 room2.setName("kitchen"); 603 Van van = new Van(); 604 van.setName("van2"); 605 houseClone.getRooms().add(room1); 606 houseClone.getRooms().add(room2); 607 houseClone.getCustomSetGetMethod().add(van); 608 assertEquals(houseClone, src); 609 } 610 611 public void testDeepMapping() throws Exception { 612 SrcDeepObj src = TestDataFactory.getSrcDeepObj(); 613 DestDeepObj dest = (DestDeepObj) mapper.map(src, DestDeepObj.class); 614 SrcDeepObj src2 = (SrcDeepObj) mapper.map(dest, SrcDeepObj.class); 615 DestDeepObj dest2 = (DestDeepObj) mapper.map(src2, DestDeepObj.class); 616 617 assertEquals(src, src2); 618 assertEquals(dest, dest2); 619 620 } 621 622 public void testMethodMapping() throws Exception { 623 MethodFieldTestObject sourceObj = new MethodFieldTestObject(); 624 sourceObj.setIntegerStr("1500"); 625 sourceObj.setPriceItem("3500"); 626 sourceObj.setFieldOne("fieldOne"); 627 MethodFieldTestObject2 result = (MethodFieldTestObject2) mapper.map(sourceObj, MethodFieldTestObject2.class); 628 assertEquals("invalid result object size", 1, result.getIntegerList().size()); 629 assertEquals("invalid result object value", 3500, result.getTotalPrice()); 630 assertEquals("invalid result object value", "fieldOne", result.getFieldOne()); 631 MethodFieldTestObject result2 = (MethodFieldTestObject) mapper.map(result, MethodFieldTestObject.class); 633 assertNull(result2.getFieldOne()); 636 } 637 638 public void testCustomConverterMapping() throws Exception { 639 TestCustomConverterObject obj = new TestCustomConverterObject(); 640 CustomDoubleObjectIF doub = new CustomDoubleObject(); 641 doub.setTheDouble(15); 642 CustomDoubleObjectIF doub2 = new CustomDoubleObject(); 643 doub2.setTheDouble(15); 644 obj.setAttribute(doub); 645 646 Collection list = new ArrayList (); 647 list.add(doub2); 648 649 obj.setNames(list); 650 651 TestCustomConverterObjectPrime dest = (TestCustomConverterObjectPrime) mapper.map(obj, 652 TestCustomConverterObjectPrime.class); 653 654 assertEquals("Custom Converter failed", dest.getDoubleAttribute().doubleValue() + "", "15.0"); 655 assertEquals("Custom Converter failed", ((Double ) dest.getNames().iterator().next()).doubleValue() + "", "15.0"); 656 657 TestCustomConverterObjectPrime objp = new TestCustomConverterObjectPrime(); 658 659 objp.setDoubleAttribute(new Double (15)); 660 661 Collection list2 = new ArrayList (); 662 objp.setNames(list2); 663 objp.getNames().add(new Double (10)); 664 665 TestCustomConverterObject destp = (TestCustomConverterObject) mapper.map(objp, TestCustomConverterObject.class); 666 667 assertEquals("Custom Converter failed", destp.getAttribute().getTheDouble() + "", "15.0"); 668 assertEquals("Custom Converter failed", ((CustomDoubleObjectIF) destp.getNames().iterator().next()).getTheDouble() 669 + "", "10.0"); 670 671 destp.getAttribute().setName("testName"); 672 673 mapper.map(objp, destp); 675 676 assertEquals("Custom Converter failed", destp.getAttribute().getTheDouble() + "", "15.0"); 677 assertEquals("testName", destp.getAttribute().getName()); 678 679 TestCustomConverterObjectPrime prime = new TestCustomConverterObjectPrime(); 681 prime.setPrimitiveDoubleAttribute(25.00); 682 TestCustomConverterObject obj2 = (TestCustomConverterObject) mapper.map(prime, TestCustomConverterObject.class); 683 CustomDoubleObjectIF customDouble = obj2.getPrimitiveDoubleAttribute(); 684 assertNotNull(customDouble); 685 assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble()); 686 687 prime = (TestCustomConverterObjectPrime) mapper.map(obj2, TestCustomConverterObjectPrime.class); 689 assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble()); 690 691 } 692 693 public void testCustomConverterWithPrimitive() throws Exception { 694 695 TestCustomConverterObjectPrime prime = new TestCustomConverterObjectPrime(); 697 prime.setPrimitiveDoubleAttribute(25.00); 698 prime.setDoubleAttribute(new Double (30.00)); 699 TestCustomConverterObject obj2 = (TestCustomConverterObject) mapper.map(prime, TestCustomConverterObject.class); 700 CustomDoubleObjectIF customDouble = obj2.getPrimitiveDoubleAttribute(); 701 assertNotNull(customDouble); 702 assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble()); 703 704 prime = (TestCustomConverterObjectPrime) mapper.map(obj2, TestCustomConverterObjectPrime.class); 706 assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble()); 707 708 } 709 710 public void testCustomConverterHashMapMapping() throws Exception { 711 TestCustomConverterHashMapObject testCustomConverterHashMapObject = new TestCustomConverterHashMapObject(); 712 TestObject to = new TestObject(); 713 to.setOne("one"); 714 testCustomConverterHashMapObject.setTestObject(to); 715 TestObjectPrime top = new TestObjectPrime(); 716 top.setOnePrime("onePrime"); 717 testCustomConverterHashMapObject.setTestObjectPrime(top); 718 TestCustomConverterHashMapPrimeObject dest = (TestCustomConverterHashMapPrimeObject) mapper.map( 719 testCustomConverterHashMapObject, TestCustomConverterHashMapPrimeObject.class); 720 assertEquals(to, dest.getTestObjects().get("object1")); 721 assertEquals(top, dest.getTestObjects().get("object2")); 722 723 } 724 725 public void testStringToDateMapping() throws Exception { 726 DateFormat df = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss:SS"); 727 String dateStr = "01/29/1975 10:45:13:25"; 728 TestObject sourceObj = new TestObject(); 729 sourceObj.setDateStr(dateStr); 730 TestObjectPrime result = (TestObjectPrime) mapper.map(sourceObj, TestObjectPrime.class); 731 assertEquals(df.parse(dateStr), result.getDateFromStr()); 732 assertEquals(dateStr, df.format(result.getDateFromStr())); 733 734 TestObject result2 = (TestObject) mapper.map(result, TestObject.class); 735 assertEquals(df.format(result.getDateFromStr()), result2.getDateStr()); 736 assertEquals(result.getDateFromStr(), df.parse(result2.getDateStr())); 737 } 738 739 public void testNullDestClass() throws Exception { 740 try { 741 mapper.map(new TestObject(), null); 742 fail("should have thrown mapping exception"); 743 } catch (MappingException e) { 744 } 745 } 746 747 public void testNullDestObj() throws Exception { 748 try { 749 Object destObj = null; 750 mapper.map(new TestObject(), destObj); 751 fail("should have thrown mapping exception"); 752 } catch (MappingException e) { 753 } 754 } 755 756 public void testNoGetReadMethod() throws Exception { 757 try { 758 mapper.map(new NoReadMethod(), NoReadMethodPrime.class); 759 fail("should have thrown mapping exception"); 760 } catch (MappingException e) { 761 assertEquals( 762 "net.sf.dozer.util.mapping.MappingException: Unable to determine read method for field: noReadMethod class: class net.sf.dozer.util.mapping.vo.NoReadMethod", 763 e.getMessage()); 764 } 765 } 766 767 public void testNoGetWriteMethod() throws Exception { 768 try { 769 mapper.map(new NoWriteMethod(), NoWriteMethodPrime.class); 770 fail("should have thrown mapping exception"); 771 } catch (MappingException e) { 772 assertEquals( 773 "net.sf.dozer.util.mapping.MappingException: Unable to determine write method for field: noWriteMethod class: class net.sf.dozer.util.mapping.vo.NoWriteMethodPrime", 774 e.getMessage()); 775 } 776 } 777 778 public void testOneWayMapping() throws Exception { 779 OneWayObject owo = new OneWayObject(); 781 OneWayObjectPrime owop = new OneWayObjectPrime(); 782 SrcNestedDeepObj nested = new SrcNestedDeepObj(); 783 nested.setSrc1("src1"); 784 owo.setNested(nested); 785 owop.setOneWayPrimeField("oneWayField"); 786 owop.setSetOnlyField("setOnly"); 787 List list = new ArrayList (); 788 list.add("stringToList"); 789 list.add("src1"); 790 owop.setStringList(list); 791 owo.setOneWayField("oneWayField"); 792 owo.setStringToList("stringToList"); 793 OneWayObjectPrime prime = (OneWayObjectPrime) mapper.map(owo, OneWayObjectPrime.class); 794 795 assertEquals(owop, prime); 796 797 OneWayObject source = (OneWayObject) mapper.map(prime, OneWayObject.class); 798 assertEquals(null, source.getOneWayField()); 800 } 801 802 public void testNullField() throws Exception { 803 AnotherTestObject src = new AnotherTestObject(); 804 src.setField2(null); 805 AnotherTestObjectPrime dest = new AnotherTestObjectPrime(); 806 dest.setField2(Integer.valueOf("555")); 807 mapper.map(src, dest); 809 assertNull("dest field should be null", dest.getField2()); 810 } 811 812 public void testNullField2() throws Exception { 813 String input = ""; 816 TestObject src = new TestObject(); 817 src.setOne(input); 818 819 TestObjectPrime dest = (TestObjectPrime) mapper.map(src, TestObjectPrime.class); 820 assertNotNull("dest field should not be null", dest.getOnePrime()); 821 assertEquals("invalid dest field value", input, dest.getOnePrime()); 822 } 823 824 public void testNullToPrimitive() throws Exception { 825 AnotherTestObject src = new AnotherTestObject(); 826 AnotherTestObjectPrime prime = new AnotherTestObjectPrime(); 827 TestObject to = new TestObject(); 828 to.setThePrimitive(AnotherTestObjectPrime.DEFAULT_FIELD1); 829 prime.setTo(to); 830 mapper.map(src, prime); 831 assertEquals("invalid field value", AnotherTestObjectPrime.DEFAULT_FIELD1, prime.getField1()); 834 assertEquals("invalid field value", AnotherTestObjectPrime.DEFAULT_FIELD1, prime.getTo().getThePrimitive()); 835 } 836 837 public void testMapByReference() throws Exception { 838 TestReferenceObject tro = new TestReferenceObject(); 840 TestReferenceFoo foo1 = new TestReferenceFoo(); 841 foo1.setA("a"); 842 TestReferenceFoo foo = new TestReferenceFoo(); 843 foo.setA("a"); 844 foo.setB(null); 845 foo.setC("c"); 846 List list2 = new ArrayList (); 847 list2.add(foo); 848 tro.setListA(list2); 849 tro.setArrayToArrayCumulative(new Object [] { foo1 }); 850 TestReferenceFoo foo2 = new TestReferenceFoo(); 851 foo2.setA("a"); 852 foo2.setB(null); 853 foo2.setC("c"); 854 TestReferenceFoo foo3 = new TestReferenceFoo(); 855 foo3.setA("a"); 856 foo3.setB(null); 857 foo3.setC("c"); 858 tro.setArrayToArrayNoncumulative(new Object [] { foo2 }); 859 List list3 = new ArrayList (); 860 list3.add("string1"); 861 list3.add("string2"); 862 tro.setListToArray(list3); 863 int[] pa = { 1, 2, 3 }; 864 tro.setPrimitiveArray(pa); 865 Integer [] integerArray = { new Integer (1), new Integer (2) }; 866 tro.setPrimitiveArrayWrapper(integerArray); 867 Set set = new HashSet (); 868 TestReferenceFoo foo4 = new TestReferenceFoo(); 869 foo4.setA("a"); 870 set.add(foo4); 871 tro.setSetToSet(set); 872 Car car = new Car(); 873 car.setName("myName"); 874 tro.setCars(new Car[] { car }); 875 Car car2 = new Car(); 876 car2.setName("myName"); 877 List vehicles = new ArrayList (); 878 vehicles.add(car2); 879 tro.setVehicles(vehicles); 880 TestReferenceObject toClone = (TestReferenceObject) SerializationUtils.clone(tro); 881 TestReferencePrimeObject trop = (TestReferencePrimeObject) mapper.map(tro, TestReferencePrimeObject.class); 882 assertEquals("myName", ((Van) trop.getVans()[0]).getName()); 883 assertEquals("myName", ((Van) trop.getMoreVans()[0]).getName()); 884 885 TestReferenceFooPrime fooPrime = (TestReferenceFooPrime) trop.getListAPrime().get(0); 886 fooPrime.setB("b"); 887 TestReferenceFooPrime fooPrime2 = (TestReferenceFooPrime) trop.getArrayToArrayNoncumulative()[0]; 888 fooPrime2.setB("b"); 889 mapper.map(trop, tro); 890 assertEquals("c", ((TestReferenceFoo) tro.getListA().get(0)).getC()); 892 assertEquals("c", ((TestReferenceFoo) tro.getArrayToArrayNoncumulative()[0]).getC()); 893 toClone.setArrayToArrayCumulative(new Object [] { foo1, foo1 }); 895 toClone.setCars(new Car[] { car, car }); 896 Van van = new Van(); 897 van.setName("myName"); 898 toClone.getVehicles().add(van); 899 toClone.getListToArray().add("string1"); 901 toClone.getListToArray().add("string2"); 902 int[] paClone = { 1, 2, 3, 1, 2, 3 }; 903 toClone.setPrimitiveArray(paClone); 904 Integer [] integerArrayClone = { new Integer (1), new Integer (2), new Integer (1), new Integer (2) }; 905 toClone.setPrimitiveArrayWrapper(integerArrayClone); 906 assertEquals(toClone, tro); 907 } 908 909 public void testHintedOnlyConverter() throws Exception { 910 String hintStr = "where's my hint?"; 911 912 CustomConverterWrapper source = new CustomConverterWrapper(); 913 HintedOnly hint = new HintedOnly(); 914 hint.setStr(hintStr); 915 source.addHint(hint); 916 917 CustomConverterWrapperPrime dest = (CustomConverterWrapperPrime) mapper.map(source, CustomConverterWrapperPrime.class); 918 String destHintStr = (String ) dest.getNeedsHint().iterator().next(); 919 assertNotNull(destHintStr); 920 assertEquals(hintStr, destHintStr); 921 922 CustomConverterWrapper sourcePrime = (CustomConverterWrapper) mapper.map(dest, CustomConverterWrapper.class); 923 String sourcePrimeHintStr = ((HintedOnly) sourcePrime.getNeedsHint().iterator().next()).getStr(); 924 assertNotNull(sourcePrimeHintStr); 925 assertEquals(hintStr, sourcePrimeHintStr); 926 } 927 928 public void testDeepPropertyOneWay() throws Exception { 929 House house = new House(); 930 Person owner = new Person(); 931 owner.setYourName("myName"); 932 house.setOwner(owner); 933 HomeDescription desc = (HomeDescription) mapper.map(house, HomeDescription.class); 934 assertEquals(desc.getDescription().getMyName(), "myName"); 935 House house2 = (House) mapper.map(desc, House.class); 937 assertNull(house2.getOwner().getYourName()); 938 } 939 940 public void testSelfMapping() throws Exception { 941 SimpleAccount simpleAccount = new SimpleAccount(); 942 simpleAccount.setName("name"); 943 simpleAccount.setPostcode(1234); 944 simpleAccount.setStreetName("streetName"); 945 simpleAccount.setSuburb("suburb"); 946 Account account = (Account) mapper.map(simpleAccount, Account.class); 947 assertEquals(account.getAddress().getStreet(), simpleAccount.getStreetName()); 948 assertEquals(account.getAddress().getSuburb(), simpleAccount.getSuburb()); 949 assertEquals(account.getAddress().getPostcode(), simpleAccount.getPostcode()); 950 951 SimpleAccount dest = (SimpleAccount) mapper.map(account, SimpleAccount.class); 953 assertEquals(account.getAddress().getStreet(), dest.getStreetName()); 954 assertEquals(account.getAddress().getSuburb(), dest.getSuburb()); 955 assertEquals(account.getAddress().getPostcode(), dest.getPostcode()); 956 } 957 958 public void testPropertyToMap() throws Exception { 959 PropertyToMap ptm = new PropertyToMap(); 960 ptm.setStringProperty("stringPropertyValue"); 961 ptm.addStringProperty2("stringProperty2Value"); 962 Map hashMap = new HashMap (); 963 hashMap.put("reverseMapString", "reverseMapStringValue"); 964 hashMap.put("reverseMapInteger", new Integer ("567")); 965 ptm.setReverseMap(hashMap); 966 MapToProperty mtp = (MapToProperty) mapper.map(ptm, MapToProperty.class); 967 assertTrue(mtp.getHashMap().containsKey("stringProperty")); 968 assertTrue(mtp.getHashMap().containsValue("stringPropertyValue")); 969 assertTrue(mtp.getHashMap().containsKey("myStringProperty")); 970 assertTrue(mtp.getHashMap().containsValue("stringProperty2Value")); 971 assertFalse(mtp.getHashMap().containsValue("nullStringProperty")); 972 assertTrue(mtp.getNullHashMap().containsValue("stringPropertyValue")); 973 assertEquals("reverseMapStringValue", mtp.getReverseMapString()); 974 assertEquals(((Integer ) hashMap.get("reverseMapInteger")).toString(), mtp.getReverseMapInteger()); 975 976 PropertyToMap dest = (PropertyToMap) mapper.map(mtp, PropertyToMap.class); 978 assertTrue(dest.getStringProperty().equals("stringPropertyValue")); 979 assertTrue(dest.getStringProperty2().equals("stringProperty2Value")); 980 assertTrue(dest.getReverseMap().containsKey("reverseMapString")); 981 assertTrue(dest.getReverseMap().containsValue("reverseMapStringValue")); 982 assertNull(dest.getNullStringProperty()); 983 } 984 985 public void testPropertyToCustomMap() throws Exception { 986 PropertyToMap ptm = new PropertyToMap(); 987 ptm.setStringProperty3("stringProperty3Value"); 988 ptm.setStringProperty4("stringProperty4Value"); 989 ptm.setStringProperty5("stringProperty5Value"); 990 MapToProperty mtp = (MapToProperty) mapper.map(ptm, MapToProperty.class); 991 assertEquals("stringProperty3Value", mtp.getCustomMap().getValue("myCustomProperty")); 992 assertEquals("stringProperty5Value", mtp.getCustomMap().getValue("stringProperty5")); 993 assertEquals("stringProperty4Value", mtp.getNullCustomMap().getValue("myCustomNullProperty")); 994 assertEquals("stringProperty5Value", mtp.getCustomMapWithDiffSetMethod().getValue("stringProperty5")); 995 996 PropertyToMap dest = (PropertyToMap) mapper.map(mtp, PropertyToMap.class); 998 assertEquals("stringProperty3Value", dest.getStringProperty3()); 999 assertEquals("stringProperty4Value", dest.getStringProperty4()); 1000 assertEquals("stringProperty5Value", dest.getStringProperty5()); 1001 } 1002 1003 public void testPropertyToClassLevelMap() throws Exception { 1004 MapTestObject mto = new MapTestObject(); 1005 PropertyToMap ptm = new PropertyToMap(); 1006 Map map = new HashMap (); 1007 map.put("reverseClassLevelMapString", "reverseClassLevelMapStringValue"); 1008 mto.setPropertyToMapMapReverse(map); 1009 ptm.setStringProperty("stringPropertyValue"); 1010 ptm.addStringProperty2("stringProperty2Value"); 1011 ptm.setStringProperty3("stringProperty3Value"); 1012 ptm.setStringProperty4("stringProperty4Value"); 1013 ptm.setStringProperty5("stringProperty5Value"); 1014 mto.setPropertyToMap(ptm); 1015 PropertyToMap ptm2 = new PropertyToMap(); 1016 ptm2.setStringProperty("stringPropertyValue"); 1017 mto.setPropertyToMapToNullMap(ptm2); 1018 MapTestObjectPrime mtop = (MapTestObjectPrime) mapper.map(mto, MapTestObjectPrime.class); 1019 assertTrue(mtop.getPropertyToMapMap().containsKey("stringProperty")); 1020 assertTrue(mtop.getPropertyToMapMap().containsKey("myStringProperty")); 1021 assertTrue(mtop.getPropertyToMapMap().containsKey("stringProperty3")); 1022 assertTrue(mtop.getPropertyToMapMap().containsKey("stringProperty4")); 1023 assertTrue(mtop.getPropertyToMapMap().containsKey("stringProperty5")); 1024 assertTrue(mtop.getPropertyToMapMap().containsKey("nullStringProperty")); 1025 assertTrue(mtop.getPropertyToMapMap().containsValue("stringPropertyValue")); 1026 assertTrue(mtop.getPropertyToMapMap().containsValue("stringProperty2Value")); 1027 assertTrue(mtop.getPropertyToMapMap().containsValue("stringProperty3Value")); 1028 assertTrue(mtop.getPropertyToMapMap().containsValue("stringProperty4Value")); 1029 assertTrue(mtop.getPropertyToMapMap().containsValue("stringProperty5Value")); 1030 assertFalse(mtop.getPropertyToMapMap().containsValue("nullStringProperty")); 1031 assertFalse(mtop.getPropertyToMapMap().containsKey("excludeMe")); 1032 assertEquals("reverseClassLevelMapStringValue", mtop.getPropertyToMapReverse().getReverseClassLevelMapString()); 1033 assertTrue(mtop.getNullPropertyToMapMap().containsKey("stringProperty")); 1034 assertEquals("stringPropertyValue", mtop.getNullPropertyToMapMap().get("stringProperty")); 1035 1036 MapTestObject mto2 = (MapTestObject) mapper.map(mtop, MapTestObject.class); 1038 assertEquals("stringPropertyValue", mto2.getPropertyToMap().getStringProperty()); 1039 assertEquals("stringProperty2Value", mto2.getPropertyToMap().getStringProperty2()); 1040 assertEquals("stringProperty3Value", mto2.getPropertyToMap().getStringProperty3()); 1041 assertEquals("stringProperty4Value", mto2.getPropertyToMap().getStringProperty4()); 1042 assertEquals("stringProperty5Value", mto2.getPropertyToMap().getStringProperty5()); 1043 assertTrue(mto2.getPropertyToMapMapReverse().containsKey("reverseClassLevelMapString")); 1044 assertEquals("reverseClassLevelMapStringValue", mto2.getPropertyToMapMapReverse().get("reverseClassLevelMapString")); 1045 1046 } 1047 1048 public void testPropertyToCustomClassLevelMap() throws Exception { 1049 MapTestObject mto = new MapTestObject(); 1050 PropertyToMap ptm = new PropertyToMap(); 1051 ptm.setStringProperty("stringPropertyValue"); 1052 ptm.addStringProperty2("stringProperty2Value"); 1053 mto.setPropertyToCustomMap(ptm); 1054 CustomMapIF customMap = new CustomMap(); 1055 customMap.putValue("stringProperty", "stringPropertyValue"); 1056 mto.setPropertyToCustomMapMapWithInterface(customMap); 1057 MapTestObjectPrime mtop = (MapTestObjectPrime) mapper.map(mto, MapTestObjectPrime.class); 1058 assertEquals("stringPropertyValue", mtop.getPropertyToCustomMapMap().getValue("stringProperty")); 1059 assertNull(mtop.getPropertyToCustomMapMap().getValue("excludeMe")); 1060 assertEquals("stringProperty2Value", mtop.getPropertyToCustomMapMap().getValue("myStringProperty")); 1061 assertEquals("stringPropertyValue", mtop.getPropertyToCustomMapWithInterface().getStringProperty()); 1062 1063 MapTestObject mto2 = (MapTestObject) mapper.map(mtop, MapTestObject.class); 1065 assertEquals("stringPropertyValue", mto2.getPropertyToCustomMap().getStringProperty()); 1066 assertEquals("stringProperty2Value", mto2.getPropertyToCustomMap().getStringProperty2()); 1067 assertNull(mto2.getPropertyToCustomMap().getExcludeMe()); 1068 assertEquals("stringPropertyValue", mto2.getPropertyToCustomMapMapWithInterface().getValue("stringProperty")); 1069 1070 } 1071 1072 public void testPropertyClassLevelMap() throws Exception { 1073 PropertyToMap ptm = new PropertyToMap(); 1074 ptm.setStringProperty("stringPropertyValue"); 1075 ptm.addStringProperty2("stringProperty2Value"); 1076 Map map = (Map ) mapper.map(ptm, HashMap .class, "myTestMapping"); 1077 assertEquals("stringPropertyValue", map.get("stringProperty")); 1078 assertEquals("stringProperty2Value", map.get("myStringProperty")); 1079 1080 CustomMapIF customMap = (CustomMapIF) mapper.map(ptm, CustomMap.class, "myCustomTestMapping"); 1081 assertEquals("stringPropertyValue", customMap.getValue("stringProperty")); 1082 assertEquals("stringProperty2Value", customMap.getValue("myStringProperty")); 1083 1084 CustomMapIF custom = new CustomMap(); 1085 custom.putValue("myKey", "myValue"); 1086 mapper.map(ptm, custom, "myCustomTestMapping"); 1087 assertEquals("stringPropertyValue", custom.getValue("stringProperty")); 1088 assertEquals("myValue", custom.getValue("myKey")); 1089 } 1090 1091 public void testPropertyClassLevelMapBack() throws Exception { 1092 Map map = new HashMap (); 1094 map.put("stringProperty", "stringPropertyValue"); 1095 map.put("integerProperty", new Integer ("567")); 1096 PropertyToMap property = (PropertyToMap) mapper.map(map, PropertyToMap.class, "myTestMapping"); 1097 assertEquals("stringPropertyValue", property.getStringProperty()); 1098 1099 CustomMapIF custom = new CustomMap(); 1100 custom.putValue("stringProperty", "stringPropertyValue"); 1101 PropertyToMap property2 = (PropertyToMap) mapper.map(custom, PropertyToMap.class, "myCustomTestMapping"); 1102 assertEquals("stringPropertyValue", property2.getStringProperty()); 1103 1104 map.put("stringProperty3", "myValue"); 1105 mapper.map(map, property, "myTestMapping"); 1106 assertEquals("myValue", property.getStringProperty3()); 1107 1108 } 1109 1110 public void testSetToArray() throws Exception { 1111 Orange orange1 = new Orange(); 1112 orange1.setName("orange1"); 1113 Orange orange2 = new Orange(); 1114 orange2.setName("orange2"); 1115 Orange orange3 = new Orange(); 1116 orange3.setName("orange3"); 1117 Orange orange4 = new Orange(); 1118 orange4.setName("orange4"); 1119 Set set = new HashSet (); 1120 set.add(orange1); 1121 set.add(orange2); 1122 Set set2 = new HashSet (); 1123 set2.add(orange3); 1124 set2.add(orange4); 1125 TestObject to = new TestObject(); 1126 to.setSetToArray(set); 1127 to.setSetToObjectArray(set2); 1128 TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 1129 assertEquals("orange1", top.getArrayToSet()[0].getName()); 1130 assertEquals("orange2", top.getArrayToSet()[1].getName()); 1131 assertEquals("orange3", ((Apple) top.getObjectArrayToSet()[1]).getName()); 1133 assertEquals("orange4", ((Apple) top.getObjectArrayToSet()[0]).getName()); 1134 Apple apple = new Apple(); 1135 apple.setName("apple1"); 1136 Apple[] appleArray = { apple }; 1137 top.setSetToArrayWithValues(appleArray); 1138 Apple apple2 = new Apple(); 1140 apple2.setName("apple2"); 1141 TestObject toDest = new TestObject(); 1142 Set hashSet = new HashSet (); 1143 hashSet.add(apple2); 1144 toDest.setSetToArrayWithValues(hashSet); 1145 mapper.map(top, toDest); 1146 assertTrue(toDest.getSetToArray().contains(top.getArrayToSet()[0])); 1147 assertTrue(toDest.getSetToArray().contains(top.getArrayToSet()[1])); 1148 assertTrue(toDest.getSetToObjectArray().contains((Apple) top.getObjectArrayToSet()[0])); 1149 assertTrue(toDest.getSetToObjectArray().contains((Apple) top.getObjectArrayToSet()[1])); 1150 assertTrue(toDest.getSetToArrayWithValues().contains(apple)); 1151 assertTrue(toDest.getSetToArrayWithValues().contains(apple2)); 1152 assertTrue(toDest.getSetToArrayWithValues() instanceof HashSet ); 1153 } 1154 1155 public void testSetToList() throws Exception { 1156 Orange orange1 = new Orange(); 1157 orange1.setName("orange1"); 1158 Orange orange2 = new Orange(); 1159 orange2.setName("orange2"); 1160 Set set = new HashSet (); 1161 set.add(orange1); 1162 set.add(orange2); 1163 TestObject to = new TestObject(); 1164 to.setSetToList(set); 1165 TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class); 1166 assertEquals("orange1", ((Orange) top.getListToSet().get(0)).getName()); 1167 assertEquals("orange2", ((Orange) top.getListToSet().get(1)).getName()); 1168 List list = new ArrayList (); 1169 Orange orange4 = new Orange(); 1170 orange4.setName("orange4"); 1171 list.add(orange4); 1172 top.setSetToListWithValues(list); 1173 Orange orange3 = new Orange(); 1175 orange3.setName("orange3"); 1176 Set set2 = new HashSet (); 1177 set2.add(orange3); 1178 set2.add(orange4); 1179 TestObject toDest = new TestObject(); 1180 toDest.setSetToListWithValues(set2); 1181 mapper.map(top, toDest); 1182 assertTrue(toDest.getSetToList().contains(top.getListToSet().get(0))); 1183 assertTrue(toDest.getSetToList().contains(top.getListToSet().get(1))); 1184 assertTrue(toDest.getSetToListWithValues().contains(orange3)); 1185 assertTrue(toDest.getSetToListWithValues().contains(orange4)); 1186 } 1187 1188 public void testInfiniteLoop() throws Exception { 1189 MapperIF mapper = getNewMapper(new String [] { "infiniteLoopMapping.xml" }); 1190 LoopObjectParent loopObjectParent = new LoopObjectParent(); 1191 LoopObjectChild loopObjectChild = new LoopObjectChild(); 1192 loopObjectChild.setParent(loopObjectParent); 1193 loopObjectParent.setChild(loopObjectChild); 1194 1195 LoopObjectParentPrime loopObjectParentPrime = (LoopObjectParentPrime)mapper.map(loopObjectParent, LoopObjectParentPrime.class); 1196 assertNotNull(loopObjectParentPrime); 1197 assertNotNull(loopObjectParentPrime.getChildPrime()); 1198 } 1199} | Popular Tags |