EvaluationTests.java 14.5 KB
Newer Older
A
Andy Clement 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright 2004-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.expression.spel;

/**
 * Tests the evaluation of real expressions in a real context.
 * 
21 22
 * Node: Some tests commented out as language support has been removed
 * 
A
Andy Clement 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 * @author Andy Clement
 */
public class EvaluationTests extends ExpressionTestCase {

	// relational operators: lt, le, gt, ge, eq, ne
	public void testRelOperatorGT01() {
		evaluate("3 > 6", "false", Boolean.class);
	}

	public void testRelOperatorLT01() {
		evaluate("3 < 6", "true", Boolean.class);
	}

	public void testRelOperatorLE01() {
		evaluate("3 <= 6", "true", Boolean.class);
	}

	public void testRelOperatorGE01() {
		evaluate("3 >= 6", "false", Boolean.class);
	}

	public void testRelOperatorGE02() {
		evaluate("3 >= 3", "true", Boolean.class);
	}

48 49 50
	// public void testRelOperatorsIn01() {
	// evaluate("3 in {1,2,3,4,5}", "true", Boolean.class);
	// }
A
Andy Clement 已提交
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	// public void testRelOperatorsIn02() {
	// evaluate("name in {null, \"Nikola Tesla\"}", "true", Boolean.class);
	// evaluate("name in {null, \"Anonymous\"}", "false", Boolean.class);
	// }
	//
	// public void testRelOperatorsBetween01() {
	// evaluate("1 between {1, 5}", "true", Boolean.class);
	// }
	//
	// public void testRelOperatorsBetween02() {
	// evaluate("'efg' between {'abc', 'xyz'}", "true", Boolean.class);
	// }
	//
	// public void testRelOperatorsBetweenErrors01() {
	// evaluateAndCheckError("1 between T(String)", SpelMessages.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST, 12);
	// }
	//
	// public void testRelOperatorsBetweenErrors02() {
	// evaluateAndCheckError("'abc' between {5,7}", SpelMessages.NOT_COMPARABLE, 6);
	// }
A
Andy Clement 已提交
72 73

	public void testRelOperatorsIs01() {
74
		evaluate("'xyz' instanceof T(int)", "false", Boolean.class);
A
Andy Clement 已提交
75 76
	}

77 78 79 80 81 82 83
	// public void testRelOperatorsIs02() {
	// evaluate("{1, 2, 3, 4, 5} instanceof T(List)", "true", Boolean.class);
	// }
	//
	// public void testRelOperatorsIs03() {
	// evaluate("{1, 2, 3, 4, 5} instanceof T(List)", "true", Boolean.class);
	// }
A
Andy Clement 已提交
84

85
	public void testRelOperatorsIs04() {
86
		evaluate("null instanceof T(String)", "false", Boolean.class);
87 88 89
	}

	public void testRelOperatorsIs05() {
90
		evaluate("null instanceof T(Integer)", "false", Boolean.class);
91 92 93
	}

	public void testRelOperatorsIs06() {
94
		evaluateAndCheckError("'A' instanceof null", SpelMessages.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND, 15, "null");
95 96
	}

A
Andy Clement 已提交
97 98 99 100 101 102 103 104
	public void testRelOperatorsMatches01() {
		evaluate("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'", "false", Boolean.class);
	}

	public void testRelOperatorsMatches02() {
		evaluate("'5.00' matches '^-?\\d+(\\.\\d{2})?$'", "true", Boolean.class);
	}

105
	public void testRelOperatorsMatches03() {
106
		evaluateAndCheckError("null matches '^.*$'", SpelMessages.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, 0, null);
107 108 109
	}

	public void testRelOperatorsMatches04() {
110
		evaluateAndCheckError("'abc' matches null", SpelMessages.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, 14, null);
111 112 113 114 115 116
	}

	public void testRelOperatorsMatches05() {
		evaluate("27 matches '^.*2.*$'", true, Boolean.class); // conversion int>string
	}

A
Andy Clement 已提交
117 118 119 120 121 122 123
	// mixing operators
	public void testMixingOperators01() {
		evaluate("true and 5>3", "true", Boolean.class);
	}

