VariableAndFunctionTests.java 3.6 KB
Newer Older
1
/*
2
 * Copyright 2002-2009 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
16

17 18
package org.springframework.expression.spel;

19 20 21 22
import junit.framework.Assert;

import org.junit.Test;
import org.springframework.expression.spel.standard.SpelExpressionParser;
23
import org.springframework.expression.spel.support.StandardEvaluationContext;
24

25

26 27 28 29 30 31 32
/**
 * Tests the evaluation of expressions that access variables and functions (lambda/java).
 * 
 * @author Andy Clement
 */
public class VariableAndFunctionTests extends ExpressionTestCase {

33
	@Test
34
	public void testVariableAccess01() {
35 36 37
		evaluate("#answer", "42", Integer.class, SHOULD_BE_WRITABLE);
		evaluate("#answer / 2", 21, Integer.class, SHOULD_NOT_BE_WRITABLE);
	}
38
	
39
	@Test
40 41 42 43
	public void testVariableAccess_WellKnownVariables() {
		evaluate("#this.getName()","Nikola Tesla",String.class);
		evaluate("#root.getName()","Nikola Tesla",String.class);
	}
44

45
	@Test
46 47 48
	public void testFunctionAccess01() {
		evaluate("#reverseInt(1,2,3)", "int[3]{3,2,1}", int[].class);
		evaluate("#reverseInt('1',2,3)", "int[3]{3,2,1}", int[].class); // requires type conversion of '1' to 1
49
		evaluateAndCheckError("#reverseInt(1)", SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 1, 3);
50 51
	}

52
	@Test
53 54 55 56
	public void testFunctionAccess02() {
		evaluate("#reverseString('hello')", "olleh", String.class);
		evaluate("#reverseString(37)", "73", String.class); // requires type conversion of 37 to '37'
	}
57

58
	@Test
59
	public void testCallVarargsFunction() {
60 61 62 63 64 65 66 67 68 69
		evaluate("#varargsFunctionReverseStringsAndMerge('a','b','c')", "cba", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge('a')", "a", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge()", "", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge('b',25)", "25b", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge(25)", "25", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge2(1,'a','b','c')", "1cba", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge2(2,'a')", "2a", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge2(3)", "3", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge2(4,'b',25)", "425b", String.class);
		evaluate("#varargsFunctionReverseStringsAndMerge2(5,25)", "525", String.class);
70
	}
71

72
	@Test
73
	public void testCallingIllegalFunctions() throws Exception {
74
		SpelExpressionParser parser = new SpelExpressionParser();
75
		StandardEvaluationContext ctx = new StandardEvaluationContext();
76
		ctx.setVariable("notStatic", this.getClass().getMethod("nonStatic"));
77 78 79
		try {
			@SuppressWarnings("unused")
			Object v = parser.parseExpression("#notStatic()").getValue(ctx);
80 81
			Assert.fail("Should have failed with exception - cannot call non static method that way");
		} catch (SpelEvaluationException se) {
82
			if (se.getMessageCode() != SpelMessage.FUNCTION_MUST_BE_STATIC) {
83
				se.printStackTrace();
84
				Assert.fail("Should have failed a message about the function needing to be static, not: "
85
						+ se.getMessageCode());
86 87 88
			}
		}
	}
89
	// this method is used by the test above
90 91 92
	public void nonStatic() {
	}

93
}