KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import java.math.BigInteger JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import net.pmonks.xml.dozer.test.ChildType;
26 import net.sf.dozer.util.mapping.util.CollectionUtils;
27 import net.sf.dozer.util.mapping.vo.Child;
28 import net.sf.dozer.util.mapping.vo.PrimitiveArrayObj;
29 import net.sf.dozer.util.mapping.vo.PrimitiveArrayObjPrime;
30 import net.sf.dozer.util.mapping.vo.km.SomeVo;
31 import net.sf.dozer.util.mapping.vo.km.Sub;
32 import net.sf.dozer.util.mapping.vo.km.Super;
33 import net.sf.dozer.util.mapping.vo.map.NestedObj;
34 import net.sf.dozer.util.mapping.vo.map.SimpleObj;
35 import net.sf.dozer.util.mapping.vo.map.SimpleObjPrime;
36 import net.sf.dozer.util.mapping.vo.set.SomeDTO;
37 import net.sf.dozer.util.mapping.vo.set.SomeOtherDTO;
38 import net.sf.dozer.util.mapping.vo.set.SomeVO;
39 import net.sf.dozer.util.mapping.vo.deep2.Dest;
40 import net.sf.dozer.util.mapping.vo.deep2.Src;
41
42
43 /**
44  * This is a holding grounds for test cases that reproduce known bugs, features, or gaps discovered during development.
45  * As the use cases are resolved, these tests should be moved to the live unit test classes.
46  *
47  * @author tierney.matt
48  */

49 public class KnownFailures extends DozerTestBase {
50   
51   //Failure discovered during development of an unrelated map type feature request
52
public void testMapType_MapToVo_NoCustomMappings() throws Exception JavaDoc {
53     //Test simple Map --> Vo without any custom mappings defined.
54
NestedObj nestedObj = new NestedObj();
55     nestedObj.setField1("nestedfield1value");
56     Map JavaDoc src = new HashMap JavaDoc();
57     src.put("field1", "mapnestedfield1value");
58     src.put("nested", nestedObj);
59     
60     SimpleObjPrime result = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class);
61     assertEquals(src.get("field1"), result.getField1());
62     assertEquals(nestedObj.getField1(), result.getNested().getField1());
63     
64     SimpleObj result2 = (SimpleObj) mapper.map(result, Map JavaDoc.class);
65     assertEquals(src, result2);
66   }
67   
68   //Failure discovered during development of an unrelated map type feature request
69
public void testMapType_NestedMapToVo_NoCustomMappings() throws Exception JavaDoc {
70     //Simple test checking that Maps get mapped to a VO without any custom mappings or map-id.
71
//Should behave like Vo --> Vo, matching on common attr(key) names.
72
Map JavaDoc nested2 = new HashMap JavaDoc();
73     nested2.put("field1", "mapnestedfield1");
74     
75     SimpleObj src = new SimpleObj();
76     src.setNested2(nested2);
77     
78     SimpleObjPrime result = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class);
79     assertNotNull(result.getNested2());
80     assertEquals(nested2.get("field1"), result.getNested2().getField1());
81     
82     SimpleObj result2 = (SimpleObj) mapper.map(result, SimpleObj.class);
83     assertEquals(src, result2);
84   }
85   
86   //Failure discovered during development of an unrelated map type feature request
87
public void testMapType_NestedMapToVoUsingMapId() {
88     //Test nested Map --> Vo using <field map-id=....>
89
mapper = super.getNewMapper(new String JavaDoc[]{"mapMapping.xml"});
90     NestedObj nested = new NestedObj();
91     nested.setField1("nestedfield1");
92     Map JavaDoc nested2 = new HashMap JavaDoc();
93     nested2.put("field1", "field1MapValue");
94     
95     SimpleObj src = new SimpleObj();
96     src.setField1("field1");
97     src.setNested(nested);
98     src.setNested2(nested2);
99     
100     SimpleObjPrime result = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class);
101     
102     assertEquals(src.getField1(), result.getField1());
103     assertEquals(src.getNested().getField1(), result.getNested().getField1());
104     assertEquals(src.getNested2().get("field1"), result.getNested2().getField1());
105     
106     SimpleObj result2 = (SimpleObj) mapper.map(result, SimpleObj.class, "caseA");
107     
108     assertEquals(src, result2);
109   }
110   
111   public void testMapType_MapToVo_CustomMapping_NoMapId() {
112     //Test nested Map --> Vo using custom mappings without map-id
113
mapper = getNewMapper(new String JavaDoc[]{"mapMapping3.xml"});
114     
115     Map JavaDoc nested2 = new HashMap JavaDoc();
116     nested2.put("field1", "mapnestedfield1value");
117     nested2.put("field2", "mapnestedfield2value");
118     
119     SimpleObj src = new SimpleObj();
120     src.setNested2(nested2);
121     src.setField1("field1value");
122     
123     SimpleObjPrime result = (SimpleObjPrime) mapper.map(src, SimpleObjPrime.class);
124     assertNull(result.getNested().getField1());//field exclude in mappings file
125
assertEquals(nested2.get("field2"), result.getNested2().getField2());
126   }
127
128   /*
129    * Related to feature request #1456486. Deep mapping with custom getter/setter does not work
130    */