	// property access
	public void testPropertyField01() {
124 125 126
		evaluate("name", "Nikola Tesla", String.class, false); // not writable because (1) name is private (2) there is
		// no
		// setter, only a getter
A
Andy Clement 已提交
127 128 129 130 131 132
		evaluateAndCheckError("madeup", SpelMessages.PROPERTY_OR_FIELD_NOT_FOUND, 0, "madeup",
				"org.springframework.expression.spel.testresources.Inventor");
	}

	// nested properties
	public void testPropertiesNested01() {
133
		evaluate("placeOfBirth.city", "SmilJan", String.class, true);
A
Andy Clement 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	}

	public void testPropertiesNested02() {
		evaluate("placeOfBirth.doubleIt(12)", "24", Integer.class);
	}

	// methods
	public void testMethods01() {
		evaluate("echo(12)", "12", String.class);
	}

	public void testMethods02() {
		evaluate("echo(name)", "Nikola Tesla", String.class);
	}

	// inline list creation
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	// public void testInlineListCreation01() {
	// evaluate("{1, 2, 3, 4, 5}", "[1, 2, 3, 4, 5]", ArrayList.class);
	// }
	//
	// public void testInlineListCreation02() {
	// evaluate("{'abc', 'xyz'}", "[abc, xyz]", ArrayList.class);
	// }
	//
	// // inline map creation
	// public void testInlineMapCreation01() {
	// evaluate("#{'key1':'Value 1', 'today':'Monday'}", "{key1=Value 1, today=Monday}", HashMap.class);
	// }
	//
	// public void testInlineMapCreation02() {
	// evaluate("#{1:'January', 2:'February', 3:'March'}.size()", 3, Integer.class);// "{2=February, 1=January,
	// // 3=March}", HashMap.class);
	// }
	//
	// public void testInlineMapCreation03() {
	// evaluate("#{'key1':'Value 1', 'today':'Monday'}['key1']", "Value 1", String.class);
	// }
	//
	// public void testInlineMapCreation04() {
	// evaluate("#{1:'January', 2:'February', 3:'March'}[3]", "March", String.class);
	// }
	//
	// public void testInlineMapCreation05() {
	// evaluate("#{1:'January', 2:'February', 3:'March'}.get(2)", "February", String.class);
	// }
	//
	// // set construction
	// public void testSetConstruction01() {
	// evaluate("new HashSet().addAll({'a','b','c'})", "true", Boolean.class);
	// }
A
Andy Clement 已提交
184 185 186 187 188 189

	// constructors
	public void testConstructorInvocation01() {
		evaluate("new String('hello')", "hello", String.class);
	}

190 191 192
	// public void testConstructorInvocation02() {
	// evaluate("new String[3]", "java.lang.String[3]{null,null,null}", String[].class);
	// }
A
Andy Clement 已提交
193

194 195 196
	// public void testConstructorInvocation03() {
	// evaluateAndCheckError("new String[]", SpelMessages.NO_SIZE_OR_INITIALIZER_FOR_ARRAY_CONSTRUCTION, 4);
	// }
A
Andy Clement 已提交
197

198 199 200
	// public void testConstructorInvocation04() {
	// evaluateAndCheckError("new String[3]{'abc',3,'def'}", SpelMessages.INCORRECT_ELEMENT_TYPE_FOR_ARRAY, 4);
	// }
A
Andy Clement 已提交
201 202 203 204 205 206

	public void testConstructorInvocation05() {
		evaluate("new java.lang.String('foobar')", "foobar", String.class);
	}

	// array construction
207 208 209
	// public void testArrayConstruction01() {
	// evaluate("new int[] {1, 2, 3, 4, 5}", "int[5]{1,2,3,4,5}", int[].class);
	// }
A
Andy Clement 已提交
210

211 212 213
	// public void testArrayConstruction02() {
	// evaluate("new String[] {'abc', 'xyz'}", "java.lang.String[2]{abc,xyz}", String[].class);
	// }
A
Andy Clement 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	// unary expressions
	public void testUnaryMinus01() {
		evaluate("-5", "-5", Integer.class);
	}

	public void testUnaryPlus01() {
		evaluate("+5", "5", Integer.class);
	}

	public void testUnaryNot01() {
		evaluate("!true", "false", Boolean.class);
	}

