Search in sources :

Example 21 with Expression

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

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

the class SpelCompilationCoverageTests method variableReference_root.

@Test
public void variableReference_root() throws Exception {
    String s = "hello";
    Expression expression = parser.parseExpression("#root");
    String resultI = expression.getValue(s, String.class);
    assertCanCompile(expression);
    String resultC = expression.getValue(s, String.class);
    assertEquals(s, resultI);
    assertEquals(s, resultC);
    expression = parser.parseExpression("#root");
    int i = (Integer) expression.getValue(42);
    assertEquals(42, i);
    assertCanCompile(expression);
    i = (Integer) expression.getValue(42);
    assertEquals(42, i);
}
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)

Example 23 with Expression

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

the class PerformanceTests method testPerformanceOfMethodAccess.

@Test
public void testPerformanceOfMethodAccess() throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    long starttime = 0;
    long endtime = 0;
    // warmup
    for (int i = 0; i < ITERATIONS; i++) {
        Expression expr = parser.parseExpression("getPlaceOfBirth().getCity()");
        if (expr == null) {
            fail("Parser returned null for expression");
        }
        expr.getValue(eContext);
    }
    starttime = System.currentTimeMillis();
    for (int i = 0; i < ITERATIONS; i++) {
        Expression expr = parser.parseExpression("getPlaceOfBirth().getCity()");
        if (expr == null) {
            fail("Parser returned null for expression");
        }
        expr.getValue(eContext);
    }
    endtime = System.currentTimeMillis();
    long freshParseTime = endtime - starttime;
    if (DEBUG) {
        System.out.println("MethodExpression: Time for parsing and evaluation x 10000: " + freshParseTime + "ms");
    }
    Expression expr = parser.parseExpression("getPlaceOfBirth().getCity()");
    if (expr == null) {
        fail("Parser returned null for expression");
    }
    starttime = System.currentTimeMillis();
    for (int i = 0; i < ITERATIONS; i++) {
        expr.getValue(eContext);
    }
    endtime = System.currentTimeMillis();
    long reuseTime = endtime - starttime;
    if (DEBUG) {
        System.out.println("MethodExpression: Time for just evaluation x 10000: " + reuseTime + "ms");
    }
    if (reuseTime > freshParseTime) {
        System.out.println("Fresh parse every time, ITERATIONS iterations = " + freshParseTime + "ms");
        System.out.println("Reuse SpelExpression, ITERATIONS iterations = " + reuseTime + "ms");
        fail("Should have been quicker to reuse!");
    }
}
Also used : Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 24 with Expression

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

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

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