KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > MapTypeTest


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import net.sf.dozer.util.mapping.vo.map.NestedObj;
22 import net.sf.dozer.util.mapping.vo.map.NestedObjPrime;
23 import net.sf.dozer.util.mapping.vo.map.SimpleObj;
24 import net.sf.dozer.util.mapping.vo.map.SimpleObjPrime;
25
26
27 /**
28  * @author tierney.matt
29  */

30 public class MapTypeTest extends DozerTestBase {
31   
32   public void testMapType_MapToVo() throws Exception JavaDoc {
33     //Test simple Map --> Vo with custom mappings defined.
34
mapper = getNewMapper(new String JavaDoc[] {"mapMapping2.xml"});
35
36     NestedObj nestedObj = new NestedObj();
37     nestedObj.setField1("nestedfield1value");
38     Map JavaDoc src = new HashMap JavaDoc();
39     src.put("field1", "mapnestedfield1value");
40     src.put("nested", nestedObj);
41     
42     SimpleObjPrime result = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class, "caseA");
43     assertEquals(src.get("field1"), result.getField1());
44     assertEquals(nestedObj.getField1(), result.getNested().getField1());
45     
46     //Map result2 = (Map) mapper.map(result, Map.class);
47
//assertEquals(src.get("field1"), result2.get("field1"));
48
//assertNotNull(result2.get("nested"));
49
//assertEquals(src.get("nested"), result2.get("nested"));
50
}
51   
52   public void testMapType_MapToVo_NoCustomMappings() throws Exception JavaDoc {
53     //Test simple Map --> Vo with custom mappings defined.
54
mapper = getNewMapper(new String JavaDoc[] {"mapMapping2.xml"});
55     Map JavaDoc src = new HashMap JavaDoc();
56     src.put("field1", "field1value");
57     src.put("field2", "field2value");
58     
59     SimpleObjPrime result = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class, "caseB");
60     assertNull(result.getField1());
61     assertEquals(src.get("field2"), result.getField2());
62   }
63   
64   public void testMapType_MapToVoUsingMapId() {
65     //Simple map --> vo using a map-id
66
mapper = super.getNewMapper(new String JavaDoc[]{"mapMapping.xml"});
67     Map JavaDoc src = new HashMap JavaDoc();
68     src.put("field1", "field1value");
69     
70     NestedObjPrime dest = (NestedObjPrime) mapper.map(src, NestedObjPrime.class, "caseB");
71     assertEquals(src.get("field1"), dest.getField1());
72   }
73   
74   public void testMapType_NestedMapToVoUsingMapId() {
75     //Another test for nested Map --> Vo using <field map-id=....>
76
mapper = super.getNewMapper(new String JavaDoc[]{"mapMapping.xml"});
77
78     Map JavaDoc nested2 = new HashMap JavaDoc();
79     nested2.put("field1", "field1MapValue");
80     
81     SimpleObj src = new SimpleObj();
82     src.setNested2(nested2);
83     
84     SimpleObjPrime result = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class, "caseA2");
85     
86     assertNull(result.getNested2().getField1());
87   }
88 }
Popular Tags