	// collection processors
	// from spring.net: count,sum,max,min,average,sort,orderBy,distinct,nonNull
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	// public void testProcessorsCount01() {
	// evaluate("new String[] {'abc','def','xyz'}.count()", "3", Integer.class);
	// }
	//
	// public void testProcessorsCount02() {
	// evaluate("new int[] {1,2,3}.count()", "3", Integer.class);
	// }
	//
	// public void testProcessorsMax01() {
	// evaluate("new int[] {1,2,3}.max()", "3", Integer.class);
	// }
	//
	// public void testProcessorsMin01() {
	// evaluate("new int[] {1,2,3}.min()", "1", Integer.class);
	// }
	//
	// public void testProcessorsKeys01() {
	// evaluate("#{1:'January', 2:'February', 3:'March'}.keySet().sort()", "[1, 2, 3]", ArrayList.class);
	// }
	//
	// public void testProcessorsValues01() {
	// evaluate("#{1:'January', 2:'February', 3:'March'}.values().sort()", "[February, January, March]",
	// ArrayList.class);
	// }
	//
	// public void testProcessorsAverage01() {
	// evaluate("new int[] {1,2,3}.average()", "2", Integer.class);
	// }
	//
	// public void testProcessorsSort01() {
	// evaluate("new int[] {3,2,1}.sort()", "int[3]{1,2,3}", int[].class);
	// }
	//
	// public void testCollectionProcessorsNonNull01() {
	// evaluate("{'a','b',null,'d',null}.nonnull()", "[a, b, d]", ArrayList.class);
	// }
	//
	// public void testCollectionProcessorsDistinct01() {
	// evaluate("{'a','b','a','d','e'}.distinct()", "[a, b, d, e]", ArrayList.class);
	// }
A
Andy Clement 已提交
270 271

	// projection and selection
272 273 274 275 276 277 278 279 280 281 282 283
	// public void testProjection01() {
	// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{#isEven(#this)}", "[n, y, n, y, n, y, n, y, n, y]", ArrayList.class);
	// }
	//
	// public void testProjection02() {
	// evaluate("#{'a':'y','b':'n','c':'y'}.!{value=='y'?key:null}.nonnull().sort()", "[a, c]", ArrayList.class);
	// }
	//
	// public void testProjection03() {
	// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{#this>5}",
	// "[false, false, false, false, false, true, true, true, true, true]", ArrayList.class);
	// }
A
Andy Clement 已提交
284

285 286 287
	// public void testProjection04() {
	// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{$index>5?'y':'n'}", "[n, n, n, n, n, n, y, y, y, y]", ArrayList.class);
	// }
A
Andy Clement 已提交
288

289 290 291
	// public void testSelection01() {
	// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{#isEven(#this) == 'y'}", "[2, 4, 6, 8, 10]", ArrayList.class);
	// }
A
Andy Clement 已提交
292

293 294 295 296
	// public void testSelectionError_NonBooleanSelectionCriteria() {
	// evaluateAndCheckError("{1,2,3,4,5,6,7,8,9,10}.?{'nonboolean'}",
	// SpelMessages.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);
	// }
A
Andy Clement 已提交
297

298 299 300
	// public void testSelectionUsingIndex() {
	// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{$index > 5 }", "[7, 8, 9, 10]", ArrayList.class);
	// }
A
Andy Clement 已提交
301

302 303 304 305 306 307 308
	// public void testSelectionFirst01() {
	// evaluate("{1,2,3,4,5,6,7,8,9,10}.^{#isEven(#this) == 'y'}", "2", Integer.class);
	// }
	//
	// public void testSelectionLast01() {
	// evaluate("{1,2,3,4,5,6,7,8,9,10}.${#isEven(#this) == 'y'}", "10", Integer.class);
	// }
A
Andy Clement 已提交
309 310 311 312 313 314 315

	// assignment
	public void testAssignmentToVariables01() {
		evaluate("#var1='value1'", "value1", String.class);
	}

	// Ternary operator
316 317 318 319 320 321 322
	// public void testTernaryOperator01() {
	// evaluate("{1}.#isEven(#this[0]) == 'y'?'it is even':'it is odd'", "it is odd", String.class);
	// }
	//
	// public void testTernaryOperator02() {
	// evaluate("{2}.#isEven(#this[0]) == 'y'?'it is even':'it is odd'", "it is even", String.class);
	// }
A
Andy Clement 已提交
323

324 325 326 327 328 329 330 331 332 333
	public void testTernaryOperator03() {
		evaluateAndCheckError("'hello'?1:2", SpelMessages.TYPE_CONVERSION_ERROR); // cannot convert String to boolean
	}

