made test changes to dozer tutorial
This commit is contained in:
parent
eb8526cfde
commit
290566add0
|
@ -1,14 +1,9 @@
|
||||||
package com.baeldung.dozer;
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.text.DateFormat;
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.dozer.DozerBeanMapper;
|
import org.dozer.DozerBeanMapper;
|
||||||
import org.dozer.loader.api.BeanMappingBuilder;
|
import org.dozer.loader.api.BeanMappingBuilder;
|
||||||
|
@ -16,14 +11,16 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class DozerTest {
|
public class DozerTest {
|
||||||
DozerBeanMapper mapper = new DozerBeanMapper();
|
private final long GMT_DIFFERENCE = 46800000;
|
||||||
private final long GMT_DIFFERENCE=46800000;
|
|
||||||
|
DozerBeanMapper mapper;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() throws Exception {
|
public void before() throws Exception {
|
||||||
mapper = new DozerBeanMapper();
|
mapper = new DozerBeanMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanMappingBuilder builder = new BeanMappingBuilder() {
|
BeanMappingBuilder builder = new BeanMappingBuilder() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
|
@ -32,7 +29,7 @@ public class DozerTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private BeanMappingBuilder builderMinusAge = new BeanMappingBuilder() {
|
BeanMappingBuilder builderMinusAge = new BeanMappingBuilder() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
|
@ -44,10 +41,12 @@ public class DozerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenApiMapper_whenMaps_thenCorrect() {
|
public void givenApiMapper_whenMaps_thenCorrect() {
|
||||||
|
mapper.addMapping(builder);
|
||||||
|
|
||||||
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
|
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
|
||||||
70);
|
70);
|
||||||
mapper.addMapping(builder);
|
|
||||||
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
|
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
|
||||||
|
|
||||||
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
|
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
|
||||||
assertEquals(englishAppPerson.getNickname(),
|
assertEquals(englishAppPerson.getNickname(),
|
||||||
frenchAppPerson.getSurnom());
|
frenchAppPerson.getSurnom());
|
||||||
|
@ -56,9 +55,11 @@ public class DozerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenApiMapper_whenMapsOnlySpecifiedFields_thenCorrect() {
|
public void givenApiMapper_whenMapsOnlySpecifiedFields_thenCorrect() {
|
||||||
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
|
|
||||||
mapper.addMapping(builderMinusAge);
|
mapper.addMapping(builderMinusAge);
|
||||||
|
|
||||||
|
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
|
||||||
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
|
||||||
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
assertEquals(frenchAppPerson.getSurnom(),
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
englishAppPerson.getNickname());
|
englishAppPerson.getNickname());
|
||||||
|
@ -67,9 +68,11 @@ public class DozerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenApiMapper_whenMapsBidirectionally_thenCorrect() {
|
public void givenApiMapper_whenMapsBidirectionally_thenCorrect() {
|
||||||
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
|
|
||||||
mapper.addMapping(builder);
|
mapper.addMapping(builder);
|
||||||
|
|
||||||
|
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
|
||||||
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
|
||||||
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
assertEquals(frenchAppPerson.getSurnom(),
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
englishAppPerson.getNickname());
|
englishAppPerson.getNickname());
|
||||||
|
@ -103,12 +106,12 @@ public class DozerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMaps_thenCorrect() {
|
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMaps_thenCorrect() {
|
||||||
List<String> mappingFiles = new ArrayList<>();
|
configureMapper("dozer_mapping.xml");
|
||||||
mappingFiles.add("dozer_mapping.xml");
|
|
||||||
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
|
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
|
||||||
70);
|
70);
|
||||||
mapper.setMappingFiles(mappingFiles);
|
|
||||||
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
|
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
|
||||||
|
|
||||||
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
|
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
|
||||||
assertEquals(englishAppPerson.getNickname(),
|
assertEquals(englishAppPerson.getNickname(),
|
||||||
frenchAppPerson.getSurnom());
|
frenchAppPerson.getSurnom());
|
||||||
|
@ -117,25 +120,52 @@ public class DozerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMapsBidirectionally_thenCorrect() {
|
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMapsBidirectionally_thenCorrect() {
|
||||||
List<String> mappingFiles = new ArrayList<>();
|
configureMapper("dozer_mapping.xml");
|
||||||
mappingFiles.add("dozer_mapping.xml");
|
|
||||||
Person englishAppPerson = new Person("Dwayne Johnson", "The Rock", 44);
|
Person englishAppPerson = new Person("Dwayne Johnson", "The Rock", 44);
|
||||||
mapper.setMappingFiles(mappingFiles);
|
|
||||||
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
|
||||||
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
assertEquals(frenchAppPerson.getSurnom(),
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
englishAppPerson.getNickname());
|
englishAppPerson.getNickname());
|
||||||
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
|
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
@Test
|
||||||
|
public void givenMappingFileOutsideClasspath_whenMaps_thenCorrect() {
|
||||||
|
configureMapper("file:e:/dozer_mapping.xml");
|
||||||
|
|
||||||
|
Person englishAppPerson = new Person("Marshall Bruce Mathers III",
|
||||||
|
"Eminem", 43);
|
||||||
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
|
||||||
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
|
englishAppPerson.getNickname());
|
||||||
|
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
|
||||||
|
}
|
||||||
|
*/
|
||||||
// @Test
|
// @Test
|
||||||
// public void givenMappingFileOutsideClasspath_whenMaps_thenCorrect() {
|
// public void givenMappingFileOutsideClasspath_whenMapsOnUnix_thenCorrect() {
|
||||||
// List<String> mappingFiles = new ArrayList<>();
|
// configureMapper("file:/home/dozer_mapping.xml");
|
||||||
// mappingFiles.add("file:E:\\dozer_mapping.xml");
|
//
|
||||||
// Person englishAppPerson = new Person("Marshall Bruce Mathers III",
|
// Person englishAppPerson = new Person("Marshall Bruce Mathers III",
|
||||||
// "Eminem", 43);
|
// "Eminem", 43);
|
||||||
// mapper.setMappingFiles(mappingFiles);
|
|
||||||
// Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
// 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_whenMapsOnMacOs_thenCorrect() {
|
||||||
|
// configureMapper("file:/Users/me/dozer_mapping.xml");
|
||||||
|
//
|
||||||
|
// Person englishAppPerson = new Person("Marshall Bruce Mathers III",
|
||||||
|
// "Eminem", 43);
|
||||||
|
// Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
//
|
||||||
// assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
// assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
// assertEquals(frenchAppPerson.getSurnom(),
|
// assertEquals(frenchAppPerson.getSurnom(),
|
||||||
// englishAppPerson.getNickname());
|
// englishAppPerson.getNickname());
|
||||||
|
@ -144,11 +174,11 @@ public class DozerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSrcAndDest_whenMapsOnlySpecifiedFields_thenCorrect() {
|
public void givenSrcAndDest_whenMapsOnlySpecifiedFields_thenCorrect() {
|
||||||
List<String> mappingFiles = new ArrayList<>();
|
configureMapper("dozer_mapping2.xml");
|
||||||
mappingFiles.add("dozer_mapping2.xml");
|
|
||||||
Person englishAppPerson = new Person("Shawn Corey Carter", "Jay Z", 46);
|
Person englishAppPerson = new Person("Shawn Corey Carter", "Jay Z", 46);
|
||||||
mapper.setMappingFiles(mappingFiles);
|
|
||||||
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
|
||||||
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
assertEquals(frenchAppPerson.getSurnom(),
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
englishAppPerson.getNickname());
|
englishAppPerson.getNickname());
|
||||||
|
@ -180,25 +210,35 @@ public class DozerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvert_thenCorrect() {
|
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvert_thenCorrect() {
|
||||||
|
configureMapper("dozer_custom_convertor.xml");
|
||||||
|
|
||||||
String dateTime = "2007-06-26T21:22:39Z";
|
String dateTime = "2007-06-26T21:22:39Z";
|
||||||
long timestamp = new Long("1182882159000");
|
long timestamp = new Long("1182882159000");
|
||||||
|
|
||||||
Person3 person = new Person3("Rich", dateTime);
|
Person3 person = new Person3("Rich", dateTime);
|
||||||
mapper.setMappingFiles(Arrays
|
|
||||||
.asList(new String[] { "dozer_custom_convertor.xml" }));
|
|
||||||
Personne3 person0 = mapper.map(person, Personne3.class);
|
Personne3 person0 = mapper.map(person, Personne3.class);
|
||||||
long timestampToTest=person0.getDtob();
|
|
||||||
assertTrue(timestampToTest==timestamp||timestampToTest>=timestamp-GMT_DIFFERENCE||timestampToTest<=timestamp+GMT_DIFFERENCE);
|
long timestampToTest = person0.getDtob();
|
||||||
|
assertTrue(timestampToTest == timestamp
|
||||||
|
|| timestampToTest >= timestamp - GMT_DIFFERENCE
|
||||||
|
|| timestampToTest <= timestamp + GMT_DIFFERENCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvertBidirectionally_thenCorrect() {
|
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvertBidirectionally_thenCorrect() {
|
||||||
String dateTime = "2007-06-26T21:22:39Z";
|
|
||||||
long timestamp = new Long("1182882159000");
|
long timestamp = new Long("1182882159000");
|
||||||
Personne3 person = new Personne3("Rich", timestamp);
|
Personne3 person = new Personne3("Rich", timestamp);
|
||||||
mapper.setMappingFiles(Arrays
|
configureMapper("dozer_custom_convertor.xml");
|
||||||
.asList(new String[] { "dozer_custom_convertor.xml" }));
|
|
||||||
Person3 person0 = mapper.map(person, Person3.class);
|
Person3 person0 = mapper.map(person, Person3.class);
|
||||||
String timestampTest=person0.getDtob();
|
String timestampTest = person0.getDtob();
|
||||||
assertTrue(timestampTest.charAt(10)=='T'&×tampTest.charAt(19)=='Z');
|
|
||||||
|
assertTrue(timestampTest.charAt(10) == 'T'
|
||||||
|
&& timestampTest.charAt(19) == 'Z');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void configureMapper(String... mappingFileUrls) {
|
||||||
|
mapper.setMappingFiles(Arrays.asList(mappingFileUrls));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue