提交 1a31d259 编写于 作者: A Andy Clement

Parser error message improvements: enhanced harness and used it in new parser error message tests

上级 cf35116a
......@@ -267,10 +267,19 @@ public abstract class ExpressionTestCase extends TestCase {
*/
protected void parseAndCheckError(String expression, SpelMessages expectedMessage, Object... otherProperties) {
try {
Expression expr = parser.parseExpression(expression);
SpelExpression expr = parser.parseExpression(expression);
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
fail("Parsing should have failed!");
} catch (ParseException pe) {
SpelException ex = (SpelException) pe.getCause();
Throwable t = pe.getCause();
if (t == null) {
fail("ParseException caught with no defined cause");
}
if (!(t instanceof SpelException)) {
t.printStackTrace();
fail("Cause of parse exception is not a SpelException");
}
SpelException ex = (SpelException) t;
if (ex.getMessageUnformatted() != expectedMessage) {
System.out.println(ex.getMessage());
ex.printStackTrace();
......
......@@ -21,19 +21,39 @@ package org.springframework.expression.spel;
* @author Andy Clement
*/
public class ParserErrorMessagesTests extends ExpressionTestCase {
// TODO (asc) extract expected insert messages into constants (just in case of changes)?
// TODO (asc) review poor messages, marked // POOR below
public void testBrokenExpression01() {
// Expression: 0xCAFEBABE - too big to be processed as an int, needs the L suffix
// will not fit into an int, needs L suffix
parseAndCheckError("0xCAFEBABE", SpelMessages.NOT_AN_INTEGER);
evaluate("0xCAFEBABEL", 0xCAFEBABEL, Long.class);
}
// parseCheck("true or ");
// parseCheck("tru or false");
// parseCheck("1 + ");
// parseCheck("0xCAFEBABEG");
// TODO 3 too many close brackets - parser recover
// public void testExpressionLists07a() { parseCheck("((3;4;)+(5;6;)))","((3;4)
// + (5;6))");}
// }
// ---
public void testBrokenExpression02() {
// rogue 'G' on the end
parseAndCheckError("0xB0BG", SpelMessages.PARSE_PROBLEM, 5, "mismatched input 'G' expecting EOF");
}
public void testBrokenExpression03() {
// too many closing brackets
parseAndCheckError("((3;4;)+(5;6;)))", SpelMessages.PARSE_PROBLEM, 15, "mismatched input ')' expecting EOF");
evaluate("((3;4;)+(5;6;))", 10 /* 4+6 */, Integer.class);
}
public void testBrokenExpression04() {
// missing right operand
parseAndCheckError("true or ", SpelMessages.PARSE_PROBLEM, -1, "no viable alternative at input '<EOF>'"); // POOR
}
public void testBrokenExpression05() {
// missing right operand
parseAndCheckError("1 + ", SpelMessages.PARSE_PROBLEM, -1, "no viable alternative at input '<EOF>'"); // POOR
}
public void testBrokenExpression06() {
// expression list missing surrounding parentheses
parseAndCheckError("1;2;3", SpelMessages.PARSE_PROBLEM, 1, "mismatched input ';' expecting EOF"); // POOR
evaluate("(1;2;3)", 3, Integer.class);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册