BinderTests - two ignored tests reactivated. Binder - modified how annotation based formatters are registered/looked up
This commit is contained in:
parent
28848f3132
commit
ac9e42fa29
|
|
@ -33,7 +33,7 @@ public class Binder<T> {
|
||||||
|
|
||||||
private Map<Class<?>, Formatter<?>> typeFormatters = new HashMap<Class<?>, Formatter<?>>();
|
private Map<Class<?>, Formatter<?>> typeFormatters = new HashMap<Class<?>, Formatter<?>>();
|
||||||
|
|
||||||
private Map<Annotation, Formatter<?>> annotationFormatters = new HashMap<Annotation, Formatter<?>>();
|
private Map<Class<?>, Formatter<?>> annotationFormatters = new HashMap<Class<?>, Formatter<?>>();
|
||||||
|
|
||||||
private ExpressionParser expressionParser;
|
private ExpressionParser expressionParser;
|
||||||
|
|
||||||
|
|
@ -90,8 +90,9 @@ public class Binder<T> {
|
||||||
typeFormatters.put(propertyType, formatter);
|
typeFormatters.put(propertyType, formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Formatter<?> formatter, Annotation propertyAnnotation) {
|
public void addAnnotationBasedFormatter(Formatter<?> formatter,
|
||||||
annotationFormatters.put(propertyAnnotation, formatter);
|
Class<?> propertyAnnotationClass) {
|
||||||
|
annotationFormatters.put(propertyAnnotationClass, formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T getModel() {
|
public T getModel() {
|
||||||
|
|
@ -222,7 +223,7 @@ public class Binder<T> {
|
||||||
} else {
|
} else {
|
||||||
Annotation[] annotations = getAnnotations();
|
Annotation[] annotations = getAnnotations();
|
||||||
for (Annotation a : annotations) {
|
for (Annotation a : annotations) {
|
||||||
formatter = annotationFormatters.get(a);
|
formatter = annotationFormatters.get(a.annotationType());
|
||||||
if (formatter != null) {
|
if (formatter != null) {
|
||||||
return formatter;
|
return formatter;
|
||||||
}
|
}
|
||||||
|
|
@ -234,7 +235,6 @@ public class Binder<T> {
|
||||||
|
|
||||||
private Class<?> getValueType() {
|
private Class<?> getValueType() {
|
||||||
try {
|
try {
|
||||||
// TODO Spring EL currently returns null here when value is null - not correct
|
|
||||||
return property.getValueType(createEvaluationContext());
|
return property.getValueType(createEvaluationContext());
|
||||||
} catch (EvaluationException e) {
|
} catch (EvaluationException e) {
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
|
|
@ -242,8 +242,12 @@ public class Binder<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Annotation[] getAnnotations() {
|
private Annotation[] getAnnotations() {
|
||||||
// TODO Spring EL presently gives us no way to get this information
|
try {
|
||||||
return new Annotation[0];
|
return property.getValueTypeDescriptor(
|
||||||
|
createEvaluationContext()).getAnnotations();
|
||||||
|
} catch (EvaluationException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copy(Iterable<?> values, String[] formattedValues) {
|
private void copy(Iterable<?> values, String[] formattedValues) {
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
@ -77,29 +79,21 @@ public class BinderTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void bindSingleValueTypeFormatterParsing() throws ParseException {
|
public void bindSingleValueTypeFormatterParsing() throws ParseException {
|
||||||
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
Binder<TestBean> binder = new Binder<TestBean>(new TestBean());
|
||||||
binder.add(new DateFormatter(), Date.class);
|
binder.add(new DateFormatter(), Date.class);
|
||||||
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");
|
||||||
// TODO presently fails because Spring EL does not obtain property valueType using property metadata
|
|
||||||
// instead it relies on value itself being not null
|
|
||||||
// talk to andy about this
|
|
||||||
binder.bind(propertyValues);
|
binder.bind(propertyValues);
|
||||||
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), binder.getModel().getDate());
|
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), binder.getModel().getDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
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.add(new CurrencyFormatter(), Currency.class);
|
binder.addAnnotationBasedFormatter(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");
|
||||||
// TODO presently fails because Spring EL does not obtain property valueType using property metadata
|
|
||||||
// instead it relies on value itself being not null
|
|
||||||
// talk to andy about this
|
|
||||||
binder.bind(propertyValues);
|
binder.bind(propertyValues);
|
||||||
assertEquals(new BigDecimal("23.56"), binder.getModel().getCurrency());
|
assertEquals(new BigDecimal("23.56"), binder.getModel().getCurrency());
|
||||||
}
|
}
|
||||||
|
|
@ -259,6 +253,7 @@ public class BinderTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Currency {
|
public @interface Currency {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue