Search in sources :

Example 21 with EvaluationContext

use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.

the class SpelReproTests method propertyAccessOnNullTarget_SPR5663.

@Test
void propertyAccessOnNullTarget_SPR5663() throws AccessException {
    PropertyAccessor accessor = new ReflectivePropertyAccessor();
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    assertThat(accessor.canRead(context, null, "abc")).isFalse();
    assertThat(accessor.canWrite(context, null, "abc")).isFalse();
    assertThatIllegalStateException().isThrownBy(() -> accessor.read(context, null, "abc"));
    assertThatIllegalStateException().isThrownBy(() -> accessor.write(context, null, "abc", "foo"));
}
Also used : PropertyAccessor(cn.taketoday.expression.PropertyAccessor) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) Test(org.junit.jupiter.api.Test)

Example 22 with EvaluationContext

use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.

the class SpelReproTests method SPR13055.

@Test
@SuppressWarnings("rawtypes")
void SPR13055() {
    List<Map<String, Object>> myPayload = new ArrayList<>();
    Map<String, Object> v1 = new HashMap<>();
    Map<String, Object> v2 = new HashMap<>();
    v1.put("test11", "test11");
    v1.put("test12", "test12");
    v2.put("test21", "test21");
    v2.put("test22", "test22");
    myPayload.add(v1);
    myPayload.add(v2);
    EvaluationContext context = new StandardEvaluationContext(myPayload);
    ExpressionParser parser = new SpelExpressionParser();
    String ex = "#root.![T(cn.taketoday.util.StringUtils).collectionToCommaDelimitedString(#this.values())]";
    List res = parser.parseExpression(ex).getValue(context, List.class);
    assertThat(res.toString()).isEqualTo("[test12,test11, test22,test21]");
    res = parser.parseExpression("#root.![#this.values()]").getValue(context, List.class);
    assertThat(res.toString()).isEqualTo("[[test12, test11], [test22, test21]]");
    res = parser.parseExpression("#root.![values()]").getValue(context, List.class);
    assertThat(res.toString()).isEqualTo("[[test12, test11], [test22, test21]]");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) ArrayList(java.util.ArrayList) List(java.util.List) 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 23 with EvaluationContext

use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.

the class SpelReproTests method wideningPrimitiveConversion_SPR8224.

/**
 * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
 * method accepting 'long' is ok.
 */
@Test
void wideningPrimitiveConversion_SPR8224() throws Exception {
    class WideningPrimitiveConversion {

        public int getX(long i) {
            return 10;
        }
    }
    final Integer INTEGER_VALUE = 7;
    WideningPrimitiveConversion target = new WideningPrimitiveConversion();
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.fromObject(INTEGER_VALUE));
    MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
    final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue();
    final int compiler = target.getX(INTEGER_VALUE);
    assertThat(actual).isEqualTo(compiler);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ReflectiveMethodResolver(cn.taketoday.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(cn.taketoday.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 24 with EvaluationContext

use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.

the class SpelReproTests method SPR13918.

@Test
void SPR13918() {
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("encoding", "UTF-8");
    Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
    Object result = ex.getValue(context);
    assertThat(result).isEqualTo(StandardCharsets.UTF_8);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 25 with EvaluationContext

use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.

the class SpelCompilationCoverageTests method functionReference.

@Test
public void functionReference() throws Exception {
    EvaluationContext ctx = new StandardEvaluationContext();
    Method m = getClass().getDeclaredMethod("concat", String.class, String.class);
    ctx.setVariable("concat", m);
    expression = parser.parseExpression("#concat('a','b')");
    assertThat(expression.getValue(ctx)).isEqualTo("ab");
    assertCanCompile(expression);
    assertThat(expression.getValue(ctx)).isEqualTo("ab");
    expression = parser.parseExpression("#concat(#concat('a','b'),'c').charAt(1)");
    assertThat(expression.getValue(ctx)).isEqualTo('b');
    assertCanCompile(expression);
    assertThat(expression.getValue(ctx)).isEqualTo('b');
    expression = parser.parseExpression("#concat(#a,#b)");
    ctx.setVariable("a", "foo");
    ctx.setVariable("b", "bar");
    assertThat(expression.getValue(ctx)).isEqualTo("foobar");
    assertCanCompile(expression);
    assertThat(expression.getValue(ctx)).isEqualTo("foobar");
    ctx.setVariable("b", "boo");
    assertThat(expression.getValue(ctx)).isEqualTo("fooboo");
    m = Math.class.getDeclaredMethod("pow", Double.TYPE, Double.TYPE);
    ctx.setVariable("kapow", m);
    expression = parser.parseExpression("#kapow(2.0d,2.0d)");
    assertThat(expression.getValue(ctx).toString()).isEqualTo("4.0");
    assertCanCompile(expression);
    assertThat(expression.getValue(ctx).toString()).isEqualTo("4.0");
}
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)

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