Search in sources :

Example 56 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)

Example 57 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 58 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 59 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 60 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)

Aggregations

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