Added binder factory; simplified public binder api

This commit is contained in:
Keith Donald
2009-06-25 20:26:22 +00:00
parent 9b7e9942db
commit 5828010bae
14 changed files with 138 additions and 169 deletions

View File

@@ -21,7 +21,6 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.binding.Binding;
import org.springframework.ui.binding.BindingConfiguration;
import org.springframework.ui.binding.BindingResult;
import org.springframework.ui.binding.BindingResults;
import org.springframework.ui.format.AnnotationFormatterFactory;
@@ -104,21 +103,27 @@ public class GenericBinderTests {
@Test
public void bindSingleValueWithFormatterRegistedByType() throws ParseException {
binder.registerFormatter(Date.class, new DateFormatter());
GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(Date.class, new DateFormatter());
binder.setFormatterRegistry(registry);
binder.bind(Collections.singletonMap("date", "2009-06-01"));
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
}
@Test
public void bindSingleValueWithFormatterRegisteredByAnnotation() throws ParseException {
binder.registerFormatter(CurrencyFormat.class, new CurrencyFormatter());
GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(CurrencyFormat.class, new CurrencyFormatter());
binder.setFormatterRegistry(registry);
binder.bind(Collections.singletonMap("currency", "$23.56"));
assertEquals(new BigDecimal("23.56"), bean.getCurrency());
}
@Test
public void bindSingleValueWithnAnnotationFormatterFactoryRegistered() throws ParseException {
binder.registerFormatterFactory(new CurrencyAnnotationFormatterFactory());
GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(new CurrencyAnnotationFormatterFactory());
binder.setFormatterRegistry(registry);
binder.bind(Collections.singletonMap("currency", "$23.56"));
assertEquals(new BigDecimal("23.56"), bean.getCurrency());
}

View File

@@ -14,8 +14,6 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.binding.Binder;
import org.springframework.ui.binding.BindingConfiguration;
import org.springframework.ui.binding.BindingResults;
import org.springframework.ui.format.date.DateFormatter;
import org.springframework.ui.format.number.CurrencyFormat;
@@ -25,7 +23,7 @@ public class WebBinderTests {
TestBean bean = new TestBean();
Binder binder = new WebBinder(bean);
WebBinder binder = new WebBinder(bean);
@Before
public void setUp() {
@@ -39,7 +37,9 @@ public class WebBinderTests {
@Test
public void bindUserValuesCreatedFromUserMap() throws ParseException {
binder.registerFormatter(CurrencyFormat.class, new CurrencyFormatter());
GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(CurrencyFormat.class, new CurrencyFormatter());
binder.setFormatterRegistry(registry);
binder.configureBinding(new BindingConfiguration("date", new DateFormatter()));
Map<String, String> userMap = new LinkedHashMap<String, String>();
userMap.put("string", "test");

View File

@@ -14,9 +14,8 @@ import org.springframework.ui.alert.Severity;
import org.springframework.ui.alert.support.DefaultAlertContext;
import org.springframework.ui.binding.Bound;
import org.springframework.ui.binding.Model;
import org.springframework.ui.binding.support.GenericFormatterRegistry;
import org.springframework.ui.binding.support.WebBinderFactory;
import org.springframework.ui.format.number.CurrencyFormat;
import org.springframework.ui.format.number.IntegerFormatter;
public class WebBindAndValidateLifecycleTests {
@@ -30,7 +29,7 @@ public class WebBindAndValidateLifecycleTests {
public void setUp() {
model = new TestBean();
alertContext = new DefaultAlertContext();
lifecycle = new WebBindAndValidateLifecycle(model, alertContext);
lifecycle = new WebBindAndValidateLifecycle(new WebBinderFactory().getBinder(model), alertContext);
}
@Test
@@ -55,52 +54,6 @@ public class WebBindAndValidateLifecycleTests {
assertEquals("Failed to bind to property 'integer'; user value 'bogus' could not be converted to property type [java.lang.Integer]", alertContext.getAlerts("integer").get(0).getMessage());
}
@Test
public void testExecuteLifecycleInvalidFormatBindingErrors() {
Map<String, Object> userMap = new HashMap<String, Object>();
GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(Integer.class, new IntegerFormatter());
lifecycle.setFormatterRegistry(registry);
userMap.put("string", "test");
userMap.put("integer", "bogus");
userMap.put("foo", "BAR");
lifecycle.execute(userMap);
assertEquals(1, alertContext.getAlerts().size());
assertEquals(Severity.ERROR, alertContext.getAlerts("integer").get(0).getSeverity());
assertEquals("Failed to bind to property 'integer'; the user value 'bogus' has an invalid format and could no be parsed", alertContext.getAlerts("integer").get(0).getMessage());
}
@Test
public void testExecuteLifecycleAnnotatedModel() {
TestAnnotatedBean model = new TestAnnotatedBean();
lifecycle = new WebBindAndValidateLifecycle(model, alertContext);
Map<String, Object> userMap = new HashMap<String, Object>();
GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(Integer.class, new IntegerFormatter());
lifecycle.setFormatterRegistry(registry);
userMap.put("editable", "foo");
lifecycle.execute(userMap);
assertEquals(0, alertContext.getAlerts().size());
assertEquals("foo", model.getEditable());
}
@Test
public void testExecuteLifecycleAnnotatedModelNonEditableBindingAttempt() {
TestAnnotatedBean model = new TestAnnotatedBean();
lifecycle = new WebBindAndValidateLifecycle(model, alertContext);
Map<String, Object> userMap = new HashMap<String, Object>();
GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(Integer.class, new IntegerFormatter());
lifecycle.setFormatterRegistry(registry);
userMap.put("editable", "foo");
userMap.put("nonEditable", "whatev");
lifecycle.execute(userMap);
assertEquals(1, alertContext.getAlerts().size());
assertEquals("foo", model.getEditable());
assertEquals(null, model.getNotEditable());
assertEquals("noSuchBinding", alertContext.getAlerts("nonEditable").get(0).getCode());
}
public static enum FooEnum {
BAR, BAZ, BOOP;
}