	public void testTernaryOperator04() {
		// an int becomes TRUE if not 0, otherwise FALSE
		evaluate("12?1:2", 1, Integer.class); // int to boolean
		evaluate("1L?1:2", 1, Integer.class); // long to boolean
	}

A
Andy Clement 已提交
334
	// Indexer
335 336 337 338 339 340 341
	// public void testCutProcessor01() {
	// evaluate("{1,2,3,4,5}.cut(1,3)", "[2, 3, 4]", ArrayList.class);
	// }
	//
	// public void testCutProcessor02() {
	// evaluate("{1,2,3,4,5}.cut(3,1)", "[4, 3, 2]", ArrayList.class);
	// }
A
Andy Clement 已提交
342 343 344 345 346 347

	public void testIndexer03() {
		evaluate("'christian'[8]", "n", String.class);
	}

	// Bean references
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	// public void testReferences01() {
	// evaluate("@(apple).name", "Apple", String.class, true);
	// }
	//
	// public void testReferences02() {
	// evaluate("@(fruits:banana).name", "Banana", String.class, true);
	// }
	//
	// public void testReferences03() {
	// evaluate("@(a.b.c)", null, null);
	// } // null - no context, a.b.c treated as name
	//
	// public void testReferences05() {
	// evaluate("@(a/b/c:orange).name", "Orange", String.class, true);
	// }
	//
	// public void testReferences06() {
	// evaluate("@(apple).color.getRGB() == 	T(java.awt.Color).green.getRGB()", "true", Boolean.class);
	// }
	//
	// public void testReferences07() {
	// evaluate("@(apple).color.getRGB().equals(T(java.awt.Color).green.getRGB())", "true", Boolean.class);
	// }
371 372 373 374 375

	// value is not public, it is accessed through getRGB()
	// public void testStaticRef01() {
	// evaluate("T(Color).green.value!=0", "true", Boolean.class);
	// }
A
Andy Clement 已提交
376 377

	public void testStaticRef02() {
378
		evaluate("T(java.awt.Color).green.getRGB()!=0", "true", Boolean.class);
A
Andy Clement 已提交
379 380 381 382
	}

	// variables and functions
	public void testVariableAccess01() {
383
		evaluate("#answer", "42", Integer.class, true);
A
Andy Clement 已提交
384 385 386 387 388 389 390 391 392 393
	}

	public void testFunctionAccess01() {
		evaluate("#reverseInt(1,2,3)", "int[3]{3,2,1}", int[].class);
	}

	public void testFunctionAccess02() {
		evaluate("#reverseString('hello')", "olleh", String.class);
	}

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	//
	// 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 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 testLambdaAlphabet01() {
	// 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',''))",
	// "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 testVariableReferences() {
	// evaluate("(#answer=42;#answer)", "42", Integer.class, true);
	// evaluate("($answer=42;$answer)", "42", Integer.class, true);
	// }
A
Andy Clement 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

	// type references
	public void testTypeReferences01() {
		evaluate("T(java.lang.String)", "class java.lang.String", Class.class);
	}

	public void testTypeReferencesPrimitive() {
		evaluate("T(int)", "int", Class.class);
		evaluate("T(byte)", "byte", Class.class);
		evaluate("T(char)", "char", Class.class);
		evaluate("T(boolean)", "boolean", Class.class);
		evaluate("T(long)", "long", Class.class);
		evaluate("T(short)", "short", Class.class);
		evaluate("T(double)", "double", Class.class);
		evaluate("T(float)", "float", Class.class);
	}

	public void testTypeReferences02() {
		evaluate("T(String)", "class java.lang.String", Class.class);
	}

450 451 452 453 454 455 456 457 458 459
	public void testStringType() {
		evaluateAndAskForReturnType("getPlaceOfBirth().getCity()", "SmilJan", String.class);
	}

	public void testNumbers01() {
		evaluateAndAskForReturnType("3*4+5", 17, Integer.class);
		evaluateAndAskForReturnType("3*4+5", 17L, Long.class);
		evaluateAndAskForReturnType("65", 'A', Character.class);
		evaluateAndAskForReturnType("3*4+5", (short) 17, Short.class);
		evaluateAndAskForReturnType("3*4+5", "17", String.class);
A
Andy Clement 已提交
460
	}
A
Andy Clement 已提交
461

A
Andy Clement 已提交
462
}