提交 17c88107 编写于 作者: A Andy Clement

Removing functionality. Removed expression list support, cannot say (foo();bar()) any more

上级 4d437688
...@@ -24,10 +24,10 @@ package org.springframework.expression; ...@@ -24,10 +24,10 @@ package org.springframework.expression;
* turn. The only rule that affects the call order is that any naming the target class directly in * turn. The only rule that affects the call order is that any naming the target class directly in
* getSpecifiedTargetClasses() will be called first, before the general resolvers. * getSpecifiedTargetClasses() will be called first, before the general resolvers.
* <p> * <p>
* If the cost of locating the property is expensive, in relation to actually retrieving its value, consider * If the cost of locating the property is expensive, in relation to actually retrieving its value, consider extending
* extending CacheablePropertyAccessor rather than directly implementing PropertyAccessor. A CacheablePropertyAccessor * CacheablePropertyAccessor rather than directly implementing PropertyAccessor. A CacheablePropertyAccessor enables the
* enables the discovery (resolution) of the property to be done once and then an object (an executor) returned * discovery (resolution) of the property to be done once and then an object (an executor) returned and cached by the
* and cached by the infrastructure that can be used repeatedly to retrieve the property value. * infrastructure that can be used repeatedly to retrieve the property value.
* *
* @author Andy Clement * @author Andy Clement
*/ */
...@@ -39,7 +39,6 @@ public interface PropertyAccessor { ...@@ -39,7 +39,6 @@ public interface PropertyAccessor {
* *
* @return an array of classes that this resolver is suitable for (or null if a general resolver) * @return an array of classes that this resolver is suitable for (or null if a general resolver)
*/ */
@SuppressWarnings("unchecked")
public Class[] getSpecificTargetClasses(); public Class[] getSpecificTargetClasses();
/** /**
......
...@@ -59,10 +59,6 @@ tokens { ...@@ -59,10 +59,6 @@ tokens {
} }
expr: expression EOF!; expr: expression EOF!;
exprList
: LPAREN expression (SEMI expression)+ (SEMIRPAREN | RPAREN)
-> ^(EXPRESSIONLIST expression+);
SEMIRPAREN : ';)'; // recoveryrelated: allows us to cope with a rogue superfluous semicolon before the rparen in an expression list SEMIRPAREN : ';)'; // recoveryrelated: allows us to cope with a rogue superfluous semicolon before the rparen in an expression list
...@@ -102,8 +98,7 @@ primaryExpression ...@@ -102,8 +98,7 @@ primaryExpression
startNode startNode
: :
(LPAREN expression SEMI) => exprList parenExpr
| parenExpr
| methodOrProperty | methodOrProperty
| functionOrVar | functionOrVar
| localFunctionOrVar | localFunctionOrVar
...@@ -135,7 +130,6 @@ dottedNode ...@@ -135,7 +130,6 @@ dottedNode
| selection | selection
| firstSelection | firstSelection
| lastSelection | lastSelection
| exprList
)) ))
; ;
......
GREATER_THAN_OR_EQUAL=79 GREATER_THAN_OR_EQUAL=78
SELECT_FIRST=58 SELECT_FIRST=57
COMMA=51 COMMA=50
HOLDER=14 HOLDER=14
GREATER_THAN=78 GREATER_THAN=77
TYPE=60 TYPE=59
EXPRESSIONLIST=4 EXPRESSIONLIST=4
MINUS=41 MINUS=40
MAP_ENTRY=25 MAP_ENTRY=25
SELECT_LAST=59 SELECT_LAST=58
NUMBER=29 NUMBER=29
LESS_THAN=76 LESS_THAN=75
BANG=46 BANG=45
ARGLIST=11 ARGLIST=11
FALSE=70 FALSE=69
METHOD=26 METHOD=26
PROPERTY_OR_FIELD=9 PROPERTY_OR_FIELD=9
LBRACKET=53 LBRACKET=52
MOD=44
INDEXER=10 INDEXER=10
MOD=43
CONSTRUCTOR_ARRAY=15 CONSTRUCTOR_ARRAY=15
FUNCTIONREF=17 FUNCTIONREF=17
NULL_LITERAL=66 NULL_LITERAL=65
NAMED_ARGUMENT=16 NAMED_ARGUMENT=16
OR=38 OR=37
PIPE=62 PIPE=61
DOT=47 DOT=46
RCURLY=56 RCURLY=55
EXPRESSION=6 EXPRESSION=6
AND=39 AND=38
LCURLY=63 LCURLY=62
DATE_LITERAL=13 DATE_LITERAL=13
REAL_TYPE_SUFFIX=89 REAL_TYPE_SUFFIX=89
STRING_LITERAL=64 STRING_LITERAL=63
SELECT=57 SELECT=56
QUALIFIED_IDENTIFIER=7 QUALIFIED_IDENTIFIER=7
RBRACKET=54 RBRACKET=53
SUBTRACT=28 SUBTRACT=28
ASSIGN=34 ASSIGN=31
BETWEEN=82 BETWEEN=81
RPAREN=33 RPAREN=36
SIGN=90 SIGN=90
LPAREN=30 LPAREN=35
HEX_DIGIT=73 HEX_DIGIT=72
PLUS=40 PLUS=39
LIST_INITIALIZER=21 LIST_INITIALIZER=21
APOS=84 APOS=84
INTEGER_LITERAL=5 INTEGER_LITERAL=5
AT=52 AT=51
ID=49 ID=48
NOT_EQUAL=75 NOT_EQUAL=74
RANGE=19 RANGE=19
POWER=45 POWER=44
TYPEREF=18 TYPEREF=18
DECIMAL_DIGIT=71 DECIMAL_DIGIT=70
WS=86 WS=86
IS=81 IS=80
DOLLAR=50 DOLLAR=49
LESS_THAN_OR_EQUAL=77 LESS_THAN_OR_EQUAL=76
SEMIRPAREN=32 SEMIRPAREN=30
DQ_STRING_LITERAL=65 DQ_STRING_LITERAL=64
HEXADECIMAL_INTEGER_LITERAL=67 HEXADECIMAL_INTEGER_LITERAL=66
MAP_INITIALIZER=22 MAP_INITIALIZER=22
LAMBDA=61 LAMBDA=60
LOCALFUNC=24 LOCALFUNC=24
IN=80 IN=79
SEMI=83
CONSTRUCTOR=12 CONSTRUCTOR=12
SEMI=31 INTEGER_TYPE_SUFFIX=71
INTEGER_TYPE_SUFFIX=72 EQUAL=73
EQUAL=74 MATCHES=82
MATCHES=83
DOT_ESCAPED=85 DOT_ESCAPED=85
UPTO=87 UPTO=87
QMARK=36 QMARK=33
REFERENCE=8 REFERENCE=8
PROJECT=55 PROJECT=54
DEFAULT=35 DEFAULT=32
COLON=37 COLON=34
DIV=43 DIV=42
LOCALVAR=23 LOCALVAR=23
STAR=42 STAR=41
REAL_LITERAL=68 REAL_LITERAL=67
VARIABLEREF=20 VARIABLEREF=20
EXPONENT_PART=88 EXPONENT_PART=88
TRUE=69 TRUE=68
ADD=27 ADD=27
POUND=48 POUND=47
'date'=92 'date'=92
'new'=91 'new'=91
...@@ -182,7 +182,6 @@ public class ReflectionUtils { ...@@ -182,7 +182,6 @@ public class ReflectionUtils {
* match an expected type * match an expected type
* @return a MatchInfo object indicating what kind of match it was or null if it was not a match * @return a MatchInfo object indicating what kind of match it was or null if it was not a match
*/ */
@SuppressWarnings("unchecked")
private static ArgumentsMatchInfo compareArguments(Class[] expectedArgTypes, Class[] suppliedArgTypes, private static ArgumentsMatchInfo compareArguments(Class[] expectedArgTypes, Class[] suppliedArgTypes,
TypeConverter typeConverter, boolean conversionAllowed) { TypeConverter typeConverter, boolean conversionAllowed) {
ArgsMatchKind match = ArgsMatchKind.EXACT; ArgsMatchKind match = ArgsMatchKind.EXACT;
...@@ -230,7 +229,6 @@ public class ReflectionUtils { ...@@ -230,7 +229,6 @@ public class ReflectionUtils {
* match an expected type * match an expected type
* @return a MatchInfo object indicating what kind of match it was or null if it was not a match * @return a MatchInfo object indicating what kind of match it was or null if it was not a match
*/ */
@SuppressWarnings("unchecked")
private static ArgumentsMatchInfo compareArgumentsVarargs(Class[] expectedArgTypes, Class[] suppliedArgTypes, private static ArgumentsMatchInfo compareArgumentsVarargs(Class[] expectedArgTypes, Class[] suppliedArgTypes,
TypeConverter typeConverter, boolean conversionAllowed) { TypeConverter typeConverter, boolean conversionAllowed) {
ArgsMatchKind match = ArgsMatchKind.EXACT; ArgsMatchKind match = ArgsMatchKind.EXACT;
...@@ -484,9 +482,9 @@ public class ReflectionUtils { ...@@ -484,9 +482,9 @@ public class ReflectionUtils {
} else { } else {
targetType = parameterTypes[i]; targetType = parameterTypes[i];
} }
if (converter==null) { if (converter == null) {
throw new SpelException(SpelMessages.PROBLEM_DURING_TYPE_CONVERSION, "No converter available to convert '" throw new SpelException(SpelMessages.PROBLEM_DURING_TYPE_CONVERSION,
+ arguments[i] + " to type '" + targetType + "'"); "No converter available to convert '" + arguments[i] + " to type '" + targetType + "'");
} }
try { try {
if (arguments[i] != null && arguments[i].getClass() != targetType) { if (arguments[i] != null && arguments[i].getClass() != targetType) {
...@@ -495,8 +493,9 @@ public class ReflectionUtils { ...@@ -495,8 +493,9 @@ public class ReflectionUtils {
} catch (EvaluationException e) { } catch (EvaluationException e) {
// allows for another type converter throwing a different kind of EvaluationException // allows for another type converter throwing a different kind of EvaluationException
if (!(e instanceof SpelException)) { if (!(e instanceof SpelException)) {
throw new SpelException(e, SpelMessages.PROBLEM_DURING_TYPE_CONVERSION, "Converter failed to convert '" throw new SpelException(e, SpelMessages.PROBLEM_DURING_TYPE_CONVERSION,
+ arguments[i].getClass().getName() + "' to type '" + targetType + "'"); "Converter failed to convert '" + arguments[i].getClass().getName() + "' to type '"
+ targetType + "'");
} }
throw e; throw e;
} }
......
...@@ -180,19 +180,11 @@ public class EvaluationTests extends ExpressionTestCase { ...@@ -180,19 +180,11 @@ public class EvaluationTests extends ExpressionTestCase {
evaluate("#{1:'January', 2:'February', 3:'March'}.get(2)", "February", String.class); evaluate("#{1:'January', 2:'February', 3:'March'}.get(2)", "February", String.class);
} }
public void testInlineMapCreation06() {
evaluate("(#pos=3;#{1:'January', 2:'February', 3:'March'}[#pos])", "March", String.class);
}
// set construction // set construction
public void testSetConstruction01() { public void testSetConstruction01() {
evaluate("new HashSet().addAll({'a','b','c'})", "true", Boolean.class); evaluate("new HashSet().addAll({'a','b','c'})", "true", Boolean.class);
} }
public void testSets01() {
evaluate("(#var=new HashSet();#var.addAll({'a','b','c'});#var[1])", "c", String.class);
}
// constructors // constructors
public void testConstructorInvocation01() { public void testConstructorInvocation01() {
evaluate("new String('hello')", "hello", String.class); evaluate("new String('hello')", "hello", String.class);
...@@ -323,19 +315,6 @@ public class EvaluationTests extends ExpressionTestCase { ...@@ -323,19 +315,6 @@ public class EvaluationTests extends ExpressionTestCase {
evaluate("#var1='value1'", "value1", String.class); evaluate("#var1='value1'", "value1", String.class);
} }
public void testAssignmentToVariables02() {
evaluate("(#var1='value1';#var1)", "value1", String.class, true);
}
// Property setting
public void testAssignmentToProperty01() {
evaluate("placeOfBirth.city='SmilJan'", "SmilJan", String.class);
evaluate(
"(#oldPOB = placeOfBirth.city;placeOfBirth.city='FairOak';'From ' + #oldPOB + ' to ' + placeOfBirth.city)",
"From SmilJan to FairOak", String.class);
evaluate("placeOfBirth.city='SmilJan'", "SmilJan", String.class);
}
// Ternary operator // Ternary operator
public void testTernaryOperator01() { public void testTernaryOperator01() {
evaluate("{1}.#isEven(#this[0]) == 'y'?'it is even':'it is odd'", "it is odd", String.class); evaluate("{1}.#isEven(#this[0]) == 'y'?'it is even':'it is odd'", "it is odd", String.class);
...@@ -389,10 +368,6 @@ public class EvaluationTests extends ExpressionTestCase { ...@@ -389,10 +368,6 @@ public class EvaluationTests extends ExpressionTestCase {
evaluate("@(apple).color.getRGB() == T(java.awt.Color).green.getRGB()", "true", Boolean.class); evaluate("@(apple).color.getRGB() == T(java.awt.Color).green.getRGB()", "true", Boolean.class);
} }
public void testReferences06b() {
evaluate("(#t='Color';@(apple).color.getRGB() == T(java.awt.Color).green.getRGB())", "true", Boolean.class);
}
public void testReferences07() { public void testReferences07() {
evaluate("@(apple).color.getRGB().equals(T(java.awt.Color).green.getRGB())", "true", Boolean.class); evaluate("@(apple).color.getRGB().equals(T(java.awt.Color).green.getRGB())", "true", Boolean.class);
} }
...@@ -424,49 +399,46 @@ public class EvaluationTests extends ExpressionTestCase { ...@@ -424,49 +399,46 @@ public class EvaluationTests extends ExpressionTestCase {
evaluate("{|| true }", "{|| true }", Lambda.class); evaluate("{|| true }", "{|| true }", Lambda.class);
} }
public void testLambdaNoArgsReferenced() {
evaluate("(#fn={|| false };#fn)", "{|| false }", Lambda.class, true);
}
public void testLambda01() { public void testLambda01() {
evaluate("{|x,y| $x > $y ? $x : $y }", "{|x,y| ($x > $y) ? $x : $y }", evaluate("{|x,y| $x > $y ? $x : $y }", "{|x,y| ($x > $y) ? $x : $y }",
org.springframework.expression.spel.ast.Lambda.class); org.springframework.expression.spel.ast.Lambda.class);
} }
public void testLambda02() { //
evaluate("(#max={|x,y| $x > $y ? $x : $y };true)", "true", Boolean.class); // public void testLambda02() {
} // evaluate("(#max={|x,y| $x > $y ? $x : $y };true)", "true", Boolean.class);
// }
public void testLambdaMax() { //
evaluate("(#max = {|x,y| $x > $y ? $x : $y }; #max(5,25))", "25", Integer.class); // public void testLambdaMax() {
} // evaluate("(#max = {|x,y| $x > $y ? $x : $y }; #max(5,25))", "25", Integer.class);
// }
public void testLambdaFactorial01() { //
evaluate("(#fact = {|n| $n <= 1 ? 1 : $n * #fact($n-1) }; #fact(5))", "120", Integer.class); // public void testLambdaFactorial01() {
} // evaluate("(#fact = {|n| $n <= 1 ? 1 : $n * #fact($n-1) }; #fact(5))", "120", Integer.class);
// }
public void testLambdaFactorial02() { //
evaluate("(#fact = {|n| $n <= 1 ? 1 : #fact($n-1) * $n }; #fact(5))", "120", Integer.class); // public void testLambdaFactorial02() {
} // evaluate("(#fact = {|n| $n <= 1 ? 1 : #fact($n-1) * $n }; #fact(5))", "120", Integer.class);
// }
public void testLambdaAlphabet01() { //
evaluate("(#alpha = {|l,s| $l>'z'?$s:#alpha($l+1,$s+$l)};#alphabet={||#alpha('a','')}; #alphabet())", // public void testLambdaAlphabet01() {
"abcdefghijklmnopqrstuvwxyz", String.class); // evaluate("(#alpha = {|l,s| $l>'z'?$s:#alpha($l+1,$s+$l)};#alphabet={||#alpha('a','')}; #alphabet())",
} // "abcdefghijklmnopqrstuvwxyz", String.class);
// }
public void testLambdaAlphabet02() { //
evaluate("(#alphabet = {|l,s| $l>'z'?$s:#alphabet($l+1,$s+$l)};#alphabet('a',''))", // public void testLambdaAlphabet02() {
"abcdefghijklmnopqrstuvwxyz", String.class); // evaluate("(#alphabet = {|l,s| $l>'z'?$s:#alphabet($l+1,$s+$l)};#alphabet('a',''))",
} // "abcdefghijklmnopqrstuvwxyz", String.class);
// }
public void testLambdaDelegation01() { //
evaluate("(#sqrt={|n| T(Math).sqrt($n)};#delegate={|f,n| $f($n)};#delegate(#sqrt,4))", "2.0", Double.class); // public void testLambdaDelegation01() {
} // evaluate("(#sqrt={|n| T(Math).sqrt($n)};#delegate={|f,n| $f($n)};#delegate(#sqrt,4))", "2.0", Double.class);
// }
public void testVariableReferences() { //
evaluate("(#answer=42;#answer)", "42", Integer.class, true); // public void testVariableReferences() {
evaluate("($answer=42;$answer)", "42", Integer.class, true); // evaluate("(#answer=42;#answer)", "42", Integer.class, true);
} // evaluate("($answer=42;$answer)", "42", Integer.class, true);
// }
// type references // type references
public void testTypeReferences01() { public void testTypeReferences01() {
......
...@@ -90,27 +90,27 @@ public class LiteralTests extends ExpressionTestCase { ...@@ -90,27 +90,27 @@ public class LiteralTests extends ExpressionTestCase {
} }
public void testLiteralReal01_CreatingDoubles() { public void testLiteralReal01_CreatingDoubles() {
evaluate("1.25",1.25d,Double.class); evaluate("1.25", 1.25d, Double.class);
evaluate("2.99",2.99d,Double.class); evaluate("2.99", 2.99d, Double.class);
evaluate("-3.141",-3.141d,Double.class); evaluate("-3.141", -3.141d, Double.class);
evaluate("1.25d",1.25d,Double.class); evaluate("1.25d", 1.25d, Double.class);
evaluate("2.99d",2.99d,Double.class); evaluate("2.99d", 2.99d, Double.class);
evaluate("-3.141d",-3.141d,Double.class); evaluate("-3.141d", -3.141d, Double.class);
evaluate("1.25D",1.25d,Double.class); evaluate("1.25D", 1.25d, Double.class);
evaluate("2.99D",2.99d,Double.class); evaluate("2.99D", 2.99d, Double.class);
evaluate("-3.141D",-3.141d,Double.class); evaluate("-3.141D", -3.141d, Double.class);
} }
public void testLiteralReal02_CreatingFloats() { public void testLiteralReal02_CreatingFloats() {
// For now, everything becomes a double... // For now, everything becomes a double...
evaluate("1.25f",1.25d,Double.class); evaluate("1.25f", 1.25d, Double.class);
evaluate("2.99f",2.99d,Double.class); evaluate("2.99f", 2.99d, Double.class);
evaluate("-3.141f",-3.141d,Double.class); evaluate("-3.141f", -3.141d, Double.class);
evaluate("1.25F",1.25d,Double.class); evaluate("1.25F", 1.25d, Double.class);
evaluate("2.99F",2.99d,Double.class); evaluate("2.99F", 2.99d, Double.class);
evaluate("-3.141F",-3.141d,Double.class); evaluate("-3.141F", -3.141d, Double.class);
} }
public void testLiteralReal03_UsingExponents() { public void testLiteralReal03_UsingExponents() {
evaluate("6.0221415E+23", "6.0221415E23", Double.class); evaluate("6.0221415E+23", "6.0221415E23", Double.class);
evaluate("6.0221415e+23", "6.0221415E23", Double.class); evaluate("6.0221415e+23", "6.0221415E23", Double.class);
...@@ -121,10 +121,10 @@ public class LiteralTests extends ExpressionTestCase { ...@@ -121,10 +121,10 @@ public class LiteralTests extends ExpressionTestCase {
} }
public void testLiteralReal04_BadExpressions() { public void testLiteralReal04_BadExpressions() {
parseAndCheckError("6.1e23e22",SpelMessages.PARSE_PROBLEM,6,"mismatched input 'e22' expecting EOF"); parseAndCheckError("6.1e23e22", SpelMessages.PARSE_PROBLEM, 6, "mismatched input 'e22' expecting EOF");
parseAndCheckError("6.1f23e22",SpelMessages.PARSE_PROBLEM,4,"mismatched input '23e22' expecting EOF"); parseAndCheckError("6.1f23e22", SpelMessages.PARSE_PROBLEM, 4, "mismatched input '23e22' expecting EOF");
} }
public void testLiteralNull01() { public void testLiteralNull01() {
evaluate("null", null, null); evaluate("null", null, null);
} }
...@@ -156,10 +156,10 @@ public class LiteralTests extends ExpressionTestCase { ...@@ -156,10 +156,10 @@ public class LiteralTests extends ExpressionTestCase {
// int year convertable from number to string // int year convertable from number to string
parseAndCheckError("date(2008,'yyyy').getTime()>0", SpelMessages.PARSE_PROBLEM); parseAndCheckError("date(2008,'yyyy').getTime()>0", SpelMessages.PARSE_PROBLEM);
} }
public void testConversions() { public void testConversions() {
// getting the expression type to be what we want - either: // getting the expression type to be what we want - either:
evaluate("(#foo=37;#foo.byteValue())",(byte)37,Byte.class); // calling byteValue() on Integer.class evaluate("new Integer(37).byteValue()", (byte) 37, Byte.class); // calling byteValue() on Integer.class
evaluateAndAskForReturnType("(#foo=37;#foo)",(byte)37,Byte.class); // relying on registered type converters evaluateAndAskForReturnType("new Integer(37)", (byte) 37, Byte.class); // relying on registered type converters
} }
} }
...@@ -35,12 +35,6 @@ public class ParserErrorMessagesTests extends ExpressionTestCase { ...@@ -35,12 +35,6 @@ public class ParserErrorMessagesTests extends ExpressionTestCase {
parseAndCheckError("0xB0BG", SpelMessages.PARSE_PROBLEM, 5, "mismatched input 'G' expecting EOF"); 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() { public void testBrokenExpression04() {
// missing right operand // missing right operand
parseAndCheckError("true or ", SpelMessages.PARSE_PROBLEM, -1, "no viable alternative at input '<EOF>'"); // POOR parseAndCheckError("true or ", SpelMessages.PARSE_PROBLEM, -1, "no viable alternative at input '<EOF>'"); // POOR
...@@ -51,21 +45,10 @@ public class ParserErrorMessagesTests extends ExpressionTestCase { ...@@ -51,21 +45,10 @@ public class ParserErrorMessagesTests extends ExpressionTestCase {
parseAndCheckError("1 + ", SpelMessages.PARSE_PROBLEM, -1, "no viable alternative at input '<EOF>'"); // POOR 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);
}
public void testBrokenExpression07() { public void testBrokenExpression07() {
// T() can only take an identifier (possibly qualified), not a literal // T() can only take an identifier (possibly qualified), not a literal
// message ought to say identifier rather than ID // message ought to say identifier rather than ID
parseAndCheckError("null is T('a')", SpelMessages.PARSE_PROBLEM, 10, "mismatched input ''a'' expecting ID"); // POOR parseAndCheckError("null is T('a')", SpelMessages.PARSE_PROBLEM, 10, "mismatched input ''a'' expecting ID"); // POOR
} }
public void testExpressionLists02a() {
// either missing semi or rogue 5. RPAREN should at least be ')', and why doesn't it give the other possibles?
parseAndCheckError("( (3;4)5)", SpelMessages.PARSE_PROBLEM, 7, "mismatched input '5' expecting RPAREN"); // POOR
}
} }
...@@ -22,7 +22,7 @@ import org.springframework.expression.ParseException; ...@@ -22,7 +22,7 @@ import org.springframework.expression.ParseException;
/** /**
* Parse some expressions and check we get the AST we expect. Rather than inspecting each node in the AST, we ask it to * Parse some expressions and check we get the AST we expect. Rather than inspecting each node in the AST, we ask it to
* write itself to a string form and check that is as expected. * write itself to a string form and check that is as expected.
* *
* @author Andy Clement * @author Andy Clement
*/ */
public class ParsingTests extends TestCase { public class ParsingTests extends TestCase {
...@@ -360,14 +360,15 @@ public class ParsingTests extends TestCase { ...@@ -360,14 +360,15 @@ public class ParsingTests extends TestCase {
parseCheck("{|x,y| $x > $y ? $x : $y }", "{|x,y| ($x > $y) ? $x : $y }"); parseCheck("{|x,y| $x > $y ? $x : $y }", "{|x,y| ($x > $y) ? $x : $y }");
} }
public void testLambdaMax() { //
parseCheck("(#max = {|x,y| $x > $y ? $x : $y }; #max(5,25))", "(#max={|x,y| ($x > $y) ? $x : $y };#max(5,25))"); // public void testLambdaMax() {
} // parseCheck("(#max = {|x,y| $x > $y ? $x : $y }; #max(5,25))", "(#max={|x,y| ($x > $y) ? $x : $y };#max(5,25))");
// }
public void testLambdaFactorial() { //
parseCheck("(#fact = {|n| $n <= 1 ? 1 : $n * #fact($n-1) }; #fact(5))", // public void testLambdaFactorial() {
"(#fact={|n| ($n <= 1) ? 1 : ($n * #fact(($n - 1))) };#fact(5))"); // parseCheck("(#fact = {|n| $n <= 1 ? 1 : $n * #fact($n-1) }; #fact(5))",
} // 120 // "(#fact={|n| ($n <= 1) ? 1 : ($n * #fact(($n - 1))) };#fact(5))");
// } // 120
// Type references // Type references
public void testTypeReferences01() { public void testTypeReferences01() {
...@@ -378,39 +379,10 @@ public class ParsingTests extends TestCase { ...@@ -378,39 +379,10 @@ public class ParsingTests extends TestCase {
parseCheck("T(String)"); parseCheck("T(String)");
} }
// Nesting expressions and expression lists
public void testExpressionLists01() {
parseCheck("(3;4;5)");
}
public void testExpressionLists02() {
parseCheck("( (3;4);5)", "((3;4);5)");
}
public void testExpressionLists03() {
parseCheck("(3;(4;5))");
}
public void testExpressionLists04() {
parseCheck("((3;4;5))", "(3;4;5)");
}
public void testExpressionLists05() {
parseCheck("((3;4)+(5;6))", "((3;4) + (5;6))");
}
public void testExpressionLists06() {
parseCheck("((3;4;)+(5;6))", "((3;4) + (5;6))");
}
public void testExpressionLists07() {
parseCheck("((3;4;)+(5;6;))", "((3;4) + (5;6))");
}
/** /**
* Parse the supplied expression and then create a string representation of the resultant AST, it should be the same * Parse the supplied expression and then create a string representation of the resultant AST, it should be the same
* as the original expression. * as the original expression.
* *
* @param expression the expression to parse *and* the expected value of the string form of the resultant AST * @param expression the expression to parse *and* the expected value of the string form of the resultant AST
*/ */
public void parseCheck(String expression) { public void parseCheck(String expression) {
...@@ -420,7 +392,7 @@ public class ParsingTests extends TestCase { ...@@ -420,7 +392,7 @@ public class ParsingTests extends TestCase {
/** /**
* Parse the supplied expression and then create a string representation of the resultant AST, it should be the * Parse the supplied expression and then create a string representation of the resultant AST, it should be the
* expected value. * expected value.
* *
* @param expression the expression to parse * @param expression the expression to parse
* @param expectedStringFormOfAST the expected string form of the AST * @param expectedStringFormOfAST the expected string form of the AST
*/ */
......
...@@ -26,7 +26,6 @@ public class VariableAndFunctionTests extends ExpressionTestCase { ...@@ -26,7 +26,6 @@ public class VariableAndFunctionTests extends ExpressionTestCase {
public void testVariableAccess() { public void testVariableAccess() {
evaluate("#answer", "42", Integer.class, SHOULD_BE_WRITABLE); evaluate("#answer", "42", Integer.class, SHOULD_BE_WRITABLE);
evaluate("(#i=5;#i)", 5, Integer.class, SHOULD_BE_WRITABLE);
evaluate("#answer / 2", 21, Integer.class, SHOULD_NOT_BE_WRITABLE); evaluate("#answer / 2", 21, Integer.class, SHOULD_NOT_BE_WRITABLE);
} }
...@@ -40,40 +39,43 @@ public class VariableAndFunctionTests extends ExpressionTestCase { ...@@ -40,40 +39,43 @@ public class VariableAndFunctionTests extends ExpressionTestCase {
evaluate("#reverseString('hello')", "olleh", String.class); evaluate("#reverseString('hello')", "olleh", String.class);
evaluate("#reverseString(37)", "73", String.class); // requires type conversion of 37 to '37' evaluate("#reverseString(37)", "73", String.class); // requires type conversion of 37 to '37'
} }
public void testCallVarargsFunction() { public void testCallVarargsFunction() {
evaluate("#varargsFunctionReverseStringsAndMerge('a','b','c')","cba",String.class); evaluate("#varargsFunctionReverseStringsAndMerge('a','b','c')", "cba", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge('a')","a",String.class); evaluate("#varargsFunctionReverseStringsAndMerge('a')", "a", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge()","",String.class); evaluate("#varargsFunctionReverseStringsAndMerge()", "", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge('b',25)","25b",String.class); evaluate("#varargsFunctionReverseStringsAndMerge('b',25)", "25b", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge(25)","25",String.class); evaluate("#varargsFunctionReverseStringsAndMerge(25)", "25", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge2(1,'a','b','c')","1cba",String.class); evaluate("#varargsFunctionReverseStringsAndMerge2(1,'a','b','c')", "1cba", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge2(2,'a')","2a",String.class); evaluate("#varargsFunctionReverseStringsAndMerge2(2,'a')", "2a", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge2(3)","3",String.class); evaluate("#varargsFunctionReverseStringsAndMerge2(3)", "3", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge2(4,'b',25)","425b",String.class); evaluate("#varargsFunctionReverseStringsAndMerge2(4,'b',25)", "425b", String.class);
evaluate("#varargsFunctionReverseStringsAndMerge2(5,25)","525",String.class); evaluate("#varargsFunctionReverseStringsAndMerge2(5,25)", "525", String.class);
} }
public void testCallingFunctionsIncorrectly() { public void testCallingFunctionsIncorrectly() {
evaluateAndCheckError("#varargsFunctionReverseStringsAndMerge(new StringBuilder())",SpelMessages.TYPE_CONVERSION_ERROR); evaluateAndCheckError("#varargsFunctionReverseStringsAndMerge(new StringBuilder())",
SpelMessages.TYPE_CONVERSION_ERROR);
} }
public void testCallingIllegalFunctions() throws Exception { public void testCallingIllegalFunctions() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext(); StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("notStatic",this.getClass().getMethod("nonStatic")); ctx.setVariable("notStatic", this.getClass().getMethod("nonStatic"));
try { try {
@SuppressWarnings("unused") @SuppressWarnings("unused")
Object v = parser.parseExpression("#notStatic()").getValue(ctx); Object v = parser.parseExpression("#notStatic()").getValue(ctx);
fail("Should have failed with exception - cannot call non static method that way"); fail("Should have failed with exception - cannot call non static method that way");
} catch (SpelException se) { } catch (SpelException se) {
if (se.getMessageUnformatted()!=SpelMessages.FUNCTION_MUST_BE_STATIC) { if (se.getMessageUnformatted() != SpelMessages.FUNCTION_MUST_BE_STATIC) {
se.printStackTrace(); se.printStackTrace();
fail("Should have failed a message about the function needing to be static, not: "+se.getMessageUnformatted()); fail("Should have failed a message about the function needing to be static, not: "
+ se.getMessageUnformatted());
} }
} }
} }
public void nonStatic() {} public void nonStatic() {
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册