updated tests

This commit is contained in:
Keith Donald 2009-07-22 19:37:26 +00:00
parent 6c5fb23e79
commit 6e05d3bd6e
3 changed files with 106 additions and 94 deletions

View File

@ -19,14 +19,14 @@ import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.style.ToStringCreator;
import org.springframework.ui.binding.Binding;
import org.springframework.ui.binding.BindingResults;
import org.springframework.ui.binding.MissingSourceValuesException;
import org.springframework.ui.binding.Binding.BindingStatus;
import org.springframework.ui.binding.binder.BindingResults;
import org.springframework.ui.binding.binder.GenericBinder;
import org.springframework.ui.binding.binder.MissingSourceValuesException;
import org.springframework.ui.format.AnnotationFormatterFactory;
import org.springframework.ui.format.Formatted;
import org.springframework.ui.format.Formatter;
@ -39,13 +39,16 @@ import org.springframework.ui.message.MockMessageSource;
public class GenericBinderTests {
private GenericBinder binder;
private GenericBindingFactory bindingFactory;
private TestBean bean;
@Before
public void setUp() {
bean = new TestBean();
binder = new GenericBinder(bean);
bindingFactory = new GenericBindingFactory(bean);
binder = new GenericBinder(bindingFactory);
LocaleContextHolder.setLocale(Locale.US);
}
@ -56,8 +59,6 @@ public class GenericBinderTests {
@Test
public void bindSingleValuesWithDefaultTypeConverterConversion() {
GenericBinder binder = new GenericBinder(bean);
Map<String, String> values = new LinkedHashMap<String, String>();
values.put("string", "test");
values.put("integer", "3");
@ -97,26 +98,26 @@ public class GenericBinderTests {
@Test
public void bindSingleValuePropertyFormatter() throws ParseException {
binder.bindingRule("date").formatWith(new DateFormatter());
bindingFactory.bindingRule("date").formatWith(new DateFormatter());
binder.bind(Collections.singletonMap("date", "2009-06-01"));
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
}
@Test
public void bindSingleValuePropertyFormatterParseException() {
binder.bindingRule("date").formatWith(new DateFormatter());
bindingFactory.bindingRule("date").formatWith(new DateFormatter());
BindingResults results = binder.bind(Collections.singletonMap("date", "bogus"));
assertEquals(1, results.size());
assertTrue(results.get(0).isFailure());
assertEquals("typeMismatch", results.get(0).getAlert().getCode());
assertEquals("typeMismatch", results.get(0).getAlert().getCode());
}
@Test
public void bindSingleValueWithFormatterRegistedByType() throws ParseException {
GenericFormatterRegistry formatterRegistry = new GenericFormatterRegistry();
formatterRegistry.add(Date.class, new DateFormatter());
binder.setFormatterRegistry(formatterRegistry);
bindingFactory.setFormatterRegistry(formatterRegistry);
binder.bind(Collections.singletonMap("date", "2009-06-01"));
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
}
@ -125,7 +126,7 @@ public class GenericBinderTests {
public void bindSingleValueWithAnnotationFormatterFactoryRegistered() throws ParseException {
GenericFormatterRegistry formatterRegistry = new GenericFormatterRegistry();
formatterRegistry.add(new CurrencyAnnotationFormatterFactory());
binder.setFormatterRegistry(formatterRegistry);
bindingFactory.setFormatterRegistry(formatterRegistry);
binder.bind(Collections.singletonMap("currency", "$23.56"));
assertEquals(new BigDecimal("23.56"), bean.getCurrency());
@ -139,19 +140,17 @@ public class GenericBinderTests {
assertEquals("propertyNotFound", results.get(0).getAlert().getCode());
}
@Test(expected=MissingSourceValuesException.class)
@Test(expected = MissingSourceValuesException.class)
public void bindMissingRequiredSourceValue() {
binder.setRequired(new String[] {
"integer"
});
binder.setRequired(new String[] { "integer" });
// missing "integer" - violated bind contract
binder.bind(Collections.singletonMap("string", "test"));
}
@Test
public void getBindingCustomFormatter() {
binder.bindingRule("currency").formatWith(new CurrencyFormatter());
Binding b = binder.getBinding("currency");
bindingFactory.bindingRule("currency").formatWith(new CurrencyFormatter());
Binding b = bindingFactory.getBinding("currency");
assertFalse(b.isList());
assertFalse(b.isMap());
assertEquals(null, b.getValue());
@ -169,8 +168,8 @@ public class GenericBinderTests {
@Test
public void getBindingCustomFormatterRequiringTypeCoersion() {
// IntegerFormatter formats Longs, so conversion from Integer -> Long is performed
binder.bindingRule("integer").formatWith(new IntegerFormatter());
Binding b = binder.getBinding("integer");
bindingFactory.bindingRule("integer").formatWith(new IntegerFormatter());
Binding b = bindingFactory.getBinding("integer");
b.applySourceValue("2,300");
assertEquals("2,300", b.getRenderValue());
b.commit();
@ -183,17 +182,18 @@ public class GenericBinderTests {
MockMessageSource messages = new MockMessageSource();
messages.addMessage("typeMismatch", Locale.US,
"Please enter an integer in format ### for the #{label} field; you entered #{value}");
binder.setMessageSource(messages);
binder.bindingRule("integer").formatWith(new IntegerFormatter());
Binding b = binder.getBinding("integer");
bindingFactory.setMessageSource(messages);
bindingFactory.bindingRule("integer").formatWith(new IntegerFormatter());
Binding b = bindingFactory.getBinding("integer");
b.applySourceValue("bogus");
assertEquals("Please enter an integer in format ### for the integer field; you entered bogus", b.getStatusAlert().getMessage());
assertEquals("Please enter an integer in format ### for the integer field; you entered bogus", b
.getStatusAlert().getMessage());
}
@SuppressWarnings("unchecked")
@Test
public void getBindingMultiValued() {
Binding b = binder.getBinding("foos");
Binding b = bindingFactory.getBinding("foos");
assertTrue(b.isList());
assertEquals(null, b.getValue());
assertEquals("", b.getRenderValue());
@ -213,7 +213,7 @@ public class GenericBinderTests {
@Test
public void getBindingMultiValuedIndexAccess() {
bean.setFoos(Arrays.asList(new FooEnum[] { FooEnum.BAR }));
Binding b = binder.getBinding("foos[0]");
Binding b = bindingFactory.getBinding("foos[0]");
assertFalse(b.isList());
assertEquals(FooEnum.BAR, b.getValue());
assertEquals("BAR", b.getRenderValue());
@ -224,7 +224,7 @@ public class GenericBinderTests {
@Test
public void getBindingMultiValuedTypeConversionFailure() {
Binding b = binder.getBinding("foos");
Binding b = bindingFactory.getBinding("foos");
assertTrue(b.isList());
assertEquals(null, b.getValue());
b.applySourceValue(new String[] { "BAR", "BOGUS", "BOOP" });
@ -235,7 +235,8 @@ public class GenericBinderTests {
@Test
public void bindToList() {
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" });
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);
assertEquals(3, bean.addresses.size());
assertEquals("4655 Macy Lane", bean.addresses.get(0).street);
@ -243,14 +244,14 @@ public class GenericBinderTests {
assertEquals("FL", bean.addresses.get(0).state);
assertEquals("35452", bean.addresses.get(0).zip);
}
@Test
public void bindToListElements() {
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[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);
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);
@ -262,9 +263,11 @@ public class GenericBinderTests {
public void bindToListSingleString() {
GenericFormatterRegistry formatterRegistry = new GenericFormatterRegistry();
formatterRegistry.add(new GenericCollectionPropertyType(List.class, Address.class), new AddressListFormatter());
binder.setFormatterRegistry(formatterRegistry);
bindingFactory.setFormatterRegistry(formatterRegistry);
Map<String, String> values = new LinkedHashMap<String, String>();
values.put("addresses", "4655 Macy Lane:Melbourne:FL:35452,1234 Rostock Circle:Palm Bay:FL:32901,1977 Bel Aire Estates:Coker:AL:12345");
values
.put("addresses",
"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);
@ -280,13 +283,14 @@ public class GenericBinderTests {
assertEquals("AL", bean.addresses.get(2).state);
assertEquals("12345", bean.addresses.get(2).zip);
}
@Test
@Ignore
public void bindToListSingleStringNoListFormatter() {
Map<String, String> values = new LinkedHashMap<String, String>();
values.put("addresses", "4655 Macy Lane:Melbourne:FL:35452,1234 Rostock Circle:Palm Bay:FL:32901,1977 Bel Aire Estates:Coker:AL:12345");
BindingResults results = binder.bind(values);
values
.put("addresses",
"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);
@ -301,12 +305,12 @@ public class GenericBinderTests {
assertEquals("AL", bean.addresses.get(2).state);
assertEquals("12345", bean.addresses.get(2).zip);
}
@Test
public void getListAsSingleString() {
GenericFormatterRegistry formatterRegistry = new GenericFormatterRegistry();
formatterRegistry.add(new GenericCollectionPropertyType(List.class, Address.class), new AddressListFormatter());
binder.setFormatterRegistry(formatterRegistry);
bindingFactory.setFormatterRegistry(formatterRegistry);
Address address1 = new Address();
address1.setStreet("s1");
address1.setCity("c1");
@ -321,12 +325,11 @@ public class GenericBinderTests {
addresses.add(address1);
addresses.add(address2);
bean.addresses = addresses;
String value = binder.getBinding("addresses").getRenderValue();
String value = bindingFactory.getBinding("addresses").getRenderValue();
assertEquals("s1:c1:st1:z1,s2:c2:st2:z2,", value);
}
@Test
@Ignore
public void getListAsSingleStringNoFormatter() {
Address address1 = new Address();
address1.setStreet("s1");
@ -342,8 +345,8 @@ public class GenericBinderTests {
addresses.add(address1);
addresses.add(address2);
bean.addresses = addresses;
String value = binder.getBinding("addresses").getRenderValue();
assertEquals("s1:c1:st1:z1,s2:c2:st2:z2,", value);
String value = bindingFactory.getBinding("addresses").getRenderValue();
assertEquals("s1:c1:st1:z1,s2:c2:st2:z2", value);
}
@Test
@ -379,10 +382,9 @@ public class GenericBinderTests {
}
@Test
@Ignore
public void bindToMap() {
Map<String, String[]> values = new LinkedHashMap<String, String[]>();
values.put("favoriteFoodsByGroup", new String[] { "DAIRY=Milk", "FRUIT=Peaches", "MEAT=Ham" });
values.put("favoriteFoodsByGroup", new String[] { "DAIRY=Milk", "FRUIT=Peaches", "MEAT=Ham" });
BindingResults results = binder.bind(values);
Assert.assertEquals(3, bean.favoriteFoodsByGroup.size());
assertEquals("Milk", bean.favoriteFoodsByGroup.get(FoodGroup.DAIRY));
@ -391,40 +393,40 @@ public class GenericBinderTests {
}
@Test
@Ignore
public void bindToMapElements() {
Map<String, String> values = new LinkedHashMap<String, String>();
values.put("favoriteFoodsByGroup['DAIRY']", "Milk");
values.put("favoriteFoodsByGroup['FRUIT']", "Peaches");
values.put("favoriteFoodsByGroup['MEAT']", "Ham");
values.put("favoriteFoodsByGroup[DAIRY]", "Milk");
values.put("favoriteFoodsByGroup[FRUIT]", "Peaches");
values.put("favoriteFoodsByGroup[MEAT]", "Ham");
BindingResults results = binder.bind(values);
System.out.println(results);
Assert.assertEquals(3, bean.favoriteFoodsByGroup.size());
assertEquals("Milk", bean.favoriteFoodsByGroup.get(FoodGroup.DAIRY));
assertEquals("Peaches", bean.favoriteFoodsByGroup.get(FoodGroup.FRUIT));
assertEquals("Ham", bean.favoriteFoodsByGroup.get(FoodGroup.MEAT));
}
@Test
public void bindToMapSingleString() {
Map<String, String> values = new LinkedHashMap<String, String>();
values.put("favoriteFoodsByGroup", "DAIRY=Milk FRUIT=Peaches MEAT=Ham");
BindingResults results = binder.bind(values);
binder.bind(values);
Assert.assertEquals(3, bean.favoriteFoodsByGroup.size());
assertEquals("Milk", bean.favoriteFoodsByGroup.get(FoodGroup.DAIRY));
assertEquals("Peaches", bean.favoriteFoodsByGroup.get(FoodGroup.FRUIT));
assertEquals("Ham", bean.favoriteFoodsByGroup.get(FoodGroup.MEAT));
}
@Test
@Ignore
public void getMapAsSingleString() {
Map<FoodGroup, String> foods = new LinkedHashMap<FoodGroup, String>();
foods.put(FoodGroup.DAIRY, "Milk");
foods.put(FoodGroup.FRUIT, "Peaches");
foods.put(FoodGroup.MEAT, "Ham");
bean.favoriteFoodsByGroup = foods;
String value = binder.getBinding("favoriteFoodsByGroup").getRenderValue();
assertEquals("DAIRY=Milk FRUIT=Peaches MEAT=Ham", value);
String value = bindingFactory.getBinding("favoriteFoodsByGroup").getRenderValue();
// TODO this is inconsistent with previous test case
assertEquals("{DAIRY=Milk, FRUIT=Peaches, MEAT=Ham}", value);
}
@Test
@ -434,14 +436,21 @@ public class GenericBinderTests {
binder.bind(values);
Assert.assertEquals("Melbourne", bean.primaryAddress.city);
}
@Test
public void formatPossibleValue() {
binder.bindingRule("currency").formatWith(new CurrencyFormatter());
Binding b = binder.getBinding("currency");
bindingFactory.bindingRule("currency").formatWith(new CurrencyFormatter());
Binding b = bindingFactory.getBinding("currency");
assertEquals("$5.00", b.formatValue(new BigDecimal("5")));
}
@Test
public void formatPossibleValueDefault() {
bindingFactory.bindingRule("currency");
Binding b = bindingFactory.getBinding("currency");
assertEquals("5", b.formatValue(new BigDecimal("5")));
}
public static enum FooEnum {
BAR, BAZ, BOOP;
}
@ -449,7 +458,7 @@ public class GenericBinderTests {
public static enum FoodGroup {
DAIRY, VEG, FRUIT, BREAD, MEAT
}
public static class TestBean {
private String string;
private int integer;
@ -460,10 +469,10 @@ public class GenericBinderTests {
private List<Address> addresses;
private Map<FoodGroup, String> favoriteFoodsByGroup;
private Address primaryAddress;
public TestBean() {
}
public String getString() {
return string;
}
@ -536,7 +545,7 @@ public class GenericBinderTests {
public void setPrimaryAddress(Address primaryAddress) {
this.primaryAddress = primaryAddress;
}
public String toString() {
return new ToStringCreator(this).append("addressses", addresses).toString();
}
@ -557,9 +566,9 @@ public class GenericBinderTests {
address.setZip(fields[3]);
return address;
}
}
public static class AddressListFormatter implements Formatter<List<Address>> {
public String format(List<Address> addresses, Locale locale) {
@ -579,9 +588,9 @@ public class GenericBinderTests {
}
return addresses;
}
}
@Formatted(AddressFormatter.class)
public static class Address {
private String street;
@ -631,7 +640,8 @@ public class GenericBinderTests {
}
public String toString() {
return new ToStringCreator(this).append("street", street).append("city", city).append("state", state).append("zip", zip).toString();
return new ToStringCreator(this).append("street", street).append("city", city).append("state", state)
.append("zip", zip).toString();
}
}

View File

@ -15,7 +15,8 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.binding.BindingResults;
import org.springframework.ui.binding.binder.BindingResults;
import org.springframework.ui.binding.binder.WebBinder;
import org.springframework.ui.format.date.DateFormatter;
import org.springframework.ui.format.number.CurrencyFormat;
import org.springframework.ui.format.number.CurrencyFormatter;
@ -23,19 +24,19 @@ import org.springframework.ui.format.number.CurrencyFormatter;
public class WebBinderTests {
TestBean bean = new TestBean();
WebBinder binder = new WebBinder(bean);
WebBinder binder = new WebBinder(new GenericBindingFactory(bean));
@Before
public void setUp() {
LocaleContextHolder.setLocale(Locale.US);
}
@After
public void tearDown() {
LocaleContextHolder.setLocale(null);
}
@Test
@Ignore
public void bindUserValuesCreatedFromUserMap() throws ParseException {
@ -55,10 +56,10 @@ public class WebBinderTests {
assertEquals("test", results.get(0).getSourceValue());
assertEquals(null, results.get(1).getSourceValue());
assertEquals(Boolean.FALSE, results.get(2).getSourceValue());
assertEquals("2009-06-10", results.get(3).getSourceValue());
assertEquals("2009-06-10", results.get(3).getSourceValue());
assertEquals("$5.00", results.get(4).getSourceValue());
assertEquals(null, results.get(5).getSourceValue());
assertEquals("test", bean.getString());
assertEquals(0, bean.getInteger());
assertEquals(new DateFormatter().parse("2009-06-10", Locale.US), bean.getDate());
@ -66,7 +67,7 @@ public class WebBinderTests {
assertEquals(new BigDecimal("5.00"), bean.getCurrency());
assertEquals(null, bean.getAddresses());
}
public static enum FooEnum {
BAR, BAZ, BOOP;
}
@ -80,7 +81,7 @@ public class WebBinderTests {
private BigDecimal currency;
private List<FooEnum> foos;
private List<Address> addresses;
public String getString() {
return string;
}
@ -96,7 +97,7 @@ public class WebBinderTests {
public void setInteger(int integer) {
this.integer = integer;
}
public boolean isBool() {
return bool;
}
@ -145,7 +146,7 @@ public class WebBinderTests {
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
}
public static class Address {
@ -154,46 +155,46 @@ public class WebBinderTests {
private String state;
private String zip;
private String country;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
}

View File

@ -15,7 +15,8 @@ import org.springframework.ui.alert.Alert;
import org.springframework.ui.alert.Alerts;
import org.springframework.ui.alert.Severity;
import org.springframework.ui.alert.support.DefaultAlertContext;
import org.springframework.ui.binding.support.WebBinder;
import org.springframework.ui.binding.binder.WebBinder;
import org.springframework.ui.binding.support.GenericBindingFactory;
import org.springframework.ui.format.number.CurrencyFormat;
import org.springframework.ui.validation.ValidationFailure;
import org.springframework.ui.validation.Validator;
@ -37,7 +38,7 @@ public class BindAndValidateLifecycleTests {
public void setUp() {
model = new TestBean();
alertContext = new DefaultAlertContext();
WebBinder binder = new WebBinder(model);
WebBinder binder = new WebBinder(new GenericBindingFactory(model));
Validator validator = new TestBeanValidator();
lifecycle = new BindAndValidateLifecycleImpl(binder, validator, alertContext);
}