Search in sources :

Example 31 with SpelExpressionParser

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

the class MethodInvocationTests method testMethodThrowingException_SPR6941.

/**
	 * Check on first usage (when the cachedExecutor in MethodReference is null) that the exception is not wrapped.
	 */
@Test
public void testMethodThrowingException_SPR6941() {
    // Test method on inventor: throwException()
    // On 1 it will throw an IllegalArgumentException
    // On 2 it will throw a RuntimeException
    // On 3 it will exit normally
    // In each case it increments the Inventor field 'counter' when invoked
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("throwException(#bar)");
    eContext.setVariable("bar", 2);
    try {
        expr.getValue(eContext);
        fail();
    } catch (Exception ex) {
        if (ex instanceof SpelEvaluationException) {
            fail("Should not be a SpelEvaluationException: " + ex);
        }
    // normal
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) AccessException(org.springframework.expression.AccessException) ExpressionInvocationTargetException(org.springframework.expression.ExpressionInvocationTargetException) Test(org.junit.Test)

Example 32 with SpelExpressionParser

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

the class MethodInvocationTests method testMethodFiltering_SPR6764.

@Test
public void testMethodFiltering_SPR6764() {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(new TestObject());
    LocalFilter filter = new LocalFilter();
    context.registerMethodFilter(TestObject.class, filter);
    // Filter will be called but not do anything, so first doit() will be invoked
    SpelExpression expr = (SpelExpression) parser.parseExpression("doit(1)");
    String result = expr.getValue(context, String.class);
    assertEquals("1", result);
    assertTrue(filter.filterCalled);
    // Filter will now remove non @Anno annotated methods
    filter.removeIfNotAnnotated = true;
    filter.filterCalled = false;
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertEquals("double 1.0", result);
    assertTrue(filter.filterCalled);
    // check not called for other types
    filter.filterCalled = false;
    context.setRootObject(new String("abc"));
    expr = (SpelExpression) parser.parseExpression("charAt(0)");
    result = expr.getValue(context, String.class);
    assertEquals("a", result);
    assertFalse(filter.filterCalled);
    // check de-registration works
    filter.filterCalled = false;
    //clear filter
    context.registerMethodFilter(TestObject.class, null);
    context.setRootObject(new TestObject());
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertEquals("1", result);
    assertFalse(filter.filterCalled);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 33 with SpelExpressionParser

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

the class SpelCompilationCoverageTests method indexerMapAccessor_12045.

@Test
public void indexerMapAccessor_12045() throws Exception {
    SpelParserConfiguration spc = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, getClass().getClassLoader());
    SpelExpressionParser sep = new SpelExpressionParser(spc);
    expression = sep.parseExpression("headers[command]");
    MyMessage root = new MyMessage();
    assertEquals("wibble", expression.getValue(root));
    // This next call was failing because the isCompilable check in Indexer did not check on the key being compilable
    // (and also generateCode in the Indexer was missing the optimization that it didn't need necessarily need to call
    // generateCode for that accessor)
    assertEquals("wibble", expression.getValue(root));
    assertCanCompile(expression);
    // What about a map key that is an expression - ensure the getKey() is evaluated in the right scope
    expression = sep.parseExpression("headers[getKey()]");
    assertEquals("wobble", expression.getValue(root));
    assertEquals("wobble", expression.getValue(root));
    expression = sep.parseExpression("list[getKey2()]");
    assertEquals("wobble", expression.getValue(root));
    assertEquals("wobble", expression.getValue(root));
    expression = sep.parseExpression("ia[getKey2()]");
    assertEquals(3, expression.getValue(root));
    assertEquals(3, expression.getValue(root));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 34 with SpelExpressionParser

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

the class MapAccessTests method testVariableMapAccess.

@Test
public void testVariableMapAccess() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
    ctx.setVariable("day", "saturday");
    Expression expr = parser.parseExpression("testMap[#day]");
    Object value = expr.getValue(ctx, String.class);
    assertEquals("samstag", value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 35 with SpelExpressionParser

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

the class MapAccessTests method testGetValueFromRootMap.

@Test
public void testGetValueFromRootMap() {
    Map<String, String> map = new HashMap<>();
    map.put("key", "value");
    ExpressionParser spelExpressionParser = new SpelExpressionParser();
    Expression expr = spelExpressionParser.parseExpression("#root['key']");
    assertEquals("value", expr.getValue(map));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) HashMap(java.util.HashMap) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Aggregations

SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)218 Test (org.junit.Test)190 Expression (org.springframework.expression.Expression)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)101 ExpressionParser (org.springframework.expression.ExpressionParser)89 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 TypedValue (org.springframework.expression.TypedValue)4 TreeMap (java.util.TreeMap)3