more tests

This commit is contained in:
Keith Donald
2009-06-01 23:03:19 +00:00
parent 3b42200cda
commit 6ea55e9bda
7 changed files with 150 additions and 61 deletions

View File

@@ -137,7 +137,7 @@ public class Binder<T> {
required = config.isRequired();
}
public String getFormattedValue() {
public String getValue() {
try {
return format(property.getValue(createEvaluationContext()));
} catch (ExpressionException e) {
@@ -146,15 +146,13 @@ public class Binder<T> {
}
public void setValue(String formatted) {
Object value = parse(formatted);
assertRequired(value);
setValue(value);
setValue(parse(formatted));
}
public String format(Object possibleValue) {
public String format(Object selectableValue) {
Formatter formatter = getFormatter();
possibleValue = typeConverter.convert(possibleValue, formatter.getFormattedObjectType());
return formatter.format(possibleValue, LocaleContextHolder.getLocale());
selectableValue = typeConverter.convert(selectableValue, formatter.getFormattedObjectType());
return formatter.format(selectableValue, LocaleContextHolder.getLocale());
}
public boolean isCollection() {
@@ -162,7 +160,7 @@ public class Binder<T> {
return type.isCollection() || type.isArray();
}
public String[] getFormattedValues() {
public String[] getValues() {
Object multiValue;
try {
multiValue = property.getValue(createEvaluationContext());
@@ -199,18 +197,12 @@ public class Binder<T> {
return required;
}
public Messages getMessages() {
public BindingFailures getFailures() {
return null;
}
// internal helpers
private void assertRequired(Object value) {
if (required && value == null) {
throw new IllegalArgumentException("Value required");
}
}
private Object parse(String formatted) {
try {
return getFormatter().parse(formatted, LocaleContextHolder.getLocale());
@@ -254,7 +246,7 @@ public class Binder<T> {
return new Annotation[0];
}
private void copy(Iterable values, String[] formattedValues) {
private void copy(Iterable<?> values, String[] formattedValues) {
int i = 0;
for (Object value : values) {
formattedValues[i] = format(value);

View File

@@ -4,24 +4,22 @@ public interface Binding {
// single-value properties
String getFormattedValue();
String getValue();
void setValue(String formatted);
String format(Object possibleValue);
String format(Object selectableValue);
// multi-value properties
boolean isCollection();
String[] getFormattedValues();
String[] getValues();
void setValues(String[] formattedValues);
// validation metadata
boolean isRequired();
Messages getMessages();
BindingFailures getFailures();
}

View File

@@ -0,0 +1,15 @@
package org.springframework.ui.binding;
import java.util.Map;
public interface BindingFailure {
String getCode();
String getSeverity();
// TODO - where does arg formatting occur
Map<String, Object> getArgs();
Map<String, String> getDetails();
}

View File

@@ -2,14 +2,14 @@ package org.springframework.ui.binding;
import java.util.List;
public interface Messages {
public interface BindingFailures {
int getCount();
Severity getMaximumSeverity();
List<Message> getAll();
List<BindingFailure> getAll();
List<Message> getBySeverity(Severity severity);
List<BindingFailure> getBySeverity(Severity severity);
}

View File

@@ -1,9 +0,0 @@
package org.springframework.ui.binding;
public interface Message {
String getText();
String getSeverity();
}

View File

@@ -41,6 +41,7 @@ public interface Formatter<T> {
/**
* Parse an object from its formatted representation.
* TODO - remove dependency on java.text by replacing ParseException?
* @param formatted a formatted representation
* @param locale the user's locale
* @return the parsed object