1 16 package net.sf.dozer.util.mapping; 17 18 19 import java.math.BigInteger ; 20 import java.util.Arrays ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 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 49 public class KnownFailures extends DozerTestBase { 50 51 public void testMapType_MapToVo_NoCustomMappings() throws Exception { 53 NestedObj nestedObj = new NestedObj(); 55 nestedObj.setField1("nestedfield1value"); 56 Map src = new HashMap (); 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 .class); 65 assertEquals(src, result2); 66 } 67 68 public void testMapType_NestedMapToVo_NoCustomMappings() throws Exception { 70 Map nested2 = new HashMap (); 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 public void testMapType_NestedMapToVoUsingMapId() { 88 mapper = super.getNewMapper(new String []{"mapMapping.xml"}); 90 NestedObj nested = new NestedObj(); 91 nested.setField1("nestedfield1"); 92 Map nested2 = new HashMap (); 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 mapper = getNewMapper(new String []{"mapMapping3.xml"}); 114 115 Map nested2 = new HashMap (); 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()); assertEquals(nested2.get("field2"), result.getNested2().getField2()); 126 } 127 128 131 public void testDeepMapping_UsingCustomGetSetMethods() { 132 mapper = super.getNewMapper(new String []{"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 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 []{"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 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 [] {"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 192 public void testSetMapping() throws Exception { 193 MapperIF mapper = getNewMapper(new String [] { "knownFailures.xml" }); 197 198 SomeDTO someDto = new SomeDTO(); 199 someDto.setField1(new Integer ("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 ("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 } 220 221 } | Popular Tags |