Search in sources :

Example 71 with Expression

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

the class IndexingTests method setPropertyContainingList.

@Test
public void setPropertyContainingList() {
    List<Integer> property = new ArrayList<>();
    property.add(3);
    this.parameterizedList = property;
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expression = parser.parseExpression("parameterizedList");
    assertEquals("java.util.ArrayList<java.lang.Integer>", expression.getValueTypeDescriptor(this).toString());
    assertEquals(property, expression.getValue(this));
    expression = parser.parseExpression("parameterizedList[0]");
    assertEquals(3, expression.getValue(this));
    expression.setValue(this, "4");
    assertEquals(4, expression.getValue(this));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 72 with Expression

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

the class MethodInvocationTests method invokeMethodWithoutConversion.

@Test
public void invokeMethodWithoutConversion() throws Exception {
    final BytesService service = new BytesService();
    byte[] bytes = new byte[100];
    StandardEvaluationContext context = new StandardEvaluationContext(bytes);
    context.setBeanResolver(new BeanResolver() {

        @Override
        public Object resolve(EvaluationContext context, String beanName) throws AccessException {
            if ("service".equals(beanName)) {
                return service;
            }
            return null;
        }
    });
    Expression expression = parser.parseExpression("@service.handleBytes(#root)");
    byte[] outBytes = expression.getValue(context, byte[].class);
    assertSame(bytes, outBytes);
}
Also used : BeanResolver(org.springframework.expression.BeanResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) AccessException(org.springframework.expression.AccessException) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 73 with Expression

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

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

the class SpelCompilationCoverageTests method methodReference_literalArguments_int.

@Test
public void methodReference_literalArguments_int() throws Exception {
    Expression expression = parser.parseExpression("'abcd'.substring(1,3)");
    String resultI = expression.getValue(new TestClass1(), String.class);
    assertCanCompile(expression);
    String resultC = expression.getValue(new TestClass1(), String.class);
    assertEquals("bc", resultI);
    assertEquals("bc", 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 75 with Expression

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

the class SpelCompilationCoverageTests method checkCalc.

private void checkCalc(PayloadX p, String expression, double expectedResult) {
    Expression expr = parse(expression);
    assertEquals(expectedResult, expr.getValue(p));
    assertCanCompile(expr);
    assertEquals(expectedResult, expr.getValue(p));
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) CompoundExpression(org.springframework.expression.spel.ast.CompoundExpression)

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