1 package org.cateproject.service;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.InputStreamReader;
6 import java.lang.reflect.Field;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.UUID;
10
11 import javax.xml.transform.stream.StreamSource;
12
13 import net.sf.dozer.util.mapping.MapperIF;
14
15 import org.junit.Before;
16 import org.junit.Ignore;
17 import org.junit.Test;
18 import org.springframework.oxm.jaxb.Jaxb2Marshaller;
19 import org.springframework.util.ReflectionUtils;
20 import org.unitils.dbunit.annotation.DataSet;
21 import org.unitils.spring.annotation.SpringBeanByName;
22 import org.unitils.spring.annotation.SpringBeanByType;
23
24 import eu.etaxonomy.cdm.api.service.IAgentService;
25 import eu.etaxonomy.cdm.api.service.INameService;
26 import eu.etaxonomy.cdm.model.agent.Person;
27 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
28 import eu.etaxonomy.cdm.model.name.BotanicalName;
29 import eu.etaxonomy.cdm.model.name.NonViralName;
30 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
31
32 public class DozerMappingTest extends CdmTransactionalIntegrationTest {
33
34 @SpringBeanByType
35 private IAgentService service;
36
37 @SpringBeanByType
38 private INameService nameService;
39
40 @SpringBeanByType
41 private Jaxb2Marshaller marshaller;
42
43 @SpringBeanByName
44 private MapperIF fieldMapper;
45
46 @SpringBeanByName
47 private MapperIF collectionMapper;
48
49 private UUID uuid;
50 private UUID nameUuid;
51 private String resource1 = "/org/cateproject/service/DozerMappingTest1.xml";
52
53 private String resource2 = "/org/cateproject/service/DozerMappingTest2.xml";
54
55 private String resource3 = "/org/cateproject/service/DozerMappingTest3.xml";
56
57 @Before
58 public void setUp() {
59
60 uuid = UUID.fromString("bbb450c0-0e2c-11de-8c30-0800200c9a66");
61 nameUuid = UUID.fromString("55ec0830-0e2e-11de-8c30-0800200c9a66");
62
63 }
64
65 @Test
66 @DataSet("DozerMappingTest.testMapBean.xml")
67 public void testMapping1() {
68 List<String> propertyPaths = new ArrayList<String>();
69 propertyPaths.add("$");
70 Person person1 = (Person)service.load(uuid,propertyPaths);
71 assert person1 != null : "Person must not be null";
72 setComplete();
73 endTransaction();
74 startNewTransaction();
75
76 StreamSource source = new StreamSource(new InputStreamReader(this.getClass().getResourceAsStream(resource1)));
77 Person person2 = (Person)marshaller.unmarshal(source);
78 assert person2 != null : "Person must not be null";
79 printPerson(person1, "Before Mapping", false);
80 printPerson(person2, "Incoming Object", false);
81
82 fieldMapper.map(person2, person1);
83 printPerson(person1, "After Field Mapping",false);
84 collectionMapper.map(person2, person1);
85 service.saveOrUpdate(person1);
86
87 setComplete();
88 endTransaction();
89 printPerson(person1, "After Mapping",true);
90 }
91
92 @Test
93 @DataSet("DozerMappingTest.testMapBean.xml")
94 public void testMapping2() {
95 List<String> propertyPaths = new ArrayList<String>();
96 propertyPaths.add("$");
97 Person person1 = (Person)service.load(uuid,propertyPaths);
98 assert person1 != null : "Person must not be null";
99 setComplete();
100 endTransaction();
101 startNewTransaction();
102
103 StreamSource source = new StreamSource(new InputStreamReader(this.getClass().getResourceAsStream(resource2)));
104 Person person2 = (Person)marshaller.unmarshal(source);
105 assert person2 != null : "Person must not be null";
106 printPerson(person1, "Before Mapping",false);
107 printPerson(person2, "Incoming Object",false);
108
109 fieldMapper.map(person2, person1);
110 collectionMapper.map(person2, person1);
111 service.saveOrUpdate(person1);
112
113 setComplete();
114 endTransaction();
115 printPerson(person1, "After Mapping",true);
116 }
117
118 @Ignore
119 @Test
120 @DataSet("DozerMappingTest.testMapBean.xml")
121 public void testMapping3() {
122 List<String> propertyPaths = new ArrayList<String>();
123 propertyPaths.add("$");
124 BotanicalName name1 = (BotanicalName)nameService.load(nameUuid,propertyPaths);
125 assert name1 != null : "Name must not be null";
126 setComplete();
127 endTransaction();
128 startNewTransaction();
129 printName(name1, "Before Mapping",false);
130
131 StreamSource source = new StreamSource(new InputStreamReader(this.getClass().getResourceAsStream(resource3)));
132 BotanicalName name2 = (BotanicalName)marshaller.unmarshal(source);
133 assert name2 != null : "Name must not be null";
134 printName(name2, "Incoming Object",false);
135
136 fieldMapper.map(name2, name1);
137 nameService.merge(name1);
138
139 setComplete();
140 endTransaction();
141 printName(name1, "After Mapping",true);
142 try {
143 this.printDataSet(new FileOutputStream("test.xml"));
144 } catch (FileNotFoundException e) {
145
146 e.printStackTrace();
147 }
148
149 }
150
151 private void printName(NonViralName name, String title, boolean collections) {
152 printAnnotatableEntity(name,title,collections);
153 System.out.println("\tBasionymAuthorTeam : " + name.getBasionymAuthorTeam());
154 System.out.println("\tNomenclaturalReference : " + name.getNomenclaturalReference());
155 System.out.println("\tNomenclaturalMicroReference : " + name.getNomenclaturalMicroReference());
156 System.out.println("\tGenusOrUninomial : " + name.getGenusOrUninomial());
157 System.out.println("\tSpecificEpithet : " + name.getSpecificEpithet());
158 System.out.println("\tInfraSpecificEpithet : " + name.getInfraSpecificEpithet());
159 }
160
161 private void printPerson(Person person, String title, boolean collections) {
162 printAnnotatableEntity(person,title,collections);
163 System.out.println("\tLifespan : " + person.getLifespan());
164 }
165
166 private void printAnnotatableEntity(AnnotatableEntity annotatableEntity, String title, boolean collections) {
167 System.out.println("==== " + title + " ====");
168 System.out.println("\tId : " + annotatableEntity.getId());
169 System.out.println("\tUuid : " + annotatableEntity.getUuid());
170 System.out.println("\tCreated : " + annotatableEntity.getCreated());
171 System.out.println("\tCreatedBy : " + annotatableEntity.getCreatedBy());
172
173 if(collections) {
174 System.out.println("\tAnnotations : " + annotatableEntity.getAnnotations());
175 } else {
176 Field f = ReflectionUtils.findField(AnnotatableEntity.class, "annotations");
177 f.setAccessible(true);
178 try {
179 System.out.println("\tAnnotations : " + f.get(annotatableEntity));
180 } catch (IllegalArgumentException e) {
181
182 e.printStackTrace();
183 } catch (IllegalAccessException e) {
184
185 e.printStackTrace();
186 }
187 }
188 }
189
190 }