提交 14eba503 编写于 作者: J Juergen Hoeller

Consistent ExpressionException-style quoting of expression string and position

Issue: SPR-14942
上级 b10045dc
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,7 +26,24 @@ package org.springframework.expression; ...@@ -26,7 +26,24 @@ package org.springframework.expression;
public class EvaluationException extends ExpressionException { public class EvaluationException extends ExpressionException {
/** /**
* Creates a new expression evaluation exception. * Create a new expression evaluation exception.
* @param message description of the problem that occurred
*/
public EvaluationException(String message) {
super(message);
}
/**
* Create a new expression evaluation exception.
* @param message description of the problem that occurred
* @param cause the underlying cause of this exception
*/
public EvaluationException(String message, Throwable cause) {
super(message,cause);
}
/**
* Create a new expression evaluation exception.
* @param position the position in the expression where the problem occurred * @param position the position in the expression where the problem occurred
* @param message description of the problem that occurred * @param message description of the problem that occurred
*/ */
...@@ -35,7 +52,7 @@ public class EvaluationException extends ExpressionException { ...@@ -35,7 +52,7 @@ public class EvaluationException extends ExpressionException {
} }
/** /**
* Creates a new expression evaluation exception. * Create a new expression evaluation exception.
* @param expressionString the expression that could not be evaluated * @param expressionString the expression that could not be evaluated
* @param message description of the problem that occurred * @param message description of the problem that occurred
*/ */
...@@ -44,7 +61,7 @@ public class EvaluationException extends ExpressionException { ...@@ -44,7 +61,7 @@ public class EvaluationException extends ExpressionException {
} }
/** /**
* Creates a new expression evaluation exception. * Create a new expression evaluation exception.
* @param position the position in the expression where the problem occurred * @param position the position in the expression where the problem occurred
* @param message description of the problem that occurred * @param message description of the problem that occurred
* @param cause the underlying cause of this exception * @param cause the underlying cause of this exception
...@@ -53,16 +70,4 @@ public class EvaluationException extends ExpressionException { ...@@ -53,16 +70,4 @@ public class EvaluationException extends ExpressionException {
super(position, message, cause); super(position, message, cause);
} }
/**
* Creates a new expression evaluation exception.
* @param message description of the problem that occurred
*/
public EvaluationException(String message) {
super(message);
}
public EvaluationException(String message, Throwable cause) {
super(message,cause);
}
} }
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -20,6 +20,7 @@ package org.springframework.expression; ...@@ -20,6 +20,7 @@ package org.springframework.expression;
* Super class for exceptions that can occur whilst processing expressions. * Super class for exceptions that can occur whilst processing expressions.
* *
* @author Andy Clement * @author Andy Clement
* @author Phil Webb
* @since 3.0 * @since 3.0
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
...@@ -27,68 +28,68 @@ public class ExpressionException extends RuntimeException { ...@@ -27,68 +28,68 @@ public class ExpressionException extends RuntimeException {
protected String expressionString; protected String expressionString;
protected int position; // -1 if not known - but should be known in all reasonable cases protected int position; // -1 if not known; should be known in all reasonable cases
/** /**
* Construct a new expression exception. * Construct a new expression exception.
* @param expressionString the expression string
* @param message a descriptive message * @param message a descriptive message
*/ */
public ExpressionException(String expressionString, String message) { public ExpressionException(String message) {
super(message); super(message);
this.position = -1;
this.expressionString = expressionString;
} }
/** /**
* Construct a new expression exception. * Construct a new expression exception.
* @param expressionString the expression string
* @param position the position in the expression string where the problem occurred
* @param message a descriptive message * @param message a descriptive message
* @param cause the underlying cause of this exception
*/ */
public ExpressionException(String expressionString, int position, String message) { public ExpressionException(String message, Throwable cause) {
super(message); super(message, cause);
this.position = position;
this.expressionString = expressionString;
} }
/** /**
* Construct a new expression exception. * Construct a new expression exception.
* @param position the position in the expression string where the problem occurred * @param expressionString the expression string
* @param message a descriptive message * @param message a descriptive message
*/ */
public ExpressionException(int position, String message) { public ExpressionException(String expressionString, String message) {
super(message); super(message);
this.position = position; this.expressionString = expressionString;
this.position = -1;
} }
/** /**
* Construct a new expression exception. * Construct a new expression exception.
* @param expressionString the expression string
* @param position the position in the expression string where the problem occurred * @param position the position in the expression string where the problem occurred
* @param message a descriptive message * @param message a descriptive message
* @param cause the underlying cause of this exception
*/ */
public ExpressionException(int position, String message, Throwable cause) { public ExpressionException(String expressionString, int position, String message) {
super(message,cause); super(message);
this.expressionString = expressionString;
this.position = position; this.position = position;
} }
/** /**
* Construct a new expression exception. * Construct a new expression exception.
* @param position the position in the expression string where the problem occurred
* @param message a descriptive message * @param message a descriptive message
*/ */
public ExpressionException(String message) { public ExpressionException(int position, String message) {
super(message); super(message);
this.position = position;
} }
/** /**
* Construct a new expression exception. * Construct a new expression exception.
* @param position the position in the expression string where the problem occurred
* @param message a descriptive message * @param message a descriptive message
* @param cause the underlying cause of this exception * @param cause the underlying cause of this exception
*/ */
public ExpressionException(String message, Throwable cause) { public ExpressionException(int position, String message, Throwable cause) {
super(message,cause); super(message,cause);
this.position = position;
} }
...@@ -107,8 +108,9 @@ public class ExpressionException extends RuntimeException { ...@@ -107,8 +108,9 @@ public class ExpressionException extends RuntimeException {
} }
/** /**
* Return the exception message. Since Spring 4.0 this method returns the * Return the exception message.
* same result as {@link #toDetailedString()}. * As of Spring 4.0, this method returns the same result as {@link #toDetailedString()}.
* @see #getSimpleMessage()
* @see java.lang.Throwable#getMessage() * @see java.lang.Throwable#getMessage()
*/ */
@Override @Override
...@@ -123,11 +125,11 @@ public class ExpressionException extends RuntimeException { ...@@ -123,11 +125,11 @@ public class ExpressionException extends RuntimeException {
public String toDetailedString() { public String toDetailedString() {
if (this.expressionString != null) { if (this.expressionString != null) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
output.append("Expression '"); output.append("Expression [");
output.append(this.expressionString); output.append(this.expressionString);
output.append("'"); output.append("]");
if (this.position != -1) { if (this.position >= 0) {
output.append(" @ "); output.append(" @");
output.append(this.position); output.append(this.position);
} }
output.append(": "); output.append(": ");
...@@ -142,6 +144,7 @@ public class ExpressionException extends RuntimeException { ...@@ -142,6 +144,7 @@ public class ExpressionException extends RuntimeException {
/** /**
* Return the exception simple message without including the expression * Return the exception simple message without including the expression
* that caused the failure. * that caused the failure.
* @since 4.0
*/ */
public String getSimpleMessage() { public String getSimpleMessage() {
return super.getMessage(); return super.getMessage();
......
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,7 +26,7 @@ package org.springframework.expression; ...@@ -26,7 +26,7 @@ package org.springframework.expression;
public class ParseException extends ExpressionException { public class ParseException extends ExpressionException {
/** /**
* Creates a new expression parsing exception. * Create a new expression parsing exception.
* @param expressionString the expression string that could not be parsed * @param expressionString the expression string that could not be parsed
* @param position the position in the expression string where the problem occurred * @param position the position in the expression string where the problem occurred
* @param message description of the problem that occurred * @param message description of the problem that occurred
...@@ -36,7 +36,7 @@ public class ParseException extends ExpressionException { ...@@ -36,7 +36,7 @@ public class ParseException extends ExpressionException {
} }
/** /**
* Creates a new expression parsing exception. * Create a new expression parsing exception.
* @param position the position in the expression string where the problem occurred * @param position the position in the expression string where the problem occurred
* @param message description of the problem that occurred * @param message description of the problem that occurred
* @param cause the underlying cause of this exception * @param cause the underlying cause of this exception
...@@ -46,7 +46,7 @@ public class ParseException extends ExpressionException { ...@@ -46,7 +46,7 @@ public class ParseException extends ExpressionException {
} }
/** /**
* Creates a new expression parsing exception. * Create a new expression parsing exception.
* @param position the position in the expression string where the problem occurred * @param position the position in the expression string where the problem occurred
* @param message description of the problem that occurred * @param message description of the problem that occurred
*/ */
......
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.expression.spel; package org.springframework.expression.spel;
import org.springframework.expression.EvaluationException; import org.springframework.expression.EvaluationException;
...@@ -23,6 +24,7 @@ import org.springframework.expression.EvaluationException; ...@@ -23,6 +24,7 @@ import org.springframework.expression.EvaluationException;
* message. See {@link SpelMessage} for the list of all possible messages that can occur. * message. See {@link SpelMessage} for the list of all possible messages that can occur.
* *
* @author Andy Clement * @author Andy Clement
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
...@@ -34,62 +36,46 @@ public class SpelEvaluationException extends EvaluationException { ...@@ -34,62 +36,46 @@ public class SpelEvaluationException extends EvaluationException {
public SpelEvaluationException(SpelMessage message, Object... inserts) { public SpelEvaluationException(SpelMessage message, Object... inserts) {
super(message.formatMessage(0, inserts)); // TODO poor position information, can the callers not really supply something? super(message.formatMessage(inserts));
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelEvaluationException(int position, SpelMessage message, Object... inserts) { public SpelEvaluationException(int position, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(position, inserts)); super(position, message.formatMessage(inserts));
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelEvaluationException(int position, Throwable cause, public SpelEvaluationException(int position, Throwable cause, SpelMessage message, Object... inserts) {
SpelMessage message, Object... inserts) { super(position, message.formatMessage(inserts),cause);
super(position,message.formatMessage(position,inserts),cause);
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelEvaluationException(Throwable cause, SpelMessage message, Object... inserts) { public SpelEvaluationException(Throwable cause, SpelMessage message, Object... inserts) {
super(message.formatMessage(0,inserts),cause); super(message.formatMessage(inserts), cause);
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
/** /**
* @return a formatted message with inserts applied * Set the position in the related expression which gave rise to this exception.
*/ */
@Override public void setPosition(int position) {
public String getMessage() { this.position = position;
if (this.message != null) {
return this.message.formatMessage(this.position, this.inserts);
}
else {
return super.getMessage();
}
} }
/** /**
* @return the message code * Return the message code.
*/ */
public SpelMessage getMessageCode() { public SpelMessage getMessageCode() {
return this.message; return this.message;
} }
/** /**
* Set the position in the related expression which gave rise to this exception. * Return the message inserts.
*
* @param position the position in the expression that gave rise to the exception
*/
public void setPosition(int position) {
this.position = position;
}
/**
* @return the message inserts
*/ */
public Object[] getInserts() { public Object[] getInserts() {
return this.inserts; return this.inserts;
......
...@@ -24,16 +24,13 @@ import java.text.MessageFormat; ...@@ -24,16 +24,13 @@ import java.text.MessageFormat;
* expect particular code numbers rather than particular text, enabling the message text * expect particular code numbers rather than particular text, enabling the message text
* to more easily be modified and the tests to run successfully in different locales. * to more easily be modified and the tests to run successfully in different locales.
* *
* <p>When a message is formatted, it will have this kind of form * <p>When a message is formatted, it will have this kind of form, capturing the prefix
* and the error kind:
* *
* <pre class="code"> * <pre class="code">EL1004E: Type cannot be found 'String'</pre>
* EL1004E: (pos 34): Type cannot be found 'String'
* </pre>
*
* The prefix captures the code and the error kind, whilst the position is included
* if it is known.
* *
* @author Andy Clement * @author Andy Clement
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
public enum SpelMessage { public enum SpelMessage {
...@@ -175,7 +172,7 @@ public enum SpelMessage { ...@@ -175,7 +172,7 @@ public enum SpelMessage {
"Cannot find terminating \" for string"), "Cannot find terminating \" for string"),
NON_TERMINATING_QUOTED_STRING(Kind.ERROR, 1046, NON_TERMINATING_QUOTED_STRING(Kind.ERROR, 1046,
"Cannot find terminating ' for string"), "Cannot find terminating '' for string"),
MISSING_LEADING_ZERO_FOR_NUMBER(Kind.ERROR, 1047, MISSING_LEADING_ZERO_FOR_NUMBER(Kind.ERROR, 1047,
"A real number must be prefixed by zero, it cannot start with just ''.''"), "A real number must be prefixed by zero, it cannot start with just ''.''"),
...@@ -190,7 +187,7 @@ public enum SpelMessage { ...@@ -190,7 +187,7 @@ public enum SpelMessage {
"The arguments '(...)' for the constructor call are missing"), "The arguments '(...)' for the constructor call are missing"),
RUN_OUT_OF_ARGUMENTS(Kind.ERROR, 1051, RUN_OUT_OF_ARGUMENTS(Kind.ERROR, 1051,
"Unexpected ran out of arguments"), "Unexpectedly ran out of arguments"),
UNABLE_TO_GROW_COLLECTION(Kind.ERROR, 1052, UNABLE_TO_GROW_COLLECTION(Kind.ERROR, 1052,
"Unable to grow collection"), "Unable to grow collection"),
...@@ -262,20 +259,42 @@ public enum SpelMessage { ...@@ -262,20 +259,42 @@ public enum SpelMessage {
private final String message; private final String message;
private SpelMessage(Kind kind, int code, String message) { SpelMessage(Kind kind, int code, String message) {
this.kind = kind; this.kind = kind;
this.code = code; this.code = code;
this.message = message; this.message = message;
} }
/**
* Produce a complete message including the prefix and with the inserts
* applied to the message.
* @param inserts the inserts to put into the formatted message
* @return a formatted message
* @since 4.3.5
*/
public String formatMessage(Object... inserts) {
StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("EL").append(this.code);
switch (this.kind) {
case ERROR:
formattedMessage.append("E");
break;
}
formattedMessage.append(": ");
formattedMessage.append(MessageFormat.format(this.message, inserts));
return formattedMessage.toString();
}
/** /**
* Produce a complete message including the prefix, the position (if known) * Produce a complete message including the prefix, the position (if known)
* and with the inserts applied to the message. * and with the inserts applied to the message.
* @param pos the position (ignored and not included in the message if less than 0) * @param pos the position (ignored and not included in the message if less than 0)
* @param inserts the inserts to put into the formatted message * @param inserts the inserts to put into the formatted message
* @return a formatted message * @return a formatted message
* @deprecated as of Spring 4.3.5, in favor of {@link #formatMessage(Object...)}
*/ */
@Deprecated
public String formatMessage(int pos, Object... inserts) { public String formatMessage(int pos, Object... inserts) {
StringBuilder formattedMessage = new StringBuilder(); StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("EL").append(this.code); formattedMessage.append("EL").append(this.code);
...@@ -285,7 +304,7 @@ public enum SpelMessage { ...@@ -285,7 +304,7 @@ public enum SpelMessage {
break; break;
} }
formattedMessage.append(":"); formattedMessage.append(":");
if (pos != -1) { if (pos >= 0) {
formattedMessage.append("(pos ").append(pos).append("): "); formattedMessage.append("(pos ").append(pos).append("): ");
} }
formattedMessage.append(MessageFormat.format(this.message, inserts)); formattedMessage.append(MessageFormat.format(this.message, inserts));
...@@ -293,6 +312,6 @@ public enum SpelMessage { ...@@ -293,6 +312,6 @@ public enum SpelMessage {
} }
public static enum Kind { INFO, WARNING, ERROR } public enum Kind { INFO, WARNING, ERROR }
} }
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.expression.spel; package org.springframework.expression.spel;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
...@@ -23,6 +24,7 @@ import org.springframework.expression.ParseException; ...@@ -23,6 +24,7 @@ import org.springframework.expression.ParseException;
* message. See {@link SpelMessage} for the list of all possible messages that can occur. * message. See {@link SpelMessage} for the list of all possible messages that can occur.
* *
* @author Andy Clement * @author Andy Clement
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
...@@ -34,45 +36,33 @@ public class SpelParseException extends ParseException { ...@@ -34,45 +36,33 @@ public class SpelParseException extends ParseException {
public SpelParseException(String expressionString, int position, SpelMessage message, Object... inserts) { public SpelParseException(String expressionString, int position, SpelMessage message, Object... inserts) {
super(expressionString, position, message.formatMessage(position,inserts)); super(expressionString, position, message.formatMessage(inserts));
this.position = position;
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelParseException(int position, SpelMessage message, Object... inserts) { public SpelParseException(int position, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(position,inserts)); super(position, message.formatMessage(inserts));
this.position = position;
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelParseException(int position, Throwable cause, SpelMessage message, Object... inserts) { public SpelParseException(int position, Throwable cause, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(position,inserts), cause); super(position, message.formatMessage(inserts), cause);
this.position = position;
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
/** /**
* @return a formatted message with inserts applied * Return the message code.
*/
@Override
public String getMessage() {
return (this.message != null ? this.message.formatMessage(this.position, this.inserts)
: super.getMessage());
}
/**
* @return the message code
*/ */
public SpelMessage getMessageCode() { public SpelMessage getMessageCode() {
return this.message; return this.message;
} }
/** /**
* @return the message inserts * Return the message inserts.
*/ */
public Object[] getInserts() { public Object[] getInserts() {
return this.inserts; return this.inserts;
......
...@@ -1663,15 +1663,15 @@ public class SpelReproTests extends AbstractExpressionTests { ...@@ -1663,15 +1663,15 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test @Test
public void SPR10146_malformedExpressions() throws Exception { public void SPR10146_malformedExpressions() throws Exception {
doTestSpr10146("/foo", "EL1070E:(pos 0): Problem parsing left operand"); doTestSpr10146("/foo", "EL1070E: Problem parsing left operand");
doTestSpr10146("*foo", "EL1070E:(pos 0): Problem parsing left operand"); doTestSpr10146("*foo", "EL1070E: Problem parsing left operand");
doTestSpr10146("%foo", "EL1070E:(pos 0): Problem parsing left operand"); doTestSpr10146("%foo", "EL1070E: Problem parsing left operand");
doTestSpr10146("<foo", "EL1070E:(pos 0): Problem parsing left operand"); doTestSpr10146("<foo", "EL1070E: Problem parsing left operand");
doTestSpr10146(">foo", "EL1070E:(pos 0): Problem parsing left operand"); doTestSpr10146(">foo", "EL1070E: Problem parsing left operand");
doTestSpr10146("&&foo", "EL1070E:(pos 0): Problem parsing left operand"); doTestSpr10146("&&foo", "EL1070E: Problem parsing left operand");
doTestSpr10146("||foo", "EL1070E:(pos 0): Problem parsing left operand"); doTestSpr10146("||foo", "EL1070E: Problem parsing left operand");
doTestSpr10146("&foo", "EL1069E:(pos 0): missing expected character '&'"); doTestSpr10146("&foo", "EL1069E: missing expected character '&'");
doTestSpr10146("|foo", "EL1069E:(pos 0): missing expected character '|'"); doTestSpr10146("|foo", "EL1069E: missing expected character '|'");
} }
private void doTestSpr10146(String expression, String expectedMessage) { private void doTestSpr10146(String expression, String expectedMessage) {
...@@ -1702,7 +1702,7 @@ public class SpelReproTests extends AbstractExpressionTests { ...@@ -1702,7 +1702,7 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test @Test
public void SPR10328() throws Exception { public void SPR10328() throws Exception {
thrown.expect(SpelParseException.class); thrown.expect(SpelParseException.class);
thrown.expectMessage("EL1071E:(pos 2): A required selection expression has not been specified"); thrown.expectMessage("EL1071E: A required selection expression has not been specified");
Expression exp = parser.parseExpression("$[]"); Expression exp = parser.parseExpression("$[]");
exp.getValue(Arrays.asList("foo", "bar", "baz")); exp.getValue(Arrays.asList("foo", "bar", "baz"));
} }
......
...@@ -19,7 +19,6 @@ package org.springframework.expression.spel.standard; ...@@ -19,7 +19,6 @@ package org.springframework.expression.spel.standard;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.ExpressionException; import org.springframework.expression.ExpressionException;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.spel.SpelMessage; import org.springframework.expression.spel.SpelMessage;
...@@ -33,11 +32,12 @@ import static org.junit.Assert.*; ...@@ -33,11 +32,12 @@ import static org.junit.Assert.*;
/** /**
* @author Andy Clement * @author Andy Clement
* @author Juergen Hoeller
*/ */
public class SpelParserTests { public class SpelParserTests {
@Test @Test
public void theMostBasic() throws EvaluationException, ParseException { public void theMostBasic() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2"); SpelExpression expr = parser.parseRaw("2");
assertNotNull(expr); assertNotNull(expr);
...@@ -48,7 +48,7 @@ public class SpelParserTests { ...@@ -48,7 +48,7 @@ public class SpelParserTests {
} }
@Test @Test
public void valueType() throws Exception { public void valueType() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
EvaluationContext ctx = new StandardEvaluationContext(); EvaluationContext ctx = new StandardEvaluationContext();
Class<?> c = parser.parseRaw("2").getValueType(); Class<?> c = parser.parseRaw("2").getValueType();
...@@ -64,7 +64,7 @@ public class SpelParserTests { ...@@ -64,7 +64,7 @@ public class SpelParserTests {
} }
@Test @Test
public void whitespace() throws EvaluationException, ParseException { public void whitespace() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2 + 3"); SpelExpression expr = parser.parseRaw("2 + 3");
assertEquals(5, expr.getValue()); assertEquals(5, expr.getValue());
...@@ -77,7 +77,7 @@ public class SpelParserTests { ...@@ -77,7 +77,7 @@ public class SpelParserTests {
} }
@Test @Test
public void arithmeticPlus1() throws EvaluationException, ParseException { public void arithmeticPlus1() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2+2"); SpelExpression expr = parser.parseRaw("2+2");
assertNotNull(expr); assertNotNull(expr);
...@@ -86,14 +86,14 @@ public class SpelParserTests { ...@@ -86,14 +86,14 @@ public class SpelParserTests {
} }
@Test @Test
public void arithmeticPlus2() throws EvaluationException, ParseException { public void arithmeticPlus2() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("37+41"); SpelExpression expr = parser.parseRaw("37+41");
assertEquals(78, expr.getValue()); assertEquals(78, expr.getValue());
} }
@Test @Test
public void arithmeticMultiply1() throws EvaluationException, ParseException { public void arithmeticMultiply1() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2*3"); SpelExpression expr = parser.parseRaw("2*3");
assertNotNull(expr); assertNotNull(expr);
...@@ -103,15 +103,14 @@ public class SpelParserTests { ...@@ -103,15 +103,14 @@ public class SpelParserTests {
} }
@Test @Test
public void arithmeticPrecedence1() throws EvaluationException, ParseException { public void arithmeticPrecedence1() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2*3+5"); SpelExpression expr = parser.parseRaw("2*3+5");
assertEquals(11, expr.getValue()); assertEquals(11, expr.getValue());
} }
@Test @Test
public void generalExpressions() throws Exception { public void generalExpressions() {
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String"); parser.parseRaw("new String");
...@@ -122,6 +121,7 @@ public class SpelParserTests { ...@@ -122,6 +121,7 @@ public class SpelParserTests {
SpelParseException spe = (SpelParseException) ex; SpelParseException spe = (SpelParseException) ex;
assertEquals(SpelMessage.MISSING_CONSTRUCTOR_ARGS, spe.getMessageCode()); assertEquals(SpelMessage.MISSING_CONSTRUCTOR_ARGS, spe.getMessageCode());
assertEquals(10, spe.getPosition()); assertEquals(10, spe.getPosition());
assertTrue(ex.getMessage().contains(ex.getExpressionString()));
} }
try { try {
...@@ -134,6 +134,7 @@ public class SpelParserTests { ...@@ -134,6 +134,7 @@ public class SpelParserTests {
SpelParseException spe = (SpelParseException) ex; SpelParseException spe = (SpelParseException) ex;
assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS, spe.getMessageCode()); assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS, spe.getMessageCode());
assertEquals(10, spe.getPosition()); assertEquals(10, spe.getPosition());
assertTrue(ex.getMessage().contains(ex.getExpressionString()));
} }
try { try {
...@@ -146,6 +147,7 @@ public class SpelParserTests { ...@@ -146,6 +147,7 @@ public class SpelParserTests {
SpelParseException spe = (SpelParseException) ex; SpelParseException spe = (SpelParseException) ex;
assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS, spe.getMessageCode()); assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS, spe.getMessageCode());
assertEquals(10, spe.getPosition()); assertEquals(10, spe.getPosition());
assertTrue(ex.getMessage().contains(ex.getExpressionString()));
} }
try { try {
...@@ -158,6 +160,7 @@ public class SpelParserTests { ...@@ -158,6 +160,7 @@ public class SpelParserTests {
SpelParseException spe = (SpelParseException) ex; SpelParseException spe = (SpelParseException) ex;
assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS, spe.getMessageCode()); assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS, spe.getMessageCode());
assertEquals(10, spe.getPosition()); assertEquals(10, spe.getPosition());
assertTrue(ex.getMessage().contains(ex.getExpressionString()));
} }
try { try {
...@@ -170,6 +173,7 @@ public class SpelParserTests { ...@@ -170,6 +173,7 @@ public class SpelParserTests {
SpelParseException spe = (SpelParseException) ex; SpelParseException spe = (SpelParseException) ex;
assertEquals(SpelMessage.NON_TERMINATING_DOUBLE_QUOTED_STRING, spe.getMessageCode()); assertEquals(SpelMessage.NON_TERMINATING_DOUBLE_QUOTED_STRING, spe.getMessageCode());
assertEquals(0, spe.getPosition()); assertEquals(0, spe.getPosition());
assertTrue(ex.getMessage().contains(ex.getExpressionString()));
} }
try { try {
...@@ -182,43 +186,44 @@ public class SpelParserTests { ...@@ -182,43 +186,44 @@ public class SpelParserTests {
SpelParseException spe = (SpelParseException) ex; SpelParseException spe = (SpelParseException) ex;
assertEquals(SpelMessage.NON_TERMINATING_QUOTED_STRING, spe.getMessageCode()); assertEquals(SpelMessage.NON_TERMINATING_QUOTED_STRING, spe.getMessageCode());
assertEquals(0, spe.getPosition()); assertEquals(0, spe.getPosition());
assertTrue(ex.getMessage().contains(ex.getExpressionString()));
} }
} }
@Test @Test
public void arithmeticPrecedence2() throws EvaluationException, ParseException { public void arithmeticPrecedence2() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2+3*5"); SpelExpression expr = parser.parseRaw("2+3*5");
assertEquals(17, expr.getValue()); assertEquals(17, expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence3() throws EvaluationException, ParseException { public void arithmeticPrecedence3() {
SpelExpression expr = new SpelExpressionParser().parseRaw("3+10/2"); SpelExpression expr = new SpelExpressionParser().parseRaw("3+10/2");
assertEquals(8, expr.getValue()); assertEquals(8, expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence4() throws EvaluationException, ParseException { public void arithmeticPrecedence4() {
SpelExpression expr = new SpelExpressionParser().parseRaw("10/2+3"); SpelExpression expr = new SpelExpressionParser().parseRaw("10/2+3");
assertEquals(8, expr.getValue()); assertEquals(8, expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence5() throws EvaluationException, ParseException { public void arithmeticPrecedence5() {
SpelExpression expr = new SpelExpressionParser().parseRaw("(4+10)/2"); SpelExpression expr = new SpelExpressionParser().parseRaw("(4+10)/2");
assertEquals(7, expr.getValue()); assertEquals(7, expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence6() throws EvaluationException, ParseException { public void arithmeticPrecedence6() {
SpelExpression expr = new SpelExpressionParser().parseRaw("(3+2)*2"); SpelExpression expr = new SpelExpressionParser().parseRaw("(3+2)*2");
assertEquals(10, expr.getValue()); assertEquals(10, expr.getValue());
} }
@Test @Test
public void booleanOperators() throws EvaluationException, ParseException { public void booleanOperators() {
SpelExpression expr = new SpelExpressionParser().parseRaw("true"); SpelExpression expr = new SpelExpressionParser().parseRaw("true");
assertEquals(Boolean.TRUE, expr.getValue(Boolean.class)); assertEquals(Boolean.TRUE, expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("false"); expr = new SpelExpressionParser().parseRaw("false");
...@@ -236,7 +241,7 @@ public class SpelParserTests { ...@@ -236,7 +241,7 @@ public class SpelParserTests {
} }
@Test @Test
public void booleanOperators_symbolic_spr9614() throws EvaluationException, ParseException { public void booleanOperators_symbolic_spr9614() {
SpelExpression expr = new SpelExpressionParser().parseRaw("true"); SpelExpression expr = new SpelExpressionParser().parseRaw("true");
assertEquals(Boolean.TRUE, expr.getValue(Boolean.class)); assertEquals(Boolean.TRUE, expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("false"); expr = new SpelExpressionParser().parseRaw("false");
...@@ -254,7 +259,7 @@ public class SpelParserTests { ...@@ -254,7 +259,7 @@ public class SpelParserTests {
} }
@Test @Test
public void stringLiterals() throws EvaluationException, ParseException { public void stringLiterals() {
SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'"); SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'");
assertEquals("howdy", expr.getValue()); assertEquals("howdy", expr.getValue());
expr = new SpelExpressionParser().parseRaw("'hello '' world'"); expr = new SpelExpressionParser().parseRaw("'hello '' world'");
...@@ -262,13 +267,13 @@ public class SpelParserTests { ...@@ -262,13 +267,13 @@ public class SpelParserTests {
} }
@Test @Test
public void stringLiterals2() throws EvaluationException, ParseException { public void stringLiterals2() {
SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'.substring(0,2)"); SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'.substring(0,2)");
assertEquals("ho", expr.getValue()); assertEquals("ho", expr.getValue());
} }
@Test @Test
public void testStringLiterals_DoubleQuotes_spr9620() throws Exception { public void testStringLiterals_DoubleQuotes_spr9620() {
SpelExpression expr = new SpelExpressionParser().parseRaw("\"double quote: \"\".\""); SpelExpression expr = new SpelExpressionParser().parseRaw("\"double quote: \"\".\"");
assertEquals("double quote: \".", expr.getValue()); assertEquals("double quote: \".", expr.getValue());
expr = new SpelExpressionParser().parseRaw("\"hello \"\" world\""); expr = new SpelExpressionParser().parseRaw("\"hello \"\" world\"");
...@@ -276,7 +281,7 @@ public class SpelParserTests { ...@@ -276,7 +281,7 @@ public class SpelParserTests {
} }
@Test @Test
public void testStringLiterals_DoubleQuotes_spr9620_2() throws Exception { public void testStringLiterals_DoubleQuotes_spr9620_2() {
try { try {
new SpelExpressionParser().parseRaw("\"double quote: \\\"\\\".\""); new SpelExpressionParser().parseRaw("\"double quote: \\\"\\\".\"");
fail("Should have failed"); fail("Should have failed");
...@@ -288,7 +293,7 @@ public class SpelParserTests { ...@@ -288,7 +293,7 @@ public class SpelParserTests {
} }
@Test @Test
public void positionalInformation() throws EvaluationException, ParseException { public void positionalInformation() {
SpelExpression expr = new SpelExpressionParser().parseRaw("true and true or false"); SpelExpression expr = new SpelExpressionParser().parseRaw("true and true or false");
SpelNode rootAst = expr.getAST(); SpelNode rootAst = expr.getAST();
OpOr operatorOr = (OpOr) rootAst; OpOr operatorOr = (OpOr) rootAst;
...@@ -355,13 +360,13 @@ public class SpelParserTests { ...@@ -355,13 +360,13 @@ public class SpelParserTests {
exprEx = new ExpressionException("wibble", "test"); exprEx = new ExpressionException("wibble", "test");
assertEquals("test", exprEx.getSimpleMessage()); assertEquals("test", exprEx.getSimpleMessage());
assertEquals("Expression 'wibble': test", exprEx.toDetailedString()); assertEquals("Expression [wibble]: test", exprEx.toDetailedString());
assertEquals("Expression 'wibble': test", exprEx.getMessage()); assertEquals("Expression [wibble]: test", exprEx.getMessage());
exprEx = new ExpressionException("wibble", 3, "test"); exprEx = new ExpressionException("wibble", 3, "test");
assertEquals("test", exprEx.getSimpleMessage()); assertEquals("test", exprEx.getSimpleMessage());
assertEquals("Expression 'wibble' @ 3: test", exprEx.toDetailedString()); assertEquals("Expression [wibble] @3: test", exprEx.toDetailedString());
assertEquals("Expression 'wibble' @ 3: test", exprEx.getMessage()); assertEquals("Expression [wibble] @3: test", exprEx.getMessage());
} }
@Test @Test
...@@ -401,16 +406,16 @@ public class SpelParserTests { ...@@ -401,16 +406,16 @@ public class SpelParserTests {
checkNumber("1e+3", 1e3d, Double.class); checkNumber("1e+3", 1e3d, Double.class);
} }
private void checkNumber(String expression, Object value, Class<?> type) { private void checkNumber(String expression, Object value, Class<?> type) {
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw(expression); SpelExpression expr = parser.parseRaw(expression);
Object o = expr.getValue(); Object exprVal = expr.getValue();
assertEquals(value, o); assertEquals(value, exprVal);
assertEquals(type, o.getClass()); assertEquals(type, exprVal.getClass());
} }
catch (Exception ex) { catch (Exception ex) {
ex.printStackTrace();
fail(ex.getMessage()); fail(ex.getMessage());
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册