From a06ce1fd810fce84478a6fd722e69613a053350a Mon Sep 17 00:00:00 2001 From: Alex Theedom Date: Sat, 6 Aug 2016 18:01:26 +0100 Subject: [PATCH] Minor changes following review --- .../main/java/com/baeldung/orika/Dest.java | 1 - .../main/java/com/baeldung/orika/Name.java | 1 - .../main/java/com/baeldung/orika/Person.java | 1 - .../main/java/com/baeldung/orika/Person3.java | 1 - .../com/baeldung/orika/PersonContainer.java | 1 - .../baeldung/orika/PersonCustomMapper.java | 36 ++ .../baeldung/orika/PersonListContainer.java | 1 - .../com/baeldung/orika/PersonNameArray.java | 1 - .../com/baeldung/orika/PersonNameList.java | 1 - .../com/baeldung/orika/PersonNameMap.java | 6 - .../com/baeldung/orika/PersonNameParts.java | 1 - .../java/com/baeldung/orika/Personne.java | 5 - .../java/com/baeldung/orika/Personne3.java | 1 - .../main/java/com/baeldung/orika/Source.java | 1 - .../java/com/baeldung/orika/OrikaTest.java | 609 +++++++++--------- 15 files changed, 327 insertions(+), 340 deletions(-) create mode 100644 orika/src/main/java/com/baeldung/orika/PersonCustomMapper.java diff --git a/orika/src/main/java/com/baeldung/orika/Dest.java b/orika/src/main/java/com/baeldung/orika/Dest.java index ffacca8f00..4f050230ce 100644 --- a/orika/src/main/java/com/baeldung/orika/Dest.java +++ b/orika/src/main/java/com/baeldung/orika/Dest.java @@ -14,7 +14,6 @@ public class Dest { } public Dest(String name, int age) { - super(); this.name = name; this.age = age; } diff --git a/orika/src/main/java/com/baeldung/orika/Name.java b/orika/src/main/java/com/baeldung/orika/Name.java index 68c9d47499..fcf0214548 100644 --- a/orika/src/main/java/com/baeldung/orika/Name.java +++ b/orika/src/main/java/com/baeldung/orika/Name.java @@ -5,7 +5,6 @@ public class Name { private String lastName; public Name(String firstName, String lastName) { - super(); this.firstName = firstName; this.lastName = lastName; } diff --git a/orika/src/main/java/com/baeldung/orika/Person.java b/orika/src/main/java/com/baeldung/orika/Person.java index 29f715b174..90ae8dee5e 100644 --- a/orika/src/main/java/com/baeldung/orika/Person.java +++ b/orika/src/main/java/com/baeldung/orika/Person.java @@ -16,7 +16,6 @@ public class Person { } public Person(String name, String nickname, int age) { - super(); this.name = name; this.nickname = nickname; this.age = age; diff --git a/orika/src/main/java/com/baeldung/orika/Person3.java b/orika/src/main/java/com/baeldung/orika/Person3.java index b946aeec40..8661edfc10 100644 --- a/orika/src/main/java/com/baeldung/orika/Person3.java +++ b/orika/src/main/java/com/baeldung/orika/Person3.java @@ -9,7 +9,6 @@ public class Person3 { } public Person3(String name, String dtob) { - super(); this.name = name; this.dtob = dtob; } diff --git a/orika/src/main/java/com/baeldung/orika/PersonContainer.java b/orika/src/main/java/com/baeldung/orika/PersonContainer.java index 2955035458..aaec136bb9 100644 --- a/orika/src/main/java/com/baeldung/orika/PersonContainer.java +++ b/orika/src/main/java/com/baeldung/orika/PersonContainer.java @@ -4,7 +4,6 @@ public class PersonContainer { private Name name; public PersonContainer(Name name) { - super(); this.name = name; } diff --git a/orika/src/main/java/com/baeldung/orika/PersonCustomMapper.java b/orika/src/main/java/com/baeldung/orika/PersonCustomMapper.java new file mode 100644 index 0000000000..d839eea30e --- /dev/null +++ b/orika/src/main/java/com/baeldung/orika/PersonCustomMapper.java @@ -0,0 +1,36 @@ +package com.baeldung.orika; + +import ma.glasnost.orika.CustomMapper; +import ma.glasnost.orika.MappingContext; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +class PersonCustomMapper extends CustomMapper { + + @Override + public void mapAtoB(Personne3 a, Person3 b, MappingContext context) { + Date date = new Date(a.getDtob()); + DateFormat format = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss'Z'"); + String isoDate = format.format(date); + b.setDtob(isoDate); + } + + @Override + public void mapBtoA(Person3 b, Personne3 a, MappingContext context) { + DateFormat format = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss'Z'"); + Date date = null; + try { + date = format.parse(b.getDtob()); + + } catch (ParseException e) { + e.printStackTrace(); + } + long timestamp = date.getTime(); + a.setDtob(timestamp); + } +} diff --git a/orika/src/main/java/com/baeldung/orika/PersonListContainer.java b/orika/src/main/java/com/baeldung/orika/PersonListContainer.java index e70f34f29d..97ae15eb48 100644 --- a/orika/src/main/java/com/baeldung/orika/PersonListContainer.java +++ b/orika/src/main/java/com/baeldung/orika/PersonListContainer.java @@ -6,7 +6,6 @@ public class PersonListContainer { private List names; public PersonListContainer(List names) { - super(); this.names = names; } diff --git a/orika/src/main/java/com/baeldung/orika/PersonNameArray.java b/orika/src/main/java/com/baeldung/orika/PersonNameArray.java index e985924cda..f2e98be537 100644 --- a/orika/src/main/java/com/baeldung/orika/PersonNameArray.java +++ b/orika/src/main/java/com/baeldung/orika/PersonNameArray.java @@ -4,7 +4,6 @@ public class PersonNameArray { private String[] nameArray; public PersonNameArray(String[] nameArray) { - super(); this.nameArray = nameArray; } diff --git a/orika/src/main/java/com/baeldung/orika/PersonNameList.java b/orika/src/main/java/com/baeldung/orika/PersonNameList.java index 46398c3782..70798ebd35 100644 --- a/orika/src/main/java/com/baeldung/orika/PersonNameList.java +++ b/orika/src/main/java/com/baeldung/orika/PersonNameList.java @@ -6,7 +6,6 @@ public class PersonNameList { private List nameList; public PersonNameList(List nameList) { - super(); this.nameList = nameList; } diff --git a/orika/src/main/java/com/baeldung/orika/PersonNameMap.java b/orika/src/main/java/com/baeldung/orika/PersonNameMap.java index ba802dfb32..8126cdfc3a 100644 --- a/orika/src/main/java/com/baeldung/orika/PersonNameMap.java +++ b/orika/src/main/java/com/baeldung/orika/PersonNameMap.java @@ -6,15 +6,9 @@ public class PersonNameMap { private Map nameMap; public PersonNameMap(Map nameMap) { - super(); this.nameMap = nameMap; } - public PersonNameMap() { - super(); - // TODO Auto-generated constructor stub - } - public Map getNameMap() { return nameMap; } diff --git a/orika/src/main/java/com/baeldung/orika/PersonNameParts.java b/orika/src/main/java/com/baeldung/orika/PersonNameParts.java index 5c4e14ef92..7cfdcdd75b 100644 --- a/orika/src/main/java/com/baeldung/orika/PersonNameParts.java +++ b/orika/src/main/java/com/baeldung/orika/PersonNameParts.java @@ -5,7 +5,6 @@ public class PersonNameParts { private String lastName; public PersonNameParts(String firstName, String lastName) { - super(); this.firstName = firstName; this.lastName = lastName; } diff --git a/orika/src/main/java/com/baeldung/orika/Personne.java b/orika/src/main/java/com/baeldung/orika/Personne.java index 29887d9987..47789dd323 100644 --- a/orika/src/main/java/com/baeldung/orika/Personne.java +++ b/orika/src/main/java/com/baeldung/orika/Personne.java @@ -5,12 +5,7 @@ public class Personne { 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; diff --git a/orika/src/main/java/com/baeldung/orika/Personne3.java b/orika/src/main/java/com/baeldung/orika/Personne3.java index 25175c241f..35323c612a 100644 --- a/orika/src/main/java/com/baeldung/orika/Personne3.java +++ b/orika/src/main/java/com/baeldung/orika/Personne3.java @@ -9,7 +9,6 @@ public class Personne3 { } public Personne3(String name, long dtob) { - super(); this.name = name; this.dtob = dtob; } diff --git a/orika/src/main/java/com/baeldung/orika/Source.java b/orika/src/main/java/com/baeldung/orika/Source.java index 0b449960d6..a973bab388 100644 --- a/orika/src/main/java/com/baeldung/orika/Source.java +++ b/orika/src/main/java/com/baeldung/orika/Source.java @@ -13,7 +13,6 @@ public class Source { } public Source(String name, int age) { - super(); this.name = name; this.age = age; } diff --git a/orika/src/test/java/com/baeldung/orika/OrikaTest.java b/orika/src/test/java/com/baeldung/orika/OrikaTest.java index 90411a74eb..18dfcfa44e 100644 --- a/orika/src/test/java/com/baeldung/orika/OrikaTest.java +++ b/orika/src/test/java/com/baeldung/orika/OrikaTest.java @@ -24,371 +24,344 @@ import org.junit.Before; import org.junit.Test; public class OrikaTest { - MapperFactory mapperFactory; - CustomMapper customMapper; - // constant to help us cover time zone differences - private final long GMT_DIFFERENCE = 46800000; + MapperFactory mapperFactory; + CustomMapper customMapper; + // constant to help us cover time zone differences + private final long GMT_DIFFERENCE = 46800000; - @Before - public void before() { - mapperFactory = new DefaultMapperFactory.Builder().build(); - customMapper = new CustomMapper() { + @Before + public void before() { + mapperFactory = new DefaultMapperFactory.Builder().build(); + customMapper = new PersonCustomMapper(); + } - @Override - public void mapAtoB(Personne3 a, Person3 b, MappingContext context) { - Date date = new Date(a.getDtob()); - DateFormat format = new SimpleDateFormat( - "yyyy-MM-dd'T'HH:mm:ss'Z'"); - String isoDate = format.format(date); - b.setDtob(isoDate); - } + @Test + public void givenSrcAndDest_whenMaps_thenCorrect() { + mapperFactory.classMap(Source.class, Dest.class); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Source src = new Source("Baeldung", 10); + Dest dest = mapper.map(src, Dest.class); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), src.getName()); + } - @Override - public void mapBtoA(Person3 b, Personne3 a, MappingContext context) { - DateFormat format = new SimpleDateFormat( - "yyyy-MM-dd'T'HH:mm:ss'Z'"); - Date date = null; - try { - date = format.parse(b.getDtob()); + @Test + public void givenSrcAndDest_whenMapsReverse_thenCorrect() { + mapperFactory.classMap(Source.class, Dest.class).byDefault(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Dest src = new Dest("Baeldung", 10); + Source dest = mapper.map(src, Source.class); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), src.getName()); + } - } catch (ParseException e) { - e.printStackTrace(); - } - long timestamp = date.getTime(); - a.setDtob(timestamp); - } + @Test + public void givenSrcAndDest_whenMapsByObject_thenCorrect() { + mapperFactory.classMap(Source.class, Dest.class).byDefault(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + 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 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 - public void givenSrcAndDest_whenMaps_thenCorrect() { - mapperFactory.classMap(Source.class, Dest.class); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Source src = new Source("Baeldung", 10); - Dest dest = mapper.map(src, Dest.class); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), src.getName()); - } + @Test + public void givenSrcAndDest_whenMapsUsingBoundMapperInReverse_thenCorrect() { + BoundMapperFacade 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 - public void givenSrcAndDest_whenMapsReverse_thenCorrect() { - mapperFactory.classMap(Source.class, Dest.class).byDefault(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Dest src = new Dest("Baeldung", 10); - Source dest = mapper.map(src, Source.class); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), src.getName()); - } + @Test + public void givenSrcAndDest_whenMapsUsingBoundMapperByObject_thenCorrect() { + BoundMapperFacade 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 - public void givenSrcAndDest_whenMapsByObject_thenCorrect() { - mapperFactory.classMap(Source.class, Dest.class).byDefault(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - 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_whenMapsUsingBoundMapperByObjectInReverse_thenCorrect() { + BoundMapperFacade boundMapper = mapperFactory + .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 givenSrcAndDest_whenMapsUsingBoundMapper_thenCorrect() { - BoundMapperFacade 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 + public void givenSrcAndDestWithDifferentFieldNames_whenMaps_thenCorrect() { + mapperFactory.classMap(Personne.class, Person.class) + .field("nom", "name").field("surnom", "nickname") + .field("age", "age").register(); - @Test - public void givenSrcAndDest_whenMapsUsingBoundMapperInReverse_thenCorrect() { - BoundMapperFacade 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()); - } + MapperFacade mapper = mapperFactory.getMapperFacade(); - @Test - public void givenSrcAndDest_whenMapsUsingBoundMapperByObject_thenCorrect() { - BoundMapperFacade 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()); - } + Personne frenchPerson = new Personne("Claire", "cla", 25); + Person englishPerson = mapper.map(frenchPerson, Person.class); - @Test - public void givenSrcAndDest_whenMapsUsingBoundMapperByObjectInReverse_thenCorrect() { - BoundMapperFacade boundMapper = mapperFactory - .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()); - } + assertEquals(englishPerson.getName(), frenchPerson.getNom()); + assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom()); + assertEquals(englishPerson.getAge(), frenchPerson.getAge()); - @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 - 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); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Personne frenchPerson = new Personne("Claire", "cla", 25); - Person englishPerson = mapper.map(frenchPerson, Person.class); - assertFalse(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 givenBothDifferentAndSameFieldNames_whenMapsSameNameFieldByDefault_thenCorrect() { - mapperFactory.classMap(Personne.class, Person.class) - .field("nom", "name").field("surnom", "nickname").byDefault() - .register(); + @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); - 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()); - 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 - public void givenSrcAndDest_whenCanExcludeField_thenCorrect() { - mapperFactory.classMap(Personne.class, Person.class).exclude("nom") - .field("surnom", "nickname").field("age", "age").register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); + @Test + public void givenSrcAndDest_whenCanExcludeField_thenCorrect() { + mapperFactory.classMap(Personne.class, Person.class).exclude("nom") + .field("surnom", "nickname").field("age", "age").register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); - Personne frenchPerson = new Personne("Claire", "cla", 25); - Person englishPerson = mapper.map(frenchPerson, Person.class); + Personne frenchPerson = new Personne("Claire", "cla", 25); + Person englishPerson = mapper.map(frenchPerson, Person.class); - assertEquals(null, englishPerson.getName()); - assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom()); - assertEquals(englishPerson.getAge(), frenchPerson.getAge()); - } + assertEquals(null, englishPerson.getName()); + assertEquals(englishPerson.getNickname(), frenchPerson.getSurnom()); + assertEquals(englishPerson.getAge(), frenchPerson.getAge()); + } - @Test - public void givenSpecificConstructorToUse_whenMaps_thenCorrect() { - mapperFactory.classMap(Personne.class, Person.class).constructorB() - .field("nom", "name").field("surnom", "nickname") - .field("age", "age").register(); - ; - MapperFacade mapper = mapperFactory.getMapperFacade(); - Personne frenchPerson = new Personne("Claire", "cla", 25); + @Test + public void givenSpecificConstructorToUse_whenMaps_thenCorrect() { + mapperFactory.classMap(Personne.class, Person.class).constructorB() + .field("nom", "name").field("surnom", "nickname") + .field("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()); + 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 givenSrcWithListAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() { - mapperFactory.classMap(PersonNameList.class, PersonNameParts.class) - .field("nameList[0]", "firstName") - .field("nameList[1]", "lastName").register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - List nameList = Arrays.asList(new String[] { "Sylvester", - "Stallone" }); - PersonNameList src = new PersonNameList(nameList); - PersonNameParts dest = mapper.map(src, PersonNameParts.class); - assertEquals(dest.getFirstName(), "Sylvester"); - assertEquals(dest.getLastName(), "Stallone"); - } + @Test + public void givenSrcWithListAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() { + mapperFactory.classMap(PersonNameList.class, PersonNameParts.class) + .field("nameList[0]", "firstName") + .field("nameList[1]", "lastName").register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + List nameList = Arrays.asList(new String[]{"Sylvester", + "Stallone"}); + PersonNameList src = new PersonNameList(nameList); + PersonNameParts dest = mapper.map(src, PersonNameParts.class); + assertEquals(dest.getFirstName(), "Sylvester"); + assertEquals(dest.getLastName(), "Stallone"); + } - @Test - public void givenSrcWithArrayAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() { - mapperFactory.classMap(PersonNameArray.class, PersonNameParts.class) - .field("nameArray[0]", "firstName") - .field("nameArray[1]", "lastName").register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - String[] nameArray = new String[] { "Vin", "Diesel" }; - PersonNameArray src = new PersonNameArray(nameArray); - PersonNameParts dest = mapper.map(src, PersonNameParts.class); - assertEquals(dest.getFirstName(), "Vin"); - assertEquals(dest.getLastName(), "Diesel"); - } + @Test + public void givenSrcWithArrayAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() { + mapperFactory.classMap(PersonNameArray.class, PersonNameParts.class) + .field("nameArray[0]", "firstName") + .field("nameArray[1]", "lastName").register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + String[] nameArray = new String[]{"Vin", "Diesel"}; + PersonNameArray src = new PersonNameArray(nameArray); + PersonNameParts dest = mapper.map(src, PersonNameParts.class); + assertEquals(dest.getFirstName(), "Vin"); + assertEquals(dest.getLastName(), "Diesel"); + } - @Test - public void givenSrcWithMapAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() { - mapperFactory.classMap(PersonNameMap.class, PersonNameParts.class) - .field("nameMap['first']", "firstName") - .field("nameMap[\"last\"]", "lastName").register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Map nameMap = new HashMap<>(); - nameMap.put("first", "Leornado"); - nameMap.put("last", "DiCaprio"); - PersonNameMap src = new PersonNameMap(nameMap); - PersonNameParts dest = mapper.map(src, PersonNameParts.class); - assertEquals(dest.getFirstName(), "Leornado"); - assertEquals(dest.getLastName(), "DiCaprio"); - } + @Test + public void givenSrcWithMapAndDestWithPrimitiveAttributes_whenMaps_thenCorrect() { + mapperFactory.classMap(PersonNameMap.class, PersonNameParts.class) + .field("nameMap['first']", "firstName") + .field("nameMap[\"last\"]", "lastName").register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Map nameMap = new HashMap<>(); + nameMap.put("first", "Leornado"); + nameMap.put("last", "DiCaprio"); + PersonNameMap src = new PersonNameMap(nameMap); + PersonNameParts dest = mapper.map(src, PersonNameParts.class); + assertEquals(dest.getFirstName(), "Leornado"); + assertEquals(dest.getLastName(), "DiCaprio"); + } - @Test - public void givenSrcWithNestedFields_whenMaps_thenCorrect() { - mapperFactory.classMap(PersonContainer.class, PersonNameParts.class) - .field("name.firstName", "firstName") - .field("name.lastName", "lastName").register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - PersonContainer src = new PersonContainer(new Name("Nick", "Canon")); - PersonNameParts dest = mapper.map(src, PersonNameParts.class); - assertEquals(dest.getFirstName(), "Nick"); - assertEquals(dest.getLastName(), "Canon"); - } + @Test + public void givenSrcWithNestedFields_whenMaps_thenCorrect() { + mapperFactory.classMap(PersonContainer.class, PersonNameParts.class) + .field("name.firstName", "firstName") + .field("name.lastName", "lastName").register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + PersonContainer src = new PersonContainer(new Name("Nick", "Canon")); + PersonNameParts dest = mapper.map(src, PersonNameParts.class); + assertEquals(dest.getFirstName(), "Nick"); + assertEquals(dest.getLastName(), "Canon"); + } - @Test - public void givenSrcWithNullField_whenMapsThenCorrect() { - mapperFactory.classMap(Source.class, Dest.class).byDefault(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Source src = new Source(null, 10); - Dest dest = mapper.map(src, Dest.class); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), src.getName()); - } + @Test + public void givenSrcWithNullField_whenMapsThenCorrect() { + mapperFactory.classMap(Source.class, Dest.class).byDefault(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Source src = new Source(null, 10); + Dest dest = mapper.map(src, Dest.class); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), src.getName()); + } - @Test - public void givenSrcWithNullAndGlobalConfigForNoNull_whenFailsToMap_ThenCorrect() { - MapperFactory mapperFactory = new DefaultMapperFactory.Builder() - .mapNulls(false).build(); - mapperFactory.classMap(Source.class, Dest.class); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Source src = new Source(null, 10); - Dest dest = new Dest("Clinton", 55); - mapper.map(src, dest); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), "Clinton"); - } + @Test + public void givenSrcWithNullAndGlobalConfigForNoNull_whenFailsToMap_ThenCorrect() { + MapperFactory mapperFactory = new DefaultMapperFactory.Builder() + .mapNulls(false).build(); + mapperFactory.classMap(Source.class, Dest.class); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Source src = new Source(null, 10); + Dest dest = new Dest("Clinton", 55); + mapper.map(src, dest); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), "Clinton"); + } - @Test - public void givenSrcWithNullAndLocalConfigForNoNull_whenFailsToMap_ThenCorrect() { - mapperFactory.classMap(Source.class, Dest.class).field("age", "age") - .mapNulls(false).field("name", "name").byDefault().register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Source src = new Source(null, 10); - Dest dest = new Dest("Clinton", 55); - mapper.map(src, dest); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), "Clinton"); - } + @Test + public void givenSrcWithNullAndLocalConfigForNoNull_whenFailsToMap_ThenCorrect() { + mapperFactory.classMap(Source.class, Dest.class).field("age", "age") + .mapNulls(false).field("name", "name").byDefault().register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Source src = new Source(null, 10); + Dest dest = new Dest("Clinton", 55); + mapper.map(src, dest); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), "Clinton"); + } - @Test - public void givenDestWithNullReverseMappedToSource_whenMapsByDefault_thenCorrect() { - mapperFactory.classMap(Source.class, Dest.class).byDefault(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Dest src = new Dest(null, 10); - Source dest = new Source("Vin", 44); - mapper.map(src, dest); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), src.getName()); - } + @Test + public void givenDestWithNullReverseMappedToSource_whenMapsByDefault_thenCorrect() { + mapperFactory.classMap(Source.class, Dest.class).byDefault(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Dest src = new Dest(null, 10); + Source dest = new Source("Vin", 44); + mapper.map(src, dest); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), src.getName()); + } - @Test - public void givenDestWithNullReverseMappedToSourceAndLocalConfigForNoNull_whenFailsToMap_thenCorrect() { - mapperFactory.classMap(Source.class, Dest.class).field("age", "age") - .mapNullsInReverse(false).field("name", "name").byDefault() - .register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Dest src = new Dest(null, 10); - Source dest = new Source("Vin", 44); - mapper.map(src, dest); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), "Vin"); - } + @Test + public void givenDestWithNullReverseMappedToSourceAndLocalConfigForNoNull_whenFailsToMap_thenCorrect() { + mapperFactory.classMap(Source.class, Dest.class).field("age", "age") + .mapNullsInReverse(false).field("name", "name").byDefault() + .register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Dest src = new Dest(null, 10); + Source dest = new Source("Vin", 44); + mapper.map(src, dest); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), "Vin"); + } - @Test - public void givenSrcWithNullAndFieldLevelConfigForNoNull_whenFailsToMap_ThenCorrect() { - mapperFactory.classMap(Source.class, Dest.class).field("age", "age") - .fieldMap("name", "name").mapNulls(false).add().byDefault() - .register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - Source src = new Source(null, 10); - Dest dest = new Dest("Clinton", 55); - mapper.map(src, dest); - assertEquals(dest.getAge(), src.getAge()); - assertEquals(dest.getName(), "Clinton"); - } + @Test + public void givenSrcWithNullAndFieldLevelConfigForNoNull_whenFailsToMap_ThenCorrect() { + mapperFactory.classMap(Source.class, Dest.class).field("age", "age") + .fieldMap("name", "name").mapNulls(false).add().byDefault() + .register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + Source src = new Source(null, 10); + Dest dest = new Dest("Clinton", 55); + mapper.map(src, dest); + assertEquals(dest.getAge(), src.getAge()); + assertEquals(dest.getName(), "Clinton"); + } - @Test - public void givenSrcAndDest_whenCustomMapperWorks_thenCorrect() { - mapperFactory.classMap(Personne3.class, Person3.class) - .customize(customMapper).register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - long timestamp = new Long("1182882159000"); - Personne3 person0 = new Personne3("Leornardo", timestamp); - Person3 person = mapper.map(person0, Person3.class); + @Test + public void givenSrcAndDest_whenCustomMapperWorks_thenCorrect() { + mapperFactory.classMap(Personne3.class, Person3.class) + .customize(customMapper).register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); + long timestamp = new Long("1182882159000"); + Personne3 personne3 = new Personne3("Leornardo", timestamp); + Person3 person3 = mapper.map(personne3, Person3.class); - String timestampTest = person.getDtob(); - // since different timezones will resolve the timestamp to a different - // datetime string, it suffices to check only for format rather than - // specific date - assertTrue(timestampTest.charAt(10) == 'T' - && timestampTest.charAt(19) == 'Z'); + String timestampTest = person3.getDtob(); + // since different timezones will resolve the timestamp to a different + // datetime string, it suffices to check only for format rather than + // specific date + assertTrue(timestampTest.charAt(10) == 'T' + && timestampTest.charAt(19) == 'Z'); + } - } + @Test + public void givenSrcAndDest_whenCustomMapperWorksBidirectionally_thenCorrect() { + mapperFactory.classMap(Personne3.class, Person3.class) + .customize(customMapper).register(); + MapperFacade mapper = mapperFactory.getMapperFacade(); - @Test - public void givenSrcAndDest_whenCustomMapperWorksBidirectionally_thenCorrect() { - mapperFactory.classMap(Personne3.class, Person3.class) - .customize(customMapper).register(); - MapperFacade mapper = mapperFactory.getMapperFacade(); - - 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(); - /* + String dateTime = "2007-06-26T21:22:39Z"; + long timestamp = new Long("1182882159000"); + Person3 person3 = new Person3("Leornardo", dateTime); + Personne3 personne3 = mapper.map(person3, Personne3.class); + long timestampToTest = personne3.getDtob(); + /* * since different timezones will resolve the datetime to a different * unix timestamp, we must provide a range of tolerance */ - assertTrue(timestampToTest == timestamp - || timestampToTest >= timestamp - GMT_DIFFERENCE - || timestampToTest <= timestamp + GMT_DIFFERENCE); + assertTrue(timestampToTest == timestamp + || timestampToTest >= timestamp - GMT_DIFFERENCE + || timestampToTest <= timestamp + GMT_DIFFERENCE); - } + } }