use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelReproTests method SPR10417_maps.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void SPR10417_maps() {
Map map1 = new HashMap();
map1.put("A", 65);
map1.put("B", 66);
map1.put("X", 66);
Map map2 = new HashMap();
map2.put("X", 66);
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("map1", map1);
context.setVariable("map2", map2);
// #this should be the element from list1
Expression ex = parser.parseExpression("#map1.?[#map2.containsKey(#this.getKey())]");
Object result = ex.getValue(context);
assertThat(result.toString()).isEqualTo("{X=66}");
ex = parser.parseExpression("#map1.?[#map2.containsKey(key)]");
result = ex.getValue(context);
assertThat(result.toString()).isEqualTo("{X=66}");
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelReproTests method SPR10417.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void SPR10417() {
List list1 = new ArrayList();
list1.add("a");
list1.add("b");
list1.add("x");
List list2 = new ArrayList();
list2.add("c");
list2.add("x");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("list1", list1);
context.setVariable("list2", list2);
// #this should be the element from list1
Expression ex = parser.parseExpression("#list1.?[#list2.contains(#this)]");
Object result = ex.getValue(context);
assertThat(result.toString()).isEqualTo("[x]");
// toString() should be called on the element from list1
ex = parser.parseExpression("#list1.?[#list2.contains(toString())]");
result = ex.getValue(context);
assertThat(result.toString()).isEqualTo("[x]");
List list3 = new ArrayList();
list3.add(1);
list3.add(2);
list3.add(3);
list3.add(4);
context = new StandardEvaluationContext();
context.setVariable("list3", list3);
ex = parser.parseExpression("#list3.?[#this > 2]");
result = ex.getValue(context);
assertThat(result.toString()).isEqualTo("[3, 4]");
ex = parser.parseExpression("#list3.?[#this >= T(java.lang.Math).abs(T(java.lang.Math).abs(#this))]");
result = ex.getValue(context);
assertThat(result.toString()).isEqualTo("[1, 2, 3, 4]");
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelCompilationCoverageTests method variableReference_userDefined.
@Test
public void variableReference_userDefined() throws Exception {
EvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("target", "abc");
expression = parser.parseExpression("#target");
assertThat(expression.getValue(ctx)).isEqualTo("abc");
assertCanCompile(expression);
assertThat(expression.getValue(ctx)).isEqualTo("abc");
ctx.setVariable("target", "123");
assertThat(expression.getValue(ctx)).isEqualTo("123");
ctx.setVariable("target", 42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expression.getValue(ctx)).withCauseInstanceOf(ClassCastException.class);
ctx.setVariable("target", "abc");
expression = parser.parseExpression("#target.charAt(0)");
assertThat(expression.getValue(ctx)).isEqualTo('a');
assertCanCompile(expression);
assertThat(expression.getValue(ctx)).isEqualTo('a');
ctx.setVariable("target", "1");
assertThat(expression.getValue(ctx)).isEqualTo('1');
ctx.setVariable("target", 42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expression.getValue(ctx)).withCauseInstanceOf(ClassCastException.class);
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelCompilationCoverageTests method functionReferenceVarargs.
@Test
public void functionReferenceVarargs() throws Exception {
EvaluationContext ctx = new StandardEvaluationContext();
Method m = getClass().getDeclaredMethod("join", String[].class);
ctx.setVariable("join", m);
expression = parser.parseExpression("#join('a','b','c')");
assertThat(expression.getValue(ctx)).isEqualTo("abc");
assertCanCompile(expression);
assertThat(expression.getValue(ctx)).isEqualTo("abc");
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class TemplateExpressionParsingTests method testCompositeStringExpression.
@Test
public void testCompositeStringExpression() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
Expression ex = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
assertThat(ex.getValue()).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(String.class)).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue((Object) null, String.class)).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(new Rooty())).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(new Rooty(), String.class)).isInstanceOf(String.class).isEqualTo("hello world");
EvaluationContext ctx = new StandardEvaluationContext();
assertThat(ex.getValue(ctx)).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(ctx, String.class)).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(ctx, null, String.class)).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(ctx, new Rooty())).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(ctx, new Rooty(), String.class)).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(ctx, new Rooty(), String.class)).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getExpressionString()).isEqualTo("hello ${'world'}");
assertThat(ex.isWritable(new StandardEvaluationContext())).isFalse();
assertThat(ex.isWritable(new Rooty())).isFalse();
assertThat(ex.isWritable(new StandardEvaluationContext(), new Rooty())).isFalse();
assertThat(ex.getValueType()).isEqualTo(String.class);
assertThat(ex.getValueType(ctx)).isEqualTo(String.class);
assertThat(ex.getValueTypeDescriptor().getType()).isEqualTo(String.class);
assertThat(ex.getValueTypeDescriptor(ctx).getType()).isEqualTo(String.class);
assertThat(ex.getValueType(new Rooty())).isEqualTo(String.class);
assertThat(ex.getValueType(ctx, new Rooty())).isEqualTo(String.class);
assertThat(ex.getValueTypeDescriptor(new Rooty()).getType()).isEqualTo(String.class);
assertThat(ex.getValueTypeDescriptor(ctx, new Rooty()).getType()).isEqualTo(String.class);
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() -> ex.setValue(ctx, null));
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() -> ex.setValue((Object) null, null));
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() -> ex.setValue(ctx, null, null));
}
Aggregations