mvc binding tests

conversion context change
This commit is contained in:
Keith Donald
2008-03-20 22:00:14 +00:00
parent ddee81604a
commit b7acf27f31
38 changed files with 379 additions and 171 deletions

View File

@@ -47,6 +47,6 @@ public interface ConversionExecutor {
* @param source the source object to convert
* @param context the conversion context, useful for influencing the behavior of the converter
*/
public Object execute(Object source, ConversionContext context) throws ConversionException;
public Object execute(Object source, Object context) throws ConversionException;
}

View File

@@ -52,6 +52,6 @@ public interface Converter {
* @return the converted object, an instance of the target type
* @throws ConversionException an exception occurred during the type conversion
*/
public Object convert(Object source, Class targetClass, ConversionContext context) throws ConversionException;
public Object convert(Object source, Class targetClass, Object context) throws ConversionException;
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.binding.convert.ConversionException;
import org.springframework.binding.convert.Converter;
@@ -58,11 +57,11 @@ public abstract class AbstractConverter implements Converter {
* @return the converted object
* @throws ConversionException an exception occured converting the source value
*/
public Object convert(Object source, ConversionContext context) throws ConversionException {
public Object convert(Object source, Object context) throws ConversionException {
return convert(source, getTargetClasses()[0], context);
}
public Object convert(Object source, Class targetClass, ConversionContext context) throws ConversionException {
public Object convert(Object source, Class targetClass, Object context) throws ConversionException {
try {
return doConvert(source, targetClass, context);
} catch (ConversionException e) {
@@ -82,8 +81,8 @@ public abstract class AbstractConverter implements Converter {
* @param targetClass the target type to convert to
* @param context an optional conversion context that may be used to influence the conversion process, could be null
* @return the converted source value
* @throws Exception an exception occured, will be wrapped in a conversion exception if necessary
* @throws Exception an exception occurred, will be wrapped in a conversion exception if necessary
*/
protected abstract Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception;
protected abstract Object doConvert(Object source, Class targetClass, Object context) throws Exception;
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.binding.format.FormatterRegistry;
import org.springframework.util.Assert;
@@ -45,7 +44,7 @@ public abstract class AbstractFormattingConverter extends AbstractConverter {
return formatterRegistry;
}
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
protected Object doConvert(Object source, Class targetClass, Object context) throws Exception {
return getFormatterRegistry().getFormatter(targetClass).parseValue((String) source);
}
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.binding.convert.ConversionException;
import org.springframework.binding.convert.ConversionExecutor;
import org.springframework.binding.convert.Converter;
@@ -91,7 +90,7 @@ class ConversionExecutorImpl implements ConversionExecutor {
return execute(source, null);
}
public Object execute(Object source, ConversionContext context) throws ConversionException {
public Object execute(Object source, Object context) throws ConversionException {
if (targetClass.isInstance(source)) {
// source is already assignment compatible with target class
return source;

View File

@@ -1,35 +0,0 @@
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.binding.convert.ConversionException;
import org.springframework.binding.convert.ConversionExecutor;
import org.springframework.binding.format.Formatter;
public class FormatterBackedConversionExecutor implements ConversionExecutor {
private Formatter formatter;
private Class targetClass;
public FormatterBackedConversionExecutor(Formatter formatter, Class targetClass) {
this.formatter = formatter;
this.targetClass = targetClass;
}
public Object execute(Object source) throws ConversionException {
return execute(source, null);
}
public Object execute(Object source, ConversionContext context) throws ConversionException {
return formatter.parseValue((String) source);
}
public Class getSourceClass() {
return String.class;
}
public Class getTargetClass() {
return targetClass;
}
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
/**
* Package private converter that is a "no op".
@@ -36,7 +35,7 @@ class NoOpConverter extends AbstractConverter {
this.targetClass = targetClass;
}
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
protected Object doConvert(Object source, Class targetClass, Object context) throws Exception {
return source;
}

View File

@@ -1,6 +1,5 @@
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.binding.convert.ConversionException;
import org.springframework.binding.convert.ConversionExecutor;
import org.springframework.binding.convert.ConversionService;
@@ -52,7 +51,7 @@ public class RuntimeBindingConversionExecutor implements ConversionExecutor {
return execute(source, null);
}
public Object execute(Object source, ConversionContext context) throws ConversionException {
public Object execute(Object source, Object context) throws ConversionException {
return conversionService.getConversionExecutor(source.getClass(), targetClass).execute(source);
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.util.StringUtils;
/**
@@ -71,7 +70,7 @@ public class TextToBoolean extends AbstractConverter {
return new Class[] { Boolean.class };
}
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
protected Object doConvert(Object source, Class targetClass, Object context) throws Exception {
String text = (String) source;
if (!StringUtils.hasText(text)) {
return null;

View File

@@ -20,7 +20,6 @@ import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.core.enums.LabeledEnum;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@@ -53,7 +52,7 @@ public class TextToClass extends AbstractConverter {
return new Class[] { Class.class };
}
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
protected Object doConvert(Object source, Class targetClass, Object context) throws Exception {
String text = (String) source;
if (StringUtils.hasText(text)) {
text = text.trim();

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.core.enums.LabeledEnum;
import org.springframework.core.enums.LabeledEnumResolver;
import org.springframework.core.enums.StaticLabeledEnumResolver;
@@ -37,7 +36,7 @@ public class TextToLabeledEnum extends AbstractConverter {
return new Class[] { LabeledEnum.class };
}
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
protected Object doConvert(Object source, Class targetClass, Object context) throws Exception {
String label = (String) source;
return labeledEnumResolver.getLabeledEnumByLabel(targetClass, label);
}

View File

@@ -26,11 +26,10 @@ import org.springframework.util.ObjectUtils;
*/
public final class StaticExpression implements Expression {
/**
* The value expression.
*/
private Object value;
private String expressionString;
/**
* Create a static evaluator for the given value.
* @param value the value
@@ -64,11 +63,19 @@ public final class StaticExpression implements Expression {
}
public Class getValueType(Object context) {
return Object.class;
return value.getClass();
}
public String getExpressionString() {
return null;
return expressionString;
}
/**
* Sets the static expression string.
* @param expressionString the static expression string
*/
public void setExpressionString(String expressionString) {
this.expressionString = expressionString;
}
public String toString() {

View File

@@ -23,6 +23,7 @@ import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.mapping.Mapping;
import org.springframework.core.style.StylerUtils;
import org.springframework.util.Assert;
/**
@@ -128,7 +129,7 @@ public class DefaultMapping implements Mapping {
if (sourceValue != null) {
if (typeConverter != null) {
try {
targetValue = typeConverter.execute(targetValue);
targetValue = typeConverter.execute(targetValue, context);
} catch (ConversionException e) {
context.setTypeConversionErrorResult(sourceValue, e.getTargetClass());
return;
@@ -147,7 +148,7 @@ public class DefaultMapping implements Mapping {
try {
ConversionExecutor typeConverter = conversionService.getConversionExecutor(sourceValue
.getClass(), targetType);
targetValue = typeConverter.execute(sourceValue);
targetValue = typeConverter.execute(sourceValue, context);
} catch (ConversionException e) {
context.setTypeConversionErrorResult(sourceValue, e.getTargetClass());
return;
@@ -156,12 +157,15 @@ public class DefaultMapping implements Mapping {
}
}
}
if (logger.isDebugEnabled()) {
logger.debug("Mapping '" + sourceExpression + "' value [" + sourceValue + "] to target '"
+ targetExpression + "'; setting target value to [" + targetValue + "]");
}
try {
targetExpression.setValue(context.getTarget(), targetValue);
if (logger.isDebugEnabled()) {
String sourceType = sourceValue != null ? sourceValue.getClass().getName() : "null";
String targetType = targetValue != null ? targetValue.getClass().getName() : "null";
logger.debug("Mapped source [" + sourceType + "] " + sourceExpression + " value "
+ StylerUtils.style(sourceValue) + " to target [" + targetType + "] " + targetExpression
+ " value " + StylerUtils.style(targetValue));
}
context.setSuccessResult(sourceValue, targetValue);
} catch (EvaluationException e) {
context.setTargetAccessError(sourceValue, e);
@@ -191,4 +195,5 @@ public class DefaultMapping implements Mapping {
public String toString() {
return sourceExpression + " -> " + targetExpression;
}
}

View File

@@ -49,6 +49,10 @@ class DefaultMappingContext implements MappingContext {
return conversionService;
}
public Mapping getCurrentMapping() {
return currentMapping;
}
public void setCurrentMapping(Mapping mapping) {
if (this.currentMapping != null) {
throw new IllegalStateException("The current mapping has not finished yet");

View File

@@ -42,6 +42,12 @@ public interface MappingContext {
*/
public ConversionService getConversionService();
/**
* Returns the current mapping.
* @return the current mapping
*/
public Mapping getCurrentMapping();
/**
* Sets the current mapping. Called when a single mapping operation is about to begin. This updates progress of the
* overall mapping transaction.