1 16 package net.sf.dozer.util.mapping; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import net.sf.dozer.util.mapping.factories.SampleCustomBeanFactory; 22 import net.sf.dozer.util.mapping.factories.SampleCustomBeanFactory2; 23 import net.sf.dozer.util.mapping.factories.SampleDefaultBeanFactory; 24 import net.sf.dozer.util.mapping.util.TestDataFactory; 25 import net.sf.dozer.util.mapping.vo.Bus; 26 import net.sf.dozer.util.mapping.vo.Car; 27 import net.sf.dozer.util.mapping.vo.MetalThingyIF; 28 import net.sf.dozer.util.mapping.vo.Moped; 29 import net.sf.dozer.util.mapping.vo.TestObject; 30 import net.sf.dozer.util.mapping.vo.TestObjectPrime; 31 import net.sf.dozer.util.mapping.vo.Van; 32 import net.sf.dozer.util.mapping.vo.deep.HomeDescription; 33 import net.sf.dozer.util.mapping.vo.deep.House; 34 35 38 public class DozerBeanMapperTest extends DozerTestBase { 39 private static MapperIF mapper; 40 41 protected void setUp() throws Exception { 42 super.setUp(); 43 if (mapper == null) { 44 mapper = getNewMapper(new String [] { "dozerBeanMapping.xml" }); 45 } 46 } 47 48 public void testNoSourceObject() throws Exception { 49 try { 50 mapper.map(null, TestObjectPrime.class); 51 fail("should have thrown exception"); 52 } catch (MappingException e) { 53 assertEquals("source object must not be null", e.getMessage()); 54 } 55 } 56 57 public void testNoDestinationClass() throws Exception { 58 try { 59 mapper.map(new TestObjectPrime(), null); 60 fail("should have thrown exception"); 61 } catch (MappingException e) { 62 assertEquals("destination class must not be null", e.getMessage()); 63 } 64 } 65 66 public void testGeneralMapping() throws Exception { 67 assertCommon(mapper); 68 } 69 70 public void testNoMappingFilesSpecified() throws Exception { 71 MapperIF mapper = new DozerBeanMapper(); 74 75 assertCommon(mapper); 76 } 77 78 public void testInjectMapperUsingSpring() throws Exception { 79 MapperIF mapper = (MapperIF) ApplicationBeanFactory.getBean(MapperIF.class); 81 DozerBeanMapper mapperImpl = (DozerBeanMapper) mapper; 82 83 MapperIF cleanMapper = (MapperIF) ApplicationBeanFactory.getBean("cleanMapper"); 84 85 assertNotNull("mapper should not be null", mapper); 86 assertNotNull("mapping file names should not be null", mapperImpl.getMappingFiles()); 87 assertTrue("mapping file names should not be empty", mapperImpl.getMappingFiles().size() > 0); 88 89 assertCommon(mapper); 91 92 Car car = new Car(); 94 Van van = (Van) cleanMapper.map(car, Van.class); 95 assertEquals("injectedName", van.getName()); 96 Car carDest = (Car) cleanMapper.map(van, Car.class); 98 assertEquals("injectedName", carDest.getName()); 99 100 Moped moped = new Moped(); 102 Bus bus = (Bus) cleanMapper.map(moped, Bus.class); 103 assertEquals("injectedName", bus.getName()); 104 105 Moped mopedDest = (Moped) cleanMapper.map(bus, Moped.class); 107 assertEquals("injectedName", mopedDest.getName()); 108 } 109 110 public void testSpringNoMappingFilesSpecified() throws Exception { 111 MapperIF mapper = (MapperIF) ApplicationBeanFactory.getBean("NoExplicitMappingsMapperIF"); 114 115 assertCommon(mapper); 116 } 117 118 public void testCustomBeanFactory() throws Exception { 119 124 List mappingFiles = new ArrayList (); 125 mappingFiles.add("customfactorymapping.xml"); 126 MapperIF mapper = getNewMapper((String []) mappingFiles.toArray(new String [0])); 127 128 TestObjectPrime prime = (TestObjectPrime) mapper.map(TestDataFactory.getInputGeneralMappingTestObject(), 129 TestObjectPrime.class); 130 TestObject source = (TestObject) mapper.map(prime, TestObject.class); 131 132 assertNotNull("created by factory name should not be null", prime.getCreatedByFactoryName()); 134 assertNotNull("created by factory name should not be null", source.getCreatedByFactoryName()); 135 assertEquals(SampleCustomBeanFactory.class.getName(), prime.getCreatedByFactoryName()); 136 assertEquals(SampleCustomBeanFactory.class.getName(), source.getCreatedByFactoryName()); 137 138 assertNotNull("created by factory name should not be null", source.getThree().getCreatedByFactoryName()); 140 assertEquals(SampleDefaultBeanFactory.class.getName(), source.getThree().getCreatedByFactoryName()); 141 142 assertNotNull("created by factory name should not be null", prime.getThreePrime().getCreatedByFactoryName()); 144 assertEquals(SampleCustomBeanFactory2.class.getName(), prime.getThreePrime().getCreatedByFactoryName()); 145 146 Van van = new Van(); 148 van.setName("testName"); 149 MetalThingyIF car = (MetalThingyIF) mapper.map(van, MetalThingyIF.class); 150 assertEquals("testName", car.getName()); 151 } 152 153 public void testDetectDuplicateMapping() throws Exception { 154 MapperIF myMapper = null; 155 try { 156 157 List mappingFiles = new ArrayList (); 158 mappingFiles.add("duplicateMapping.xml"); 159 myMapper = new DozerBeanMapper(mappingFiles); 160 161 myMapper.map(new net.sf.dozer.util.mapping.vo.SuperSuperSuperClass(), 162 net.sf.dozer.util.mapping.vo.SuperSuperSuperClassPrime.class); 163 fail("should have thrown exception"); 164 } catch (Exception e) { 165 assertTrue("invalid exception", e.getMessage().indexOf("Duplicate Class Mapping Found") != -1); 166 } 167 } 168 169 private void assertCommon(MapperIF mapper) throws Exception { 170 TestObjectPrime prime = (TestObjectPrime) mapper.map(TestDataFactory.getInputGeneralMappingTestObject(), 171 TestObjectPrime.class); 172 TestObject source = (TestObject) mapper.map(prime, TestObject.class); 173 TestObjectPrime prime2 = (TestObjectPrime) mapper.map(source, TestObjectPrime.class); 174 175 assertEquals(prime2, prime); 176 } 177 178 public void testEventListeners() throws Exception { 179 MapperIF eventMapper = (MapperIF) ApplicationBeanFactory.getBean("EventMapper"); 182 House src = TestDataFactory.getHouse(); 183 HomeDescription dest = (HomeDescription) eventMapper.map(src, HomeDescription.class); 184 185 } 186 187 } | Popular Tags |