Search in sources :

Example 26 with EvaluationContext

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

the class ReflectionHelperTests method testReflectivePropertyResolver.

@Test
public void testReflectivePropertyResolver() throws Exception {
    ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
    Tester t = new Tester();
    t.setProperty("hello");
    EvaluationContext ctx = new StandardEvaluationContext(t);
    assertTrue(rpr.canRead(ctx, t, "property"));
    assertEquals("hello", rpr.read(ctx, t, "property").getValue());
    // cached accessor used
    assertEquals("hello", rpr.read(ctx, t, "property").getValue());
    assertTrue(rpr.canRead(ctx, t, "field"));
    assertEquals(3, rpr.read(ctx, t, "field").getValue());
    // cached accessor used
    assertEquals(3, rpr.read(ctx, t, "field").getValue());
    assertTrue(rpr.canWrite(ctx, t, "property"));
    rpr.write(ctx, t, "property", "goodbye");
    // cached accessor used
    rpr.write(ctx, t, "property", "goodbye");
    assertTrue(rpr.canWrite(ctx, t, "field"));
    rpr.write(ctx, t, "field", 12);
    rpr.write(ctx, t, "field", 12);
    // Attempted write as first activity on this field and property to drive testing
    // of populating type descriptor cache
    rpr.write(ctx, t, "field2", 3);
    rpr.write(ctx, t, "property2", "doodoo");
    assertEquals(3, rpr.read(ctx, t, "field2").getValue());
    // Attempted read as first activity on this field and property (no canRead before them)
    assertEquals(0, rpr.read(ctx, t, "field3").getValue());
    assertEquals("doodoo", rpr.read(ctx, t, "property3").getValue());
    // Access through is method
    //		assertEquals(0,rpr.read(ctx,t,"field3").getValue());
    assertEquals(false, rpr.read(ctx, t, "property4").getValue());
    assertTrue(rpr.canRead(ctx, t, "property4"));
    // repro SPR-9123, ReflectivePropertyAccessor JavaBean property names compliance tests
    assertEquals("iD", rpr.read(ctx, t, "iD").getValue());
    assertTrue(rpr.canRead(ctx, t, "iD"));
    assertEquals("id", rpr.read(ctx, t, "id").getValue());
    assertTrue(rpr.canRead(ctx, t, "id"));
    assertEquals("ID", rpr.read(ctx, t, "ID").getValue());
    assertTrue(rpr.canRead(ctx, t, "ID"));
    // note: "Id" is not a valid JavaBean name, nevertheless it is treated as "id"
    assertEquals("id", rpr.read(ctx, t, "Id").getValue());
    assertTrue(rpr.canRead(ctx, t, "Id"));
    // repro SPR-10994
    assertEquals("xyZ", rpr.read(ctx, t, "xyZ").getValue());
    assertTrue(rpr.canRead(ctx, t, "xyZ"));
    assertEquals("xY", rpr.read(ctx, t, "xY").getValue());
    assertTrue(rpr.canRead(ctx, t, "xY"));
    // SPR-10122, ReflectivePropertyAccessor JavaBean property names compliance tests - setters
    rpr.write(ctx, t, "pEBS", "Test String");
    assertEquals("Test String", rpr.read(ctx, t, "pEBS").getValue());
}
Also used : EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Example 27 with EvaluationContext

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

the class SpelReproTests method SPR9495.

@Test
public void SPR9495() throws Exception {
    SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
    ExpressionParser parser = new SpelExpressionParser(configuration);
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression spel = parser.parseExpression("#enumType.values()");
    context.setVariable("enumType", ABC.class);
    Object result = spel.getValue(context);
    assertNotNull(result);
    assertTrue(result.getClass().isArray());
    assertEquals(ABC.A, Array.get(result, 0));
    assertEquals(ABC.B, Array.get(result, 1));
    assertEquals(ABC.C, Array.get(result, 2));
    context.addMethodResolver(new MethodResolver() {

        @Override
        public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
            return new MethodExecutor() {

                @Override
                public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
                    try {
                        Method method = XYZ.class.getMethod("values");
                        Object value = method.invoke(target, arguments);
                        return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
                    } catch (Exception ex) {
                        throw new AccessException(ex.getMessage(), ex);
                    }
                }
            };
        }
    });
    result = spel.getValue(context);
    assertNotNull(result);
    assertTrue(result.getClass().isArray());
    assertEquals(XYZ.X, Array.get(result, 0));
    assertEquals(XYZ.Y, Array.get(result, 1));
    assertEquals(XYZ.Z, Array.get(result, 2));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodResolver(org.springframework.expression.MethodResolver) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) Method(java.lang.reflect.Method) ExpressionException(org.springframework.expression.ExpressionException) EvaluationException(org.springframework.expression.EvaluationException) ExpectedException(org.junit.rules.ExpectedException) AccessException(org.springframework.expression.AccessException) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) MethodExecutor(org.springframework.expression.MethodExecutor) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodParameter(org.springframework.core.MethodParameter) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 28 with EvaluationContext

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

the class SpelReproTests method SPR13055.

@Test
@SuppressWarnings("rawtypes")
public void SPR13055() throws Exception {
    List<Map<String, Object>> myPayload = new ArrayList<>();
    Map<String, Object> v1 = new HashMap<>();
    Map<String, Object> v2 = new HashMap<>();
    v1.put("test11", "test11");
    v1.put("test12", "test12");
    v2.put("test21", "test21");
    v2.put("test22", "test22");
    myPayload.add(v1);
    myPayload.add(v2);
    EvaluationContext context = new StandardEvaluationContext(myPayload);
    ExpressionParser parser = new SpelExpressionParser();
    String ex = "#root.![T(org.springframework.util.StringUtils).collectionToCommaDelimitedString(#this.values())]";
    List res = parser.parseExpression(ex).getValue(context, List.class);
    assertEquals("[test12,test11, test22,test21]", res.toString());
    res = parser.parseExpression("#root.![#this.values()]").getValue(context, List.class);
    assertEquals("[[test12, test11], [test22, test21]]", res.toString());
    res = parser.parseExpression("#root.![values()]").getValue(context, List.class);
    assertEquals("[[test12, test11], [test22, test21]]", res.toString());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 29 with EvaluationContext

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

the class SpelDocumentationTests method testPropertyAccess.

@Test
public void testPropertyAccess() throws Exception {
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    // 1856
    int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
    assertEquals(1856, year);
    String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
    assertEquals("SmilJan", city);
}
Also used : EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 30 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project camel by apache.

the class SpelExpression method evaluate.

public <T> T evaluate(Exchange exchange, Class<T> tClass) {
    try {
        Expression expression = parseExpression();
        EvaluationContext evaluationContext = createEvaluationContext(exchange);
        Object value = expression.getValue(evaluationContext);
        // Let Camel handle the type conversion
        return exchange.getContext().getTypeConverter().convertTo(tClass, value);
    } catch (Exception e) {
        throw new ExpressionEvaluationException(this, exchange, e);
    }
}
Also used : ExpressionEvaluationException(org.apache.camel.ExpressionEvaluationException) Expression(org.springframework.expression.Expression) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) ExpressionEvaluationException(org.apache.camel.ExpressionEvaluationException)

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