|
|
@ -24,371 +24,344 @@ import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
|
|
public class OrikaTest {
|
|
|
|
public class OrikaTest {
|
|
|
|
MapperFactory mapperFactory;
|
|
|
|
MapperFactory mapperFactory;
|
|
|
|
CustomMapper<Personne3, Person3> customMapper;
|
|
|
|
CustomMapper<Personne3, Person3> customMapper;
|
|
|
|
// constant to help us cover time zone differences
|
|
|
|
// constant to help us cover time zone differences
|
|
|
|
private final long GMT_DIFFERENCE = 46800000;
|
|
|
|
private final long GMT_DIFFERENCE = 46800000;
|
|
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
@Before
|
|
|
|
public void before() {
|
|
|
|
public void before() {
|
|
|
|
mapperFactory = new DefaultMapperFactory.Builder().build();
|
|
|
|
mapperFactory = new DefaultMapperFactory.Builder().build();
|
|
|
|
customMapper = new CustomMapper<Personne3, Person3>() {
|
|
|
|
customMapper = new PersonCustomMapper();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Test
|
|
|
|
public void mapAtoB(Personne3 a, Person3 b, MappingContext context) {
|
|
|
|
public void givenSrcAndDest_whenMaps_thenCorrect() {
|
|
|
|
Date date = new Date(a.getDtob());
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class);
|
|
|
|
DateFormat format = new SimpleDateFormat(
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
|
|
Source src = new Source("Baeldung", 10);
|
|
|
|
String isoDate = format.format(date);
|
|
|
|
Dest dest = mapper.map(src, Dest.class);
|
|
|
|
b.setDtob(isoDate);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
}
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Test
|
|
|
|
public void mapBtoA(Person3 b, Personne3 a, MappingContext context) {
|
|
|
|
public void givenSrcAndDest_whenMapsReverse_thenCorrect() {
|
|
|
|
DateFormat format = new SimpleDateFormat(
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Date date = null;
|
|
|
|
Dest src = new Dest("Baeldung", 10);
|
|
|
|
try {
|
|
|
|
Source dest = mapper.map(src, Source.class);
|
|
|
|
date = format.parse(b.getDtob());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
@Test
|
|
|
|
e.printStackTrace();
|
|
|
|
public void givenSrcAndDest_whenMapsByObject_thenCorrect() {
|
|
|
|
}
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
long timestamp = date.getTime();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
a.setDtob(timestamp);
|
|
|
|
Source src = new Source("Baeldung", 10);
|
|
|
|
}
|
|
|
|
Dest dest = new Dest();
|
|
|
|
|
|
|
|
mapper.map(src, dest);
|
|
|
|
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
@Test
|
|
|
|
}
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapper_thenCorrect() {
|
|
|
|
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
|
|
|
|
Source src = new Source("baeldung", 10);
|
|
|
|
|
|
|
|
Dest dest = boundMapper.map(src);
|
|
|
|
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcAndDest_whenMaps_thenCorrect() {
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapperInReverse_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class);
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
Source src = new Source("Baeldung", 10);
|
|
|
|
Dest src = new Dest("baeldung", 10);
|
|
|
|
Dest dest = mapper.map(src, Dest.class);
|
|
|
|
Source dest = boundMapper.mapReverse(src);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcAndDest_whenMapsReverse_thenCorrect() {
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapperByObject_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
Dest src = new Dest("Baeldung", 10);
|
|
|
|
Source src = new Source("baeldung", 10);
|
|
|
|
Source dest = mapper.map(src, Source.class);
|
|
|
|
Dest dest = new Dest();
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
boundMapper.map(src, dest);
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
}
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcAndDest_whenMapsByObject_thenCorrect() {
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapperByObjectInReverse_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
Source src = new Source("Baeldung", 10);
|
|
|
|
Dest src = new Dest("baeldung", 10);
|
|
|
|
Dest dest = new Dest();
|
|
|
|
Source dest = new Source();
|
|
|
|
mapper.map(src, dest);
|
|
|
|
boundMapper.mapReverse(src, dest);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapper_thenCorrect() {
|
|
|
|
public void givenSrcAndDestWithDifferentFieldNames_whenMaps_thenCorrect() {
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
.field("nom", "name").field("surnom", "nickname")
|
|
|
|
Source src = new Source("baeldung", 10);
|
|
|
|
.field("age", "age").register();
|
|
|
|
Dest dest = boundMapper.map(src);
|
|
|
|
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapperInReverse_thenCorrect() {
|
|
|
|
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
|
|
|
|
Dest src = new Dest("baeldung", 10);
|
|
|
|
|
|
|
|
Source dest = boundMapper.mapReverse(src);
|
|
|
|
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapperByObject_thenCorrect() {
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
|
|
|
|
Source src = new Source("baeldung", 10);
|
|
|
|
|
|
|
|
Dest dest = new Dest();
|
|
|
|
|
|
|
|
boundMapper.map(src, dest);
|
|
|
|
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
public void givenSrcAndDest_whenMapsUsingBoundMapperByObjectInReverse_thenCorrect() {
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
BoundMapperFacade<Source, Dest> boundMapper = mapperFactory
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
.getMapperFacade(Source.class, Dest.class);
|
|
|
|
|
|
|
|
Dest src = new Dest("baeldung", 10);
|
|
|
|
|
|
|
|
Source dest = new Source();
|
|
|
|
|
|
|
|
boundMapper.mapReverse(src, dest);
|
|
|
|
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
}
|
|
|
|
public void givenSrcAndDestWithDifferentFieldNames_whenMaps_thenCorrect() {
|
|
|
|
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
|
|
|
|
.field("nom", "name").field("surnom", "nickname")
|
|
|
|
|
|
|
|
.field("age", "age").register();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void givenBothDifferentAndSameFieldNames_whenFailsToMapSameNameFieldAutomatically_thenCorrect() {
|
|
|
|
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
|
|
|
|
.field("nom", "name").field("surnom", "nickname").register();
|
|
|
|
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
|
|
|
|
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
assertFalse(englishPerson.getAge() == frenchPerson.getAge());
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
}
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void givenBothDifferentAndSameFieldNames_whenMapsSameNameFieldByDefault_thenCorrect() {
|
|
|
|
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
|
|
|
|
.field("nom", "name").field("surnom", "nickname").byDefault()
|
|
|
|
|
|
|
|
.register();
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
public void givenBothDifferentAndSameFieldNames_whenFailsToMapSameNameFieldAutomatically_thenCorrect() {
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
|
|
|
|
.field("nom", "name").field("surnom", "nickname").register();
|
|
|
|
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
assertFalse(englishPerson.getAge() == frenchPerson.getAge());
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenBothDifferentAndSameFieldNames_whenMapsSameNameFieldByDefault_thenCorrect() {
|
|
|
|
public void givenUnidirectionalMappingSetup_whenMapsUnidirectionally_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
.field("nom", "name").field("surnom", "nickname").byDefault()
|
|
|
|
.fieldAToB("nom", "name").fieldAToB("surnom", "nickname")
|
|
|
|
.register();
|
|
|
|
.fieldAToB("age", "age").register();
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
|
|
|
|
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
|
|
|
|
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
}
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void givenUnidirectionalMappingSetup_whenMapsUnidirectionally_thenCorrect() {
|
|
|
|
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class)
|
|
|
|
|
|
|
|
.fieldAToB("nom", "name").fieldAToB("surnom", "nickname")
|
|
|
|
|
|
|
|
.fieldAToB("age", "age").register();
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcAndDest_whenCanExcludeField_thenCorrect() {
|
|
|
|
public void givenSrcAndDest_whenCanExcludeField_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class).exclude("nom")
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class).exclude("nom")
|
|
|
|
.field("surnom", "nickname").field("age", "age").register();
|
|
|
|
.field("surnom", "nickname").field("age", "age").register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
|
|
|
|
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(null, englishPerson.getName());
|
|
|
|
assertEquals(null, englishPerson.getName());
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSpecificConstructorToUse_whenMaps_thenCorrect() {
|
|
|
|
public void givenSpecificConstructorToUse_whenMaps_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class).constructorB()
|
|
|
|
mapperFactory.classMap(Personne.class, Person.class).constructorB()
|
|
|
|
.field("nom", "name").field("surnom", "nickname")
|
|
|
|
.field("nom", "name").field("surnom", "nickname")
|
|
|
|
.field("age", "age").register();
|
|
|
|
.field("age", "age").register();
|
|
|
|
;
|
|
|
|
;
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
Personne frenchPerson = new Personne("Claire", "cla", 25);
|
|
|
|
|
|
|
|
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
Person englishPerson = mapper.map(frenchPerson, Person.class);
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
assertEquals(englishPerson.getName(), frenchPerson.getNom());
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom());
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
assertEquals(englishPerson.getAge(), frenchPerson.getAge());
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithListAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() {
|
|
|
|
public void givenSrcWithListAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() {
|
|
|
|
mapperFactory.classMap(PersonNameList.class, PersonNameParts.class)
|
|
|
|
mapperFactory.classMap(PersonNameList.class, PersonNameParts.class)
|
|
|
|
.field("nameList[0]", "firstName")
|
|
|
|
.field("nameList[0]", "firstName")
|
|
|
|
.field("nameList[1]", "lastName").register();
|
|
|
|
.field("nameList[1]", "lastName").register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
List<String> nameList = Arrays.asList(new String[] { "Sylvester",
|
|
|
|
List<String> nameList = Arrays.asList(new String[]{"Sylvester",
|
|
|
|
"Stallone" });
|
|
|
|
"Stallone"});
|
|
|
|
PersonNameList src = new PersonNameList(nameList);
|
|
|
|
PersonNameList src = new PersonNameList(nameList);
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
assertEquals(dest.getFirstName(), "Sylvester");
|
|
|
|
assertEquals(dest.getFirstName(), "Sylvester");
|
|
|
|
assertEquals(dest.getLastName(), "Stallone");
|
|
|
|
assertEquals(dest.getLastName(), "Stallone");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithArrayAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() {
|
|
|
|
public void givenSrcWithArrayAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() {
|
|
|
|
mapperFactory.classMap(PersonNameArray.class, PersonNameParts.class)
|
|
|
|
mapperFactory.classMap(PersonNameArray.class, PersonNameParts.class)
|
|
|
|
.field("nameArray[0]", "firstName")
|
|
|
|
.field("nameArray[0]", "firstName")
|
|
|
|
.field("nameArray[1]", "lastName").register();
|
|
|
|
.field("nameArray[1]", "lastName").register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
String[] nameArray = new String[] { "Vin", "Diesel" };
|
|
|
|
String[] nameArray = new String[]{"Vin", "Diesel"};
|
|
|
|
PersonNameArray src = new PersonNameArray(nameArray);
|
|
|
|
PersonNameArray src = new PersonNameArray(nameArray);
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
assertEquals(dest.getFirstName(), "Vin");
|
|
|
|
assertEquals(dest.getFirstName(), "Vin");
|
|
|
|
assertEquals(dest.getLastName(), "Diesel");
|
|
|
|
assertEquals(dest.getLastName(), "Diesel");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithMapAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() {
|
|
|
|
public void givenSrcWithMapAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() {
|
|
|
|
mapperFactory.classMap(PersonNameMap.class, PersonNameParts.class)
|
|
|
|
mapperFactory.classMap(PersonNameMap.class, PersonNameParts.class)
|
|
|
|
.field("nameMap['first']", "firstName")
|
|
|
|
.field("nameMap['first']", "firstName")
|
|
|
|
.field("nameMap[\"last\"]", "lastName").register();
|
|
|
|
.field("nameMap[\"last\"]", "lastName").register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Map<String, String> nameMap = new HashMap<>();
|
|
|
|
Map<String, String> nameMap = new HashMap<>();
|
|
|
|
nameMap.put("first", "Leornado");
|
|
|
|
nameMap.put("first", "Leornado");
|
|
|
|
nameMap.put("last", "DiCaprio");
|
|
|
|
nameMap.put("last", "DiCaprio");
|
|
|
|
PersonNameMap src = new PersonNameMap(nameMap);
|
|
|
|
PersonNameMap src = new PersonNameMap(nameMap);
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
assertEquals(dest.getFirstName(), "Leornado");
|
|
|
|
assertEquals(dest.getFirstName(), "Leornado");
|
|
|
|
assertEquals(dest.getLastName(), "DiCaprio");
|
|
|
|
assertEquals(dest.getLastName(), "DiCaprio");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithNestedFields_whenMaps_thenCorrect() {
|
|
|
|
public void givenSrcWithNestedFields_whenMaps_thenCorrect() {
|
|
|
|
mapperFactory.classMap(PersonContainer.class, PersonNameParts.class)
|
|
|
|
mapperFactory.classMap(PersonContainer.class, PersonNameParts.class)
|
|
|
|
.field("name.firstName", "firstName")
|
|
|
|
.field("name.firstName", "firstName")
|
|
|
|
.field("name.lastName", "lastName").register();
|
|
|
|
.field("name.lastName", "lastName").register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
PersonContainer src = new PersonContainer(new Name("Nick", "Canon"));
|
|
|
|
PersonContainer src = new PersonContainer(new Name("Nick", "Canon"));
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
PersonNameParts dest = mapper.map(src, PersonNameParts.class);
|
|
|
|
assertEquals(dest.getFirstName(), "Nick");
|
|
|
|
assertEquals(dest.getFirstName(), "Nick");
|
|
|
|
assertEquals(dest.getLastName(), "Canon");
|
|
|
|
assertEquals(dest.getLastName(), "Canon");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithNullField_whenMapsThenCorrect() {
|
|
|
|
public void givenSrcWithNullField_whenMapsThenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Dest dest = mapper.map(src, Dest.class);
|
|
|
|
Dest dest = mapper.map(src, Dest.class);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithNullAndGlobalConfigForNoNull_whenFailsToMap_ThenCorrect() {
|
|
|
|
public void givenSrcWithNullAndGlobalConfigForNoNull_whenFailsToMap_ThenCorrect() {
|
|
|
|
MapperFactory mapperFactory = new DefaultMapperFactory.Builder()
|
|
|
|
MapperFactory mapperFactory = new DefaultMapperFactory.Builder()
|
|
|
|
.mapNulls(false).build();
|
|
|
|
.mapNulls(false).build();
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class);
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class);
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Dest dest = new Dest("Clinton", 55);
|
|
|
|
Dest dest = new Dest("Clinton", 55);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), "Clinton");
|
|
|
|
assertEquals(dest.getName(), "Clinton");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithNullAndLocalConfigForNoNull_whenFailsToMap_ThenCorrect() {
|
|
|
|
public void givenSrcWithNullAndLocalConfigForNoNull_whenFailsToMap_ThenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).field("age", "age")
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).field("age", "age")
|
|
|
|
.mapNulls(false).field("name", "name").byDefault().register();
|
|
|
|
.mapNulls(false).field("name", "name").byDefault().register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Dest dest = new Dest("Clinton", 55);
|
|
|
|
Dest dest = new Dest("Clinton", 55);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), "Clinton");
|
|
|
|
assertEquals(dest.getName(), "Clinton");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenDestWithNullReverseMappedToSource_whenMapsByDefault_thenCorrect() {
|
|
|
|
public void givenDestWithNullReverseMappedToSource_whenMapsByDefault_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).byDefault();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Dest src = new Dest(null, 10);
|
|
|
|
Dest src = new Dest(null, 10);
|
|
|
|
Source dest = new Source("Vin", 44);
|
|
|
|
Source dest = new Source("Vin", 44);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
assertEquals(dest.getName(), src.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenDestWithNullReverseMappedToSourceAndLocalConfigForNoNull_whenFailsToMap_thenCorrect() {
|
|
|
|
public void givenDestWithNullReverseMappedToSourceAndLocalConfigForNoNull_whenFailsToMap_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).field("age", "age")
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).field("age", "age")
|
|
|
|
.mapNullsInReverse(false).field("name", "name").byDefault()
|
|
|
|
.mapNullsInReverse(false).field("name", "name").byDefault()
|
|
|
|
.register();
|
|
|
|
.register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Dest src = new Dest(null, 10);
|
|
|
|
Dest src = new Dest(null, 10);
|
|
|
|
Source dest = new Source("Vin", 44);
|
|
|
|
Source dest = new Source("Vin", 44);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), "Vin");
|
|
|
|
assertEquals(dest.getName(), "Vin");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcWithNullAndFieldLevelConfigForNoNull_whenFailsToMap_ThenCorrect() {
|
|
|
|
public void givenSrcWithNullAndFieldLevelConfigForNoNull_whenFailsToMap_ThenCorrect() {
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).field("age", "age")
|
|
|
|
mapperFactory.classMap(Source.class, Dest.class).field("age", "age")
|
|
|
|
.fieldMap("name", "name").mapNulls(false).add().byDefault()
|
|
|
|
.fieldMap("name", "name").mapNulls(false).add().byDefault()
|
|
|
|
.register();
|
|
|
|
.register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Source src = new Source(null, 10);
|
|
|
|
Dest dest = new Dest("Clinton", 55);
|
|
|
|
Dest dest = new Dest("Clinton", 55);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
mapper.map(src, dest);
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getAge(), src.getAge());
|
|
|
|
assertEquals(dest.getName(), "Clinton");
|
|
|
|
assertEquals(dest.getName(), "Clinton");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void givenSrcAndDest_whenCustomMapperWorks_thenCorrect() {
|
|
|
|
public void givenSrcAndDest_whenCustomMapperWorks_thenCorrect() {
|
|
|
|
mapperFactory.classMap(Personne3.class, Person3.class)
|
|
|
|
mapperFactory.classMap(Personne3.class, Person3.class)
|
|
|
|
.customize(customMapper).register();
|
|
|
|
.customize(customMapper).register();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
long timestamp = new Long("1182882159000");
|
|
|
|
long timestamp = new Long("1182882159000");
|
|
|
|
Personne3 person0 = new Personne3("Leornardo", timestamp);
|
|
|
|
Personne3 personne3 = new Personne3("Leornardo", timestamp);
|
|
|
|
Person3 person = mapper.map(person0, Person3.class);
|
|
|
|
Person3 person3 = mapper.map(personne3, Person3.class);
|
|
|
|
|
|
|
|
|
|
|
|
String timestampTest = person.getDtob();
|
|
|
|
String timestampTest = person3.getDtob();
|
|
|
|
// since different timezones will resolve the timestamp to a different
|
|
|
|
// since different timezones will resolve the timestamp to a different
|
|
|
|
// datetime string, it suffices to check only for format rather than
|
|
|
|
// datetime string, it suffices to check only for format rather than
|
|
|
|
// specific date
|
|
|
|
// specific date
|
|
|
|
assertTrue(timestampTest.charAt(10) == 'T'
|
|
|
|
assertTrue(timestampTest.charAt(10) == 'T'
|
|
|
|
&& timestampTest.charAt(19) == 'Z');
|
|
|
|
&& timestampTest.charAt(19) == 'Z');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void givenSrcAndDest_whenCustomMapperWorksBidirectionally_thenCorrect() {
|
|
|
|
|
|
|
|
mapperFactory.classMap(Personne3.class, Person3.class)
|
|
|
|
|
|
|
|
.customize(customMapper).register();
|
|
|
|
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
String dateTime = "2007-06-26T21:22:39Z";
|
|
|
|
public void givenSrcAndDest_whenCustomMapperWorksBidirectionally_thenCorrect() {
|
|
|
|
long timestamp = new Long("1182882159000");
|
|
|
|
mapperFactory.classMap(Personne3.class, Person3.class)
|
|
|
|
Person3 person3 = new Person3("Leornardo", dateTime);
|
|
|
|
.customize(customMapper).register();
|
|
|
|
Personne3 personne3 = mapper.map(person3, Personne3.class);
|
|
|
|
MapperFacade mapper = mapperFactory.getMapperFacade();
|
|
|
|
long timestampToTest = personne3.getDtob();
|
|
|
|
|
|
|
|
/*
|
|
|
|
String dateTime = "2007-06-26T21:22:39Z";
|
|
|
|
|
|
|
|
long timestamp = new Long("1182882159000");
|
|
|
|
|
|
|
|
Person3 person0 = new Person3("Leornardo", dateTime);
|
|
|
|
|
|
|
|
Personne3 person = mapper.map(person0, Personne3.class);
|
|
|
|
|
|
|
|
long timestampToTest = person.getDtob();
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* since different timezones will resolve the datetime to a different
|
|
|
|
* since different timezones will resolve the datetime to a different
|
|
|
|
* unix timestamp, we must provide a range of tolerance
|
|
|
|
* unix timestamp, we must provide a range of tolerance
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
assertTrue(timestampToTest == timestamp
|
|
|
|
assertTrue(timestampToTest == timestamp
|
|
|
|
|| timestampToTest >= timestamp - GMT_DIFFERENCE
|
|
|
|
|| timestampToTest >= timestamp - GMT_DIFFERENCE
|
|
|
|
|| timestampToTest <= timestamp + GMT_DIFFERENCE);
|
|
|
|
|| timestampToTest <= timestamp + GMT_DIFFERENCE);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|