use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class EvaluationTests method incdecTogether.
@Test
public void incdecTogether() {
Spr9751 helper = new Spr9751();
StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = null;
// index1 is 2 at the start - the 'intArray[#root.index1++]' should not be evaluated twice!
// intArray[2] is 3
e = parser.parseExpression("intArray[#root.index1++]++");
e.getValue(ctx, Integer.class);
assertEquals(3, helper.index1);
assertEquals(4, helper.intArray[2]);
// index1 is 3 intArray[3] is 4
e = parser.parseExpression("intArray[#root.index1++]--");
assertEquals(4, e.getValue(ctx, Integer.class).intValue());
assertEquals(4, helper.index1);
assertEquals(3, helper.intArray[3]);
// index1 is 4, intArray[3] is 3
e = parser.parseExpression("intArray[--#root.index1]++");
assertEquals(3, e.getValue(ctx, Integer.class).intValue());
assertEquals(3, helper.index1);
assertEquals(4, helper.intArray[3]);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method ternaryOperator_SPR15192.
@Test
public void ternaryOperator_SPR15192() {
SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null);
Expression exp;
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("map", Collections.singletonMap("foo", "qux"));
exp = new SpelExpressionParser(configuration).parseExpression("bar(#map['foo'] != null ? #map['foo'] : 'qux')");
assertEquals("QUX", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("QUX", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("3==3?3:'foo'");
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("3!=3?3:'foo'");
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// When the condition is a double slot primitive
exp = new SpelExpressionParser(configuration).parseExpression("3==3?3L:'foo'");
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("3!=3?3L:'foo'");
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// When the condition is an empty string
exp = new SpelExpressionParser(configuration).parseExpression("''==''?'abc':4L");
assertEquals("abc", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("abc", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// null condition
exp = new SpelExpressionParser(configuration).parseExpression("3==3?null:4L");
assertEquals(null, exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals(null, exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// variable access returning primitive
exp = new SpelExpressionParser(configuration).parseExpression("#x==#x?50:'foo'");
context.setVariable("x", 50);
assertEquals("50", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("50", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("#x!=#x?50:'foo'");
context.setVariable("x", null);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// variable access returning array
exp = new SpelExpressionParser(configuration).parseExpression("#x==#x?'1,2,3':'foo'");
context.setVariable("x", new int[] { 1, 2, 3 });
assertEquals("1,2,3", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("1,2,3", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method indexer.
@SuppressWarnings("unchecked")
@Test
public void indexer() throws Exception {
String[] sss = new String[] { "a", "b", "c" };
Number[] ns = new Number[] { 2, 8, 9 };
int[] is = new int[] { 8, 9, 10 };
double[] ds = new double[] { 3.0d, 4.0d, 5.0d };
long[] ls = new long[] { 2L, 3L, 4L };
short[] ss = new short[] { (short) 33, (short) 44, (short) 55 };
float[] fs = new float[] { 6.0f, 7.0f, 8.0f };
byte[] bs = new byte[] { (byte) 2, (byte) 3, (byte) 4 };
char[] cs = new char[] { 'a', 'b', 'c' };
// Access String (reference type) array
expression = parser.parseExpression("[0]");
assertEquals("a", expression.getValue(sss));
assertCanCompile(expression);
assertEquals("a", expression.getValue(sss));
assertEquals("Ljava/lang/String", getAst().getExitDescriptor());
expression = parser.parseExpression("[1]");
assertEquals(8, expression.getValue(ns));
assertCanCompile(expression);
assertEquals(8, expression.getValue(ns));
assertEquals("Ljava/lang/Number", getAst().getExitDescriptor());
// Access int array
expression = parser.parseExpression("[2]");
assertEquals(10, expression.getValue(is));
assertCanCompile(expression);
assertEquals(10, expression.getValue(is));
assertEquals("I", getAst().getExitDescriptor());
// Access double array
expression = parser.parseExpression("[1]");
assertEquals(4.0d, expression.getValue(ds));
assertCanCompile(expression);
assertEquals(4.0d, expression.getValue(ds));
assertEquals("D", getAst().getExitDescriptor());
// Access long array
expression = parser.parseExpression("[0]");
assertEquals(2L, expression.getValue(ls));
assertCanCompile(expression);
assertEquals(2L, expression.getValue(ls));
assertEquals("J", getAst().getExitDescriptor());
// Access short array
expression = parser.parseExpression("[2]");
assertEquals((short) 55, expression.getValue(ss));
assertCanCompile(expression);
assertEquals((short) 55, expression.getValue(ss));
assertEquals("S", getAst().getExitDescriptor());
// Access float array
expression = parser.parseExpression("[0]");
assertEquals(6.0f, expression.getValue(fs));
assertCanCompile(expression);
assertEquals(6.0f, expression.getValue(fs));
assertEquals("F", getAst().getExitDescriptor());
// Access byte array
expression = parser.parseExpression("[2]");
assertEquals((byte) 4, expression.getValue(bs));
assertCanCompile(expression);
assertEquals((byte) 4, expression.getValue(bs));
assertEquals("B", getAst().getExitDescriptor());
// Access char array
expression = parser.parseExpression("[1]");
assertEquals('b', expression.getValue(cs));
assertCanCompile(expression);
assertEquals('b', expression.getValue(cs));
assertEquals("C", getAst().getExitDescriptor());
// Collections
List<String> strings = new ArrayList<>();
strings.add("aaa");
strings.add("bbb");
strings.add("ccc");
expression = parser.parseExpression("[1]");
assertEquals("bbb", expression.getValue(strings));
assertCanCompile(expression);
assertEquals("bbb", expression.getValue(strings));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
List<Integer> ints = new ArrayList<>();
ints.add(123);
ints.add(456);
ints.add(789);
expression = parser.parseExpression("[2]");
assertEquals(789, expression.getValue(ints));
assertCanCompile(expression);
assertEquals(789, expression.getValue(ints));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
// Maps
Map<String, Integer> map1 = new HashMap<>();
map1.put("aaa", 111);
map1.put("bbb", 222);
map1.put("ccc", 333);
expression = parser.parseExpression("['aaa']");
assertEquals(111, expression.getValue(map1));
assertCanCompile(expression);
assertEquals(111, expression.getValue(map1));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
// Object
TestClass6 tc = new TestClass6();
expression = parser.parseExpression("['orange']");
assertEquals("value1", expression.getValue(tc));
assertCanCompile(expression);
assertEquals("value1", expression.getValue(tc));
assertEquals("Ljava/lang/String", getAst().getExitDescriptor());
expression = parser.parseExpression("['peach']");
assertEquals(34L, expression.getValue(tc));
assertCanCompile(expression);
assertEquals(34L, expression.getValue(tc));
assertEquals("J", getAst().getExitDescriptor());
// getter
expression = parser.parseExpression("['banana']");
assertEquals("value3", expression.getValue(tc));
assertCanCompile(expression);
assertEquals("value3", expression.getValue(tc));
assertEquals("Ljava/lang/String", getAst().getExitDescriptor());
// list of arrays
List<String[]> listOfStringArrays = new ArrayList<>();
listOfStringArrays.add(new String[] { "a", "b", "c" });
listOfStringArrays.add(new String[] { "d", "e", "f" });
expression = parser.parseExpression("[1]");
assertEquals("d e f", stringify(expression.getValue(listOfStringArrays)));
assertCanCompile(expression);
assertEquals("d e f", stringify(expression.getValue(listOfStringArrays)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
expression = parser.parseExpression("[1][0]");
assertEquals("d", stringify(expression.getValue(listOfStringArrays)));
assertCanCompile(expression);
assertEquals("d", stringify(expression.getValue(listOfStringArrays)));
assertEquals("Ljava/lang/String", getAst().getExitDescriptor());
List<Integer[]> listOfIntegerArrays = new ArrayList<>();
listOfIntegerArrays.add(new Integer[] { 1, 2, 3 });
listOfIntegerArrays.add(new Integer[] { 4, 5, 6 });
expression = parser.parseExpression("[0]");
assertEquals("1 2 3", stringify(expression.getValue(listOfIntegerArrays)));
assertCanCompile(expression);
assertEquals("1 2 3", stringify(expression.getValue(listOfIntegerArrays)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
expression = parser.parseExpression("[0][1]");
assertEquals(2, expression.getValue(listOfIntegerArrays));
assertCanCompile(expression);
assertEquals(2, expression.getValue(listOfIntegerArrays));
assertEquals("Ljava/lang/Integer", getAst().getExitDescriptor());
// array of lists
List<String>[] stringArrayOfLists = new ArrayList[2];
stringArrayOfLists[0] = new ArrayList<>();
stringArrayOfLists[0].add("a");
stringArrayOfLists[0].add("b");
stringArrayOfLists[0].add("c");
stringArrayOfLists[1] = new ArrayList<>();
stringArrayOfLists[1].add("d");
stringArrayOfLists[1].add("e");
stringArrayOfLists[1].add("f");
expression = parser.parseExpression("[1]");
assertEquals("d e f", stringify(expression.getValue(stringArrayOfLists)));
assertCanCompile(expression);
assertEquals("d e f", stringify(expression.getValue(stringArrayOfLists)));
assertEquals("Ljava/util/ArrayList", getAst().getExitDescriptor());
expression = parser.parseExpression("[1][2]");
assertEquals("f", stringify(expression.getValue(stringArrayOfLists)));
assertCanCompile(expression);
assertEquals("f", stringify(expression.getValue(stringArrayOfLists)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
// array of arrays
String[][] referenceTypeArrayOfArrays = new String[][] { new String[] { "a", "b", "c" }, new String[] { "d", "e", "f" } };
expression = parser.parseExpression("[1]");
assertEquals("d e f", stringify(expression.getValue(referenceTypeArrayOfArrays)));
assertCanCompile(expression);
assertEquals("[Ljava/lang/String", getAst().getExitDescriptor());
assertEquals("d e f", stringify(expression.getValue(referenceTypeArrayOfArrays)));
assertEquals("[Ljava/lang/String", getAst().getExitDescriptor());
expression = parser.parseExpression("[1][2]");
assertEquals("f", stringify(expression.getValue(referenceTypeArrayOfArrays)));
assertCanCompile(expression);
assertEquals("f", stringify(expression.getValue(referenceTypeArrayOfArrays)));
assertEquals("Ljava/lang/String", getAst().getExitDescriptor());
int[][] primitiveTypeArrayOfArrays = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
expression = parser.parseExpression("[1]");
assertEquals("4 5 6", stringify(expression.getValue(primitiveTypeArrayOfArrays)));
assertCanCompile(expression);
assertEquals("4 5 6", stringify(expression.getValue(primitiveTypeArrayOfArrays)));
assertEquals("[I", getAst().getExitDescriptor());
expression = parser.parseExpression("[1][2]");
assertEquals("6", stringify(expression.getValue(primitiveTypeArrayOfArrays)));
assertCanCompile(expression);
assertEquals("6", stringify(expression.getValue(primitiveTypeArrayOfArrays)));
assertEquals("I", getAst().getExitDescriptor());
// list of lists of reference types
List<List<String>> listOfListOfStrings = new ArrayList<>();
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
listOfListOfStrings.add(list);
list = new ArrayList<>();
list.add("d");
list.add("e");
list.add("f");
listOfListOfStrings.add(list);
expression = parser.parseExpression("[1]");
assertEquals("d e f", stringify(expression.getValue(listOfListOfStrings)));
assertCanCompile(expression);
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
assertEquals("d e f", stringify(expression.getValue(listOfListOfStrings)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
expression = parser.parseExpression("[1][2]");
assertEquals("f", stringify(expression.getValue(listOfListOfStrings)));
assertCanCompile(expression);
assertEquals("f", stringify(expression.getValue(listOfListOfStrings)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
// Map of lists
Map<String, List<String>> mapToLists = new HashMap<>();
list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
mapToLists.put("foo", list);
expression = parser.parseExpression("['foo']");
assertEquals("a b c", stringify(expression.getValue(mapToLists)));
assertCanCompile(expression);
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
assertEquals("a b c", stringify(expression.getValue(mapToLists)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
expression = parser.parseExpression("['foo'][2]");
assertEquals("c", stringify(expression.getValue(mapToLists)));
assertCanCompile(expression);
assertEquals("c", stringify(expression.getValue(mapToLists)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
// Map to array
Map<String, int[]> mapToIntArray = new HashMap<>();
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new CompilableMapAccessor());
mapToIntArray.put("foo", new int[] { 1, 2, 3 });
expression = parser.parseExpression("['foo']");
assertEquals("1 2 3", stringify(expression.getValue(mapToIntArray)));
assertCanCompile(expression);
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
assertEquals("1 2 3", stringify(expression.getValue(mapToIntArray)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
expression = parser.parseExpression("['foo'][1]");
assertEquals(2, expression.getValue(mapToIntArray));
assertCanCompile(expression);
assertEquals(2, expression.getValue(mapToIntArray));
expression = parser.parseExpression("foo");
assertEquals("1 2 3", stringify(expression.getValue(ctx, mapToIntArray)));
assertCanCompile(expression);
assertEquals("1 2 3", stringify(expression.getValue(ctx, mapToIntArray)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
expression = parser.parseExpression("foo[1]");
assertEquals(2, expression.getValue(ctx, mapToIntArray));
assertCanCompile(expression);
assertEquals(2, expression.getValue(ctx, mapToIntArray));
expression = parser.parseExpression("['foo'][2]");
assertEquals("3", stringify(expression.getValue(ctx, mapToIntArray)));
assertCanCompile(expression);
assertEquals("3", stringify(expression.getValue(ctx, mapToIntArray)));
assertEquals("I", getAst().getExitDescriptor());
// Map array
Map<String, String>[] mapArray = new Map[1];
mapArray[0] = new HashMap<>();
mapArray[0].put("key", "value1");
expression = parser.parseExpression("[0]");
assertEquals("{key=value1}", stringify(expression.getValue(mapArray)));
assertCanCompile(expression);
assertEquals("Ljava/util/Map", getAst().getExitDescriptor());
assertEquals("{key=value1}", stringify(expression.getValue(mapArray)));
assertEquals("Ljava/util/Map", getAst().getExitDescriptor());
expression = parser.parseExpression("[0]['key']");
assertEquals("value1", stringify(expression.getValue(mapArray)));
assertCanCompile(expression);
assertEquals("value1", stringify(expression.getValue(mapArray)));
assertEquals("Ljava/lang/Object", getAst().getExitDescriptor());
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method functionReferenceVarargs_SPR12359.
@Test
public void functionReferenceVarargs_SPR12359() throws Exception {
StandardEvaluationContext context = new StandardEvaluationContext();
context.registerFunction("append", SomeCompareMethod2.class.getDeclaredMethod("append", String[].class));
context.registerFunction("append2", SomeCompareMethod2.class.getDeclaredMethod("append2", Object[].class));
context.registerFunction("append3", SomeCompareMethod2.class.getDeclaredMethod("append3", String[].class));
context.registerFunction("append4", SomeCompareMethod2.class.getDeclaredMethod("append4", String.class, String[].class));
context.registerFunction("appendChar", SomeCompareMethod2.class.getDeclaredMethod("appendChar", char[].class));
context.registerFunction("sum", SomeCompareMethod2.class.getDeclaredMethod("sum", int[].class));
context.registerFunction("sumDouble", SomeCompareMethod2.class.getDeclaredMethod("sumDouble", double[].class));
context.registerFunction("sumFloat", SomeCompareMethod2.class.getDeclaredMethod("sumFloat", float[].class));
context.setVariable("stringArray", new String[] { "x", "y", "z" });
context.setVariable("intArray", new int[] { 5, 6, 9 });
context.setVariable("doubleArray", new double[] { 5.0d, 6.0d, 9.0d });
context.setVariable("floatArray", new float[] { 5.0f, 6.0f, 9.0f });
expression = parser.parseExpression("#append('a','b','c')");
assertEquals("abc", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("abc", expression.getValue(context).toString());
expression = parser.parseExpression("#append('a')");
assertEquals("a", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("a", expression.getValue(context).toString());
expression = parser.parseExpression("#append()");
assertEquals("", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("", expression.getValue(context).toString());
expression = parser.parseExpression("#append(#stringArray)");
assertEquals("xyz", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("xyz", expression.getValue(context).toString());
// This is a methodreference invocation, to compare with functionreference
expression = parser.parseExpression("append(#stringArray)");
assertEquals("xyz", expression.getValue(context, new SomeCompareMethod2()).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("xyz", expression.getValue(context, new SomeCompareMethod2()).toString());
expression = parser.parseExpression("#append2('a','b','c')");
assertEquals("abc", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("abc", expression.getValue(context).toString());
expression = parser.parseExpression("append2('a','b')");
assertEquals("ab", expression.getValue(context, new SomeCompareMethod2()).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("ab", expression.getValue(context, new SomeCompareMethod2()).toString());
expression = parser.parseExpression("#append2('a','b')");
assertEquals("ab", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("ab", expression.getValue(context).toString());
expression = parser.parseExpression("#append2()");
assertEquals("", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("", expression.getValue(context).toString());
expression = parser.parseExpression("#append3(#stringArray)");
assertEquals("xyz", expression.getValue(context, new SomeCompareMethod2()).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("xyz", expression.getValue(context, new SomeCompareMethod2()).toString());
// TODO fails due to conversionservice handling of String[] to Object...
// expression = parser.parseExpression("#append2(#stringArray)");
// assertEquals("xyz", expression.getValue(context).toString());
// assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable());
// assertCanCompile(expression);
// assertEquals("xyz", expression.getValue(context).toString());
expression = parser.parseExpression("#sum(1,2,3)");
assertEquals(6, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(6, expression.getValue(context));
expression = parser.parseExpression("#sum(2)");
assertEquals(2, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(2, expression.getValue(context));
expression = parser.parseExpression("#sum()");
assertEquals(0, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(0, expression.getValue(context));
expression = parser.parseExpression("#sum(#intArray)");
assertEquals(20, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(20, expression.getValue(context));
expression = parser.parseExpression("#sumDouble(1.0d,2.0d,3.0d)");
assertEquals(6, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(6, expression.getValue(context));
expression = parser.parseExpression("#sumDouble(2.0d)");
assertEquals(2, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(2, expression.getValue(context));
expression = parser.parseExpression("#sumDouble()");
assertEquals(0, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(0, expression.getValue(context));
expression = parser.parseExpression("#sumDouble(#doubleArray)");
assertEquals(20, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(20, expression.getValue(context));
expression = parser.parseExpression("#sumFloat(1.0f,2.0f,3.0f)");
assertEquals(6, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(6, expression.getValue(context));
expression = parser.parseExpression("#sumFloat(2.0f)");
assertEquals(2, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(2, expression.getValue(context));
expression = parser.parseExpression("#sumFloat()");
assertEquals(0, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(0, expression.getValue(context));
expression = parser.parseExpression("#sumFloat(#floatArray)");
assertEquals(20, expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals(20, expression.getValue(context));
expression = parser.parseExpression("#appendChar('abc'.charAt(0),'abc'.charAt(1))");
assertEquals("ab", expression.getValue(context));
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("ab", expression.getValue(context));
expression = parser.parseExpression("#append4('a','b','c')");
assertEquals("a::bc", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("a::bc", expression.getValue(context).toString());
expression = parser.parseExpression("#append4('a','b')");
assertEquals("a::b", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("a::b", expression.getValue(context).toString());
expression = parser.parseExpression("#append4('a')");
assertEquals("a::", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("a::", expression.getValue(context).toString());
expression = parser.parseExpression("#append4('a',#stringArray)");
assertEquals("a::xyz", expression.getValue(context).toString());
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
assertCanCompile(expression);
assertEquals("a::xyz", expression.getValue(context).toString());
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method variantGetter.
@Test
public void variantGetter() throws Exception {
Payload2Holder holder = new Payload2Holder();
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new MyAccessor());
expression = parser.parseExpression("payload2.var1");
Object v = expression.getValue(ctx, holder);
assertEquals("abc", v);
// // time it interpreted
// long stime = System.currentTimeMillis();
// for (int i = 0; i < 100000; i++) {
// v = expression.getValue(ctx,holder);
// }
// System.out.println((System.currentTimeMillis() - stime));
assertCanCompile(expression);
v = expression.getValue(ctx, holder);
assertEquals("abc", v);
// // time it compiled
// stime = System.currentTimeMillis();
// for (int i = 0; i < 100000; i++) {
// v = expression.getValue(ctx,holder);
// }
// System.out.println((System.currentTimeMillis() - stime));
}
Aggregations