Browse Source

polish

conversation
Keith Donald 16 years ago
parent
commit
a696d78bd1
  1. 28
      org.springframework.context/src/main/java/org/springframework/ui/binding/Binder.java
  2. 9
      org.springframework.context/src/main/java/org/springframework/ui/binding/BindingConfiguration.java
  3. 40
      org.springframework.context/src/test/java/org/springframework/ui/binding/BinderTests.java

28
org.springframework.context/src/main/java/org/springframework/ui/binding/Binder.java

@ -40,7 +40,7 @@ public class Binder<T> { @@ -40,7 +40,7 @@ public class Binder<T> {
private TypeConverter typeConverter;
private boolean optimisticBinding = true;
private boolean strict = false;
private static Formatter defaultFormatter = new Formatter() {
@ -75,6 +75,10 @@ public class Binder<T> { @@ -75,6 +75,10 @@ public class Binder<T> {
expressionParser = new SpelExpressionParser(parserConfig);
typeConverter = new DefaultTypeConverter();
}
public void setStrict(boolean strict) {
this.strict = strict;
}
public Binding add(BindingConfiguration binding) {
Binding newBinding;
@ -91,12 +95,11 @@ public class Binder<T> { @@ -91,12 +95,11 @@ public class Binder<T> {
if (propertyType == null) {
propertyType = formatter.getFormattedObjectType();
}
typeFormatters.put(propertyType, formatter);
}
public void addAnnotationBasedFormatter(Formatter<?> formatter,
Class<?> propertyAnnotationClass) {
annotationFormatters.put(propertyAnnotationClass, formatter);
if (propertyType.isAnnotation()) {
annotationFormatters.put(propertyType, formatter);
} else {
typeFormatters.put(propertyType, formatter);
}
}
public T getModel() {
@ -105,8 +108,8 @@ public class Binder<T> { @@ -105,8 +108,8 @@ public class Binder<T> {
public Binding getBinding(String property) {
Binding binding = bindings.get(property);
if (binding == null && optimisticBinding) {
return add(new BindingConfiguration(property, null, false));
if (binding == null && !strict) {
return add(new BindingConfiguration(property, null));
} else {
return binding;
}
@ -133,13 +136,10 @@ public class Binder<T> { @@ -133,13 +136,10 @@ public class Binder<T> {
private Formatter formatter;
private boolean required;
public BindingImpl(BindingConfiguration config)
throws org.springframework.expression.ParseException {
property = expressionParser.parseExpression(config.getProperty());
formatter = config.getFormatter();
required = config.isRequired();
}
public String getValue() {
@ -198,10 +198,6 @@ public class Binder<T> { @@ -198,10 +198,6 @@ public class Binder<T> {
setValue(values);
}
public boolean isRequired() {
return required;
}
public BindingFailures getFailures() {
return null;
}

9
org.springframework.context/src/main/java/org/springframework/ui/binding/BindingConfiguration.java

@ -8,12 +8,9 @@ public class BindingConfiguration { @@ -8,12 +8,9 @@ public class BindingConfiguration {
private Formatter<?> formatter;
private boolean required;
public BindingConfiguration(String property, Formatter<?> formatter, boolean required) {
public BindingConfiguration(String property, Formatter<?> formatter) {
this.property = property;
this.formatter = formatter;
this.required = required;
}
public String getProperty() {
@ -24,8 +21,4 @@ public class BindingConfiguration { @@ -24,8 +21,4 @@ public class BindingConfiguration {
return formatter;
}
public boolean isRequired() {
return required;
}
}

40
org.springframework.context/src/test/java/org/springframework/ui/binding/BinderTests.java

@ -2,6 +2,7 @@ package org.springframework.ui.binding; @@ -2,6 +2,7 @@ package org.springframework.ui.binding;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.lang.annotation.Retention;
@ -18,7 +19,6 @@ import junit.framework.Assert; @@ -18,7 +19,6 @@ 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.ui.format.DateFormatter;
@ -63,7 +63,7 @@ public class BinderTests { @@ -63,7 +63,7 @@ public class BinderTests {
@Test
public void bindSingleValuePropertyFormatterParsing() throws ParseException {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
binder.add(new BindingConfiguration("date", new DateFormatter(), false));
binder.add(new BindingConfiguration("date", new DateFormatter()));
Map<String, String> propertyValues = new HashMap<String, String>();
propertyValues.put("date", "2009-06-01");
binder.bind(propertyValues);
@ -74,7 +74,7 @@ public class BinderTests { @@ -74,7 +74,7 @@ public class BinderTests {
@Test(expected=IllegalArgumentException.class)
public void bindSingleValuePropertyFormatterParseException() {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
binder.add(new BindingConfiguration("date", new DateFormatter(), false));
binder.add(new BindingConfiguration("date", new DateFormatter()));
Map<String, String> propertyValues = new HashMap<String, String>();
propertyValues.put("date", "bogus");
binder.bind(propertyValues);
@ -93,7 +93,7 @@ public class BinderTests { @@ -93,7 +93,7 @@ public class BinderTests {
@Test
public void bindSingleValueAnnotationFormatterParsing() throws ParseException {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
binder.addAnnotationBasedFormatter(new CurrencyFormatter(), Currency.class);
binder.add(new CurrencyFormatter(), Currency.class);
Map<String, String> propertyValues = new HashMap<String, String>();
propertyValues.put("currency", "$23.56");
binder.bind(propertyValues);
@ -110,10 +110,24 @@ public class BinderTests { @@ -110,10 +110,24 @@ public class BinderTests {
assertEquals("5", b.getValue());
}
@Test
public void getBindingStrict() {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
binder.setStrict(true);
Binding b = binder.getBinding("integer");
assertNull(b);
binder.add(new BindingConfiguration("integer", null));
b = binder.getBinding("integer");
assertFalse(b.isCollection());
assertEquals("0", b.getValue());
b.setValue("5");
assertEquals("5", b.getValue());
}
@Test
public void getBindingCustomFormatter() {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
binder.add(new BindingConfiguration("currency", new CurrencyFormatter(), false));
binder.add(new BindingConfiguration("currency", new CurrencyFormatter()));
Binding b = binder.getBinding("currency");
assertFalse(b.isCollection());
assertEquals("", b.getValue());
@ -125,7 +139,7 @@ public class BinderTests { @@ -125,7 +139,7 @@ public class BinderTests {
public void getBindingMultiValued() {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
Binding b = binder.getBinding("foos");
// TODO should work - assertTrue(b.isCollection());
assertTrue(b.isCollection());
assertEquals(0, b.getValues().length);
b.setValues(new String[] { "BAR", "BAZ", "BOOP" });
assertEquals(FooEnum.BAR, binder.getModel().getFoos().get(0));
@ -142,7 +156,7 @@ public class BinderTests { @@ -142,7 +156,7 @@ public class BinderTests {
public void getBindingMultiValuedTypeConversionError() {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
Binding b = binder.getBinding("foos");
// TODO should work -- assertTrue(b.isCollection());
assertTrue(b.isCollection());
assertEquals(0, b.getValues().length);
b.setValues(new String[] { "BAR", "BOGUS", "BOOP" });
}
@ -186,21 +200,11 @@ public class BinderTests { @@ -186,21 +200,11 @@ public class BinderTests {
@Test
public void formatPossibleValue() {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
binder.add(new BindingConfiguration("currency", new CurrencyFormatter(), false));
binder.add(new BindingConfiguration("currency", new CurrencyFormatter()));
Binding b = binder.getBinding("currency");
assertEquals("$5.00", b.format(new BigDecimal("5")));
}
@Test
public void getBindingRequiredConstraint() {
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
// TODO add constraint API
binder.add(new BindingConfiguration("string", null, true));
Binding b = binder.getBinding("string");
assertEquals("", b.getValue());
b.setValue("");
}
public static enum FooEnum {
BAR, BAZ, BOOP;
}

Loading…
Cancel
Save