SPR-6179, additional mapper test cases
This commit is contained in:
@@ -27,8 +27,9 @@ public interface Mapper<S, T> {
|
||||
* Map the source to the target.
|
||||
* @param source the source to map from
|
||||
* @param target the target to map to
|
||||
* @return the mapped target object
|
||||
* @throws MappingException if the mapping process failed
|
||||
*/
|
||||
void map(S source, T target);
|
||||
Object map(S source, T target);
|
||||
|
||||
}
|
||||
|
||||
@@ -60,11 +60,12 @@ public class MappingException extends RuntimeException {
|
||||
public void printStackTrace(PrintStream ps) {
|
||||
super.printStackTrace(ps);
|
||||
synchronized (ps) {
|
||||
ps.println("Failure cause traces:");
|
||||
ps.println();
|
||||
ps.println("Mapping Failure Traces:");
|
||||
int i = 1;
|
||||
for (Iterator<MappingFailure> it = this.mappingFailures.iterator(); it.hasNext(); i++) {
|
||||
MappingFailure failure = it.next();
|
||||
ps.println("- MappingFailure #" + i + " Cause: ");
|
||||
ps.println("- MappingFailure #" + i + ":");
|
||||
Throwable t = failure.getCause();
|
||||
if (t != null) {
|
||||
t.printStackTrace(ps);
|
||||
@@ -79,11 +80,12 @@ public class MappingException extends RuntimeException {
|
||||
public void printStackTrace(PrintWriter pw) {
|
||||
super.printStackTrace(pw);
|
||||
synchronized (pw) {
|
||||
pw.println("Failure cause traces:");
|
||||
pw.println();
|
||||
pw.println("Mapping Failure Traces:");
|
||||
int i = 1;
|
||||
for (Iterator<MappingFailure> it = this.mappingFailures.iterator(); it.hasNext(); i++) {
|
||||
MappingFailure failure = it.next();
|
||||
pw.println("- MappingFailure #" + i + " Cause: ");
|
||||
pw.println("- MappingFailure #" + i + ":");
|
||||
Throwable t = failure.getCause();
|
||||
if (t != null) {
|
||||
t.printStackTrace(pw);
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Set;
|
||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ParseException;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
@@ -60,27 +61,27 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
this.autoMappingEnabled = autoMappingEnabled;
|
||||
}
|
||||
|
||||
public MappingConfiguration addMapping(String expression) {
|
||||
return addMapping(expression, expression);
|
||||
public MappingConfiguration addMapping(String fieldExpression) {
|
||||
return addMapping(fieldExpression, fieldExpression);
|
||||
}
|
||||
|
||||
public ConverterRegistry getConverterRegistry() {
|
||||
return conversionService;
|
||||
}
|
||||
|
||||
public MappingConfiguration addMapping(String sourceExpression, String targetExpression) {
|
||||
public MappingConfiguration addMapping(String sourceFieldExpression, String targetFieldExpression) {
|
||||
Expression sourceExp;
|
||||
try {
|
||||
sourceExp = sourceExpressionParser.parseExpression(sourceExpression);
|
||||
sourceExp = sourceExpressionParser.parseExpression(sourceFieldExpression);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("The mapping source '" + sourceExpression
|
||||
throw new IllegalArgumentException("The mapping source '" + sourceFieldExpression
|
||||
+ "' is not a parseable value expression", e);
|
||||
}
|
||||
Expression targetExp;
|
||||
try {
|
||||
targetExp = targetExpressionParser.parseExpression(targetExpression);
|
||||
targetExp = targetExpressionParser.parseExpression(targetFieldExpression);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("The mapping target '" + targetExpression
|
||||
throw new IllegalArgumentException("The mapping target '" + targetFieldExpression
|
||||
+ "' is not a parseable property expression", e);
|
||||
}
|
||||
Mapping mapping = new Mapping(sourceExp, targetExp);
|
||||
@@ -88,33 +89,33 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
public void map(Object source, Object target) {
|
||||
public Object map(Object source, Object target) {
|
||||
EvaluationContext sourceContext = getMappingContext(source);
|
||||
EvaluationContext targetContext = getMappingContext(target);
|
||||
List<MappingFailure> failures = new LinkedList<MappingFailure>();
|
||||
for (Mapping mapping : this.mappings) {
|
||||
mapping.map(sourceContext, targetContext, failures);
|
||||
}
|
||||
Set<Mapping> autoMappings = getAutoMappings(source, target);
|
||||
Set<Mapping> autoMappings = getAutoMappings(sourceContext, targetContext);
|
||||
for (Mapping mapping : autoMappings) {
|
||||
mapping.map(sourceContext, targetContext, failures);
|
||||
}
|
||||
if (!failures.isEmpty()) {
|
||||
throw new MappingException(failures);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
private EvaluationContext getMappingContext(Object object) {
|
||||
return mappableTypeFactory.getMappableType(object).getEvaluationContext(object, this.conversionService);
|
||||
}
|
||||
|
||||
private Set<Mapping> getAutoMappings(Object source, Object target) {
|
||||
private Set<Mapping> getAutoMappings(EvaluationContext sourceContext, EvaluationContext targetContext) {
|
||||
if (this.autoMappingEnabled) {
|
||||
Set<Mapping> autoMappings = new LinkedHashSet<Mapping>();
|
||||
Set<String> sourceFields = getMappableFields(source);
|
||||
Set<String> targetFields = getMappableFields(target);
|
||||
Set<String> sourceFields = getMappableFields(sourceContext.getRootObject().getValue());
|
||||
for (String field : sourceFields) {
|
||||
if (!explicitlyMapped(field) && targetFields.contains(field)) {
|
||||
if (!explicitlyMapped(field)) {
|
||||
Expression sourceExpression;
|
||||
Expression targetExpression;
|
||||
try {
|
||||
@@ -129,8 +130,13 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
throw new IllegalArgumentException("The mapping target '" + field
|
||||
+ "' is not a parseable value expression", e);
|
||||
}
|
||||
Mapping mapping = new Mapping(sourceExpression, targetExpression);
|
||||
autoMappings.add(mapping);
|
||||
try {
|
||||
if (targetExpression.isWritable(targetContext)) {
|
||||
autoMappings.add(new Mapping(sourceExpression, targetExpression));
|
||||
}
|
||||
} catch (EvaluationException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return autoMappings;
|
||||
@@ -145,7 +151,7 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
|
||||
private boolean explicitlyMapped(String field) {
|
||||
for (Mapping mapping : this.mappings) {
|
||||
if (mapping.getSourceExpressionString().equals(field)) {
|
||||
if (mapping.getSourceExpressionString().startsWith(field)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.mapping.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -31,15 +32,14 @@ public class SpelMapperTests {
|
||||
|
||||
@Test
|
||||
public void mapExplicit() throws MappingException {
|
||||
mapper.setAutoMappingEnabled(false);
|
||||
mapper.addMapping("name");
|
||||
|
||||
Map<String, Object> source = new HashMap<String, Object>();
|
||||
source.put("name", "Keith");
|
||||
source.put("age", 31);
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.setAutoMappingEnabled(false);
|
||||
mapper.addMapping("name");
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("Keith", target.name);
|
||||
@@ -48,8 +48,6 @@ public class SpelMapperTests {
|
||||
|
||||
@Test
|
||||
public void mapAutomaticWithExplictOverrides() {
|
||||
mapper.addMapping("test", "age");
|
||||
|
||||
Map<String, Object> source = new HashMap<String, Object>();
|
||||
source.put("name", "Keith");
|
||||
source.put("test", "3");
|
||||
@@ -57,6 +55,7 @@ public class SpelMapperTests {
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.addMapping("test", "age");
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("Keith", target.name);
|
||||
@@ -65,15 +64,29 @@ public class SpelMapperTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapSameSourceFieldToMultipleTargets() {
|
||||
mapper.addMapping("test", "name");
|
||||
mapper.addMapping("test", "favoriteSport");
|
||||
public void mapAutomaticIgnoreUnknownField() {
|
||||
Map<String, Object> source = new HashMap<String, Object>();
|
||||
source.put("name", "Keith");
|
||||
source.put("age", 31);
|
||||
source.put("unknown", "foo");
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("Keith", target.name);
|
||||
assertEquals(31, target.age);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapSameSourceFieldToMultipleTargets() {
|
||||
Map<String, Object> source = new HashMap<String, Object>();
|
||||
source.put("test", "FOOTBALL");
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.addMapping("test", "name");
|
||||
mapper.addMapping("test", "favoriteSport");
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("FOOTBALL", target.name);
|
||||
@@ -92,7 +105,6 @@ public class SpelMapperTests {
|
||||
|
||||
mapper.addMapping("fullName", "name");
|
||||
mapper.addMapping("sport", "favoriteSport");
|
||||
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("Keith Donald", target.name);
|
||||
@@ -100,24 +112,46 @@ public class SpelMapperTests {
|
||||
assertEquals(Sport.FOOTBALL, target.favoriteSport);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapBeanDeep() {
|
||||
PersonDto source = new PersonDto();
|
||||
source.age = "0";
|
||||
NestedDto nested = new NestedDto();
|
||||
nested.foo = "bar";
|
||||
source.setNested(nested);
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.addMapping("nested.foo", "nested.foo");
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("bar", target.nested.foo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapBeanNested() {
|
||||
PersonDto source = new PersonDto();
|
||||
source.setFullName("Keith Donald");
|
||||
source.setAge("31");
|
||||
source.setSport("FOOTBALL");
|
||||
NestedDto nested = new NestedDto();
|
||||
nested.foo = "bar";
|
||||
source.setNested(nested);
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.addMapping("fullName", "nested.fullName");
|
||||
mapper.addMapping("age", "nested.age");
|
||||
mapper.addMapping("sport", "nested.sport");
|
||||
|
||||
mapper.addMapping("fullName", "name");
|
||||
mapper.addMapping("sport", "favoriteSport");
|
||||
mapper.addMapping("nested", "nested").setConverter(new Converter<NestedDto, Nested>() {
|
||||
public Nested convert(NestedDto source) {
|
||||
return (Nested) new SpelMapper().map(source, new Nested());
|
||||
}
|
||||
});
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("Keith Donald", target.getNested().getFullName());
|
||||
assertEquals("31", target.nested.age);
|
||||
assertEquals("FOOTBALL", target.nested.sport);
|
||||
assertEquals("Keith Donald", target.name);
|
||||
assertEquals(31, target.age);
|
||||
assertEquals(Sport.FOOTBALL, target.favoriteSport);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -132,13 +166,30 @@ public class SpelMapperTests {
|
||||
|
||||
mapper.setAutoMappingEnabled(false);
|
||||
mapper.addMapping("sports", "favoriteSports");
|
||||
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals(Sport.FOOTBALL, target.favoriteSports.get(0));
|
||||
assertEquals(Sport.BASKETBALL, target.favoriteSports.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapListFlatten() {
|
||||
PersonDto source = new PersonDto();
|
||||
List<String> sports = new ArrayList<String>();
|
||||
sports.add("FOOTBALL");
|
||||
sports.add("BASKETBALL");
|
||||
source.setSports(sports);
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.setAutoMappingEnabled(false);
|
||||
mapper.addMapping("sports[0]", "favoriteSport");
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals(Sport.FOOTBALL, target.favoriteSport);
|
||||
assertNull(target.favoriteSports);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapMap() {
|
||||
PersonDto source = new PersonDto();
|
||||
@@ -187,7 +238,7 @@ public class SpelMapperTests {
|
||||
public void mapFailure() {
|
||||
Map<String, Object> source = new HashMap<String, Object>();
|
||||
source.put("name", "Keith");
|
||||
source.put("age", "bogus");
|
||||
source.put("age", "invalid");
|
||||
Person target = new Person();
|
||||
try {
|
||||
mapper.map(source, target);
|
||||
@@ -208,7 +259,7 @@ public class SpelMapperTests {
|
||||
|
||||
private Map<String, String> friendRankings;
|
||||
|
||||
private NestedDto nestedDto;
|
||||
private NestedDto nested;
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
@@ -250,12 +301,12 @@ public class SpelMapperTests {
|
||||
this.friendRankings = friendRankings;
|
||||
}
|
||||
|
||||
public NestedDto getNestedDto() {
|
||||
return nestedDto;
|
||||
public NestedDto getNested() {
|
||||
return nested;
|
||||
}
|
||||
|
||||
public void setNestedDto(NestedDto nestedDto) {
|
||||
this.nestedDto = nestedDto;
|
||||
public void setNested(NestedDto nested) {
|
||||
this.nested = nested;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -277,7 +328,7 @@ public class SpelMapperTests {
|
||||
|
||||
private Sport favoriteSport;
|
||||
|
||||
private PersonDto nested;
|
||||
private Nested nested;
|
||||
|
||||
// private Person cyclic;
|
||||
|
||||
@@ -317,11 +368,11 @@ public class SpelMapperTests {
|
||||
this.favoriteSport = favoriteSport;
|
||||
}
|
||||
|
||||
public PersonDto getNested() {
|
||||
public Nested getNested() {
|
||||
return nested;
|
||||
}
|
||||
|
||||
public void setNested(PersonDto nested) {
|
||||
public void setNested(Nested nested) {
|
||||
this.nested = nested;
|
||||
}
|
||||
|
||||
@@ -364,6 +415,20 @@ public class SpelMapperTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class Nested {
|
||||
|
||||
private String foo;
|
||||
|
||||
public String getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(String foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum Sport {
|
||||
FOOTBALL, BASKETBALL
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user