提交 2aef75b9 编写于 作者: K Keith Donald

list binding tests

上级 cbe66952
...@@ -52,11 +52,12 @@ public class GenericFormatterRegistry implements FormatterRegistry { ...@@ -52,11 +52,12 @@ public class GenericFormatterRegistry implements FormatterRegistry {
return factory.getFormatter(a); return factory.getFormatter(a);
} }
} }
Formatter<?> formatter = typeFormatters.get(propertyType.getType()); Class<?> type = getType(propertyType);
Formatter<?> formatter = typeFormatters.get(type);
if (formatter != null) { if (formatter != null) {
return formatter; return formatter;
} else { } else {
Formatted formatted = AnnotationUtils.findAnnotation(propertyType.getType(), Formatted.class); Formatted formatted = AnnotationUtils.findAnnotation(type, Formatted.class);
if (formatted != null) { if (formatted != null) {
Class formatterClass = formatted.value(); Class formatterClass = formatted.value();
try { try {
...@@ -67,7 +68,7 @@ public class GenericFormatterRegistry implements FormatterRegistry { ...@@ -67,7 +68,7 @@ public class GenericFormatterRegistry implements FormatterRegistry {
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
typeFormatters.put(propertyType.getType(), formatter); typeFormatters.put(type, formatter);
return formatter; return formatter;
} else { } else {
return null; return null;
...@@ -108,6 +109,14 @@ public class GenericFormatterRegistry implements FormatterRegistry { ...@@ -108,6 +109,14 @@ public class GenericFormatterRegistry implements FormatterRegistry {
+ factory.getClass().getName() + "]; does the factory parameterize the <A> generic type?"); + factory.getClass().getName() + "]; does the factory parameterize the <A> generic type?");
} }
private Class getType(TypeDescriptor descriptor) {
if (descriptor.isArray() || descriptor.isCollection()) {
return descriptor.getElementType();
} else {
return descriptor.getType();
}
}
private Class getParameterClass(Type parameterType, Class converterClass) { private Class getParameterClass(Type parameterType, Class converterClass) {
if (parameterType instanceof TypeVariable) { if (parameterType instanceof TypeVariable) {
parameterType = GenericTypeResolver.resolveTypeVariable((TypeVariable) parameterType, converterClass); parameterType = GenericTypeResolver.resolveTypeVariable((TypeVariable) parameterType, converterClass);
......
...@@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals; ...@@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.lang.annotation.Annotation;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.ParseException; import java.text.ParseException;
import java.util.Collections; import java.util.Collections;
...@@ -225,21 +224,37 @@ public class GenericBinderTests { ...@@ -225,21 +224,37 @@ public class GenericBinderTests {
assertTrue(result.isFailure()); assertTrue(result.isFailure());
assertEquals("conversionFailed", result.getAlert().getCode()); assertEquals("conversionFailed", result.getAlert().getCode());
} }
@Test @Test
public void bindToList() { public void bindToList() {
binder.addBinding("addresses");
Map<String, String[]> values = new LinkedHashMap<String, String[]>();
values.put("addresses", new String[] { "4655 Macy Lane:Melbourne:FL:35452", "1234 Rostock Circle:Palm Bay:FL:32901", "1977 Bel Aire Estates:Coker:AL:12345" });
binder.bind(values);
Assert.assertEquals(3, bean.addresses.size());
assertEquals("4655 Macy Lane", bean.addresses.get(0).street);
assertEquals("Melbourne", bean.addresses.get(0).city);
assertEquals("FL", bean.addresses.get(0).state);
assertEquals("35452", bean.addresses.get(0).zip);
}
@Test
public void bindToListElements() {
binder.addBinding("addresses"); binder.addBinding("addresses");
Map<String, String> values = new LinkedHashMap<String, String>(); Map<String, String> values = new LinkedHashMap<String, String>();
values.put("addresses[0]", "4655 Macy Lane, Melbourne FL 35452"); values.put("addresses[0]", "4655 Macy Lane:Melbourne:FL:35452");
values.put("addresses[1]", "1234 Rostock Circle, Palm Bay FL 32901"); values.put("addresses[1]", "1234 Rostock Circle:Palm Bay:FL:32901");
values.put("addresses[5]", "1977 Bel Aire Estates, Coker AL 12345"); values.put("addresses[5]", "1977 Bel Aire Estates:Coker:AL:12345");
BindingResults results = binder.bind(values); binder.bind(values);
System.out.println(results);
Assert.assertEquals(6, bean.addresses.size()); Assert.assertEquals(6, bean.addresses.size());
assertEquals("4655 Macy Lane", bean.addresses.get(0).street);
assertEquals("Melbourne", bean.addresses.get(0).city);
assertEquals("FL", bean.addresses.get(0).state);
assertEquals("35452", bean.addresses.get(0).zip);
} }
@Test @Test
public void bindHandleNullValueInNestedPath() { public void bindToListHandleNullValueInNestedPath() {
binder.addBinding("addresses.street"); binder.addBinding("addresses.street");
binder.addBinding("addresses.city"); binder.addBinding("addresses.city");
binder.addBinding("addresses.state"); binder.addBinding("addresses.state");
...@@ -289,6 +304,10 @@ public class GenericBinderTests { ...@@ -289,6 +304,10 @@ public class GenericBinderTests {
BAR, BAZ, BOOP; BAR, BAZ, BOOP;
} }
public static enum FoodGroup {
DAIRY, VEG, FRUIT, BREAD, MEAT
}
public static class TestBean { public static class TestBean {
private String string; private String string;
private int integer; private int integer;
...@@ -297,7 +316,8 @@ public class GenericBinderTests { ...@@ -297,7 +316,8 @@ public class GenericBinderTests {
private BigDecimal currency; private BigDecimal currency;
private List<FooEnum> foos; private List<FooEnum> foos;
private List<Address> addresses; private List<Address> addresses;
private Map<FoodGroup, String> favoriteFoodsByGroup;
public String getString() { public String getString() {
return string; return string;
} }
...@@ -355,16 +375,29 @@ public class GenericBinderTests { ...@@ -355,16 +375,29 @@ public class GenericBinderTests {
this.addresses = addresses; this.addresses = addresses;
} }
public Map<FoodGroup, String> getFavoriteFoodsByGroup() {
return favoriteFoodsByGroup;
}
public void setFavoriteFoodsByGroup(Map<FoodGroup, String> favoriteFoodsByGroup) {
this.favoriteFoodsByGroup = favoriteFoodsByGroup;
}
} }
public static class AddressFormatter implements Formatter<Address> { public static class AddressFormatter implements Formatter<Address> {
public String format(Address address, Locale locale) { public String format(Address address, Locale locale) {
return address.getStreet() + " " + address.getCity() + ", " + address.getState() + " " + address.getZip(); return address.getStreet() + ":" + address.getCity() + ":" + address.getState() + ":" + address.getZip();
} }
public Address parse(String formatted, Locale locale) throws ParseException { public Address parse(String formatted, Locale locale) throws ParseException {
Address address = new Address(); Address address = new Address();
String[] fields = formatted.split(":");
address.setStreet(fields[0]);
address.setCity(fields[1]);
address.setState(fields[2]);
address.setZip(fields[3]);
return address; return address;
} }
......
...@@ -37,10 +37,7 @@ import org.springframework.util.Assert; ...@@ -37,10 +37,7 @@ import org.springframework.util.Assert;
/** /**
* Base implementation of a conversion service. Initially empty, e.g. no converters are registered by default. * Base implementation of a conversion service. Initially empty, e.g. no converters are registered by default.
*
* TODO - object to collection/map converters * TODO - object to collection/map converters
* TODO - allow registration of converters to apply on presence of annotation values on setter or field
*
* @author Keith Donald * @author Keith Donald
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册