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

list binding tests

上级 cbe66952
......@@ -52,11 +52,12 @@ public class GenericFormatterRegistry implements FormatterRegistry {
return factory.getFormatter(a);
}
}
Formatter<?> formatter = typeFormatters.get(propertyType.getType());
Class<?> type = getType(propertyType);
Formatter<?> formatter = typeFormatters.get(type);
if (formatter != null) {
return formatter;
} else {
Formatted formatted = AnnotationUtils.findAnnotation(propertyType.getType(), Formatted.class);
Formatted formatted = AnnotationUtils.findAnnotation(type, Formatted.class);
if (formatted != null) {
Class formatterClass = formatted.value();
try {
......@@ -67,7 +68,7 @@ public class GenericFormatterRegistry implements FormatterRegistry {
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
typeFormatters.put(propertyType.getType(), formatter);
typeFormatters.put(type, formatter);
return formatter;
} else {
return null;
......@@ -108,6 +109,14 @@ public class GenericFormatterRegistry implements FormatterRegistry {
+ 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) {
if (parameterType instanceof TypeVariable) {
parameterType = GenericTypeResolver.resolveTypeVariable((TypeVariable) parameterType, converterClass);
......
......@@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Collections;
......@@ -225,21 +224,37 @@ public class GenericBinderTests {
assertTrue(result.isFailure());
assertEquals("conversionFailed", result.getAlert().getCode());
}
@Test
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");
Map<String, String> values = new LinkedHashMap<String, String>();
values.put("addresses[0]", "4655 Macy Lane, Melbourne FL 35452");
values.put("addresses[1]", "1234 Rostock Circle, Palm Bay FL 32901");
values.put("addresses[5]", "1977 Bel Aire Estates, Coker AL 12345");
BindingResults results = binder.bind(values);
System.out.println(results);
values.put("addresses[0]", "4655 Macy Lane:Melbourne:FL:35452");
values.put("addresses[1]", "1234 Rostock Circle:Palm Bay:FL:32901");
values.put("addresses[5]", "1977 Bel Aire Estates:Coker:AL:12345");
binder.bind(values);
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
public void bindHandleNullValueInNestedPath() {
public void bindToListHandleNullValueInNestedPath() {
binder.addBinding("addresses.street");
binder.addBinding("addresses.city");
binder.addBinding("addresses.state");
......@@ -289,6 +304,10 @@ public class GenericBinderTests {
BAR, BAZ, BOOP;
}
public static enum FoodGroup {
DAIRY, VEG, FRUIT, BREAD, MEAT
}
public static class TestBean {
private String string;
private int integer;
......@@ -297,7 +316,8 @@ public class GenericBinderTests {
private BigDecimal currency;
private List<FooEnum> foos;
private List<Address> addresses;
private Map<FoodGroup, String> favoriteFoodsByGroup;
public String getString() {
return string;
}
......@@ -355,16 +375,29 @@ public class GenericBinderTests {
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 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 {
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;
}
......
......@@ -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.
*
* 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
*/
@SuppressWarnings("unchecked")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册