binding result tracking
This commit is contained in:
@@ -7,11 +7,10 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
@@ -19,6 +18,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.ui.format.date.DateFormatter;
|
||||
import org.springframework.ui.format.number.CurrencyAnnotationFormatterFactory;
|
||||
import org.springframework.ui.format.number.CurrencyFormat;
|
||||
@@ -40,54 +40,67 @@ public class BinderTests {
|
||||
@Test
|
||||
public void bindSingleValuesWithDefaultTypeConverterConversion() {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
propertyValues.put("string", "test");
|
||||
propertyValues.put("integer", "3");
|
||||
propertyValues.put("foo", "BAR");
|
||||
binder.bind(propertyValues);
|
||||
List<UserValue> values = new ArrayList<UserValue>();
|
||||
values.add(new UserValue("string", "test"));
|
||||
values.add(new UserValue("integer", "3"));
|
||||
values.add(new UserValue("foo", "BAR"));
|
||||
List<BindingResult> results = binder.bind(values);
|
||||
assertEquals(3, results.size());
|
||||
|
||||
assertEquals("string", results.get(0).getProperty());
|
||||
assertFalse(results.get(0).isError());
|
||||
assertNull(results.get(0).getErrorCause());
|
||||
assertEquals("test", results.get(0).getUserValue());
|
||||
|
||||
assertEquals("integer", results.get(1).getProperty());
|
||||
assertFalse(results.get(1).isError());
|
||||
assertNull(results.get(1).getErrorCause());
|
||||
assertEquals("3", results.get(1).getUserValue());
|
||||
|
||||
assertEquals("foo", results.get(2).getProperty());
|
||||
assertFalse(results.get(2).isError());
|
||||
assertNull(results.get(2).getErrorCause());
|
||||
assertEquals("BAR", results.get(2).getUserValue());
|
||||
|
||||
assertEquals("test", binder.getModel().getString());
|
||||
assertEquals(3, binder.getModel().getInteger());
|
||||
assertEquals(FooEnum.BAR, binder.getModel().getFoo());
|
||||
}
|
||||
|
||||
// TODO should update error context, not throw exception
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
@Test
|
||||
public void bindSingleValuesWithDefaultTypeCoversionFailure() {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
propertyValues.put("string", "test");
|
||||
propertyValues.put("integer", "bogus");
|
||||
propertyValues.put("foo", "bogus");
|
||||
binder.bind(propertyValues);
|
||||
List<UserValue> values = new ArrayList<UserValue>();
|
||||
values.add(new UserValue("string", "test"));
|
||||
// bad value
|
||||
values.add(new UserValue("integer", "bogus"));
|
||||
values.add(new UserValue("foo", "BAR"));
|
||||
List<BindingResult> results = binder.bind(values);
|
||||
assertEquals(3, results.size());
|
||||
assertTrue(results.get(1).isError());
|
||||
assertEquals("typeConversionFailure", results.get(1).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindSingleValuePropertyFormatter() throws ParseException {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
binder.add(new BindingConfiguration("date", new DateFormatter()));
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
propertyValues.put("date", "2009-06-01");
|
||||
binder.bind(propertyValues);
|
||||
binder.bind(UserValue.singleton("date", "2009-06-01"));
|
||||
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), binder.getModel().getDate());
|
||||
}
|
||||
|
||||
// TODO should update error context, not throw exception
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
@Test
|
||||
public void bindSingleValuePropertyFormatterParseException() {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
binder.add(new BindingConfiguration("date", new DateFormatter()));
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
propertyValues.put("date", "bogus");
|
||||
binder.bind(propertyValues);
|
||||
binder.bind(UserValue.singleton("date", "bogus"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindSingleValueWithFormatterRegistedByType() throws ParseException {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
binder.add(new DateFormatter(), Date.class);
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
propertyValues.put("date", "2009-06-01");
|
||||
binder.bind(propertyValues);
|
||||
binder.bind(UserValue.singleton("date", "2009-06-01"));
|
||||
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), binder.getModel().getDate());
|
||||
}
|
||||
|
||||
@@ -95,9 +108,7 @@ public class BinderTests {
|
||||
public void bindSingleValueWithFormatterRegisteredByAnnotation() throws ParseException {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
binder.add(new CurrencyFormatter(), CurrencyFormat.class);
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
propertyValues.put("currency", "$23.56");
|
||||
binder.bind(propertyValues);
|
||||
binder.bind(UserValue.singleton("currency", "$23.56"));
|
||||
assertEquals(new BigDecimal("23.56"), binder.getModel().getCurrency());
|
||||
}
|
||||
|
||||
@@ -105,20 +116,28 @@ public class BinderTests {
|
||||
public void bindSingleValueWithnAnnotationFormatterFactoryRegistered() throws ParseException {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
binder.add(new CurrencyAnnotationFormatterFactory());
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
propertyValues.put("currency", "$23.56");
|
||||
binder.bind(propertyValues);
|
||||
binder.bind(UserValue.singleton("currency", "$23.56"));
|
||||
assertEquals(new BigDecimal("23.56"), binder.getModel().getCurrency());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindSingleValuePropertyNotFound() throws ParseException {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
List<BindingResult> results = binder.bind(UserValue.singleton("bogus", "2009-06-01"));
|
||||
assertEquals(1, results.size());
|
||||
assertTrue(results.get(0).isError());
|
||||
assertEquals("propertyNotFound", results.get(0).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBindingOptimistic() {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
Binding b = binder.getBinding("integer");
|
||||
assertFalse(b.isCollection());
|
||||
assertEquals("0", b.getValue());
|
||||
b.setValue("5");
|
||||
BindingResult result = b.setValue("5");
|
||||
assertEquals("5", b.getValue());
|
||||
assertFalse(result.isError());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -131,8 +150,9 @@ public class BinderTests {
|
||||
b = binder.getBinding("integer");
|
||||
assertFalse(b.isCollection());
|
||||
assertEquals("0", b.getValue());
|
||||
b.setValue("5");
|
||||
BindingResult result = b.setValue("5");
|
||||
assertEquals("5", b.getValue());
|
||||
assertFalse(result.isError());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -172,21 +192,24 @@ public class BinderTests {
|
||||
assertEquals("BAZ", values[1]);
|
||||
assertEquals("BOOP", values[2]);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void getBindingMultiValuedTypeConversionError() {
|
||||
|
||||
@Test
|
||||
public void getBindingMultiValuedTypeConversionFailure() {
|
||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||
Binding b = binder.getBinding("foos");
|
||||
assertTrue(b.isCollection());
|
||||
assertEquals(0, b.getValues().length);
|
||||
b.setValues(new String[] { "BAR", "BOGUS", "BOOP" });
|
||||
BindingResult result = b.setValues(new String[] { "BAR", "BOGUS", "BOOP" });
|
||||
assertTrue(result.isError());
|
||||
assertTrue(result.getErrorCause() instanceof EvaluationException);
|
||||
assertEquals("typeConversionFailure", result.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindHandleNullValueInNestedPath() {
|
||||
TestBean testbean = new TestBean();
|
||||
Binder<TestBean> binder = new Binder<TestBean>(testbean);
|
||||
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||
List<UserValue> values = new ArrayList<UserValue>();
|
||||
|
||||
// EL configured with some options from SpelExpressionParserConfiguration:
|
||||
// (see where Binder creates the parser)
|
||||
@@ -195,27 +218,28 @@ public class BinderTests {
|
||||
// are new instances of the type of the list entry, they are not null.
|
||||
// not currently doing anything for maps or arrays
|
||||
|
||||
propertyValues.put("addresses[0].street", "4655 Macy Lane");
|
||||
propertyValues.put("addresses[0].city", "Melbourne");
|
||||
propertyValues.put("addresses[0].state", "FL");
|
||||
propertyValues.put("addresses[0].state", "35452");
|
||||
values.add(new UserValue("addresses[0].street", "4655 Macy Lane"));
|
||||
values.add(new UserValue("addresses[0].city", "Melbourne"));
|
||||
values.add(new UserValue("addresses[0].state", "FL"));
|
||||
values.add(new UserValue("addresses[0].state", "35452"));
|
||||
|
||||
// Auto adds new Address at 1
|
||||
propertyValues.put("addresses[1].street", "1234 Rostock Circle");
|
||||
propertyValues.put("addresses[1].city", "Palm Bay");
|
||||
propertyValues.put("addresses[1].state", "FL");
|
||||
propertyValues.put("addresses[1].state", "32901");
|
||||
values.add(new UserValue("addresses[1].street", "1234 Rostock Circle"));
|
||||
values.add(new UserValue("addresses[1].city", "Palm Bay"));
|
||||
values.add(new UserValue("addresses[1].state", "FL"));
|
||||
values.add(new UserValue("addresses[1].state", "32901"));
|
||||
|
||||
// Auto adds new Address at 5 (plus intermediates 2,3,4)
|
||||
propertyValues.put("addresses[5].street", "1234 Rostock Circle");
|
||||
propertyValues.put("addresses[5].city", "Palm Bay");
|
||||
propertyValues.put("addresses[5].state", "FL");
|
||||
propertyValues.put("addresses[5].state", "32901");
|
||||
values.add(new UserValue("addresses[5].street", "1234 Rostock Circle"));
|
||||
values.add(new UserValue("addresses[5].city", "Palm Bay"));
|
||||
values.add(new UserValue("addresses[5].state", "FL"));
|
||||
values.add(new UserValue("addresses[5].state", "32901"));
|
||||
|
||||
binder.bind(propertyValues);
|
||||
List<BindingResult> results = binder.bind(values);
|
||||
Assert.assertEquals(6,testbean.addresses.size());
|
||||
Assert.assertEquals("Palm Bay",testbean.addresses.get(1).city);
|
||||
Assert.assertNotNull(testbean.addresses.get(2));
|
||||
assertEquals(12, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user