Search in sources :

Example 1 with SpelExpression

use of cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.

the class MethodInvocationTests method testMethodFiltering_SPR6764.

@Test
public void testMethodFiltering_SPR6764() {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(new TestObject());
    LocalFilter filter = new LocalFilter();
    context.registerMethodFilter(TestObject.class, filter);
    // Filter will be called but not do anything, so first doit() will be invoked
    SpelExpression expr = (SpelExpression) parser.parseExpression("doit(1)");
    String result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("1");
    assertThat(filter.filterCalled).isTrue();
    // Filter will now remove non @Anno annotated methods
    filter.removeIfNotAnnotated = true;
    filter.filterCalled = false;
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("double 1.0");
    assertThat(filter.filterCalled).isTrue();
    // check not called for other types
    filter.filterCalled = false;
    context.setRootObject("abc");
    expr = (SpelExpression) parser.parseExpression("charAt(0)");
    result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("a");
    assertThat(filter.filterCalled).isFalse();
    // check de-registration works
    filter.filterCalled = false;
    // clear filter
    context.registerMethodFilter(TestObject.class, null);
    context.setRootObject(new TestObject());
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("1");
    assertThat(filter.filterCalled).isFalse();
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 2 with SpelExpression

use of cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.

the class MapTests method checkConstantMap.

private void checkConstantMap(String expressionText, boolean expectedToBeConstant) {
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
    SpelNode node = expression.getAST();
    boolean condition = node instanceof InlineMap;
    assertThat(condition).isTrue();
    InlineMap inlineMap = (InlineMap) node;
    if (expectedToBeConstant) {
        assertThat(inlineMap.isConstant()).isTrue();
    } else {
        assertThat(inlineMap.isConstant()).isFalse();
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) InlineMap(cn.taketoday.expression.spel.ast.InlineMap)

Example 3 with SpelExpression

use of cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.

the class MapTests method testMapKeysThatAreAlsoSpELKeywords.

@Test
public void testMapKeysThatAreAlsoSpELKeywords() {
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = null;
    Object o = null;
    // expression = (SpelExpression) parser.parseExpression("foo['NEW']");
    // o = expression.getValue(new MapHolder());
    // assertEquals("VALUE",o);
    expression = (SpelExpression) parser.parseExpression("foo[T]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("TV");
    expression = (SpelExpression) parser.parseExpression("foo[t]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("tv");
    expression = (SpelExpression) parser.parseExpression("foo[NEW]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("VALUE");
    expression = (SpelExpression) parser.parseExpression("foo[new]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("value");
    expression = (SpelExpression) parser.parseExpression("foo['abc.def']");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("value");
    expression = (SpelExpression) parser.parseExpression("foo[foo[NEW]]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("37");
    expression = (SpelExpression) parser.parseExpression("foo[foo[new]]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("38");
    expression = (SpelExpression) parser.parseExpression("foo[foo[foo[T]]]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("value");
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 4 with SpelExpression

use of cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.

the class OperatorOverloaderTests method testSimpleOperations.

@Test
public void testSimpleOperations() throws Exception {
    // no built in support for this:
    evaluateAndCheckError("'abc'-true", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    eContext.setOperatorOverloader(new StringAndBooleanAddition());
    SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'+true");
    assertThat(expr.getValue(eContext)).isEqualTo("abctrue");
    expr = (SpelExpression) parser.parseExpression("'abc'-true");
    assertThat(expr.getValue(eContext)).isEqualTo("abc");
    expr = (SpelExpression) parser.parseExpression("'abc'+null");
    assertThat(expr.getValue(eContext)).isEqualTo("abcnull");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 5 with SpelExpression

use of cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.

the class OperatorTests method testPlus.

@Test
void testPlus() {
    evaluate("7 + 2", "9", Integer.class);
    evaluate("3.0f + 5.0f", 8.0f, Float.class);
    evaluate("3.0d + 5.0d", 8.0d, Double.class);
    evaluate("3 + new java.math.BigDecimal('5')", new BigDecimal("8"), BigDecimal.class);
    evaluate("'ab' + 2", "ab2", String.class);
    evaluate("2 + 'a'", "2a", String.class);
    evaluate("'ab' + null", "abnull", String.class);
    evaluate("null + 'ab'", "nullab", String.class);
    // AST:
    SpelExpression expr = (SpelExpression) parser.parseExpression("+3");
    assertThat(expr.toStringAST()).isEqualTo("+3");
    expr = (SpelExpression) parser.parseExpression("2+3");
    assertThat(expr.toStringAST()).isEqualTo("(2 + 3)");
    // use as a unary operator
    evaluate("+5d", 5d, Double.class);
    evaluate("+5L", 5L, Long.class);
    evaluate("+5", 5, Integer.class);
    evaluate("+new java.math.BigDecimal('5')", new BigDecimal("5"), BigDecimal.class);
    evaluateAndCheckError("+'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    // string concatenation
    evaluate("'abc'+'def'", "abcdef", String.class);
    evaluate("5 + new Integer('37')", 42, Integer.class);
}
Also used : SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Aggregations

SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)66 Test (org.junit.jupiter.api.Test)54 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)38 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)32 Expression (cn.taketoday.expression.Expression)8 ArrayList (java.util.ArrayList)8 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 CompoundExpression (cn.taketoday.expression.spel.ast.CompoundExpression)4 SpelNodeImpl (cn.taketoday.expression.spel.ast.SpelNodeImpl)4 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)4 List (java.util.List)4 EvaluationContext (cn.taketoday.expression.EvaluationContext)2 EvaluationException (cn.taketoday.expression.EvaluationException)2 ExpressionParser (cn.taketoday.expression.ExpressionParser)2 InlineList (cn.taketoday.expression.spel.ast.InlineList)2 InlineMap (cn.taketoday.expression.spel.ast.InlineMap)2 OpLT (cn.taketoday.expression.spel.ast.OpLT)2 Operator (cn.taketoday.expression.spel.ast.Operator)2 Ternary (cn.taketoday.expression.spel.ast.Ternary)2