Source Code Cross Referenced for GranularDozerBeanMapperTest.java in  » Database-ORM » dozer » net » sf » dozer » functional_tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » dozer » net.sf.dozer.functional_tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.text.DateFormat;
019:        import java.text.SimpleDateFormat;
020:        import java.util.ArrayList;
021:        import java.util.Arrays;
022:        import java.util.Date;
023:        import java.util.HashSet;
024:        import java.util.List;
025:        import java.util.Set;
026:        import java.util.SortedSet;
027:        import java.util.TreeSet;
028:
029:        import net.sf.dozer.util.mapping.CustomFieldMapperIF;
030:        import net.sf.dozer.util.mapping.DozerBeanMapper;
031:        import net.sf.dozer.util.mapping.MapperIF;
032:        import net.sf.dozer.util.mapping.MappingException;
033:        import net.sf.dozer.util.mapping.fieldmapper.TestCustomFieldMapper;
034:        import net.sf.dozer.util.mapping.util.CollectionUtils;
035:        import net.sf.dozer.util.mapping.vo.AnotherTestObject;
036:        import net.sf.dozer.util.mapping.vo.AnotherTestObjectPrime;
037:        import net.sf.dozer.util.mapping.vo.FieldValue;
038:        import net.sf.dozer.util.mapping.vo.Fruit;
039:        import net.sf.dozer.util.mapping.vo.InsideTestObject;
040:        import net.sf.dozer.util.mapping.vo.MethodFieldTestObject;
041:        import net.sf.dozer.util.mapping.vo.MethodFieldTestObject2;
042:        import net.sf.dozer.util.mapping.vo.NoDefaultConstructor;
043:        import net.sf.dozer.util.mapping.vo.NoReadMethod;
044:        import net.sf.dozer.util.mapping.vo.NoReadMethodPrime;
045:        import net.sf.dozer.util.mapping.vo.NoWriteMethod;
046:        import net.sf.dozer.util.mapping.vo.NoWriteMethodPrime;
047:        import net.sf.dozer.util.mapping.vo.PrimitiveArrayObj;
048:        import net.sf.dozer.util.mapping.vo.PrimitiveArrayObjPrime;
049:        import net.sf.dozer.util.mapping.vo.SimpleObj;
050:        import net.sf.dozer.util.mapping.vo.SimpleObjPrime;
051:        import net.sf.dozer.util.mapping.vo.TestObject;
052:        import net.sf.dozer.util.mapping.vo.TestObjectPrime;
053:        import net.sf.dozer.util.mapping.vo.TestObjectPrime2;
054:        import net.sf.dozer.util.mapping.vo.allowedexceptions.TestException;
055:        import net.sf.dozer.util.mapping.vo.allowedexceptions.ThrowException;
056:        import net.sf.dozer.util.mapping.vo.allowedexceptions.ThrowExceptionPrime;
057:        import net.sf.dozer.util.mapping.vo.context.ContextMapping;
058:        import net.sf.dozer.util.mapping.vo.context.ContextMappingNested;
059:        import net.sf.dozer.util.mapping.vo.context.ContextMappingNestedPrime;
060:        import net.sf.dozer.util.mapping.vo.context.ContextMappingPrime;
061:        import net.sf.dozer.util.mapping.vo.iface.ApplicationUser;
062:        import net.sf.dozer.util.mapping.vo.iface.UpdateMember;
063:        import net.sf.dozer.util.mapping.vo.index.Mccoy;
064:        import net.sf.dozer.util.mapping.vo.index.MccoyPrime;
065:        import net.sf.dozer.util.mapping.vo.isaccessible.Foo;
066:        import net.sf.dozer.util.mapping.vo.isaccessible.FooPrime;
067:        import net.sf.dozer.util.mapping.vo.isaccessible.PrivateConstructorBean;
068:        import net.sf.dozer.util.mapping.vo.isaccessible.PrivateConstructorBeanPrime;
069:        import net.sf.dozer.util.mapping.vo.orphan.Child;
070:        import net.sf.dozer.util.mapping.vo.orphan.ChildPrime;
071:        import net.sf.dozer.util.mapping.vo.orphan.Parent;
072:        import net.sf.dozer.util.mapping.vo.orphan.ParentPrime;
073:        import net.sf.dozer.util.mapping.vo.perf.MyClassA;
074:        import net.sf.dozer.util.mapping.vo.perf.MyClassB;
075:        import net.sf.dozer.util.mapping.vo.set.NamesArray;
076:        import net.sf.dozer.util.mapping.vo.set.NamesSet;
077:        import net.sf.dozer.util.mapping.vo.set.NamesSortedSet;
078:        import net.sf.dozer.util.mapping.vo.set.SomeDTO;
079:        import net.sf.dozer.util.mapping.vo.set.SomeOtherDTO;
080:        import net.sf.dozer.util.mapping.vo.set.SomeVO;
081:
082:        /**
083:         * @author garsombke.franz
084:         */
085:        public class GranularDozerBeanMapperTest extends AbstractMapperTest {
086:
087:            public void testNoDefaultConstructor() throws Exception {
088:                try {
089:                    mapper.map("test", NoDefaultConstructor.class);
090:                    fail("should have thrown exception");
091:                } catch (MappingException e) {
092:                    assertEquals(
093:                            "java.lang.NoSuchMethodException: net.sf.dozer.util.mapping.vo.NoDefaultConstructor.<init>()",
094:                            e.getMessage());
095:                }
096:            }
097:
098:            public void testFieldAccessible() throws Exception {
099:                MapperIF mapper = getMapper(new String[] { "fieldAttributeMapping.xml" });
100:                TestObject to = (TestObject) newInstance(TestObject.class);
101:                to.setFieldAccessible("fieldAccessible");
102:                to.setFieldAccessiblePrimInt(2);
103:                InsideTestObject ito = (InsideTestObject) newInstance(InsideTestObject.class);
104:                ito.setLabel("label");
105:                to.setFieldAccessibleComplexType(ito);
106:                String[] stringArray = new String[] { "one", "two" };
107:                to.setFieldAccessibleArrayToList(stringArray);
108:                TestObjectPrime top = (TestObjectPrime) mapper.map(to,
109:                        TestObjectPrime.class);
110:                assertEquals("fieldAccessible", top.fieldAccessible);
111:                assertEquals("label", top.fieldAccessibleComplexType
112:                        .getLabelPrime());
113:                assertEquals(2, top.fieldAccessiblePrimInt);
114:                assertEquals("one", top.fieldAccessibleArrayToList.get(0));
115:                assertEquals("two", top.fieldAccessibleArrayToList.get(1));
116:
117:                // Map Back
118:                TestObject toDest = (TestObject) mapper.map(top,
119:                        TestObject.class);
120:                assertEquals("fieldAccessible", toDest.getFieldAccessible());
121:                assertEquals("label", toDest.getFieldAccessibleComplexType()
122:                        .getLabel());
123:                assertEquals(2, toDest.getFieldAccessiblePrimInt());
124:                assertEquals("one", toDest.getFieldAccessibleArrayToList()[0]);
125:                assertEquals("two", toDest.getFieldAccessibleArrayToList()[1]);
126:            }
127:
128:            public void testOverloadGetSetMethods() throws Exception {
129:                MapperIF mapper = getMapper(new String[] { "fieldAttributeMapping.xml" });
130:                TestObject to = (TestObject) newInstance(TestObject.class);
131:                Date date = new Date();
132:                to.setOverloadGetField(new Date());
133:                TestObjectPrime top = (TestObjectPrime) mapper.map(to,
134:                        TestObjectPrime.class);
135:                assertEquals(date, top.getOverloadSetField());
136:
137:                // Map Back
138:                TestObject toDest = (TestObject) mapper.map(top,
139:                        TestObject.class);
140:                assertEquals(date, toDest.getOverloadGetField());
141:            }
142:
143:            public void testFieldCreateMethod() throws Exception {
144:                MapperIF mapper = getMapper(new String[] { "fieldAttributeMapping.xml" });
145:                TestObject to = (TestObject) newInstance(TestObject.class);
146:                InsideTestObject ito = (InsideTestObject) newInstance(InsideTestObject.class);
147:                // we did not set any values. this will be set in the 'createMethod'
148:                to.setCreateMethodType(ito);
149:                TestObjectPrime top = (TestObjectPrime) mapper.map(to,
150:                        TestObjectPrime.class);
151:                assertEquals("myField", top.getCreateMethodType().getMyField());
152:
153:                // Map Back
154:                TestObject toDest = (TestObject) mapper.map(top,
155:                        TestObject.class);
156:                assertEquals("testCreateMethod", toDest.getCreateMethodType()
157:                        .getTestCreateMethod());
158:            }
159:
160:            public void testIntegerToString() throws Exception {
161:                MapperIF mapper = getMapper(new String[] { "fieldAttributeMapping.xml" });
162:                TestObject to = (TestObject) newInstance(TestObject.class);
163:                Integer[] array = new Integer[] { new Integer(1),
164:                        new Integer(2) };
165:                to.setArrayForLists(array);
166:                TestObjectPrime top = (TestObjectPrime) mapper.map(to,
167:                        TestObjectPrime.class);
168:                assertEquals("1", top.getListForArray().get(0));
169:            }
170:
171:            public void testMapNull_MappingLevel() throws Exception {
172:                MapperIF mapper = getMapper(new String[] { "nullFieldMapping.xml" });
173:                // check that null does not override an existing value when map-null="false"
174:                AnotherTestObject src = (AnotherTestObject) newInstance(AnotherTestObject.class);
175:                src.setField3(null);
176:                src.setField4(null);
177:                AnotherTestObjectPrime dest = (AnotherTestObjectPrime) newInstance(AnotherTestObjectPrime.class);
178:                dest.setTo((TestObject) newInstance(TestObject.class));
179:                dest.setField3("555");
180:                dest.getTo().setOne("4641");
181:
182:                // dest field should remain unchanged
183:                mapper.map(src, dest);
184:                assertEquals("invalid dest field value", "555", dest
185:                        .getField3());
186:                assertEquals("invalid dest field value2", "4641", dest.getTo()
187:                        .getOne());
188:            }
189:
190:            public void testMapNull_MappingLevel2() throws Exception {
191:                MapperIF mapper = getMapper(new String[] { "nullFieldMapping.xml" });
192:                // Reverse mapping
193:                AnotherTestObjectPrime src = (AnotherTestObjectPrime) newInstance(AnotherTestObjectPrime.class);
194:                src.setTo((TestObject) newInstance(TestObject.class));
195:                src.setField3(null);
196:                src.getTo().setOne(null);
197:                AnotherTestObject dest = (AnotherTestObject) newInstance(AnotherTestObject.class);
198:                dest.setField3("555");
199:                dest.setField4("4641");
200:
201:                // dest field should remain unchanged
202:                mapper.map(src, dest);
203:                assertEquals("invalid dest field value", "555", dest
204:                        .getField3());
205:                assertEquals("invalid dest field value2", "4641", dest
206:                        .getField4());
207:            }
208:
209:            public void testMapEmptyString_MappingLevel() throws Exception {
210:                MapperIF mapper = getMapper(new String[] { "nullFieldMapping.xml" });
211:                // check that "" does not override an existing value when
212:                // map-empty-string="false"
213:                AnotherTestObject src = (AnotherTestObject) newInstance(AnotherTestObject.class);
214:                src.setField3("");
215:                src.setField4("");
216:                AnotherTestObjectPrime dest = (AnotherTestObjectPrime) newInstance(AnotherTestObjectPrime.class);
217:                dest.setTo((TestObject) newInstance(TestObject.class));
218:                dest.setField3("555");
219:                dest.getTo().setOne("4641");
220:
221:                // dest field should remain unchanged
222:                mapper.map(src, dest);
223:                assertEquals("invalid dest field value", "555", dest
224:                        .getField3());
225:                assertEquals("invalid dest field value2", "4641", dest.getTo()
226:                        .getOne());
227:            }
228:
229:            public void testMapEmptyString_MappingLevel2() throws Exception {
230:                MapperIF mapper = getMapper(new String[] { "nullFieldMapping.xml" });
231:                // reverse mapping
232:                AnotherTestObjectPrime src = (AnotherTestObjectPrime) newInstance(AnotherTestObjectPrime.class);
233:                src.setTo((TestObject) newInstance(TestObject.class));
234:                src.setField3("");
235:                src.getTo().setOne("");
236:                AnotherTestObject dest = (AnotherTestObject) newInstance(AnotherTestObject.class);
237:                dest.setField3("555");
238:                dest.setField4("4641");
239:
240:                // dest field should remain unchanged
241:                mapper.map(src, dest);
242:                assertEquals("invalid dest field value", "555", dest
243:                        .getField3());
244:                assertEquals("invalid dest field value2", "4641", dest
245:                        .getField4());
246:            }
247:
248:            public void testMapNull_ClassLevel() throws Exception {
249:                MapperIF mapper = getMapper(new String[] { "nullFieldMapping.xml" });
250:                // check that null does not override an existing value when map-null="false"
251:                TestObject src = (TestObject) newInstance(TestObject.class);
252:                src.setOne(null);
253:                TestObjectPrime2 dest = (TestObjectPrime2) newInstance(TestObjectPrime2.class);
254:                dest.setOne("555");
255:
256:                // dest field should remain unchanged
257:                mapper.map(src, dest);
258:                assertNotNull("dest should not be null", dest.getOne());
259:                assertEquals("invalid dest field value", "555", dest.getOne());
260:
261:                // reverse mapping
262:                TestObjectPrime2 src2 = (TestObjectPrime2) newInstance(TestObjectPrime2.class);
263:                src2.setOne(null);
264:                TestObject dest2 = (TestObject) newInstance(TestObject.class);
265:                dest2.setOne("555");
266:
267:                // dest field should NOT remain unchanged
268:                mapper.map(src2, dest2);
269:                assertNull("dest should be null", dest2.getOne());
270:            }
271:
272:            public void testMapEmptyString_ClassLevel() throws Exception {
273:                MapperIF mapper = getMapper(new String[] { "nullFieldMapping.xml" });
274:                // check that "" does not override an existing value when
275:                // map-empty-string="false"
276:                TestObject src = (TestObject) newInstance(TestObject.class);
277:                src.setOne("");
278:                TestObjectPrime2 dest = (TestObjectPrime2) newInstance(TestObjectPrime2.class);
279:                dest.setOne("555");
280:
281:                // dest field should remain unchanged
282:                mapper.map(src, dest);
283:                assertNotNull("dest should not be null", dest.getOne());
284:                assertEquals("invalid dest field value", "555", dest.getOne());
285:
286:                // reverse mapping
287:                TestObjectPrime2 src2 = (TestObjectPrime2) newInstance(TestObjectPrime2.class);
288:                src2.setOne("");
289:                TestObject dest2 = (TestObject) newInstance(TestObject.class);
290:                dest2.setOne("555");
291:
292:                // dest field should NOT remain unchanged
293:                mapper.map(src2, dest2);
294:                assertNotNull("dest should not be null", dest2.getOne());
295:                assertEquals("dest should be an empty string", "", dest2
296:                        .getOne());
297:
298:            }
299:
300:            public void testContextMappingWithNestedContext() throws Exception {
301:                MapperIF mapper = getMapper(new String[] { "contextMapping.xml" });
302:
303:                ContextMappingNested cmn = (ContextMappingNested) newInstance(ContextMappingNested.class);
304:                cmn.setLoanNo("loanNoNested");
305:                List list = (List) newInstance(ArrayList.class);
306:                list.add(cmn);
307:                ContextMapping cm = (ContextMapping) newInstance(ContextMapping.class);
308:                cm.setLoanNo("loanNo");
309:                cm.setContextList(list);
310:
311:                ContextMappingPrime cmpA = (ContextMappingPrime) mapper.map(cm,
312:                        ContextMappingPrime.class, "caseA");
313:                assertNull(cmpA.getLoanNo());
314:                assertNull(((ContextMappingNestedPrime) cmpA.getContextList()
315:                        .get(0)).getLoanNo());
316:
317:                ContextMappingPrime cmpB = (ContextMappingPrime) mapper.map(cm,
318:                        ContextMappingPrime.class, "caseB");
319:                assertEquals("loanNo", cmpB.getLoanNo());
320:                assertEquals("loanNoNested", ((ContextMappingNested) cmpB
321:                        .getContextList().get(0)).getLoanNo());
322:
323:                ContextMappingNestedPrime cmn2 = (ContextMappingNestedPrime) newInstance(ContextMappingNestedPrime.class);
324:                cmn2.setLoanNo("loanNoNested");
325:                List list2 = (List) newInstance(ArrayList.class);
326:                list2.add(cmn2);
327:                ContextMappingPrime prime = (ContextMappingPrime) newInstance(ContextMappingPrime.class);
328:                prime.setLoanNo("loanNo");
329:                prime.setContextList(list2);
330:
331:                ContextMapping cmDest = (ContextMapping) mapper.map(prime,
332:                        ContextMapping.class, "caseA");
333:                assertNull(cmDest.getLoanNo());
334:                assertNull(((ContextMappingNested) cmDest.getContextList().get(
335:                        0)).getLoanNo());
336:
337:                ContextMapping cmpBDest = (ContextMapping) mapper.map(prime,
338:                        ContextMapping.class, "caseB");
339:                assertEquals("loanNo", cmpBDest.getLoanNo());
340:                assertEquals("loanNoNested",
341:                        ((ContextMappingNestedPrime) cmpBDest.getContextList()
342:                                .get(0)).getLoanNo());
343:            }
344:
345:            public void testArrayToSortedSet() {
346:                NamesArray from = (NamesArray) newInstance(NamesArray.class);
347:                NamesSortedSet to = null;
348:                String[] names = { "John", "Bill", "Tony", "Fred", "Bruce" };
349:
350:                from.setNames(names);
351:
352:                to = (NamesSortedSet) mapper.map(from, NamesSortedSet.class);
353:
354:                assertNotNull(to);
355:                assertEquals(names.length, to.getNames().size());
356:            }
357:
358:            public void testSortedSetToArray() {
359:                NamesSortedSet from = (NamesSortedSet) newInstance(NamesSortedSet.class);
360:                NamesArray to = null;
361:                SortedSet names = (TreeSet) newInstance(TreeSet.class);
362:
363:                names.add("Jen");
364:                names.add("Sue");
365:                names.add("Sally");
366:                names.add("Jill");
367:                from.setNames(names);
368:
369:                to = (NamesArray) mapper.map(from, NamesArray.class);
370:
371:                assertNotNull(to);
372:                assertEquals(names.size(), to.getNames().length);
373:            }
374:
375:            public void testSetToSortedSet() {
376:                NamesSet from = (NamesSet) newInstance(NamesSet.class);
377:                NamesSortedSet to = null;
378:                Set names = (HashSet) newInstance(HashSet.class);
379:
380:                names.add("Red");
381:                names.add("Blue");
382:                names.add("Green");
383:                from.setNames(names);
384:
385:                to = (NamesSortedSet) mapper.map(from, NamesSortedSet.class);
386:
387:                assertNotNull(to);
388:                assertEquals(names.size(), to.getNames().size());
389:            }
390:
391:            public void testSortedSetToSet() {
392:                NamesSortedSet from = (NamesSortedSet) newInstance(NamesSortedSet.class);
393:                NamesSet to = null;
394:                SortedSet names = (TreeSet) newInstance(TreeSet.class);
395:
396:                names.add("Bone");
397:                names.add("White");
398:                names.add("Beige");
399:                names.add("Ivory");
400:                names.add("Cream");
401:                names.add("Off white");
402:                from.setNames(names);
403:
404:                to = (NamesSet) mapper.map(from, NamesSet.class);
405:
406:                assertNotNull(to);
407:                assertEquals(names.size(), to.getNames().size());
408:            }
409:
410:            public void testSetPrivateField() {
411:                mapper = super 
412:                        .getMapper(new String[] { "isaccessiblemapping.xml" });
413:                Foo src = (Foo) newInstance(Foo.class);
414:                List list = (ArrayList) newInstance(ArrayList.class);
415:                list.add("test1");
416:                list.add("test2");
417:                src.setCategories(list);
418:
419:                FooPrime dest = (FooPrime) mapper.map(src, FooPrime.class);
420:                assertNotNull(dest);
421:            }
422:
423:            public void testStringToIndexedSet_UsingHint() {
424:                mapper = getMapper(new String[] { "indexMapping.xml" });
425:                Mccoy src = (Mccoy) newInstance(Mccoy.class);
426:                src.setStringProperty(String
427:                        .valueOf(System.currentTimeMillis()));
428:                src.setField2("someValue");
429:
430:                MccoyPrime dest = (MccoyPrime) mapper.map(src,
431:                        MccoyPrime.class, "usingDestHint");
432:                Set destSet = dest.getFieldValueObjects();
433:                assertNotNull("dest set should not be null", destSet);
434:                assertEquals("dest set should contain 1 entry", 1, destSet
435:                        .size());
436:                Object entry = destSet.iterator().next();
437:                assertTrue("dest set entry should be instance of FieldValue",
438:                        entry instanceof  FieldValue);
439:                assertEquals("invalid value for dest object", src
440:                        .getStringProperty(), ((FieldValue) entry)
441:                        .getValue("theKey"));
442:            }
443:
444:            public void testAllowedExceptions() throws Exception {
445:                MapperIF mapper = getMapper(new String[] { "allowedExceptionsMapping.xml" });
446:                TestObject to = (TestObject) newInstance(TestObject.class);
447:                to.setThrowAllowedExceptionOnMap("throw me");
448:                try {
449:                    mapper.map(to, TestObjectPrime.class);
450:                    fail("We should have thrown TestException");
451:                } catch (RuntimeException e) {
452:                    if (e instanceof  TestException) {
453:                        assertTrue(true);
454:                    } else {
455:                        fail("This should be an instance of TestException");
456:                    }
457:                }
458:                TestObject to2 = (TestObject) newInstance(TestObject.class);
459:                to2.setThrowNonAllowedExceptionOnMap("do not throw me");
460:                try {
461:                    mapper.map(to2, TestObjectPrime.class);
462:                } catch (RuntimeException e) {
463:                    fail("This should not have been thrown");
464:                }
465:            }
466:
467:            public void testAllowedExceptions_Implicit() throws Exception {
468:                MapperIF mapper = getMapper(new String[] { "implicitAllowedExceptionsMapping.xml" });
469:                ThrowException to = (ThrowException) newInstance(ThrowException.class);
470:                to.setThrowAllowedException("throw me");
471:                try {
472:                    mapper.map(to, ThrowExceptionPrime.class);
473:                    fail("We should have thrown TestException");
474:                } catch (RuntimeException e) {
475:                    if (e instanceof  TestException) {
476:                        assertTrue(true);
477:                    } else {
478:                        fail("This should be an instance of TestException");
479:                    }
480:                }
481:                ThrowException to2 = (ThrowException) newInstance(ThrowException.class);
482:                to2.setThrowNotAllowedException("do not throw me");
483:                try {
484:                    mapper.map(to2, ThrowExceptionPrime.class);
485:                } catch (RuntimeException e) {
486:                    fail("This should not have been thrown");
487:                }
488:            }
489:
490:            public void testPrimitiveArrayToList() throws Exception {
491:                mapper = getMapper(new String[] { "primitiveArrayToListMapping.xml" });
492:
493:                int[] i = new int[] { 1, 2, 3 };
494:                PrimitiveArrayObj src = (PrimitiveArrayObj) newInstance(PrimitiveArrayObj.class);
495:                src.setField1(i);
496:
497:                PrimitiveArrayObjPrime dest = (PrimitiveArrayObjPrime) mapper
498:                        .map(src, PrimitiveArrayObjPrime.class);
499:
500:                assertNotNull("dest list field should not be null", dest
501:                        .getField1());
502:                assertEquals("invalid dest field size", i.length, dest
503:                        .getField1().size());
504:
505:                List srcObjectList = CollectionUtils
506:                        .convertPrimitiveArrayToList(i);
507:                assertEquals("invalid dest field value", srcObjectList, dest
508:                        .getField1());
509:            }
510:
511:            public void testPrimitiveArrayToList_UsingHint() throws Exception {
512:                mapper = getMapper(new String[] { "primitiveArrayToListMapping.xml" });
513:
514:                int[] srcArray = new int[] { 1, 2, 3 };
515:                PrimitiveArrayObj src = (PrimitiveArrayObj) newInstance(PrimitiveArrayObj.class);
516:                src.setField1(srcArray);
517:
518:                PrimitiveArrayObjPrime dest = (PrimitiveArrayObjPrime) mapper
519:                        .map(src, PrimitiveArrayObjPrime.class,
520:                                "primitiveToArrayUsingHint");
521:
522:                assertNotNull("dest list field should not be null", dest
523:                        .getField1());
524:                assertEquals("invalid dest field size", srcArray.length, dest
525:                        .getField1().size());
526:
527:                for (int i = 0; i < srcArray.length; i++) {
528:                    String srcValue = String.valueOf(srcArray[i]);
529:                    String resultValue = (String) dest.getField1().get(i);
530:                    assertEquals("invalid result entry value", srcValue,
531:                            resultValue);
532:                }
533:            }
534:
535:            public void testInterface() throws Exception {
536:                mapper = getMapper(new String[] { "interfaceMapping.xml" });
537:                ApplicationUser user = (ApplicationUser) newInstance(ApplicationUser.class);
538:                user.setSubscriberNumber("123");
539:
540:                // Mapping works
541:                UpdateMember destObject = (UpdateMember) mapper.map(user,
542:                        UpdateMember.class);
543:
544:                assertEquals("invalid value for subsriber #", user
545:                        .getSubscriberNumber(), destObject.getSubscriberKey()
546:                        .getSubscriberNumber());
547:
548:                // Clear value
549:                destObject = new UpdateMember();
550:
551:                // Mapping doesn't work
552:                mapper.map(user, destObject);
553:
554:                assertNotNull("dest field should not be null", destObject
555:                        .getSubscriberKey());
556:                assertEquals("invalid value for subsriber #", user
557:                        .getSubscriberNumber(), destObject.getSubscriberKey()
558:                        .getSubscriberNumber());
559:            }
560:
561:            public void testCustomFieldMapper() throws Exception {
562:                CustomFieldMapperIF customFieldMapper = new TestCustomFieldMapper();
563:                ((DozerBeanMapper) mapper)
564:                        .setCustomFieldMapper(customFieldMapper);
565:
566:                String currentTime = String.valueOf(System.currentTimeMillis());
567:                SimpleObj src = (SimpleObj) newInstance(SimpleObj.class);
568:                src.setField1(currentTime);
569:                src.setField6("field6Value" + currentTime);
570:
571:                SimpleObjPrime dest = (SimpleObjPrime) mapper.map(src,
572:                        SimpleObjPrime.class);
573:
574:                assertNotNull("dest field1 should not be null", dest
575:                        .getField1());
576:                assertNotNull("dest field6 should not be null", dest
577:                        .getField6());
578:                assertEquals(
579:                        "dest field1 should have been set by custom field mapper",
580:                        TestCustomFieldMapper.FIELD_VALUE, dest.getField1());
581:                assertEquals(
582:                        "dest field6 should NOT have been set by custom field mapper",
583:                        src.getField6(), dest.getField6());
584:            }
585:
586:            public void testPrivateConstructor() throws Exception {
587:                PrivateConstructorBean src = PrivateConstructorBean
588:                        .newInstance();
589:                src.setField1("someValue");
590:
591:                PrivateConstructorBeanPrime dest = (PrivateConstructorBeanPrime) mapper
592:                        .map(src, PrivateConstructorBeanPrime.class);
593:
594:                assertNotNull("dest bean should not be null", dest);
595:                assertEquals("field1 not mapped correctly", src.getField1(),
596:                        dest.getField1());
597:            }
598:
599:            /*
600:             * Bug #1549738
601:             */
602:            public void testSetMapping_UppercaseFieldNameInXML()
603:                    throws Exception {
604:                // For some reason the resulting SomeVO contains a Set with 4 objects. 2 SomeOtherDTO's and 2 SomeOtherVO's. I
605:                // believe it
606:                // should only contain 2 SomeOtherVO's. It has something to do with specifying the field name starting with cap in
607:                // the mapping file. If
608:                // you change the field mapping to start with lower case it seems to map correctly.
609:                MapperIF mapper = getMapper(new String[] { "setMappingWithUpperCaseFieldName.xml" });
610:
611:                SomeDTO someDto = (SomeDTO) newInstance(SomeDTO.class);
612:                someDto.setField1(new Integer("1"));
613:
614:                SomeOtherDTO someOtherDto = (SomeOtherDTO) newInstance(SomeOtherDTO.class);
615:                someOtherDto.setOtherField2(someDto);
616:                someOtherDto.setOtherField3("value1");
617:
618:                SomeDTO someDto2 = (SomeDTO) newInstance(SomeDTO.class);
619:                someDto2.setField1(new Integer("2"));
620:
621:                SomeOtherDTO someOtherDto2 = (SomeOtherDTO) newInstance(SomeOtherDTO.class);
622:                someOtherDto2.setOtherField2(someDto2);
623:                someOtherDto2.setOtherField3("value2");
624:
625:                SomeDTO src = (SomeDTO) newInstance(SomeDTO.class);
626:                src
627:                        .setField2(new SomeOtherDTO[] { someOtherDto2,
628:                                someOtherDto });
629:
630:                SomeVO dest = (SomeVO) mapper.map(src, SomeVO.class);
631:
632:                assertEquals("incorrect resulting set size",
633:                        src.getField2().length, dest.getField2().size());
634:                // TODO: add more asserts
635:            }
636:
637:            public void testGlobalBeanFactoryAppliedToDefaultMappings()
638:                    throws Exception {
639:                mapper = getMapper(new String[] { "global-configuration.xml" });
640:                TestObjectPrime dest = (TestObjectPrime) mapper.map(
641:                        (TestObject) newInstance(TestObject.class),
642:                        TestObjectPrime.class);
643:
644:                assertNotNull("created by factory name should not be null",
645:                        dest.getCreatedByFactoryName());
646:                assertEquals(
647:                        "",
648:                        "net.sf.dozer.util.mapping.factories.SampleDefaultBeanFactory",
649:                        dest.getCreatedByFactoryName());
650:            }
651:
652:            public void testStringToDateMapping() throws Exception {
653:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
654:                DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SS");
655:                String dateStr = "01/29/1975 10:45:13:25";
656:                TestObject sourceObj = (TestObject) newInstance(TestObject.class);
657:                sourceObj.setDateStr(dateStr);
658:                TestObjectPrime result = (TestObjectPrime) mapper.map(
659:                        sourceObj, TestObjectPrime.class);
660:                assertEquals(df.parse(dateStr), result.getDateFromStr());
661:                assertEquals(dateStr, df.format(result.getDateFromStr()));
662:
663:                TestObject result2 = (TestObject) mapper.map(result,
664:                        TestObject.class);
665:                assertEquals(df.format(result.getDateFromStr()), result2
666:                        .getDateStr());
667:                assertEquals(result.getDateFromStr(), df.parse(result2
668:                        .getDateStr()));
669:            }
670:
671:            public void testMethodMapping() throws Exception {
672:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
673:                MethodFieldTestObject sourceObj = (MethodFieldTestObject) newInstance(MethodFieldTestObject.class);
674:                sourceObj.setIntegerStr("1500");
675:                sourceObj.setPriceItem("3500");
676:                sourceObj.setFieldOne("fieldOne");
677:                MethodFieldTestObject2 result = (MethodFieldTestObject2) mapper
678:                        .map(sourceObj, MethodFieldTestObject2.class);
679:                assertEquals("invalid result object size", 1, result
680:                        .getIntegerList().size());
681:                assertEquals("invalid result object value", 3500, result
682:                        .getTotalPrice());
683:                assertEquals("invalid result object value", "fieldOne", result
684:                        .getFieldOne());
685:                // map back
686:                MethodFieldTestObject result2 = (MethodFieldTestObject) mapper
687:                        .map(result, MethodFieldTestObject.class);
688:                // if no exceptions we thrown we are good. stopOnErrors = true. both values will be null
689:                // since this is a one-way mapping we shouldn't have a value
690:                assertNull(result2.getFieldOne());
691:            }
692:
693:            public void testNoReadMethod() throws Exception {
694:                // If the field doesnt have a getter/setter, dont add it is a default field to be mapped.
695:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
696:                NoReadMethod src = (NoReadMethod) newInstance(NoReadMethod.class);
697:                src.setNoReadMethod("somevalue");
698:
699:                NoReadMethodPrime dest = (NoReadMethodPrime) mapper.map(src,
700:                        NoReadMethodPrime.class);
701:                assertNull(
702:                        "field should be null because no read method exists for field",
703:                        dest.getXXXXX());
704:            }
705:
706:            public void testNoReadMethodSameClassTypes() throws Exception {
707:                // If the field doesnt have a getter/setter, dont add it is a default field to be mapped.
708:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
709:                NoReadMethod src = (NoReadMethod) newInstance(NoReadMethod.class);
710:                src.setNoReadMethod("somevalue");
711:
712:                NoReadMethod dest = (NoReadMethod) mapper.map(src,
713:                        NoReadMethod.class);
714:                assertNull(
715:                        "field should be null because no read method exists for field",
716:                        dest.getXXXXX());
717:            }
718:
719:            public void testNoReadMethod_GetterOnlyWithParams()
720:                    throws Exception {
721:                // Dont use getter methods that have a param when discovering default fields to be mapped.
722:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
723:                NoReadMethod src = (NoReadMethod) newInstance(NoReadMethod.class);
724:                src.setOtherNoReadMethod("someValue");
725:
726:                NoReadMethod dest = (NoReadMethod) mapper.map(src,
727:                        NoReadMethod.class);
728:                assertNull(
729:                        "field should be null because no read method exists for field",
730:                        dest.getOtherNoReadMethod(-1));
731:            }
732:
733:            public void testNoWriteMethod() throws Exception {
734:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
735:                NoWriteMethod src = (NoWriteMethod) newInstance(NoWriteMethod.class);
736:                src.setXXXXXX("someValue");
737:
738:                NoWriteMethodPrime dest = (NoWriteMethodPrime) mapper.map(src,
739:                        NoWriteMethodPrime.class);
740:                assertNull(
741:                        "field should be null because no write method exists for field",
742:                        dest.getNoWriteMethod());
743:
744:            }
745:
746:            public void testNoWriteMethodSameClassTypes() throws Exception {
747:                // When mapping between identical types, if the field doesnt have a getter/setter, dont
748:                // add it is a default field to be mapped.
749:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
750:                NoWriteMethod src = (NoWriteMethod) newInstance(NoWriteMethod.class);
751:                src.setXXXXXX("someValue");
752:
753:                mapper.map((NoReadMethod) newInstance(NoReadMethod.class),
754:                        NoReadMethod.class);
755:
756:                NoWriteMethod dest = (NoWriteMethod) mapper.map(src,
757:                        NoWriteMethod.class);
758:                assertNull(
759:                        "field should be null because no write method exists for field",
760:                        dest.getNoWriteMethod());
761:            }
762:
763:            public void testNullField() throws Exception {
764:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
765:                AnotherTestObject src = (AnotherTestObject) newInstance(AnotherTestObject.class);
766:                src.setField2(null);
767:                AnotherTestObjectPrime dest = (AnotherTestObjectPrime) newInstance(AnotherTestObjectPrime.class);
768:                dest.setField2(Integer.valueOf("555"));
769:                // check that null overrides an existing value
770:                mapper.map(src, dest);
771:                assertNull("dest field should be null", dest.getField2());
772:            }
773:
774:            public void testNullField2() throws Exception {
775:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
776:                // Test that String --> String with an empty String input value results
777:                // in the destination field being an empty String and not null.
778:                String input = "";
779:                TestObject src = (TestObject) newInstance(TestObject.class);
780:                src.setOne(input);
781:
782:                TestObjectPrime dest = (TestObjectPrime) mapper.map(src,
783:                        TestObjectPrime.class);
784:                assertNotNull("dest field should not be null", dest
785:                        .getOnePrime());
786:                assertEquals("invalid dest field value", input, dest
787:                        .getOnePrime());
788:            }
789:
790:            public void testNullToPrimitive() throws Exception {
791:                mapper = getMapper(new String[] { "dozerBeanMapping.xml" });
792:                AnotherTestObject src = (AnotherTestObject) newInstance(AnotherTestObject.class);
793:                AnotherTestObjectPrime prime = (AnotherTestObjectPrime) newInstance(AnotherTestObjectPrime.class);
794:                TestObject to = (TestObject) newInstance(TestObject.class);
795:                to.setThePrimitive(AnotherTestObjectPrime.DEFAULT_FIELD1);
796:                prime.setTo(to);
797:                mapper.map(src, prime);
798:                // check primitive on deep field
799:                // primitive should still be default
800:                assertEquals("invalid field value",
801:                        AnotherTestObjectPrime.DEFAULT_FIELD1, prime
802:                                .getField1());
803:                assertEquals("invalid field value",
804:                        AnotherTestObjectPrime.DEFAULT_FIELD1, prime.getTo()
805:                                .getThePrimitive());
806:            }
807:
808:            public void testGlobalRelationshipType() throws Exception {
809:                mapper = getMapper(new String[] { "relationship-type-global-configuration.xml" });
810:                TestObject src = new TestObject();
811:                src.setHintList(new ArrayList(Arrays
812:                        .asList(new String[] { "a" })));
813:
814:                TestObjectPrime dest = new TestObjectPrime();
815:                dest.setHintList(new ArrayList(Arrays.asList(new String[] {
816:                        "a", "b" })));
817:
818:                mapper.map(src, dest);
819:
820:                assertEquals(
821:                        "wrong # of elements in dest list for non-cumulative mapping",
822:                        2, dest.getHintList().size());
823:            }
824:
825:            public void testClassMapRelationshipType() throws Exception {
826:                mapper = getMapper(new String[] { "relationshipTypeMapping.xml" });
827:                TestObject src = new TestObject();
828:                src.setHintList(new ArrayList(Arrays
829:                        .asList(new String[] { "a" })));
830:
831:                TestObjectPrime dest = new TestObjectPrime();
832:                dest.setHintList(new ArrayList(Arrays.asList(new String[] {
833:                        "a", "b" })));
834:
835:                mapper.map(src, dest);
836:
837:                assertEquals(
838:                        "wrong # of elements in dest list for non-cumulative mapping",
839:                        2, dest.getHintList().size());
840:            }
841:
842:            public void testRemoveOrphans() {
843:                mapper = getMapper(new String[] { "removeOrphansMapping.xml" });
844:
845:                MyClassA myClassA = new MyClassA();
846:                MyClassB myClassB = new MyClassB();
847:
848:                Fruit apple = new Fruit();
849:                apple.setName("Apple");
850:                Fruit banana = new Fruit();
851:                banana.setName("Banana");
852:                Fruit grape = new Fruit();
853:                grape.setName("Grape");
854:                Fruit orange = new Fruit();
855:                orange.setName("Orange");
856:                Fruit kiwiFruit = new Fruit();
857:                kiwiFruit.setName("Kiwi Fruit");
858:
859:                List srcFruits = new ArrayList();
860:                srcFruits.add(apple);
861:                srcFruits.add(banana);
862:                srcFruits.add(kiwiFruit);
863:
864:                List destFruits = new ArrayList();
865:                destFruits.add(grape); // not in src
866:                destFruits.add(banana); // shared with src fruits
867:                destFruits.add(orange); // not in src
868:
869:                myClassA.setAStringList(srcFruits);
870:                myClassB.setAStringList(destFruits);
871:
872:                mapper.map(myClassA, myClassB, "testRemoveOrphansOnList");
873:
874:                assertEquals(3, myClassB.getAStringList().size());
875:                assertTrue(myClassB.getAStringList().contains(apple));
876:                assertTrue(myClassB.getAStringList().contains(banana));
877:                assertTrue(myClassB.getAStringList().contains(kiwiFruit));
878:                assertFalse(myClassB.getAStringList().contains(grape));
879:                assertFalse(myClassB.getAStringList().contains(orange));
880:            }
881:
882:            public void testOrphanRemovalSet() {
883:                mapper = getMapper(new String[] { "removeOrphansMapping.xml" });
884:                Parent parent = new Parent(new Long(1), "parent");
885:                Child child1 = new Child(new Long(1), "child1");
886:                Set childrenSet = new HashSet();
887:                childrenSet.add(child1);
888:                parent.setChildrenSet(childrenSet);
889:
890:                ParentPrime parentPrime = (ParentPrime) mapper.map(parent,
891:                        ParentPrime.class);
892:                // Make sure the first one was mapped ok.
893:                assertEquals(parent.getChildrenSet().size(), parentPrime
894:                        .getChildrenSet().size());
895:
896:                ChildPrime child2 = new ChildPrime(new Long(2L), "child2");
897:                parentPrime.getChildrenSet().add(child2);
898:                mapper.map(parentPrime, parent);
899:                // Make sure adding one works ok.
900:                assertEquals(parentPrime.getChildrenSet().size(), parent
901:                        .getChildrenSet().size());
902:
903:                parentPrime.getChildrenSet().clear();
904:                mapper.map(parentPrime, parent);
905:                // Make sure REMOVING them (the orphan children) works ok.
906:                assertEquals(parentPrime.getChildrenSet().size(), parent
907:                        .getChildrenSet().size());
908:            }
909:
910:            public void testOrphanRemovalList() {
911:                mapper = getMapper(new String[] { "removeOrphansMapping.xml" });
912:                Parent parent = new Parent(new Long(1), "parent");
913:                Child child1 = new Child(new Long(1), "child1");
914:                List childrenList = new ArrayList();
915:                childrenList.add(child1);
916:                parent.setChildrenList(childrenList);
917:
918:                ParentPrime parentPrime = (ParentPrime) mapper.map(parent,
919:                        ParentPrime.class);
920:                // Make sure the first one was mapped ok.
921:                assertEquals(parent.getChildrenList().size(), parentPrime
922:                        .getChildrenList().size());
923:
924:                ChildPrime child2 = new ChildPrime(new Long(2L), "child2");
925:                parentPrime.getChildrenList().add(child2);
926:                mapper.map(parentPrime, parent);
927:                // Make sure adding one works ok.
928:                assertEquals(parentPrime.getChildrenList().size(), parent
929:                        .getChildrenList().size());
930:
931:                parentPrime.getChildrenList().clear();
932:                mapper.map(parentPrime, parent);
933:                // Make sure REMOVING them (the orphan children) works ok.
934:                assertEquals(parentPrime.getChildrenList().size(), parent
935:                        .getChildrenList().size());
936:            }
937:
938:            protected DataObjectInstantiator getDataObjectInstantiator() {
939:                return NoProxyDataObjectInstantiator.INSTANCE;
940:            }
941:
942:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.