Search in sources :

Example 16 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class EvaluationTests method decrement02prefix.

@Test
public void decrement02prefix() {
    Spr9751 helper = new Spr9751();
    StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = null;
    // BigDecimal
    e = parser.parseExpression("--bd");
    assertTrue(new BigDecimal("2").equals(helper.bd));
    BigDecimal return_bd = e.getValue(ctx, BigDecimal.class);
    assertTrue(new BigDecimal("1").equals(return_bd));
    assertTrue(new BigDecimal("1").equals(helper.bd));
    // double
    e = parser.parseExpression("--ddd");
    assertEquals(2.0d, helper.ddd, 0d);
    double return_ddd = e.getValue(ctx, Double.TYPE);
    assertEquals(1.0d, return_ddd, 0d);
    assertEquals(1.0d, helper.ddd, 0d);
    // float
    e = parser.parseExpression("--fff");
    assertEquals(3.0f, helper.fff, 0d);
    float return_fff = e.getValue(ctx, Float.TYPE);
    assertEquals(2.0f, return_fff, 0d);
    assertEquals(2.0f, helper.fff, 0d);
    // long
    e = parser.parseExpression("--lll");
    assertEquals(66666L, helper.lll);
    long return_lll = e.getValue(ctx, Long.TYPE);
    assertEquals(66665L, return_lll);
    assertEquals(66665L, helper.lll);
    // int
    e = parser.parseExpression("--iii");
    assertEquals(42, helper.iii);
    int return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(41, return_iii);
    assertEquals(41, helper.iii);
    return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(40, return_iii);
    assertEquals(40, helper.iii);
    // short
    e = parser.parseExpression("--sss");
    assertEquals(15, helper.sss);
    int return_sss = (Integer) e.getValue(ctx);
    assertEquals(14, return_sss);
    assertEquals(14, helper.sss);
}
Also used : BigInteger(java.math.BigInteger) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 17 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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]);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 18 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) CompoundExpression(org.springframework.expression.spel.ast.CompoundExpression) Test(org.junit.Test)

Example 19 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class PropertyAccessTests method testAddingSpecificPropertyAccessor.

@Test
public // Adding a new property accessor just for a particular type
void testAddingSpecificPropertyAccessor() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    // Even though this property accessor is added after the reflection one, it specifically
    // names the String class as the type it is interested in so is chosen in preference to
    // any 'default' ones
    ctx.addPropertyAccessor(new StringyPropertyAccessor());
    Expression expr = parser.parseRaw("new String('hello').flibbles");
    Integer i = expr.getValue(ctx, Integer.class);
    assertEquals((int) i, 7);
    // The reflection one will be used for other properties...
    expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
    Object o = expr.getValue(ctx);
    assertNotNull(o);
    expr = parser.parseRaw("new String('hello').flibbles");
    expr.setValue(ctx, 99);
    i = expr.getValue(ctx, Integer.class);
    assertEquals((int) i, 99);
    // Cannot set it to a string value
    try {
        expr.setValue(ctx, "not allowed");
        fail("Should not have been allowed");
    } catch (EvaluationException ex) {
    // success - message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
    // 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
    // System.out.println(e.getMessage());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 20 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class ScenariosForSpringSecurity method testScenario02_ComparingNames.

@Test
public void testScenario02_ComparingNames() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new SecurityPrincipalAccessor());
    // Multiple options for supporting this expression: "p.name == principal.name"
    // (1) If the right person is the root context object then "name==principal.name" is good enough
    Expression expr = parser.parseRaw("name == principal.name");
    ctx.setRootObject(new Person("Andy"));
    Boolean value = expr.getValue(ctx, Boolean.class);
    assertTrue(value);
    ctx.setRootObject(new Person("Christian"));
    value = expr.getValue(ctx, Boolean.class);
    assertFalse(value);
    // (2) Or register an accessor that can understand 'p' and return the right person
    expr = parser.parseRaw("p.name == principal.name");
    PersonAccessor pAccessor = new PersonAccessor();
    ctx.addPropertyAccessor(pAccessor);
    ctx.setRootObject(null);
    pAccessor.setPerson(new Person("Andy"));
    value = expr.getValue(ctx, Boolean.class);
    assertTrue(value);
    pAccessor.setPerson(new Person("Christian"));
    value = expr.getValue(ctx, Boolean.class);
    assertFalse(value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Aggregations

SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)210 Test (org.junit.Test)190 Expression (org.springframework.expression.Expression)172 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)151 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)101 ExpressionParser (org.springframework.expression.ExpressionParser)87 EvaluationContext (org.springframework.expression.EvaluationContext)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)16 List (java.util.List)14 EvaluationException (org.springframework.expression.EvaluationException)13 Map (java.util.Map)11 LinkedHashMap (java.util.LinkedHashMap)8 BigInteger (java.math.BigInteger)6 AccessException (org.springframework.expression.AccessException)6 CompositeStringExpression (org.springframework.expression.common.CompositeStringExpression)6 ParseException (org.springframework.expression.ParseException)5 BigDecimal (java.math.BigDecimal)4 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)4 TypedValue (org.springframework.expression.TypedValue)4