Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.acelvia.dozermappingexample;
- import org.dozer.DozerBeanMapper;
- import org.dozer.Mapper;
- import org.dozer.loader.api.BeanMappingBuilder;
- import org.dozer.loader.api.TypeMappingOptions;
- import org.junit.Before;
- import org.junit.Test;
- import java.io.ByteArrayInputStream;
- import java.util.Date;
- import static org.junit.Assert.assertEquals;
- /**
- * An example of doing the same Dozer mapping with API and XML.
- * @see <a href="http://stackoverflow.com/questions/24619653/dozer-api-date-mapping-config-without-xml">Dozer API Date Mapping config without XML</a>
- */
- public class ApiAndXmlMappingTest {
- private Mapper apiMapper;
- private Mapper xmlMapper;
- @Before
- public void setUp() {
- apiMapper = buildDozerWithApiMapping();
- xmlMapper = buildDozerWithXMLMapping();
- }
- /**
- * @return A Dozer instance created using the API mapping style. Note
- * the duplicating of {@code TypeMappingOptions.dateFormat(dateFormat)},
- * configuration element cannot be represented with API mappings in 5.5.1.
- */
- private Mapper buildDozerWithApiMapping() {
- BeanMappingBuilder mappingBuilder = new BeanMappingBuilder() {
- @Override
- protected void configure() {
- String dateFormat = "MM/dd/yyyy HH:mm";
- mapping(TestObject.class, TestObjectPrime.class,
- TypeMappingOptions.wildcard(true),
- TypeMappingOptions.dateFormat(dateFormat))
- .fields("dateString", "dateObject");
- mapping(SomeObject.class, SomeOtherObject.class,
- TypeMappingOptions.dateFormat(dateFormat))
- .fields("srcField", "destField");
- }
- };
- DozerBeanMapper apiBeanMapper = new DozerBeanMapper();
- apiBeanMapper.addMapping(mappingBuilder);
- return apiBeanMapper;
- }
- /**
- * @return A Dozer instance created using the XML mapping style. Typically you
- * would have the XML in a separate file in the resources directory,
- * it is included here just to fit this example in a single file.
- */
- private Mapper buildDozerWithXMLMapping() {
- String configurationXml =
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
- "<mappings xmlns=\"http://dozer.sourceforge.net\"\n" +
- " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
- " xsi:schemaLocation=\"http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd\">\n" +
- " <configuration>\n" +
- " <date-format>MM/dd/yyyy HH:mm</date-format>\n" +
- " </configuration>\n" +
- "\n" +
- " <mapping wildcard=\"true\">\n" +
- " <class-a>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$TestObject</class-a>\n" +
- " <class-b>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$TestObjectPrime</class-b>\n" +
- " <field>\n" +
- " <a>dateString</a>\n" +
- " <b>dateObject</b>\n" +
- " </field>\n" +
- " </mapping>\n" +
- " <mapping>\n" +
- " <class-a>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$SomeObject</class-a>\n" +
- " <class-b>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$SomeOtherObject</class-b>\n" +
- " <field>\n" +
- " <a>srcField</a>\n" +
- " <b>destField</b>\n" +
- " </field>\n" +
- " </mapping>\n" +
- "</mappings>";
- DozerBeanMapper xmlBeanMapper = new DozerBeanMapper();
- xmlBeanMapper.addMapping(new ByteArrayInputStream(configurationXml.getBytes()));
- return xmlBeanMapper;
- }
- @Test
- public void mapWithAPIAndXML() {
- // Create instances of the test objects
- TestObject testObject = new TestObject();
- testObject.setDateString("07/16/2014 11:22");
- SomeObject someObject = new SomeObject();
- someObject.setSrcField("exampleString");
- // Assert that both API and XML mappings produce the same values in the fields
- assertEquals(
- apiMapper.map(testObject, TestObjectPrime.class).getDateObject(),
- xmlMapper.map(testObject, TestObjectPrime.class).getDateObject()
- );
- assertEquals(
- apiMapper.map(someObject, SomeOtherObject.class).getDestField(),
- xmlMapper.map(someObject, SomeOtherObject.class).getDestField()
- );
- }
- // Objects for use in the tests. Some setters & getters look unused, but Dozer
- // needs them (we haven't configured field access).
- public static class TestObject {
- private String dateString;
- @SuppressWarnings("unused")
- public String getDateString() {
- return dateString;
- }
- public void setDateString(String dateString) {
- this.dateString = dateString;
- }
- }
- public static class TestObjectPrime {
- private Date dateObject;
- public Date getDateObject() {
- return dateObject;
- }
- @SuppressWarnings("unused")
- public void setDateObject(Date dateObject) {
- this.dateObject = dateObject;
- }
- }
- public static class SomeObject {
- private String srcField;
- @SuppressWarnings("unused")
- public String getSrcField() {
- return srcField;
- }
- public void setSrcField(String srcField) {
- this.srcField = srcField;
- }
- }
- public static class SomeOtherObject {
- private String destField;
- public String getDestField() {
- return destField;
- }
- @SuppressWarnings("unused")
- public void setDestField(String destField) {
- this.destField = destField;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement