Search in sources :

Example 21 with EvaluationContext

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

the class SpelReproTests method SPR13055_maps.

@Test
public void SPR13055_maps() {
    EvaluationContext context = new StandardEvaluationContext();
    ExpressionParser parser = new SpelExpressionParser();
    Expression ex = parser.parseExpression("{'a':'y','b':'n'}.![value=='y'?key:null]");
    assertEquals("[a, null]", ex.getValue(context).toString());
    ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.key) + 5]");
    assertEquals("[7, 8]", ex.getValue(context).toString());
    ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.value) + 5]");
    assertEquals("[9, 11]", ex.getValue(context).toString());
}
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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 22 with EvaluationContext

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

the class SpelReproTests method accessingNullPropertyViaReflection_SPR5663.

@Test
public void accessingNullPropertyViaReflection_SPR5663() throws AccessException {
    PropertyAccessor propertyAccessor = new ReflectivePropertyAccessor();
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    assertFalse(propertyAccessor.canRead(context, null, "abc"));
    assertFalse(propertyAccessor.canWrite(context, null, "abc"));
    try {
        propertyAccessor.read(context, null, "abc");
        fail("Should have failed with an AccessException");
    } catch (AccessException ae) {
    // success
    }
    try {
        propertyAccessor.write(context, null, "abc", "foo");
        fail("Should have failed with an AccessException");
    } catch (AccessException ae) {
    // success
    }
}
Also used : ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) AccessException(org.springframework.expression.AccessException) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) Test(org.junit.Test)

Example 23 with EvaluationContext

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

the class SpelReproTests method SPR10417.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void SPR10417() {
    List list1 = new ArrayList();
    list1.add("a");
    list1.add("b");
    list1.add("x");
    List list2 = new ArrayList();
    list2.add("c");
    list2.add("x");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("list1", list1);
    context.setVariable("list2", list2);
    // #this should be the element from list1
    Expression ex = parser.parseExpression("#list1.?[#list2.contains(#this)]");
    Object result = ex.getValue(context);
    assertEquals("[x]", result.toString());
    // toString() should be called on the element from list1
    ex = parser.parseExpression("#list1.?[#list2.contains(toString())]");
    result = ex.getValue(context);
    assertEquals("[x]", result.toString());
    List list3 = new ArrayList();
    list3.add(1);
    list3.add(2);
    list3.add(3);
    list3.add(4);
    context = new StandardEvaluationContext();
    context.setVariable("list3", list3);
    ex = parser.parseExpression("#list3.?[#this > 2]");
    result = ex.getValue(context);
    assertEquals("[3, 4]", result.toString());
    ex = parser.parseExpression("#list3.?[#this >= T(java.lang.Math).abs(T(java.lang.Math).abs(#this))]");
    result = ex.getValue(context);
    assertEquals("[1, 2, 3, 4]", result.toString());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) 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 24 with EvaluationContext

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

the class SpelParserTests method valueType.

@Test
public void valueType() {
    SpelExpressionParser parser = new SpelExpressionParser();
    EvaluationContext ctx = new StandardEvaluationContext();
    Class<?> c = parser.parseRaw("2").getValueType();
    assertEquals(Integer.class, c);
    c = parser.parseRaw("12").getValueType(ctx);
    assertEquals(Integer.class, c);
    c = parser.parseRaw("null").getValueType();
    assertNull(c);
    c = parser.parseRaw("null").getValueType(ctx);
    assertNull(c);
    Object o = parser.parseRaw("null").getValue(ctx, Integer.class);
    assertNull(o);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Example 25 with EvaluationContext

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

the class ReflectionHelperTests method testOptimalReflectivePropertyResolver.

@Test
public void testOptimalReflectivePropertyResolver() 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());
    //		assertEquals("hello",rpr.read(ctx, t, "property").getValue()); // cached accessor used
    PropertyAccessor optA = rpr.createOptimalAccessor(ctx, t, "property");
    assertTrue(optA.canRead(ctx, t, "property"));
    assertFalse(optA.canRead(ctx, t, "property2"));
    try {
        optA.canWrite(ctx, t, "property");
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
    try {
        optA.canWrite(ctx, t, "property2");
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
    assertEquals("hello", optA.read(ctx, t, "property").getValue());
    // cached accessor used
    assertEquals("hello", optA.read(ctx, t, "property").getValue());
    try {
        optA.getSpecificTargetClasses();
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
    try {
        optA.write(ctx, t, "property", null);
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
    optA = rpr.createOptimalAccessor(ctx, t, "field");
    assertTrue(optA.canRead(ctx, t, "field"));
    assertFalse(optA.canRead(ctx, t, "field2"));
    try {
        optA.canWrite(ctx, t, "field");
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
    try {
        optA.canWrite(ctx, t, "field2");
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
    assertEquals(3, optA.read(ctx, t, "field").getValue());
    // cached accessor used
    assertEquals(3, optA.read(ctx, t, "field").getValue());
    try {
        optA.getSpecificTargetClasses();
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
    try {
        optA.write(ctx, t, "field", null);
        fail();
    } catch (UnsupportedOperationException uoe) {
    // success
    }
}
Also used : PropertyAccessor(org.springframework.expression.PropertyAccessor) EvaluationContext(org.springframework.expression.EvaluationContext) 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