131   public void testDeepMapping_UsingCustomGetSetMethods() {
132     mapper = super.getNewMapper(new String JavaDoc[]{"knownFailures.xml"});
133     
134     Src src = new Src();
135     src.setSrcField("srcFieldValue");
136     
137     Dest dest = (Dest) mapper.map(src, Dest.class);
138     
139     assertNotNull(dest.getDestField().getNestedDestField().getNestedNestedDestField());
140     assertEquals(src.getSrcField(), dest.getDestField().getNestedDestField().getNestedNestedDestField());
141     
142     Src dest2 = (Src)mapper.map(dest, Src.class);
143     
144     assertNotNull(dest2.getSrcField());
145     assertEquals(dest.getDestField().getNestedDestField().getNestedNestedDestField(), dest2.getSrcField());
146     
147   }
148   
149   /*
150    * Related to bug #1486105. This test is successful, but provides context for testKM2 which fails
151    */

152   public void testKM1() {
153     SomeVo request = new SomeVo();
154     request.setUserName("yo");
155     request.setAge("2");
156     request.setColor("blue");
157     
158     MapperIF mapper = getNewMapper(new String JavaDoc[]{"kmmapping.xml"});
159     
160     Super afterMapping = (Super) mapper.map(request,Super.class);
161     
162     assertNotNull("login name should not be null", afterMapping.getLoginName());
163     assertNotNull("age should not be null", afterMapping.getAge());
164     assertEquals("should map SuperClass.name to SubClassPrime userName.",request.getUserName(),afterMapping.getLoginName());
165     assertEquals(request.getAge(), afterMapping.getAge());
166   }
167   
168   /*
169    * Bug #1486105
170    */

171   public void testKM2() {
172     Sub request = new Sub();
173     request.setAge("2");
174     request.setColor("blue");
175     request.setLoginName("fred");
176         
177     MapperIF mapper = getNewMapper(new String JavaDoc[] {"kmmapping.xml"});
178       
179     SomeVo afterMapping = (SomeVo) mapper.map(request,SomeVo.class);
180        
181     assertNotNull("un should not be null", afterMapping.getUserName());
182     assertNotNull("color should not be null",afterMapping.getColor());
183     assertNotNull("age should not be null", afterMapping.getAge());
184     assertEquals("should map SuperClass.name to SubClassPrime userName.",request.getLoginName(),afterMapping.getUserName());
185     assertEquals(request.getColor(),afterMapping.getColor());
186     assertEquals(request.getAge(), afterMapping.getAge());
187   }
188
189   /*
190    * Bug #1549738
191    */

192   public void testSetMapping() throws Exception JavaDoc {
193     //For some reason the resulting SomeVO contains a Set with 4 objects. 2 SomeOtherDTO's and 2 SomeOtherVO's. I believe it
194
//should only contain 2 SomeOtherVO's. It has something to do with specifying the field name starting with cap in the mapping file. If
195
//you change the field mapping to start with lower case it seems to map correctly.
196
MapperIF mapper = getNewMapper(new String JavaDoc[] { "knownFailures.xml" });
197     
198     SomeDTO someDto = new SomeDTO();
199     someDto.setField1(new Integer JavaDoc("1"));
200     
201     SomeOtherDTO someOtherDto = new SomeOtherDTO();
202     someOtherDto.setOtherField2(someDto);
203     someOtherDto.setOtherField3("value1");
204     
205     SomeDTO someDto2 = new SomeDTO();
206     someDto2.setField1(new Integer JavaDoc("2"));
207     
208     SomeOtherDTO someOtherDto2 = new SomeOtherDTO();
209     someOtherDto2.setOtherField2(someDto2);
210     someOtherDto2.setOtherField3("value2");
211     
212     SomeDTO src = new SomeDTO();
213     src.setField2(new SomeOtherDTO[] { someOtherDto2,someOtherDto });
214     
215     SomeVO dest = (SomeVO) mapper.map(src, SomeVO.class);
216     
217     assertEquals("incorrect resulting set size", src.getField2().length, dest.getField2().size());
218     //TODO: add more asserts
219
}
220   
221 }
Popular Tags