Search in sources :

Example 16 with Expression

use of org.springframework.expression.Expression in project spring-framework by spring-projects.

the class ExpressionLanguageScenarioTests method testScenario_DefiningVariablesThatWillBeAccessibleInExpressions.

/**
	 * Scenario: using the standard context but adding your own variables
	 */
@Test
public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception {
    // Create a parser
    SpelExpressionParser parser = new SpelExpressionParser();
    // Use the standard evaluation context
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("favouriteColour", "blue");
    List<Integer> primes = new ArrayList<>();
    primes.addAll(Arrays.asList(2, 3, 5, 7, 11, 13, 17));
    ctx.setVariable("primes", primes);
    Expression expr = parser.parseRaw("#favouriteColour");
    Object value = expr.getValue(ctx);
    assertEquals("blue", value);
    expr = parser.parseRaw("#primes.get(1)");
    value = expr.getValue(ctx);
    assertEquals(3, value);
    // all prime numbers > 10 from the list (using selection ?{...})
    expr = parser.parseRaw("#primes.?[#this>10]");
    value = expr.getValue(ctx);
    assertEquals("[11, 13, 17]", value.toString());
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 17 with Expression

use of org.springframework.expression.Expression in project spring-framework by spring-projects.

the class EvaluationTests method increment02postfix.

@Test
public void increment02postfix() {
    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("2").equals(return_bd));
    assertTrue(new BigDecimal("3").equals(helper.bd));
    // double
    e = parser.parseExpression("ddd++");
    assertEquals(2.0d, helper.ddd, 0d);
    double return_ddd = e.getValue(ctx, Double.TYPE);
    assertEquals(2.0d, return_ddd, 0d);
    assertEquals(3.0d, helper.ddd, 0d);
    // float
    e = parser.parseExpression("fff++");
    assertEquals(3.0f, helper.fff, 0d);
    float return_fff = e.getValue(ctx, Float.TYPE);
    assertEquals(3.0f, return_fff, 0d);
    assertEquals(4.0f, helper.fff, 0d);
    // long
    e = parser.parseExpression("lll++");
    assertEquals(66666L, helper.lll);
    long return_lll = e.getValue(ctx, Long.TYPE);
    assertEquals(66666L, return_lll);
    assertEquals(66667L, helper.lll);
    // int
    e = parser.parseExpression("iii++");
    assertEquals(42, helper.iii);
    int return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(42, return_iii);
    assertEquals(43, helper.iii);
    return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(43, return_iii);
    assertEquals(44, helper.iii);
    // short
    e = parser.parseExpression("sss++");
    assertEquals(15, helper.sss);
    short return_sss = e.getValue(ctx, Short.TYPE);
    assertEquals(15, return_sss);
    assertEquals(16, helper.sss);
}
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) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 18 with Expression

use of org.springframework.expression.Expression 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 19 with Expression

use of org.springframework.expression.Expression 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 20 with Expression

use of org.springframework.expression.Expression in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method methodReference_simpleInstanceMethodOneArgReturnPrimitive1.

@Test
public void methodReference_simpleInstanceMethodOneArgReturnPrimitive1() throws Exception {
    Expression expression = parser.parseExpression("indexOf('b')");
    int resultI = expression.getValue("abc", Integer.TYPE);
    assertCanCompile(expression);
    int resultC = expression.getValue("abc", Integer.TYPE);
    assertEquals(1, resultI);
    assertEquals(1, resultC);
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) CompoundExpression(org.springframework.expression.spel.ast.CompoundExpression) Test(org.junit.Test)

Aggregations

Expression (org.springframework.expression.Expression)288 Test (org.junit.Test)228 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)172 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)114 ExpressionParser (org.springframework.expression.ExpressionParser)76 EvaluationContext (org.springframework.expression.EvaluationContext)52 ArrayList (java.util.ArrayList)32 CompoundExpression (org.springframework.expression.spel.ast.CompoundExpression)18 HashMap (java.util.HashMap)17 EvaluationException (org.springframework.expression.EvaluationException)17 Authentication (org.springframework.security.core.Authentication)16 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 List (java.util.List)13 Map (java.util.Map)13 ParseException (org.springframework.expression.ParseException)11 LinkedHashMap (java.util.LinkedHashMap)10 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9