001: /*
002: * Copyright 2005-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.dozer.functional_tests;
017:
018: import java.util.ArrayList;
019: import java.util.HashSet;
020: import java.util.List;
021: import java.util.Set;
022:
023: import net.sf.dozer.util.mapping.DozerBeanMapper;
024: import net.sf.dozer.util.mapping.MapperIF;
025: import net.sf.dozer.util.mapping.vo.Apple;
026: import net.sf.dozer.util.mapping.vo.Car;
027: import net.sf.dozer.util.mapping.vo.CustomConverterWrapper;
028: import net.sf.dozer.util.mapping.vo.CustomConverterWrapperPrime;
029: import net.sf.dozer.util.mapping.vo.DehydrateTestObject;
030: import net.sf.dozer.util.mapping.vo.FurtherTestObject;
031: import net.sf.dozer.util.mapping.vo.FurtherTestObjectPrime;
032: import net.sf.dozer.util.mapping.vo.HintedOnly;
033: import net.sf.dozer.util.mapping.vo.HydrateTestObject;
034: import net.sf.dozer.util.mapping.vo.NoCustomMappingsObject;
035: import net.sf.dozer.util.mapping.vo.NoCustomMappingsObjectPrime;
036: import net.sf.dozer.util.mapping.vo.OneWayObject;
037: import net.sf.dozer.util.mapping.vo.OneWayObjectPrime;
038: import net.sf.dozer.util.mapping.vo.Orange;
039: import net.sf.dozer.util.mapping.vo.TestObject;
040: import net.sf.dozer.util.mapping.vo.TestObjectPrime;
041: import net.sf.dozer.util.mapping.vo.TestReferenceFoo;
042: import net.sf.dozer.util.mapping.vo.TestReferenceFooPrime;
043: import net.sf.dozer.util.mapping.vo.TestReferenceObject;
044: import net.sf.dozer.util.mapping.vo.TestReferencePrimeObject;
045: import net.sf.dozer.util.mapping.vo.TheFirstSubClass;
046: import net.sf.dozer.util.mapping.vo.Van;
047: import net.sf.dozer.util.mapping.vo.WeirdGetter;
048: import net.sf.dozer.util.mapping.vo.WeirdGetter2;
049: import net.sf.dozer.util.mapping.vo.WeirdGetterPrime;
050: import net.sf.dozer.util.mapping.vo.WeirdGetterPrime2;
051: import net.sf.dozer.util.mapping.vo.deep.HomeDescription;
052: import net.sf.dozer.util.mapping.vo.deep.House;
053: import net.sf.dozer.util.mapping.vo.deep.Room;
054: import net.sf.dozer.util.mapping.vo.deep.SrcNestedDeepObj;
055: import net.sf.dozer.util.mapping.vo.self.Account;
056: import net.sf.dozer.util.mapping.vo.self.SimpleAccount;
057:
058: import org.apache.commons.lang.SerializationUtils;
059:
060: /**
061: * @author garsombke.franz
062: * @author sullins.ben
063: * @author tierney.matt
064: */
065: public class MapperTest extends AbstractMapperTest {
066:
067: protected void setUp() throws Exception {
068: super .setUp();
069: mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
070: }
071:
072: public void testNoSourceValueIterateFieldMap() throws Exception {
073: DehydrateTestObject inputDto = (DehydrateTestObject) newInstance(DehydrateTestObject.class);
074: HydrateTestObject hto = (HydrateTestObject) mapper.map(
075: inputDto, HydrateTestObject.class);
076: assertEquals(
077: testDataFactory
078: .getExpectedTestNoSourceValueIterateFieldMapHydrateTestObject(),
079: hto);
080: }
081:
082: public void testCustomGetterSetterMap() throws Exception {
083: WeirdGetter inputDto = (WeirdGetter) newInstance(WeirdGetter.class);
084: inputDto.placeValue("theValue");
085: inputDto.setWildValue("wild");
086:
087: WeirdGetterPrime prime = (WeirdGetterPrime) mapper.map(
088: inputDto, WeirdGetterPrime.class);
089: assertNull(prime.getWildValue()); // testing global wildcard, expect this to be null
090: assertEquals(inputDto.buildValue(), prime.getValue());
091:
092: inputDto = (WeirdGetter) mapper.map(prime, WeirdGetter.class);
093: assertEquals(inputDto.buildValue(), prime.getValue());
094:
095: WeirdGetterPrime2 inputDto2 = (WeirdGetterPrime2) newInstance(WeirdGetterPrime2.class);
096: inputDto2.placeValue("theValue");
097: WeirdGetter2 prime2 = (WeirdGetter2) mapper.map(inputDto2,
098: WeirdGetter2.class);
099: assertEquals(inputDto2.buildValue(), prime2.getValue());
100:
101: inputDto2 = (WeirdGetterPrime2) mapper.map(prime2,
102: WeirdGetterPrime2.class);
103: assertEquals(inputDto2.buildValue(), prime2.getValue());
104:
105: }
106:
107: public void testNoClassMappings() throws Exception {
108: MapperIF mapper = new DozerBeanMapper();
109: // Should attempt mapping even though it is not in the beanmapping.xml file
110: NoCustomMappingsObjectPrime dest1 = (NoCustomMappingsObjectPrime) mapper
111: .map(
112: testDataFactory
113: .getInputTestNoClassMappingsNoCustomMappingsObject(),
114: NoCustomMappingsObjectPrime.class);
115: NoCustomMappingsObject source = (NoCustomMappingsObject) mapper
116: .map(dest1, NoCustomMappingsObject.class);
117: NoCustomMappingsObjectPrime dest3 = (NoCustomMappingsObjectPrime) mapper
118: .map(source, NoCustomMappingsObjectPrime.class);
119: assertEquals(dest1, dest3);
120: }
121:
122: public void testImplicitInnerObject() {
123: // This tests that we implicitly map an inner object to an inner object without defining it in the mapping file
124: TestObject to = (TestObject) newInstance(TestObject.class);
125: to.setNoMappingsObj(testDataFactory
126: .getInputTestNoClassMappingsNoCustomMappingsObject());
127: TestObjectPrime dest2 = (TestObjectPrime) mapper.map(to,
128: TestObjectPrime.class);
129: TestObject source2 = (TestObject) mapper.map(dest2,
130: TestObject.class);
131: TestObjectPrime dest4 = (TestObjectPrime) mapper.map(source2,
132: TestObjectPrime.class);
133: assertEquals(dest2, dest4);
134: }
135:
136: public void testMapField() throws Exception {
137: NoCustomMappingsObjectPrime dest = (NoCustomMappingsObjectPrime) mapper
138: .map(
139: testDataFactory
140: .getInputTestMapFieldWithMapNoCustomMappingsObject(),
141: NoCustomMappingsObjectPrime.class);
142:
143: NoCustomMappingsObject source = (NoCustomMappingsObject) mapper
144: .map(dest, NoCustomMappingsObject.class);
145: NoCustomMappingsObjectPrime dest2 = (NoCustomMappingsObjectPrime) mapper
146: .map(source, NoCustomMappingsObjectPrime.class);
147:
148: assertEquals(dest2, dest);
149:
150: dest = (NoCustomMappingsObjectPrime) mapper.map(
151: new NoCustomMappingsObject(),
152: NoCustomMappingsObjectPrime.class);
153: source = (NoCustomMappingsObject) mapper.map(dest,
154: NoCustomMappingsObject.class);
155: dest2 = (NoCustomMappingsObjectPrime) mapper.map(source,
156: NoCustomMappingsObjectPrime.class);
157: assertEquals(dest2, dest);
158:
159: // empty Map
160: dest = (NoCustomMappingsObjectPrime) mapper
161: .map(
162: testDataFactory
163: .getInputTestMapFieldWithEmptyMapNoCustomMappingsObject(),
164: NoCustomMappingsObjectPrime.class);
165: source = (NoCustomMappingsObject) mapper.map(dest,
166: NoCustomMappingsObject.class);
167: dest2 = (NoCustomMappingsObjectPrime) mapper.map(source,
168: NoCustomMappingsObjectPrime.class);
169: assertEquals(dest2, dest);
170: }
171:
172: public void testSetField() throws Exception {
173: // basic set --> set
174: NoCustomMappingsObjectPrime dest = (NoCustomMappingsObjectPrime) mapper
175: .map(
176: testDataFactory
177: .getInputTestSetFieldWithSetNoCustomMappingsObject(),
178: NoCustomMappingsObjectPrime.class);
179: NoCustomMappingsObject source = (NoCustomMappingsObject) mapper
180: .map(dest, NoCustomMappingsObject.class);
181: NoCustomMappingsObjectPrime dest2 = (NoCustomMappingsObjectPrime) mapper
182: .map(source, NoCustomMappingsObjectPrime.class);
183: assertEquals(dest2, dest);
184:
185: // null set --> set
186: dest = (NoCustomMappingsObjectPrime) mapper.map(
187: new NoCustomMappingsObject(),
188: NoCustomMappingsObjectPrime.class);
189: source = (NoCustomMappingsObject) mapper.map(dest,
190: NoCustomMappingsObject.class);
191: dest2 = (NoCustomMappingsObjectPrime) mapper.map(source,
192: NoCustomMappingsObjectPrime.class);
193: assertEquals(dest2, dest);
194:
195: // empty set --> set
196: dest = (NoCustomMappingsObjectPrime) mapper
197: .map(
198: testDataFactory
199: .getInputTestSetFieldWithSetEmptyCustomMappingsObject(),
200: NoCustomMappingsObjectPrime.class);
201: source = (NoCustomMappingsObject) mapper.map(dest,
202: NoCustomMappingsObject.class);
203: dest2 = (NoCustomMappingsObjectPrime) mapper.map(source,
204: NoCustomMappingsObjectPrime.class);
205: assertEquals(dest2, dest);
206:
207: // complex type set -->
208: dest = (NoCustomMappingsObjectPrime) mapper
209: .map(
210: testDataFactory
211: .getInputTestSetFieldComplexSetNoCustomMappingsObject(),
212: NoCustomMappingsObjectPrime.class);
213: source = (NoCustomMappingsObject) mapper.map(dest,
214: NoCustomMappingsObject.class);
215: dest2 = (NoCustomMappingsObjectPrime) mapper.map(source,
216: NoCustomMappingsObjectPrime.class);
217: assertEquals(dest2, dest);
218: }
219:
220: public void testListField() throws Exception {
221: // test empty list --> empty list
222: TestObjectPrime dest = (TestObjectPrime) mapper.map(
223: testDataFactory
224: .getInputTestListFieldEmptyListTestObject(),
225: TestObjectPrime.class);
226: TestObject source = (TestObject) mapper.map(dest,
227: TestObject.class);
228: TestObjectPrime dest2 = (TestObjectPrime) mapper.map(source,
229: TestObjectPrime.class);
230: assertEquals(dest2, dest);
231:
232: // test empty array --> empty list
233: dest = (TestObjectPrime) mapper.map(testDataFactory
234: .getInputTestListFieldArrayListTestObject(),
235: TestObjectPrime.class);
236: source = (TestObject) mapper.map(dest, TestObject.class);
237: dest2 = (TestObjectPrime) mapper.map(source,
238: TestObjectPrime.class);
239: assertEquals(dest2, dest);
240: }
241:
242: public void testListUsingDestHint() throws Exception {
243: TestObjectPrime dest = (TestObjectPrime) mapper.map(
244: testDataFactory
245: .getInputTestListUsingDestHintTestObject(),
246: TestObjectPrime.class);
247: TestObject source = (TestObject) mapper.map(dest,
248: TestObject.class);
249: TestObjectPrime dest2 = (TestObjectPrime) mapper.map(source,
250: TestObjectPrime.class);
251: assertEquals(dest, dest2);
252: }
253:
254: public void testExcludeFields() throws Exception {
255: // Map
256: TestObjectPrime prime = (TestObjectPrime) mapper.map(
257: testDataFactory.getInputGeneralMappingTestObject(),
258: TestObjectPrime.class);
259: assertEquals("excludeMe", prime.getExcludeMe());
260: assertEquals("excludeMeOneWay", prime.getExcludeMeOneWay());
261: // map back
262: TestObject to = (TestObject) mapper
263: .map(prime, TestObject.class);
264: assertNull(to.getExcludeMe());
265: assertEquals("excludeMeOneWay", to.getExcludeMeOneWay());
266: }
267:
268: public void testGeneralMapping() throws Exception {
269: // Map
270: TestObject to = testDataFactory
271: .getInputGeneralMappingTestObject();
272: TestObjectPrime prime = (TestObjectPrime) mapper.map(to,
273: TestObjectPrime.class);
274: // valdidate that we copied by object reference -
275: TestObject source = (TestObject) mapper.map(prime,
276: TestObject.class);
277: TestObjectPrime prime2 = (TestObjectPrime) mapper.map(source,
278: TestObjectPrime.class);
279: assertEquals(prime2, prime);
280: }
281:
282: public void testMappingNoDestSpecified() throws Exception {
283: // Map
284: House src = testDataFactory.getHouse();
285: HomeDescription dest = (HomeDescription) mapper.map(src,
286: HomeDescription.class);
287: House src2 = (House) mapper.map(dest, House.class);
288: HomeDescription dest2 = (HomeDescription) mapper.map(src2,
289: HomeDescription.class);
290:
291: long[] prim = { 1, 2, 3, 1, 2, 3 };
292: // cumulative relationship
293: dest.setPrim(prim);
294: assertEquals(dest, dest2);
295:
296: // By reference
297: src = testDataFactory.getHouse();
298: House houseClone = (House) SerializationUtils.clone(src);
299: dest = (HomeDescription) mapper.map(src, HomeDescription.class);
300: mapper.map(dest, House.class);
301: assertEquals(houseClone, src);
302: }
303:
304: public void testGeneralMappingPassByReference() throws Exception {
305: // Map
306: TestObject to = testDataFactory
307: .getInputGeneralMappingTestObject();
308: TestObject toClone = (TestObject) SerializationUtils.clone(to);
309: TestObjectPrime prime = (TestObjectPrime) mapper.map(to,
310: TestObjectPrime.class);
311: mapper.map(prime, to);
312: // more objects should be added to the clone from the ArrayList
313: TheFirstSubClass fsc = new TheFirstSubClass();
314: fsc.setS("s");
315: toClone.getHintList().add(fsc);
316: toClone.getHintList().add(fsc);
317: toClone.getEqualNamedList().add("1value");
318: toClone.getEqualNamedList().add("2value");
319: int[] pa = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 };
320: int[] intArray = { 1, 1, 1, 1 };
321: Integer[] integerArray = { new Integer(1), new Integer(1),
322: new Integer(1), new Integer(1) };
323: toClone.setAnArray(intArray);
324: toClone.setArrayForLists(integerArray);
325: toClone.setPrimArray(pa);
326: toClone.setBlankDate(null);
327: toClone.setBlankStringToLong(null);
328: // since we copy by reference the attribute copyByReference we need to null it out. The clone method above creates
329: // two versions of it...
330: // which is incorrect
331: to.setCopyByReference(null);
332: toClone.setCopyByReference(null);
333: to.setCopyByReferenceDeep(null);
334: toClone.setCopyByReferenceDeep(null);
335: to.setGlobalCopyByReference(null);
336: toClone.setGlobalCopyByReference(null);
337: // null out string array because we get NPE since a NULL value in the String []
338: to.setStringArrayWithNullValue(null);
339: toClone.setStringArrayWithNullValue(null);
340: toClone.setExcludeMeOneWay("excludeMeOneWay");
341: assertEquals(toClone, to);
342: }
343:
344: public void testLongToLongMapping() throws Exception {
345: // Map
346: TestObject source = testDataFactory
347: .getInputGeneralMappingTestObject();
348: source.setAnotherLongValue(42);
349: TestObjectPrime prime2 = (TestObjectPrime) mapper.map(source,
350: TestObjectPrime.class);
351: Long value = prime2.getTheLongValue();
352: assertEquals(value.longValue(), 42);
353: }
354:
355: public void testNoWildcards() throws Exception {
356: // Map
357: FurtherTestObjectPrime prime = (FurtherTestObjectPrime) mapper
358: .map(testDataFactory
359: .getInputTestNoWildcardsFurtherTestObject(),
360: FurtherTestObjectPrime.class);
361: FurtherTestObject source = (FurtherTestObject) mapper.map(
362: prime, FurtherTestObject.class);
363: FurtherTestObjectPrime prime2 = (FurtherTestObjectPrime) mapper
364: .map(source, FurtherTestObjectPrime.class);
365: assertEquals(prime2, prime);
366: }
367:
368: public void testHydrateAndMore() throws Exception {
369: HydrateTestObject dest = (HydrateTestObject) mapper
370: .map(
371: testDataFactory
372: .getInputTestHydrateAndMoreDehydrateTestObject(),
373: HydrateTestObject.class);
374: // validate results
375: assertEquals(testDataFactory
376: .getExpectedTestHydrateAndMoreHydrateTestObject(), dest);
377:
378: // map it back
379: DehydrateTestObject dhto = (DehydrateTestObject) mapper.map(
380: testDataFactory
381: .getInputTestHydrateAndMoreHydrateTestObject(),
382: DehydrateTestObject.class);
383: assertEquals(testDataFactory
384: .getExpectedTestHydrateAndMoreDehydrateTestObject(),
385: dhto);
386: }
387:
388: public void testDeepProperties() throws Exception {
389: House src = testDataFactory.getHouse();
390: HomeDescription dest = (HomeDescription) mapper.map(src,
391: HomeDescription.class);
392: House src2 = (House) mapper.map(dest, House.class);
393: HomeDescription dest2 = (HomeDescription) mapper.map(src2,
394: HomeDescription.class);
395:
396: long[] prim = { 1, 2, 3, 1, 2, 3 };
397: // cumulative relationship
398: dest.setPrim(prim);
399: assertEquals(dest, dest2);
400:
401: // By reference
402: src = testDataFactory.getHouse();
403: House houseClone = (House) SerializationUtils.clone(src);
404: dest = (HomeDescription) mapper.map(src, HomeDescription.class);
405: mapper.map(dest, src);
406: // cumulative relationship
407: int[] prims = { 1, 2, 3, 1, 2, 3 };
408: houseClone.getOwner().setPrim(prims);
409: // add two more rooms
410: Room room1 = new Room();
411: room1.setName("Living");
412: Room room2 = new Room();
413: room2.setName("kitchen");
414: Van van = new Van();
415: van.setName("van2");
416: houseClone.getRooms().add(room1);
417: houseClone.getRooms().add(room2);
418: houseClone.getCustomSetGetMethod().add(van);
419: assertEquals(houseClone, src);
420: }
421:
422: public void testOneWayMapping() throws Exception {
423: // Map
424: OneWayObject owo = (OneWayObject) newInstance(OneWayObject.class);
425: OneWayObjectPrime owop = (OneWayObjectPrime) newInstance(OneWayObjectPrime.class);
426: SrcNestedDeepObj nested = (SrcNestedDeepObj) newInstance(SrcNestedDeepObj.class);
427: nested.setSrc1("src1");
428: owo.setNested(nested);
429: owop.setOneWayPrimeField("oneWayField");
430: owop.setSetOnlyField("setOnly");
431: List list = new ArrayList();
432: list.add("stringToList");
433: list.add("src1");
434: owop.setStringList(list);
435: owo.setOneWayField("oneWayField");
436: owo.setStringToList("stringToList");
437: OneWayObjectPrime prime = (OneWayObjectPrime) mapper.map(owo,
438: OneWayObjectPrime.class);
439:
440: assertEquals(owop, prime);
441:
442: OneWayObject source = (OneWayObject) mapper.map(prime,
443: OneWayObject.class);
444: // should have not mapped this way
445: assertEquals(null, source.getOneWayField());
446: }
447:
448: //TODO - fix this test
449: public void donttestMapByReference() throws Exception {
450: // Map
451: TestReferenceObject tro = (TestReferenceObject) newInstance(TestReferenceObject.class);
452: TestReferenceFoo foo1 = (TestReferenceFoo) newInstance(TestReferenceFoo.class);
453: foo1.setA("a");
454: TestReferenceFoo foo = (TestReferenceFoo) newInstance(TestReferenceFoo.class);
455: foo.setA("a");
456: foo.setB(null);
457: foo.setC("c");
458: List list2 = (ArrayList) newInstance(ArrayList.class);
459: list2.add(foo);
460: tro.setListA(list2);
461: tro.setArrayToArrayCumulative(new Object[] { foo1 });
462: TestReferenceFoo foo2 = (TestReferenceFoo) newInstance(TestReferenceFoo.class);
463: foo2.setA("a");
464: foo2.setB(null);
465: foo2.setC("c");
466: TestReferenceFoo foo3 = (TestReferenceFoo) newInstance(TestReferenceFoo.class);
467: foo3.setA("a");
468: foo3.setB(null);
469: foo3.setC("c");
470: tro.setArrayToArrayNoncumulative(new Object[] { foo2 });
471: List list3 = (ArrayList) newInstance(ArrayList.class);
472: list3.add("string1");
473: list3.add("string2");
474: tro.setListToArray(list3);
475: int[] pa = { 1, 2, 3 };
476: tro.setPrimitiveArray(pa);
477: Integer[] integerArray = { new Integer(1), new Integer(2) };
478: tro.setPrimitiveArrayWrapper(integerArray);
479: Set set = (HashSet) newInstance(HashSet.class);
480: TestReferenceFoo foo4 = (TestReferenceFoo) newInstance(TestReferenceFoo.class);
481: foo4.setA("a");
482: set.add(foo4);
483: tro.setSetToSet(set);
484: Car car = new Car();
485: car.setName("myName");
486: tro.setCars(new Car[] { car });
487: Car car2 = new Car();
488: car2.setName("myName");
489: List vehicles = (ArrayList) newInstance(ArrayList.class);
490: vehicles.add(car2);
491: tro.setVehicles(vehicles);
492: TestReferenceObject toClone = (TestReferenceObject) SerializationUtils
493: .clone(tro);
494: TestReferencePrimeObject trop = (TestReferencePrimeObject) mapper
495: .map(tro, TestReferencePrimeObject.class);
496: assertEquals("myName", (trop.getVans()[0]).getName());
497: assertEquals("myName", (trop.getMoreVans()[0]).getName());
498:
499: TestReferenceFooPrime fooPrime = (TestReferenceFooPrime) trop
500: .getListAPrime().get(0);
501: fooPrime.setB("b");
502: TestReferenceFooPrime fooPrime2 = (TestReferenceFooPrime) trop
503: .getArrayToArrayNoncumulative()[0];
504: fooPrime2.setB("b");
505: mapper.map(trop, tro);
506: // make sure we update the array list and didnt lose the value 'c' - non-cumulative
507: assertEquals("c", ((TestReferenceFoo) tro.getListA().get(0))
508: .getC());
509: assertEquals("c", ((TestReferenceFoo) tro
510: .getArrayToArrayNoncumulative()[0]).getC());
511: // cumulative
512: toClone.setArrayToArrayCumulative(new Object[] { foo1, foo1 });
513: toClone.setCars(new Car[] { car, car });
514: Van van = new Van();
515: van.setName("myName");
516: toClone.getVehicles().add(van);
517: // cumulative
518: toClone.getListToArray().add("string1");
519: toClone.getListToArray().add("string2");
520: int[] paClone = { 1, 2, 3, 1, 2, 3 };
521: toClone.setPrimitiveArray(paClone);
522: Integer[] integerArrayClone = { new Integer(1), new Integer(2),
523: new Integer(1), new Integer(2) };
524: toClone.setPrimitiveArrayWrapper(integerArrayClone);
525: assertEquals(toClone, tro);
526: }
527:
528: public void testHintedOnlyConverter() throws Exception {
529: String hintStr = "where's my hint?";
530:
531: CustomConverterWrapper source = (CustomConverterWrapper) newInstance(CustomConverterWrapper.class);
532: HintedOnly hint = (HintedOnly) newInstance(HintedOnly.class);
533: hint.setStr(hintStr);
534: source.addHint(hint);
535:
536: CustomConverterWrapperPrime dest = (CustomConverterWrapperPrime) mapper
537: .map(source, CustomConverterWrapperPrime.class);
538: String destHintStr = (String) dest.getNeedsHint().iterator()
539: .next();
540: assertNotNull(destHintStr);
541: assertEquals(hintStr, destHintStr);
542:
543: CustomConverterWrapper sourcePrime = (CustomConverterWrapper) mapper
544: .map(dest, CustomConverterWrapper.class);
545: String sourcePrimeHintStr = ((HintedOnly) sourcePrime
546: .getNeedsHint().iterator().next()).getStr();
547: assertNotNull(sourcePrimeHintStr);
548: assertEquals(hintStr, sourcePrimeHintStr);
549: }
550:
551: public void testSelfMapping() throws Exception {
552: SimpleAccount simpleAccount = (SimpleAccount) newInstance(SimpleAccount.class);
553: simpleAccount.setName("name");
554: simpleAccount.setPostcode(1234);
555: simpleAccount.setStreetName("streetName");
556: simpleAccount.setSuburb("suburb");
557: Account account = (Account) mapper.map(simpleAccount,
558: Account.class);
559: assertEquals(account.getAddress().getStreet(), simpleAccount
560: .getStreetName());
561: assertEquals(account.getAddress().getSuburb(), simpleAccount
562: .getSuburb());
563: assertEquals(account.getAddress().getPostcode(), simpleAccount
564: .getPostcode());
565:
566: // try mapping back
567: SimpleAccount dest = (SimpleAccount) mapper.map(account,
568: SimpleAccount.class);
569: assertEquals(account.getAddress().getStreet(), dest
570: .getStreetName());
571: assertEquals(account.getAddress().getSuburb(), dest.getSuburb());
572: assertEquals(account.getAddress().getPostcode(), dest
573: .getPostcode());
574: }
575:
576: public void testSetToArray() throws Exception {
577: Orange orange1 = (Orange) newInstance(Orange.class);
578: orange1.setName("orange1");
579: Orange orange2 = (Orange) newInstance(Orange.class);
580: orange2.setName("orange2");
581: Orange orange3 = (Orange) newInstance(Orange.class);
582: orange3.setName("orange3");
583: Orange orange4 = (Orange) newInstance(Orange.class);
584: orange4.setName("orange4");
585: Set set = (HashSet) newInstance(HashSet.class);
586: set.add(orange1);
587: set.add(orange2);
588: Set set2 = (HashSet) newInstance(HashSet.class);
589: set2.add(orange3);
590: set2.add(orange4);
591: TestObject to = (TestObject) newInstance(TestObject.class);
592: to.setSetToArray(set);
593: to.setSetToObjectArray(set2);
594: TestObjectPrime top = (TestObjectPrime) mapper.map(to,
595: TestObjectPrime.class);
596: assertEquals("orange1", top.getArrayToSet()[0].getName());
597: assertEquals("orange2", top.getArrayToSet()[1].getName());
598: // Hashset changes the order
599: assertEquals("orange3", ((Apple) top.getObjectArrayToSet()[1])
600: .getName());
601: assertEquals("orange4", ((Apple) top.getObjectArrayToSet()[0])
602: .getName());
603: Apple apple = (Apple) newInstance(Apple.class);
604: apple.setName("apple1");
605: Apple[] appleArray = { apple };
606: top.setSetToArrayWithValues(appleArray);
607: // now map back
608: Apple apple2 = (Apple) newInstance(Apple.class);
609: apple2.setName("apple2");
610: TestObject toDest = (TestObject) newInstance(TestObject.class);
611: Set hashSet = (HashSet) newInstance(HashSet.class);
612: hashSet.add(apple2);
613: toDest.setSetToArrayWithValues(hashSet);
614: mapper.map(top, toDest);
615: assertTrue(toDest.getSetToArray().contains(
616: top.getArrayToSet()[0]));
617: assertTrue(toDest.getSetToArray().contains(
618: top.getArrayToSet()[1]));
619: assertTrue(toDest.getSetToObjectArray().contains(
620: top.getObjectArrayToSet()[0]));
621: assertTrue(toDest.getSetToObjectArray().contains(
622: top.getObjectArrayToSet()[1]));
623: assertTrue(toDest.getSetToArrayWithValues().contains(apple));
624: assertTrue(toDest.getSetToArrayWithValues().contains(apple2));
625: assertTrue(toDest.getSetToArrayWithValues() instanceof HashSet);
626: }
627:
628: public void testSetToList() throws Exception {
629: Orange orange1 = (Orange) newInstance(Orange.class);
630: orange1.setName("orange1");
631: Orange orange2 = (Orange) newInstance(Orange.class);
632: orange2.setName("orange2");
633: Set set = (HashSet) newInstance(HashSet.class);
634: set.add(orange1);
635: set.add(orange2);
636: TestObject to = (TestObject) newInstance(TestObject.class);
637: to.setSetToList(set);
638: TestObjectPrime top = (TestObjectPrime) mapper.map(to,
639: TestObjectPrime.class);
640: assertEquals("orange1", ((Orange) top.getListToSet().get(0))
641: .getName());
642: assertEquals("orange2", ((Orange) top.getListToSet().get(1))
643: .getName());
644: List list = (ArrayList) newInstance(ArrayList.class);
645: Orange orange4 = (Orange) newInstance(Orange.class);
646: orange4.setName("orange4");
647: list.add(orange4);
648: top.setSetToListWithValues(list);
649: // Map back
650: Orange orange3 = (Orange) newInstance(Orange.class);
651: orange3.setName("orange3");
652: Set set2 = (HashSet) newInstance(HashSet.class);
653: set2.add(orange3);
654: set2.add(orange4);
655: TestObject toDest = (TestObject) newInstance(TestObject.class);
656: toDest.setSetToListWithValues(set2);
657: mapper.map(top, toDest);
658: assertTrue(toDest.getSetToList().contains(
659: top.getListToSet().get(0)));
660: assertTrue(toDest.getSetToList().contains(
661: top.getListToSet().get(1)));
662: assertTrue(toDest.getSetToListWithValues().contains(orange3));
663: assertTrue(toDest.getSetToListWithValues().contains(orange4));
664: }
665:
666: protected DataObjectInstantiator getDataObjectInstantiator() {
667: return NoProxyDataObjectInstantiator.INSTANCE;
668: }
669:
670: }
|