polish
This commit is contained in:
parent
8079262705
commit
a696d78bd1
|
|
@ -40,7 +40,7 @@ public class Binder<T> {
|
||||||
|
|
||||||
private TypeConverter typeConverter;
|
private TypeConverter typeConverter;
|
||||||
|
|
||||||
private boolean optimisticBinding = true;
|
private boolean strict = false;
|
||||||
|
|
||||||
private static Formatter defaultFormatter = new Formatter() {
|
private static Formatter defaultFormatter = new Formatter() {
|
||||||
|
|
||||||
|
|
@ -76,6 +76,10 @@ public class Binder<T> {
|
||||||
typeConverter = new DefaultTypeConverter();
|
typeConverter = new DefaultTypeConverter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setStrict(boolean strict) {
|
||||||
|
this.strict = strict;
|
||||||
|
}
|
||||||
|
|
||||||
public Binding add(BindingConfiguration binding) {
|
public Binding add(BindingConfiguration binding) {
|
||||||
Binding newBinding;
|
Binding newBinding;
|
||||||
try {
|
try {
|
||||||
|
|
@ -91,12 +95,11 @@ public class Binder<T> {
|
||||||
if (propertyType == null) {
|
if (propertyType == null) {
|
||||||
propertyType = formatter.getFormattedObjectType();
|
propertyType = formatter.getFormattedObjectType();
|
||||||
}
|
}
|
||||||
typeFormatters.put(propertyType, formatter);
|
if (propertyType.isAnnotation()) {
|
||||||
}
|
annotationFormatters.put(propertyType, formatter);
|
||||||
|
} else {
|
||||||
public void addAnnotationBasedFormatter(Formatter<?> formatter,
|
typeFormatters.put(propertyType, formatter);
|
||||||
Class<?> propertyAnnotationClass) {
|
}
|
||||||
annotationFormatters.put(propertyAnnotationClass, formatter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T getModel() {
|
public T getModel() {
|
||||||
|
|
@ -105,8 +108,8 @@ public class Binder<T> {
|
||||||
|
|
||||||
public Binding getBinding(String property) {
|
public Binding getBinding(String property) {
|
||||||
Binding binding = bindings.get(property);
|
Binding binding = bindings.get(property);
|
||||||
if (binding == null && optimisticBinding) {
|
if (binding == null && !strict) {
|
||||||
return add(new BindingConfiguration(property, null, false));
|
return add(new BindingConfiguration(property, null));
|
||||||
} else {
|
} else {
|
||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
|
|
@ -133,13 +136,10 @@ public class Binder<T> {
|
||||||
|
|
||||||
private Formatter formatter;
|
private Formatter formatter;
|
||||||
|
|
||||||
private boolean required;
|
|
||||||
|
|
||||||
public BindingImpl(BindingConfiguration config)
|
public BindingImpl(BindingConfiguration config)
|
||||||
throws org.springframework.expression.ParseException {
|
throws org.springframework.expression.ParseException {
|
||||||
property = expressionParser.parseExpression(config.getProperty());
|
property = expressionParser.parseExpression(config.getProperty());
|
||||||
formatter = config.getFormatter();
|
formatter = config.getFormatter();
|
||||||
required = config.isRequired();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getValue() {
|
public String getValue() {
|
||||||
|
|
@ -198,10 +198,6 @@ public class Binder<T> {
|
||||||
setValue(values);
|
setValue(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRequired() {
|
|
||||||
return required;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BindingFailures getFailures() {
|
public BindingFailures getFailures() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,9 @@ public class BindingConfiguration {
|
||||||
|
|
||||||
private Formatter<?> formatter;
|
private Formatter<?> formatter;
|
||||||
|
|
||||||
private boolean required;
|
public BindingConfiguration(String property, Formatter<?> formatter) {
|
||||||
|
|
||||||
public BindingConfiguration(String property, Formatter<?> formatter, boolean required) {
|
|
||||||
this.property = property;
|
this.property = property;
|
||||||
this.formatter = formatter;
|
this.formatter = formatter;
|
||||||
this.required = required;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProperty() {
|
public String getProperty() {
|
||||||
|
|
@ -24,8 +21,4 @@ public class BindingConfiguration {
|
||||||
return formatter;
|
return formatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRequired() {
|
|
||||||
return required;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package org.springframework.ui.binding;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
|
|
@ -18,7 +19,6 @@ import junit.framework.Assert;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
import org.springframework.ui.format.DateFormatter;
|
import org.springframework.ui.format.DateFormatter;
|
||||||
|
|
@ -63,7 +63,7 @@ public class BinderTests {
|
||||||
@Test
|
@Test
|
||||||
public void bindSingleValuePropertyFormatterParsing() throws ParseException {
|
public void bindSingleValuePropertyFormatterParsing() throws ParseException {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
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>();
|
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||||
propertyValues.put("date", "2009-06-01");
|
propertyValues.put("date", "2009-06-01");
|
||||||
binder.bind(propertyValues);
|
binder.bind(propertyValues);
|
||||||
|
|
@ -74,7 +74,7 @@ public class BinderTests {
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected=IllegalArgumentException.class)
|
||||||
public void bindSingleValuePropertyFormatterParseException() {
|
public void bindSingleValuePropertyFormatterParseException() {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
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>();
|
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||||
propertyValues.put("date", "bogus");
|
propertyValues.put("date", "bogus");
|
||||||
binder.bind(propertyValues);
|
binder.bind(propertyValues);
|
||||||
|
|
@ -93,7 +93,7 @@ public class BinderTests {
|
||||||
@Test
|
@Test
|
||||||
public void bindSingleValueAnnotationFormatterParsing() throws ParseException {
|
public void bindSingleValueAnnotationFormatterParsing() throws ParseException {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
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>();
|
Map<String, String> propertyValues = new HashMap<String, String>();
|
||||||
propertyValues.put("currency", "$23.56");
|
propertyValues.put("currency", "$23.56");
|
||||||
binder.bind(propertyValues);
|
binder.bind(propertyValues);
|
||||||
|
|
@ -110,10 +110,24 @@ public class BinderTests {
|
||||||
assertEquals("5", b.getValue());
|
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
|
@Test
|
||||||
public void getBindingCustomFormatter() {
|
public void getBindingCustomFormatter() {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
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");
|
Binding b = binder.getBinding("currency");
|
||||||
assertFalse(b.isCollection());
|
assertFalse(b.isCollection());
|
||||||
assertEquals("", b.getValue());
|
assertEquals("", b.getValue());
|
||||||
|
|
@ -125,7 +139,7 @@ public class BinderTests {
|
||||||
public void getBindingMultiValued() {
|
public void getBindingMultiValued() {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||||
Binding b = binder.getBinding("foos");
|
Binding b = binder.getBinding("foos");
|
||||||
// TODO should work - assertTrue(b.isCollection());
|
assertTrue(b.isCollection());
|
||||||
assertEquals(0, b.getValues().length);
|
assertEquals(0, b.getValues().length);
|
||||||
b.setValues(new String[] { "BAR", "BAZ", "BOOP" });
|
b.setValues(new String[] { "BAR", "BAZ", "BOOP" });
|
||||||
assertEquals(FooEnum.BAR, binder.getModel().getFoos().get(0));
|
assertEquals(FooEnum.BAR, binder.getModel().getFoos().get(0));
|
||||||
|
|
@ -142,7 +156,7 @@ public class BinderTests {
|
||||||
public void getBindingMultiValuedTypeConversionError() {
|
public void getBindingMultiValuedTypeConversionError() {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||||
Binding b = binder.getBinding("foos");
|
Binding b = binder.getBinding("foos");
|
||||||
// TODO should work -- assertTrue(b.isCollection());
|
assertTrue(b.isCollection());
|
||||||
assertEquals(0, b.getValues().length);
|
assertEquals(0, b.getValues().length);
|
||||||
b.setValues(new String[] { "BAR", "BOGUS", "BOOP" });
|
b.setValues(new String[] { "BAR", "BOGUS", "BOOP" });
|
||||||
}
|
}
|
||||||
|
|
@ -186,21 +200,11 @@ public class BinderTests {
|
||||||
@Test
|
@Test
|
||||||
public void formatPossibleValue() {
|
public void formatPossibleValue() {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
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");
|
Binding b = binder.getBinding("currency");
|
||||||
assertEquals("$5.00", b.format(new BigDecimal("5")));
|
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 {
|
public static enum FooEnum {
|
||||||
BAR, BAZ, BOOP;
|
BAR, BAZ, BOOP;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue