Skip to content

Instantly share code, notes, and snippets.

@chhsiao90
Created May 9, 2017 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chhsiao90/45e53804f93868478a3a27a7d5bd056c to your computer and use it in GitHub Desktop.
Save chhsiao90/45e53804f93868478a3a27a7d5bd056c to your computer and use it in GitHub Desktop.
ModelMapper-GH224
package org.modelmapper.bugs;
import static org.testng.Assert.assertEquals;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test
public class GH224 {
static class Source {
private SourceEmployer employer;
public SourceEmployer getEmployer() {
return employer;
}
public void setEmployer(SourceEmployer employer) {
this.employer = employer;
}
}
static class SourceEmployer {
private String name;
private SourceEmployerName employerName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SourceEmployerName getEmployerName() {
return employerName;
}
public void setEmployerName(SourceEmployerName employerName) {
this.employerName = employerName;
}
}
static class SourceEmployerName {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
static class Destination {
private DestinationEmployer employer;
public DestinationEmployer getEmployer() {
return employer;
}
public void setEmployer(DestinationEmployer employer) {
this.employer = employer;
}
}
static class DestinationEmployer {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private ModelMapper modelMapper;
@BeforeMethod
public void setUp() {
modelMapper = new ModelMapper();
}
public void shouldMapButFailed() {
SourceEmployerName name = new SourceEmployerName();
name.setName("correct");
SourceEmployer employer = new SourceEmployer();
employer.setEmployerName(name);
employer.setName("incorrect");
Source source = new Source();
source.setEmployer(employer);
modelMapper.addMappings(new PropertyMap<Source, Destination>() {
@Override
protected void configure() {
map(source.getEmployer(), destination.getEmployer());
}
});
modelMapper.addMappings(new PropertyMap<SourceEmployer, DestinationEmployer>() {
@Override
protected void configure() {
map().setName(source.getEmployerName().getName());
}
});
Destination destination = modelMapper.map(source, Destination.class);
assertEquals(destination.getEmployer().getName(), "correct"); // Failed
}
public void shouldMapWithSkip() {
SourceEmployerName name = new SourceEmployerName();
name.setName("correct");
SourceEmployer employer = new SourceEmployer();
employer.setEmployerName(name);
employer.setName("incorrect");
Source source = new Source();
source.setEmployer(employer);
modelMapper.addMappings(new PropertyMap<Source, Destination>() {
@Override
protected void configure() {
map(source.getEmployer(), destination.getEmployer());
skip().getEmployer().setName(null);
}
});
modelMapper.addMappings(new PropertyMap<SourceEmployer, DestinationEmployer>() {
@Override
protected void configure() {
map().setName(source.getEmployerName().getName());
}
});
Destination destination = modelMapper.map(source, Destination.class);
assertEquals(destination.getEmployer().getName(), "correct");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment