EvaluationTests.java 14.4 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * 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.
 * 
 * @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);
	}

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

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	// 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 已提交
70 71

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

75 76 77 78 79 80 81
	// 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 已提交
82

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

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

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

A
Andy Clement 已提交
95 96 97 98 99 100 101 102
	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);
	}

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

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

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

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

	// property access
	public void testPropertyField01() {
122 123 124
		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 已提交
125 126 127 128 129 130
		evaluateAndCheckError("madeup", SpelMessages.PROPERTY_OR_FIELD_NOT_FOUND, 0, "madeup",
				"org.springframework.expression.spel.testresources.Inventor");
	}

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

	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
148 149 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
	// 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 已提交
182 183 184 185 186 187

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

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

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

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

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

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

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

	// 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
228 229 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
	// 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 已提交
268 269

	// projection and selection
270 271 272 273 274 275 276 277 278 279 280 281
	// 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 已提交
282

283 284 285
	// 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 已提交
286

287 288 289
	// 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 已提交
290

291 292 293 294
	// 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 已提交
295

296 297 298
	// 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 已提交
299

300 301 302 303 304 305 306
	// 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 已提交
307 308 309 310 311 312 313

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

	// Ternary operator
314 315 316 317 318 319 320
	// 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 已提交
321

322 323 324 325 326 327 328 329 330 331
	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 已提交
332
	// Indexer
333 334 335 336 337 338 339
	// 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 已提交
340 341 342 343 344 345

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

	// Bean references
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	// 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);
	// }
369 370 371 372 373

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

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

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

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

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

392 393 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
	//
	// 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 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

	// 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);
	}

448 449 450 451 452 453 454 455 456 457
	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 已提交
458
	}
A
Andy Clement 已提交
459

A
Andy Clement 已提交
460
}