Search in sources :

Example 26 with EvaluationContext

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}");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 27 with EvaluationContext

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]");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 28 with EvaluationContext

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);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 29 with EvaluationContext

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");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 30 with EvaluationContext

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));
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) CompositeStringExpression(cn.taketoday.expression.common.CompositeStringExpression) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) EvaluationException(cn.taketoday.expression.EvaluationException) Test(org.junit.jupiter.api.Test)

Aggregations

EvaluationContext (cn.taketoday.expression.EvaluationContext)126 Test (org.junit.jupiter.api.Test)120 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)110 Expression (cn.taketoday.expression.Expression)68 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)62 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)28 ArrayList (java.util.ArrayList)24 SimpleEvaluationContext (cn.taketoday.expression.spel.support.SimpleEvaluationContext)20 List (java.util.List)18 ExpressionParser (cn.taketoday.expression.ExpressionParser)16 TypedValue (cn.taketoday.expression.TypedValue)14 RecordPerson (cn.taketoday.expression.spel.testresources.RecordPerson)14 Map (java.util.Map)12 Person (cn.taketoday.expression.spel.testresources.Person)11 TypeDescriptor (cn.taketoday.core.TypeDescriptor)10 MethodExecutor (cn.taketoday.expression.MethodExecutor)10 ReflectiveMethodResolver (cn.taketoday.expression.spel.support.ReflectiveMethodResolver)10 Method (java.lang.reflect.Method)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 EvaluationException (cn.taketoday.expression.EvaluationException)6