added dozer tutorial maven project

This commit is contained in:
egimaben 2016-07-29 02:46:59 +03:00
parent 12e8296e65
commit 24dba2108a
16 changed files with 748 additions and 0 deletions

59
dozer-tutorial/pom.xml Normal file
View File

@ -0,0 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>dozer-tutorial</artifactId>
<version>1.0</version>
<name>Dozer</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,33 @@
package com.baeldung.dozer;
public class Dest {
private String name;
private int age;
public Dest() {
}
public Dest(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.dozer;
public class Dest2 {
private int id;
private int points;
public Dest2() {
}
public Dest2(int id, int points) {
super();
this.id = id;
this.points = points;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
@Override
public String toString() {
return "Dest2 [id=" + id + ", points=" + points + "]";
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.dozer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.dozer.CustomConverter;
import org.dozer.MappingException;
public class MyCustomConvertor implements CustomConverter {
@Override
public Object convert(Object dest, Object source, Class<?> arg2,
Class<?> arg3) {
if (source == null) {
return null;
}
if (source instanceof Personne3) {
Personne3 person = (Personne3) source;
Date date = new Date(person.getDtob());
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String isoDate = format.format(date);
return new Person3(person.getName(), isoDate);
} else if (source instanceof Person3) {
Person3 person = (Person3) source;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = null;
try {
date = format.parse(person.getDtob());
} catch (ParseException e) {
throw new MappingException("Converter MyCustomConvertor "
+ "used incorrectly:" + e.getMessage());
}
long timestamp = date.getTime();
return new Personne3(person.getName(), timestamp);
} else {
throw new MappingException("Converter MyCustomConvertor "
+ "used incorrectly. Arguments passed in were:" + dest
+ " and " + source);
}
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.dozer;
public class Person {
private String name;
private String nickname;
private int age;
public Person() {
}
public Person(String name, String nickname, int age) {
super();
this.name = name;
this.nickname = nickname;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.dozer;
public class Person2 {
private String name;
private String nickname;
private int age;
public Person2() {
}
public Person2(String name, String nickname, int age) {
super();
this.name = name;
this.nickname = nickname;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.dozer;
public class Person3 {
private String name;
private String dtob;
public Person3() {
}
public Person3(String name, String dtob) {
super();
this.name = name;
this.dtob = dtob;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDtob() {
return dtob;
}
public void setDtob(String dtob) {
this.dtob = dtob;
}
@Override
public String toString() {
return "Person3 [name=" + name + ", dtob=" + dtob + "]";
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.dozer;
public class Personne {
private String nom;
private String surnom;
private int age;
public Personne() {
}
public Personne(String nom, String surnom, int age) {
super();
this.nom = nom;
this.surnom = surnom;
this.age = age;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getSurnom() {
return surnom;
}
public void setSurnom(String surnom) {
this.surnom = surnom;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.dozer;
import org.dozer.Mapping;
public class Personne2 {
private String nom;
private String surnom;
private int age;
public Personne2() {
}
public Personne2(String nom, String surnom, int age) {
super();
this.nom = nom;
this.surnom = surnom;
this.age = age;
}
@Mapping("name")
public String getNom() {
return nom;
}
@Mapping("nickname")
public String getSurnom() {
return surnom;
}
public void setNom(String nom) {
this.nom = nom;
}
public void setSurnom(String surnom) {
this.surnom = surnom;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.dozer;
public class Personne3 {
private String name;
private long dtob;
public Personne3() {
}
public Personne3(String name, long dtob) {
super();
this.name = name;
this.dtob = dtob;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getDtob() {
return dtob;
}
public void setDtob(long dtob) {
this.dtob = dtob;
}
@Override
public String toString() {
return "Personne3 [name=" + name + ", dtob=" + dtob + "]";
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.dozer;
public class Source {
private String name;
private int age;
public Source() {
}
public Source(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.dozer;
public class Source2 {
private String id;
private double points;
public Source2() {
}
public Source2(String id, double points) {
super();
this.id = id;
this.points = points;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getPoints() {
return points;
}
public void setPoints(double points) {
this.points = points;
}
@Override
public String toString() {
return "Source2 [id=" + id + ", points=" + points + "]";
}
}

View File

@ -0,0 +1,199 @@
package com.baeldung.dozer;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.dozer.loader.api.BeanMappingBuilder;
import org.junit.Before;
import org.junit.Test;
public class DozerTest {
DozerBeanMapper mapper = new DozerBeanMapper();
@Before
public void before() throws Exception {
mapper = new DozerBeanMapper();
}
BeanMappingBuilder builder = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(Person.class, Personne.class).fields("name", "nom").fields(
"nickname", "surnom");
}
};
BeanMappingBuilder builderMinusAge = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(Person.class, Personne.class).fields("name", "nom")
.fields("nickname", "surnom").exclude("age");
}
};
@Test
public void givenApiMapper_whenMaps_thenCorrect() {
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
70);
mapper.addMapping(builder);
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
assertEquals(englishAppPerson.getNickname(),
frenchAppPerson.getSurnom());
assertEquals(englishAppPerson.getAge(), frenchAppPerson.getAge());
}
@Test
public void givenApiMapper_whenMapsOnlySpecifiedFields_thenCorrect() {
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
mapper.addMapping(builderMinusAge);
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
assertEquals(frenchAppPerson.getSurnom(),
englishAppPerson.getNickname());
assertEquals(frenchAppPerson.getAge(), 0);
}
@Test
public void givenApiMapper_whenMapsBidirectionally_thenCorrect() {
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
mapper.addMapping(builder);
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
assertEquals(frenchAppPerson.getSurnom(),
englishAppPerson.getNickname());
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
}
@Test
public void givenSourceObjectAndDestClass_whenMapsSameNameFieldsCorrectly_thenCorrect() {
Source source = new Source("Baeldung", 10);
Dest dest = mapper.map(source, Dest.class);
assertEquals(dest.getName(), "Baeldung");
assertEquals(dest.getAge(), 10);
}
@Test
public void givenSourceObjectAndDestObject_whenMapsSameNameFieldsCorrectly_thenCorrect() {
Source source = new Source("Baeldung", 10);
Dest dest = new Dest();
mapper.map(source, dest);
assertEquals(dest.getName(), "Baeldung");
assertEquals(dest.getAge(), 10);
}
@Test
public void givenSourceAndDestWithDifferentFieldTypes_whenMapsAndAutoConverts_thenCorrect() {
Source2 source = new Source2("320", 15.2);
Dest2 dest = mapper.map(source, Dest2.class);
assertEquals(dest.getId(), 320);
assertEquals(dest.getPoints(), 15);
}
@Test
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMaps_thenCorrect() {
List<String> mappingFiles = new ArrayList<>();
mappingFiles.add("dozer_mapping.xml");
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
70);
mapper.setMappingFiles(mappingFiles);
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
assertEquals(englishAppPerson.getNickname(),
frenchAppPerson.getSurnom());
assertEquals(englishAppPerson.getAge(), frenchAppPerson.getAge());
}
@Test
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMapsBidirectionally_thenCorrect() {
List<String> mappingFiles = new ArrayList<>();
mappingFiles.add("dozer_mapping.xml");
Person englishAppPerson = new Person("Dwayne Johnson", "The Rock", 44);
mapper.setMappingFiles(mappingFiles);
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
assertEquals(frenchAppPerson.getSurnom(),
englishAppPerson.getNickname());
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
}
// @Test
// public void givenMappingFileOutsideClasspath_whenMaps_thenCorrect() {
// List<String> mappingFiles = new ArrayList<>();
// mappingFiles.add("file:E:\\dozer_mapping.xml");
// Person englishAppPerson = new Person("Marshall Bruce Mathers III",
// "Eminem", 43);
// mapper.setMappingFiles(mappingFiles);
// Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
// assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
// assertEquals(frenchAppPerson.getSurnom(),
// englishAppPerson.getNickname());
// assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
// }
@Test
public void givenSrcAndDest_whenMapsOnlySpecifiedFields_thenCorrect() {
List<String> mappingFiles = new ArrayList<>();
mappingFiles.add("dozer_mapping2.xml");
Person englishAppPerson = new Person("Shawn Corey Carter", "Jay Z", 46);
mapper.setMappingFiles(mappingFiles);
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
assertEquals(frenchAppPerson.getSurnom(),
englishAppPerson.getNickname());
assertEquals(frenchAppPerson.getAge(), 0);
}
@Test
public void givenAnnotatedSrcFields_whenMapsToRightDestField_thenCorrect() {
Person2 englishAppPerson = new Person2("Jean-Claude Van Damme", "JCVD",
55);
Personne2 frenchAppPerson = mapper.map(englishAppPerson,
Personne2.class);
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
assertEquals(frenchAppPerson.getSurnom(),
englishAppPerson.getNickname());
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
}
@Test
public void givenAnnotatedSrcFields_whenMapsToRightDestFieldBidirectionally_thenCorrect() {
Personne2 frenchAppPerson = new Personne2("Jason Statham",
"transporter", 49);
Person2 englishAppPerson = mapper.map(frenchAppPerson, Person2.class);
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
assertEquals(englishAppPerson.getNickname(),
frenchAppPerson.getSurnom());
assertEquals(englishAppPerson.getAge(), frenchAppPerson.getAge());
}
@Test
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvert_thenCorrect() {
String dateTime = "2007-06-26T21:22:39Z";
long timestamp = new Long("1182882159000");
Person3 person = new Person3("Rich", dateTime);
mapper.setMappingFiles(Arrays
.asList(new String[] { "dozer_custom_convertor.xml" }));
Personne3 person0 = mapper.map(person, Personne3.class);
assertEquals(timestamp, person0.getDtob());
}
@Test
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvertBidirectionally_thenCorrect() {
String dateTime = "2007-06-26T21:22:39Z";
long timestamp = new Long("1182882159000");
Personne3 person = new Personne3("Rich", timestamp);
mapper.setMappingFiles(Arrays
.asList(new String[] { "dozer_custom_convertor.xml" }));
Person3 person0 = mapper.map(person, Person3.class);
assertEquals(dateTime, person0.getDtob());
}
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
<custom-converters>
<converter type="com.baeldung.dozer.MyCustomConvertor">
<class-a>com.baeldung.dozer.Personne3</class-a>
<class-b>com.baeldung.dozer.Person3</class-b>
</converter>
</custom-converters>
</configuration>
</mappings>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<mapping>
<class-a>com.baeldung.dozer.Personne</class-a>
<class-b>com.baeldung.dozer.Person</class-b>
<field>
<a>nom</a>
<b>name</b>
</field>
<field>
<a>surnom</a>
<b>nickname</b>
</field>
</mapping>
</mappings>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<mapping wildcard="false">
<class-a>com.baeldung.dozer.Personne</class-a>
<class-b>com.baeldung.dozer.Person</class-b>
<field>
<a>nom</a>
<b>name</b>
</field>
<field>
<a>surnom</a>
<b>nickname</b>
</field>
</mapping>
</mappings>