From 2aef75b9072bd57b865d3c323372b2e71e315dd4 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Thu, 9 Jul 2009 03:30:19 +0000 Subject: [PATCH] list binding tests --- .../support/GenericFormatterRegistry.java | 15 ++++-- .../binding/support/GenericBinderTests.java | 53 +++++++++++++++---- .../convert/support/GenericTypeConverter.java | 3 -- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericFormatterRegistry.java b/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericFormatterRegistry.java index bd18b21d0e..1e32d1c3e8 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericFormatterRegistry.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericFormatterRegistry.java @@ -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 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); diff --git a/org.springframework.context/src/test/java/org/springframework/ui/binding/support/GenericBinderTests.java b/org.springframework.context/src/test/java/org/springframework/ui/binding/support/GenericBinderTests.java index 00eda5bda2..3b7c66e2b5 100644 --- a/org.springframework.context/src/test/java/org/springframework/ui/binding/support/GenericBinderTests.java +++ b/org.springframework.context/src/test/java/org/springframework/ui/binding/support/GenericBinderTests.java @@ -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 values = new LinkedHashMap(); + 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 values = new LinkedHashMap(); - 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 foos; private List
addresses; - + private Map favoriteFoodsByGroup; + public String getString() { return string; } @@ -355,16 +375,29 @@ public class GenericBinderTests { this.addresses = addresses; } + public Map getFavoriteFoodsByGroup() { + return favoriteFoodsByGroup; + } + + public void setFavoriteFoodsByGroup(Map favoriteFoodsByGroup) { + this.favoriteFoodsByGroup = favoriteFoodsByGroup; + } + } public static class AddressFormatter implements Formatter
{ 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; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java index 7a0d2f2588..8ef76df56a 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java @@ -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") -- GitLab