list binding tests
This commit is contained in:
parent
cbe6695273
commit
2aef75b907
|
|
@ -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;
|
||||
|
|
@ -229,17 +228,33 @@ public class GenericBinderTests {
|
|||
@Test
|
||||
public void bindToList() {
|
||||
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);
|
||||
Assert.assertEquals(6, bean.addresses.size());
|
||||
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 bindHandleNullValueInNestedPath() {
|
||||
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");
|
||||
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 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,6 +316,7 @@ 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")
|
||||
|
|
|
|||
Loading…
Reference in New Issue