Advertisement
lharpf

Dozer mapping example (same mapping with XML and API)

Jul 16th, 2014
1,596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 KB | None | 0 0
  1. package com.acelvia.dozermappingexample;
  2.  
  3. import org.dozer.DozerBeanMapper;
  4. import org.dozer.Mapper;
  5. import org.dozer.loader.api.BeanMappingBuilder;
  6. import org.dozer.loader.api.TypeMappingOptions;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9.  
  10. import java.io.ByteArrayInputStream;
  11. import java.util.Date;
  12.  
  13. import static org.junit.Assert.assertEquals;
  14.  
  15. /**
  16.  * An example of doing the same Dozer mapping with API and XML.
  17.  * @see <a href="http://stackoverflow.com/questions/24619653/dozer-api-date-mapping-config-without-xml">Dozer API Date Mapping config without XML</a>
  18.  */
  19. public class ApiAndXmlMappingTest {
  20.     private Mapper apiMapper;
  21.     private Mapper xmlMapper;
  22.  
  23.     @Before
  24.     public void setUp() {
  25.         apiMapper = buildDozerWithApiMapping();
  26.         xmlMapper = buildDozerWithXMLMapping();
  27.     }
  28.  
  29.     /**
  30.      * @return A Dozer instance created using the API mapping style. Note
  31.      *         the duplicating of {@code TypeMappingOptions.dateFormat(dateFormat)},
  32.      *         configuration element cannot be represented with API mappings in 5.5.1.
  33.      */
  34.     private Mapper buildDozerWithApiMapping() {
  35.         BeanMappingBuilder mappingBuilder = new BeanMappingBuilder() {
  36.             @Override
  37.             protected void configure() {
  38.  
  39.                 String dateFormat = "MM/dd/yyyy HH:mm";
  40.  
  41.                 mapping(TestObject.class, TestObjectPrime.class,
  42.                         TypeMappingOptions.wildcard(true),
  43.                         TypeMappingOptions.dateFormat(dateFormat))
  44.                         .fields("dateString", "dateObject");
  45.  
  46.                 mapping(SomeObject.class, SomeOtherObject.class,
  47.                         TypeMappingOptions.dateFormat(dateFormat))
  48.                         .fields("srcField", "destField");
  49.             }
  50.         };
  51.  
  52.         DozerBeanMapper apiBeanMapper = new DozerBeanMapper();
  53.         apiBeanMapper.addMapping(mappingBuilder);
  54.         return apiBeanMapper;
  55.     }
  56.  
  57.     /**
  58.      * @return A Dozer instance created using the XML mapping style. Typically you
  59.      *         would have the XML in a separate file in the resources directory,
  60.      *         it is included here just to fit this example in a single file.
  61.      */
  62.     private Mapper buildDozerWithXMLMapping() {
  63.         String configurationXml =
  64.                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  65.                         "<mappings xmlns=\"http://dozer.sourceforge.net\"\n" +
  66.                         "          xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
  67.                         "          xsi:schemaLocation=\"http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd\">\n" +
  68.                         "  <configuration>\n" +
  69.                         "    <date-format>MM/dd/yyyy HH:mm</date-format>\n" +
  70.                         "  </configuration>\n" +
  71.                         "\n" +
  72.                         "  <mapping wildcard=\"true\">\n" +
  73.                         "    <class-a>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$TestObject</class-a>\n" +
  74.                         "    <class-b>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$TestObjectPrime</class-b>\n" +
  75.                         "    <field>\n" +
  76.                         "      <a>dateString</a>\n" +
  77.                         "      <b>dateObject</b>\n" +
  78.                         "    </field>\n" +
  79.                         "  </mapping>\n" +
  80.                         "  <mapping>\n" +
  81.                         "    <class-a>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$SomeObject</class-a>\n" +
  82.                         "    <class-b>com.acelvia.dozermappingexample.ApiAndXmlMappingTest$SomeOtherObject</class-b>\n" +
  83.                         "    <field>\n" +
  84.                         "      <a>srcField</a>\n" +
  85.                         "      <b>destField</b>\n" +
  86.                         "    </field>\n" +
  87.                         "  </mapping>\n" +
  88.                         "</mappings>";
  89.  
  90.         DozerBeanMapper xmlBeanMapper = new DozerBeanMapper();
  91.         xmlBeanMapper.addMapping(new ByteArrayInputStream(configurationXml.getBytes()));
  92.         return xmlBeanMapper;
  93.     }
  94.  
  95.     @Test
  96.     public void mapWithAPIAndXML() {
  97.         // Create instances of the test objects
  98.         TestObject testObject = new TestObject();
  99.         testObject.setDateString("07/16/2014 11:22");
  100.  
  101.         SomeObject someObject = new SomeObject();
  102.         someObject.setSrcField("exampleString");
  103.  
  104.         // Assert that both API and XML mappings produce the same values in the fields
  105.         assertEquals(
  106.                 apiMapper.map(testObject, TestObjectPrime.class).getDateObject(),
  107.                 xmlMapper.map(testObject, TestObjectPrime.class).getDateObject()
  108.         );
  109.  
  110.         assertEquals(
  111.                 apiMapper.map(someObject, SomeOtherObject.class).getDestField(),
  112.                 xmlMapper.map(someObject, SomeOtherObject.class).getDestField()
  113.         );
  114.     }
  115.  
  116.     // Objects for use in the tests. Some setters & getters look unused, but Dozer
  117.     // needs them (we haven't configured field access).
  118.  
  119.     public static class TestObject {
  120.         private String dateString;
  121.  
  122.         @SuppressWarnings("unused")
  123.         public String getDateString() {
  124.             return dateString;
  125.         }
  126.  
  127.         public void setDateString(String dateString) {
  128.             this.dateString = dateString;
  129.         }
  130.     }
  131.  
  132.     public static class TestObjectPrime {
  133.         private Date dateObject;
  134.  
  135.         public Date getDateObject() {
  136.             return dateObject;
  137.         }
  138.  
  139.         @SuppressWarnings("unused")
  140.         public void setDateObject(Date dateObject) {
  141.             this.dateObject = dateObject;
  142.         }
  143.     }
  144.  
  145.     public static class SomeObject {
  146.         private String srcField;
  147.  
  148.         @SuppressWarnings("unused")
  149.         public String getSrcField() {
  150.             return srcField;
  151.         }
  152.  
  153.         public void setSrcField(String srcField) {
  154.             this.srcField = srcField;
  155.         }
  156.     }
  157.  
  158.     public static class SomeOtherObject {
  159.         private String destField;
  160.  
  161.         public String getDestField() {
  162.             return destField;
  163.         }
  164.  
  165.         @SuppressWarnings("unused")
  166.         public void setDestField(String destField) {
  167.             this.destField = destField;
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement