cause linking tests and improvements

This commit is contained in:
Keith Donald
2008-11-04 12:10:16 +00:00
parent 4014d389ef
commit ed67d269c0
3 changed files with 61 additions and 15 deletions

View File

@@ -91,7 +91,7 @@ class OgnlExpression implements Expression {
} else {
throw new EvaluationException(context.getClass(), getExpressionString(),
"An OgnlException occurred getting the value for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e.getReason());
+ "' on context [" + context.getClass() + "]", causeFor(e));
}
}
}
@@ -109,24 +109,11 @@ class OgnlExpression implements Expression {
} else {
throw new EvaluationException(context.getClass(), getExpressionString(),
"An OgnlException occurred setting the value of expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "] to [" + value + "]", e.getReason());
+ "' on context [" + context.getClass() + "] to [" + value + "]", causeFor(e));
}
}
}
private TypeConverter createTypeConverter() {
return new TypeConverter() {
public Object convertValue(Map context, Object target, Member member, String propertyName, Object value,
Class toType) throws ValueCoercionException {
try {
return conversionService.executeConversion(value, toType);
} catch (ConversionException e) {
throw new ValueCoercionException(context.getClass(), expressionString, value, toType, e);
}
}
};
}
public Class getValueType(Object context) {
try {
// OGNL has no native way to get this information
@@ -144,6 +131,36 @@ class OgnlExpression implements Expression {
return expressionString;
}
// internal helpers
private Throwable causeFor(OgnlException e) {
if (e.getReason() != null) {
if (e.getCause() == null) {
try {
e.initCause(e.getReason());
} catch (IllegalStateException ex) {
// we tried
}
}
return e;
} else {
return e;
}
}
private TypeConverter createTypeConverter() {
return new TypeConverter() {
public Object convertValue(Map context, Object target, Member member, String propertyName, Object value,
Class toType) throws ValueCoercionException {
try {
return conversionService.executeConversion(value, toType);
} catch (ConversionException e) {
throw new ValueCoercionException(context.getClass(), expressionString, value, toType, e);
}
}
};
}
private Map getVariables(Object context) {
if (variableExpressions == null) {
return Collections.EMPTY_MAP;

View File

@@ -17,6 +17,7 @@ package org.springframework.binding.expression.ognl;
import junit.framework.TestCase;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionVariable;
import org.springframework.binding.expression.ParserException;
@@ -215,4 +216,24 @@ public class OgnlExpressionParserTests extends TestCase {
}
}
public void testReasonCauseLinkingGetValue() {
String exp = "getException()";
Expression e = parser.parseExpression(exp, null);
try {
e.getValue(bean);
} catch (EvaluationException ex) {
assertTrue(ex.getCause().getCause() instanceof IllegalStateException);
}
}
public void testReasonCauseLinkingSetValue() {
String exp = "exceptionProperty";
Expression e = parser.parseExpression(exp, null);
try {
e.setValue(bean, "does not matter");
} catch (EvaluationException ex) {
assertTrue(ex.getCause().getCause() instanceof IllegalStateException);
}
}
}

View File

@@ -61,4 +61,12 @@ public class TestBean {
this.date = date;
}
public Exception getException() {
throw new IllegalStateException("Test");
}
public void setExceptionProperty(String whatever) {
throw new IllegalStateException("Test");
}
}