Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -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>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
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 {
|
||||
|
||||
private DozerBeanMapper mapper = new DozerBeanMapper();
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
mapper = new DozerBeanMapper();
|
||||
}
|
||||
|
||||
private BeanMappingBuilder builder = new BeanMappingBuilder() {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
mapping(Person.class, Personne.class).fields("name", "nom").fields(
|
||||
"nickname", "surnom");
|
||||
|
||||
}
|
||||
};
|
||||
private 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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
|
||||
public class RemoteServiceSimulator {
|
||||
|
||||
public String checkSomething(final long timeout) throws InterruptedException {
|
||||
|
||||
System.out.print(String.format("Waiting %sms. ", timeout));
|
||||
|
||||
// to simulate a real world delay in processing.
|
||||
Thread.sleep(timeout);
|
||||
|
||||
return "Done waiting.";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>immutables</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.immutables</groupId>
|
||||
<artifactId>value</artifactId>
|
||||
<version>2.2.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.5.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mutabilitydetector</groupId>
|
||||
<artifactId>MutabilityDetector</artifactId>
|
||||
<version>0.9.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public interface Address {
|
||||
String getStreetName();
|
||||
Integer getNumber();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public abstract class Person {
|
||||
abstract String getName();
|
||||
abstract Integer getAge();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.immutable.auxiliary;
|
||||
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public abstract class Person {
|
||||
abstract String getName();
|
||||
abstract Integer getAge();
|
||||
|
||||
@Value.Auxiliary
|
||||
abstract String getAuxiliaryField();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.immutable.default_;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable(prehash = true)
|
||||
public abstract class Person {
|
||||
|
||||
abstract String getName();
|
||||
|
||||
@Value.Default
|
||||
Integer getAge() {
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.immutable.parameter;
|
||||
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public abstract class Person {
|
||||
|
||||
@Value.Parameter
|
||||
abstract String getName();
|
||||
|
||||
@Value.Parameter
|
||||
abstract Integer getAge();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable.prehash;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable(prehash = true)
|
||||
public abstract class Person {
|
||||
abstract String getName();
|
||||
abstract Integer getAge();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable;
|
||||
|
||||
public class ImmutablePersonTest {
|
||||
|
||||
@Test
|
||||
public void whenModifying_shouldCreateNewInstance() throws Exception {
|
||||
final ImmutablePerson john = ImmutablePerson.builder()
|
||||
.age(42)
|
||||
.name("John")
|
||||
.build();
|
||||
|
||||
final ImmutablePerson john43 = john.withAge(43);
|
||||
|
||||
assertThat(john)
|
||||
.isNotSameAs(john43);
|
||||
|
||||
assertThat(john.getAge())
|
||||
.isEqualTo(42);
|
||||
|
||||
assertImmutable(ImmutablePerson.class);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.immutable.auxiliary;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ImmutablePersonAuxiliaryTest {
|
||||
|
||||
@Test
|
||||
public void whenComparing_shouldIgnore() throws Exception {
|
||||
final ImmutablePerson john1 = ImmutablePerson.builder()
|
||||
.name("John")
|
||||
.age(42)
|
||||
.auxiliaryField("Value1")
|
||||
.build();
|
||||
|
||||
final ImmutablePerson john2 = ImmutablePerson.builder()
|
||||
.name("John")
|
||||
.age(42)
|
||||
.auxiliaryField("Value2")
|
||||
.build();
|
||||
|
||||
|
||||
assertThat(john1.equals(john2))
|
||||
.isTrue();
|
||||
|
||||
assertThat(john1.toString())
|
||||
.isEqualTo(john2.toString());
|
||||
|
||||
assertThat(john1.hashCode())
|
||||
.isEqualTo(john2.hashCode());
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.immutable.default_;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ImmutablePersonDefaultTest {
|
||||
|
||||
@Test
|
||||
public void whenInstantiating_shouldUseDefaultValue() throws Exception {
|
||||
|
||||
final ImmutablePerson john = ImmutablePerson.builder().name("John").build();
|
||||
|
||||
assertThat(john.getAge()).isEqualTo(42);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@
|
||||
<module>guava19</module>
|
||||
<module>handling-spring-static-resources</module>
|
||||
<module>httpclient</module>
|
||||
<module>immutables</module>
|
||||
<module>jackson</module>
|
||||
<module>javaxval</module>
|
||||
<module>jjwt</module>
|
||||
@@ -55,7 +56,7 @@
|
||||
<module>spring-autowire</module>
|
||||
<module>spring-batch</module>
|
||||
<module>spring-boot</module>
|
||||
<module>spring-controller</module>
|
||||
<module>spring-cucumber</module>
|
||||
<module>spring-data-cassandra</module>
|
||||
<module>spring-data-couchbase-2</module>
|
||||
<module>spring-data-couchbase-2b</module>
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src/main/webapp"/>
|
||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
|
||||
<attributes>
|
||||
<attribute name="hide" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
|
||||
<classpathentry kind="output" path=""/>
|
||||
</classpath>
|
||||
@@ -1,95 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
|
||||
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
|
||||
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
|
||||
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
|
||||
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
|
||||
org.eclipse.jdt.core.compiler.problem.deadCode=warning
|
||||
org.eclipse.jdt.core.compiler.problem.deprecation=warning
|
||||
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.fieldHiding=error
|
||||
org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
|
||||
org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
|
||||
org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
|
||||
org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.localVariableHiding=error
|
||||
org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
|
||||
org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
|
||||
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
|
||||
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
|
||||
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
|
||||
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
|
||||
org.eclipse.jdt.core.compiler.problem.nullReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
|
||||
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
|
||||
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
|
||||
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
|
||||
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
|
||||
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
|
||||
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error
|
||||
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
|
||||
org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
|
||||
org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
|
||||
org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
|
||||
org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
|
||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
|
||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.unusedImport=warning
|
||||
org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
|
||||
org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
|
||||
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore
|
||||
org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
|
||||
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
|
||||
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
|
||||
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user