Search in sources :

Example 11 with EvaluationContext

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

the class SelectionAndProjectionTests method projectionWithSet.

@Test
public void projectionWithSet() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testList", IntegerTestBean.createSet());
    Object value = expression.getValue(context);
    assertTrue(value instanceof List);
    List<?> list = (List<?>) value;
    assertEquals(3, list.size());
    assertEquals(5, list.get(0));
    assertEquals(6, list.get(1));
    assertEquals(7, list.get(2));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 12 with EvaluationContext

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

the class SelectionAndProjectionTests method selectionWithList.

@Test
public void selectionWithList() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof List);
    List<?> list = (List<?>) value;
    assertEquals(5, list.size());
    assertEquals(0, list.get(0));
    assertEquals(1, list.get(1));
    assertEquals(2, list.get(2));
    assertEquals(3, list.get(3));
    assertEquals(4, list.get(4));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 13 with EvaluationContext

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

the class ExpressionStateTests method testRootObjectConstructor.

@Test
public void testRootObjectConstructor() {
    EvaluationContext ctx = getContext();
    // TypedValue root = ctx.getRootObject();
    // supplied should override root on context
    ExpressionState state = new ExpressionState(ctx, new TypedValue("i am a string"));
    TypedValue stateRoot = state.getRootContextObject();
    assertEquals(String.class, stateRoot.getTypeDescriptor().getType());
    assertEquals("i am a string", stateRoot.getValue());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 14 with EvaluationContext

use of org.springframework.expression.EvaluationContext 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 15 with EvaluationContext

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

the class SpelCompilationCoverageTests method functionReferenceVarargs.

@Test
public void functionReferenceVarargs() throws Exception {
    EvaluationContext ctx = new StandardEvaluationContext();
    Method m = getClass().getDeclaredMethod("join", String[].class);
    ctx.setVariable("join", m);
    expression = parser.parseExpression("#join('a','b','c')");
    assertEquals("abc", expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals("abc", expression.getValue(ctx));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

EvaluationContext (org.springframework.expression.EvaluationContext)85 Test (org.junit.Test)72 Expression (org.springframework.expression.Expression)52 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)52 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)32 ArrayList (java.util.ArrayList)12 Authentication (org.springframework.security.core.Authentication)11 ExpressionParser (org.springframework.expression.ExpressionParser)10 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)10 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9 List (java.util.List)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)8 TypedValue (org.springframework.expression.TypedValue)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 Map (java.util.Map)5 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)5 AccessException (org.springframework.expression.AccessException)5 Method (java.lang.reflect.Method)4