📄 granulardozerbeanmappertest.java
字号:
/*
* Copyright 2005-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.dozer.util.mapping;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import net.sf.dozer.util.mapping.vo.AnotherTestObject;
import net.sf.dozer.util.mapping.vo.AnotherTestObjectPrime;
import net.sf.dozer.util.mapping.vo.Car;
import net.sf.dozer.util.mapping.vo.FieldValue;
import net.sf.dozer.util.mapping.vo.GetWeatherByZipCodeDocument;
import net.sf.dozer.util.mapping.vo.InsideTestObject;
import net.sf.dozer.util.mapping.vo.InsideTestObjectPrime;
import net.sf.dozer.util.mapping.vo.MetalThingyIF;
import net.sf.dozer.util.mapping.vo.TestObject;
import net.sf.dozer.util.mapping.vo.TestObjectPrime;
import net.sf.dozer.util.mapping.vo.TestObjectPrime2;
import net.sf.dozer.util.mapping.vo.GetWeatherByZipCodeDocument.GetWeatherByZipCode;
import net.sf.dozer.util.mapping.vo.context.ContextMapping;
import net.sf.dozer.util.mapping.vo.context.ContextMappingNested;
import net.sf.dozer.util.mapping.vo.context.ContextMappingNestedPrime;
import net.sf.dozer.util.mapping.vo.context.ContextMappingPrime;
import net.sf.dozer.util.mapping.vo.deep.House;
import net.sf.dozer.util.mapping.vo.map.MapToMap;
import net.sf.dozer.util.mapping.vo.map.MapToMapPrime;
import net.sf.dozer.util.mapping.vo.set.NamesArray;
import net.sf.dozer.util.mapping.vo.set.NamesSet;
import net.sf.dozer.util.mapping.vo.set.NamesSortedSet;
import net.sf.dozer.util.mapping.vo.index.Mccoy;
import net.sf.dozer.util.mapping.vo.index.MccoyPrime;
import net.sf.dozer.util.mapping.vo.isaccessible.Foo;
import net.sf.dozer.util.mapping.vo.isaccessible.FooPrime;
/**
* @author garsombke.franz
*/
public class GranularDozerBeanMapperTest extends DozerTestBase {
public void testMapToMap() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "mapInterfaceMapping.xml", "dozerBeanMapping.xml" });
TestObject to = new TestObject();
to.setOne("one");
TestObject to2 = new TestObject();
to2.setTwo(new Integer(2));
Map map = new HashMap();
map.put("to", to);
map.put("to2", to2);
MapToMap mtm = new MapToMap();
mtm.setStandardMap(map);
Map map2 = new HashMap();
map2.put("to", to);
map2.put("to2", to2);
mtm.setStandardMapWithHint(map2);
MapToMapPrime mtmp = (MapToMapPrime) mapper.map(mtm, MapToMapPrime.class);
assertEquals("one", ((TestObject) mtmp.getStandardMap().get("to")).getOne());
assertEquals(2, ((TestObject) mtmp.getStandardMap().get("to2")).getTwo().intValue());
// verify that we transformed from object to object prime
assertEquals("one", ((TestObjectPrime) mtmp.getStandardMapWithHint().get("to")).getOnePrime());
assertEquals(2, ((TestObjectPrime) mtmp.getStandardMapWithHint().get("to2")).getTwoPrime().intValue());
}
public void testMapToMapExistingDestination() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "mapInterfaceMapping.xml", "dozerBeanMapping.xml" });
TestObject to = new TestObject();
to.setOne("one");
TestObject to2 = new TestObject();
to2.setTwo(new Integer(2));
Map map = new HashMap();
map.put("to", to);
map.put("to2", to2);
MapToMap mtm = new MapToMap();
mtm.setStandardMap(map);
// create an existing map and set a value so we can test if it exists after mapping
MapToMapPrime mtmp = new MapToMapPrime();
Map map2 = new Hashtable();
map2.put("toDest", to);
mtmp.setStandardMap(map2);
mapper.map(mtm, mtmp);
assertEquals("one", ((TestObject) mtmp.getStandardMap().get("to")).getOne());
assertEquals(2, ((TestObject) mtmp.getStandardMap().get("to2")).getTwo().intValue());
assertEquals("one", ((TestObject) mtmp.getStandardMap().get("toDest")).getOne());
}
public void testFieldAccessible() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "fieldAttributeMapping.xml" });
TestObject to = new TestObject();
to.setFieldAccessible("fieldAccessible");
to.setFieldAccessiblePrimInt(2);
InsideTestObject ito = new InsideTestObject();
ito.setLabel("label");
to.setFieldAccessibleComplexType(ito);
String[] stringArray = new String[] { "one", "two" };
to.setFieldAccessibleArrayToList(stringArray);
TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class);
assertEquals("fieldAccessible", top.fieldAccessible);
assertEquals("label", top.fieldAccessibleComplexType.getLabelPrime());
assertEquals(2, top.fieldAccessiblePrimInt);
assertEquals("one", top.fieldAccessibleArrayToList.get(0));
assertEquals("two", top.fieldAccessibleArrayToList.get(1));
// Map Back
TestObject toDest = (TestObject) mapper.map(top, TestObject.class);
assertEquals("fieldAccessible", toDest.getFieldAccessible());
assertEquals("label", toDest.getFieldAccessibleComplexType().getLabel());
assertEquals(2, toDest.getFieldAccessiblePrimInt());
assertEquals("one", toDest.getFieldAccessibleArrayToList()[0]);
assertEquals("two", toDest.getFieldAccessibleArrayToList()[1]);
}
public void testOverloadGetSetMethods() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "fieldAttributeMapping.xml" });
TestObject to = new TestObject();
Date date = new Date();
to.setOverloadGetField(new Date());
TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class);
assertEquals(date, top.getOverloadSetField());
// Map Back
TestObject toDest = (TestObject) mapper.map(top, TestObject.class);
assertEquals(date, toDest.getOverloadGetField());
}
public void testFieldCreateMethod() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "fieldAttributeMapping.xml" });
TestObject to = new TestObject();
InsideTestObject ito = new InsideTestObject();
// we did not set any values. this will be set in the 'createMethod'
to.setCreateMethodType(ito);
TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class);
assertEquals("myField", top.getCreateMethodType().getMyField());
// Map Back
TestObject toDest = (TestObject) mapper.map(top, TestObject.class);
assertEquals("testCreateMethod", toDest.getCreateMethodType().getTestCreateMethod());
}
public void testDeepInterfaceWithHint() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "fieldAttributeMapping.xml" });
InsideTestObject ito = new InsideTestObject();
House house = new House();
MetalThingyIF thingy = new Car();
thingy.setName("name");
house.setThingy(thingy);
ito.setHouse(house);
InsideTestObjectPrime itop = (InsideTestObjectPrime) mapper.map(ito, InsideTestObjectPrime.class);
assertEquals("name", itop.getDeepInterfaceString());
// Map Back
InsideTestObject dest = (InsideTestObject) mapper.map(itop, InsideTestObject.class);
assertEquals("name", ito.getHouse().getThingy().getName());
}
public void testIntegerToString() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "fieldAttributeMapping.xml" });
TestObject to = new TestObject();
Integer[] array = new Integer[] { new Integer(1), new Integer(2) };
to.setArrayForLists(array);
TestObjectPrime top = (TestObjectPrime) mapper.map(to, TestObjectPrime.class);
assertEquals("1", top.getListForArray().get(0));
// Map Back
// InsideTestObject dest = (InsideTestObject) mapper.map(itop, InsideTestObject.class);
// assertEquals("name", ito.getHouse().getThingy().getName());
}
public void testMapNull_MappingLevel() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "nullFieldMapping.xml" });
// check that null does not override an existing value when map-null="false"
AnotherTestObject src = new AnotherTestObject();
src.setField3(null);
src.setField4(null);
AnotherTestObjectPrime dest = new AnotherTestObjectPrime();
dest.setTo(new TestObject());
dest.setField3("555");
dest.getTo().setOne("4641");
// dest field should remain unchanged
mapper.map(src, dest);
assertEquals("invalid dest field value", "555", dest.getField3());
assertEquals("invalid dest field value2", "4641", dest.getTo().getOne());
}
public void testMapNull_MappingLevel2() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "nullFieldMapping.xml" });
// Reverse mapping
AnotherTestObjectPrime src = new AnotherTestObjectPrime();
src.setTo(new TestObject());
src.setField3(null);
src.getTo().setOne(null);
AnotherTestObject dest = new AnotherTestObject();
dest.setField3("555");
dest.setField4("4641");
// dest field should remain unchanged
mapper.map(src, dest);
assertEquals("invalid dest field value", "555", dest.getField3());
assertEquals("invalid dest field value2", "4641", dest.getField4());
}
public void testMapEmptyString_MappingLevel() throws Exception {
MapperIF mapper = getNewMapper(new String[] { "nullFieldMapping.xml" });
// check that "" does not override an existing value when map-empty-string="false"
AnotherTestObject src = new AnotherTestObject();
src.setField3("");
src.setField4("");
AnotherTestObjectPrime dest = new AnotherTestObjectPrime();
dest.setTo(new TestObject());
dest.setField3("555");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -