Search in sources :

Example 66 with Expression

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

the class SpelReproTests method SPR11142.

@Test
public void SPR11142() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Spr11142 rootObject = new Spr11142();
    Expression expression = parser.parseExpression("something");
    thrown.expect(SpelEvaluationException.class);
    thrown.expectMessage("'something' cannot be found");
    expression.getValue(context, rootObject);
}
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) Test(org.junit.Test)

Example 67 with Expression

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

the class SpelReproTests method AccessingFactoryBean_spr9511.

@Test
public void AccessingFactoryBean_spr9511() {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new MyBeanResolver());
    Expression expr = new SpelExpressionParser().parseRaw("@foo");
    assertEquals("custard", expr.getValue(context));
    expr = new SpelExpressionParser().parseRaw("&foo");
    assertEquals("foo factory", expr.getValue(context));
    try {
        expr = new SpelExpressionParser().parseRaw("&@foo");
        fail("Illegal syntax, error expected");
    } catch (SpelParseException spe) {
        assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
        assertEquals(0, spe.getPosition());
    }
    try {
        expr = new SpelExpressionParser().parseRaw("@&foo");
        fail("Illegal syntax, error expected");
    } catch (SpelParseException spe) {
        assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
        assertEquals(0, spe.getPosition());
    }
}
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) Test(org.junit.Test)

Example 68 with Expression

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

the class SpelReproTests method SPR13055_maps.

@Test
public void SPR13055_maps() {
    EvaluationContext context = new StandardEvaluationContext();
    ExpressionParser parser = new SpelExpressionParser();
    Expression ex = parser.parseExpression("{'a':'y','b':'n'}.![value=='y'?key:null]");
    assertEquals("[a, null]", ex.getValue(context).toString());
    ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.key) + 5]");
    assertEquals("[7, 8]", ex.getValue(context).toString());
    ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.value) + 5]");
    assertEquals("[9, 11]", ex.getValue(context).toString());
}
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) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 69 with Expression

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

the class SpelReproTests method SPR9486_multiplyFloatWithFloat.

@Test
public void SPR9486_multiplyFloatWithFloat() {
    Number expectedNumber = 10.21f * 10.2f;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("10.21f * 10.2f");
    Number result = expression.getValue(context, null, Number.class);
    assertEquals(expectedNumber, result);
}
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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 70 with Expression

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

the class SpelReproTests method mapOfMap_SPR7244.

@Test
public void mapOfMap_SPR7244() throws Exception {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("uri", "http:");
    Map<String, String> nameMap = new LinkedHashMap<>();
    nameMap.put("givenName", "Arthur");
    map.put("value", nameMap);
    StandardEvaluationContext ctx = new StandardEvaluationContext(map);
    ExpressionParser parser = new SpelExpressionParser();
    String el1 = "#root['value'].get('givenName')";
    Expression exp = parser.parseExpression(el1);
    Object evaluated = exp.getValue(ctx);
    assertEquals("Arthur", evaluated);
    String el2 = "#root['value']['givenName']";
    exp = parser.parseExpression(el2);
    evaluated = exp.getValue(ctx);
    assertEquals("Arthur", evaluated);
}
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) LinkedHashMap(java.util.LinkedHashMap) 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