KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
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 /**
36  * @author tierney.matt
37  */

38 public class DozerBeanMapperTest extends DozerTestBase {
39   private static MapperIF mapper;
40
41   protected void setUp() throws Exception JavaDoc {
42     super.setUp();
43     if (mapper == null) {
44       mapper = getNewMapper(new String JavaDoc[] { "dozerBeanMapping.xml" });
45     }
46   }
47
48   public void testNoSourceObject() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
67     assertCommon(mapper);
68   }
69
70   public void testNoMappingFilesSpecified() throws Exception JavaDoc {
71     // Mapper can be used without specifying any mapping files. Fields that have the same name will be mapped
72
// automatically.
73
MapperIF mapper = new DozerBeanMapper();
74
75     assertCommon(mapper);
76   }
77
78   public void testInjectMapperUsingSpring() throws Exception JavaDoc {
79     // Try to get mapper from spring. Mapping files are injected via Spring config.
80
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     // Do some mapping so that the mapping files are actually loaded
90
assertCommon(mapper);
91
92     // make sure that the customconverter was injected
93
Car car = new Car();
94     Van van = (Van) cleanMapper.map(car, Van.class);
95     assertEquals("injectedName", van.getName());
96     // map back
97
Car carDest = (Car) cleanMapper.map(van, Car.class);
98     assertEquals("injectedName", carDest.getName());
99
100     // test that we get customconverter even though it wasn't defined in the mapping file
101
Moped moped = new Moped();
102     Bus bus = (Bus) cleanMapper.map(moped, Bus.class);
103     assertEquals("injectedName", bus.getName());
104
105     // map back
106
Moped mopedDest = (Moped) cleanMapper.map(bus, Moped.class);
107     assertEquals("injectedName", mopedDest.getName());
108   }
109
110   public void testSpringNoMappingFilesSpecified() throws Exception JavaDoc {
111     // Mapper can be used without specifying any mapping files. Fields that have the same name will be mapped
112
// automatically.
113
MapperIF mapper = (MapperIF) ApplicationBeanFactory.getBean("NoExplicitMappingsMapperIF");
114
115     assertCommon(mapper);
116   }
117
118   public void testCustomBeanFactory() throws Exception JavaDoc {
119     // -----------------------------------------------------------
120
// Test that java beans get created with explicitly specified
121
// custom bean factory
122
// -----------------------------------------------------------
123

124     List JavaDoc mappingFiles = new ArrayList JavaDoc();
125     mappingFiles.add("customfactorymapping.xml");
126     MapperIF mapper = getNewMapper((String JavaDoc[]) mappingFiles.toArray(new String JavaDoc[0]));
127
128     TestObjectPrime prime = (TestObjectPrime) mapper.map(TestDataFactory.getInputGeneralMappingTestObject(),
129         TestObjectPrime.class);
130     TestObject source = (TestObject) mapper.map(prime, TestObject.class);
131
132     // The following asserts will verify that the ClassMap beanFactory attr gets applied to both classes
133
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     // The following asserts will verify that default configuration is being applied
139
assertNotNull("created by factory name should not be null", source.getThree().getCreatedByFactoryName());
140     assertEquals(SampleDefaultBeanFactory.class.getName(), source.getThree().getCreatedByFactoryName());
141
142     // The following asserts will verify that dest or src class level attr's override classMap and default config attr's
143
assertNotNull("created by factory name should not be null", prime.getThreePrime().getCreatedByFactoryName());
144     assertEquals(SampleCustomBeanFactory2.class.getName(), prime.getThreePrime().getCreatedByFactoryName());
145
146     // test returning an Interface
147
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 JavaDoc {
154     MapperIF myMapper = null;
155     try {
156
157       List JavaDoc mappingFiles = new ArrayList JavaDoc();
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 JavaDoc e) {
165       assertTrue("invalid exception", e.getMessage().indexOf("Duplicate Class Mapping Found") != -1);
166     }
167   }
168   
169   private void assertCommon(MapperIF mapper) throws Exception JavaDoc {
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 JavaDoc {
179     //TestObjectPrime top = (TestObjectPrime) eventMapper.map(TestDataFactory.getInputGeneralMappingTestObject(),
180
// TestObjectPrime.class);
181
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