Created
November 11, 2013 19:52
-
-
Save jhorstmann/7419280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.modelmapper; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.modelmapper.spi.MappingContext; | |
import org.modelmapper.spi.MappingEngine; | |
import org.testng.Assert; | |
import org.testng.annotations.Test; | |
@Test | |
public class MappingEngineShouldCreateSeparateObjects extends AbstractTest { | |
public static class Source { | |
private List<SourcePosition> positions; | |
public List<SourcePosition> getPositions() { | |
return positions; | |
} | |
public void setPositions(final List<SourcePosition> positions) { | |
this.positions = positions; | |
} | |
} | |
public static class SourcePosition { | |
private int quantity; | |
private String name; | |
public int getQuantity() { | |
return quantity; | |
} | |
public void setQuantity(final int quantity) { | |
this.quantity = quantity; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(final String name) { | |
this.name = name; | |
} | |
} | |
public static class Destination { | |
private List<DestinationPosition> positions; | |
public List<DestinationPosition> getPositions() { | |
return positions; | |
} | |
public void setPositions(final List<DestinationPosition> positions) { | |
this.positions = positions; | |
} | |
} | |
public static class DestinationPosition { | |
private String name; | |
public String getName() { | |
return name; | |
} | |
public void setName(final String name) { | |
this.name = name; | |
} | |
} | |
static class PositionConverter implements Converter<List<SourcePosition>, List<DestinationPosition>> { | |
public List<DestinationPosition> convert( | |
final MappingContext<List<SourcePosition>, List<DestinationPosition>> context) { | |
List<DestinationPosition> result = new ArrayList<DestinationPosition>(); | |
for (SourcePosition sourcePosition : context.getSource()) { | |
for (int i = 0; i < sourcePosition.quantity; i++) { | |
MappingEngine mappingEngine = context.getMappingEngine(); | |
MappingContext<SourcePosition, DestinationPosition> context1 = context.create(sourcePosition, | |
DestinationPosition.class); | |
DestinationPosition destinationPosition = mappingEngine.map(context1); | |
result.add(destinationPosition); | |
} | |
} | |
return result; | |
} | |
} | |
public void testMappingShouldReturnDistinctObjects() { | |
Source source = new Source(); | |
{ | |
SourcePosition pos1 = new SourcePosition(); | |
pos1.name = "name"; | |
pos1.quantity = 2; | |
source.positions = Arrays.asList(pos1); | |
} | |
modelMapper.addMappings(new PropertyMap<Source, Destination>() { | |
@Override | |
protected void configure() { | |
using(new PositionConverter()).map(source.getPositions()).setPositions(null); | |
} | |
}); | |
modelMapper.validate(); | |
Destination destination = modelMapper.map(source, Destination.class); | |
List<DestinationPosition> positions = destination.getPositions(); | |
Assert.assertNotNull(positions); | |
Assert.assertEquals(2, positions.size()); | |
DestinationPosition pos1 = positions.get(0); | |
DestinationPosition pos2 = positions.get(1); | |
Assert.assertNotNull(pos1); | |
Assert.assertNotNull(pos2); | |
Assert.assertEquals(pos1.getName(), "name"); | |
Assert.assertEquals(pos2.getName(), "name"); | |
Assert.assertNotEquals(pos1, pos2, "Separate positions should not be equal"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment