Search in sources :

Example 31 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class PropertyAccessTests method testAddingRemovingAccessors.

@Test
public void testAddingRemovingAccessors() {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    // reflective property accessor is the only one by default
    List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
    assertEquals(1, propertyAccessors.size());
    StringyPropertyAccessor spa = new StringyPropertyAccessor();
    ctx.addPropertyAccessor(spa);
    assertEquals(2, ctx.getPropertyAccessors().size());
    List<PropertyAccessor> copy = new ArrayList<>();
    copy.addAll(ctx.getPropertyAccessors());
    assertTrue(ctx.removePropertyAccessor(spa));
    assertFalse(ctx.removePropertyAccessor(spa));
    assertEquals(1, ctx.getPropertyAccessors().size());
    ctx.setPropertyAccessors(copy);
    assertEquals(2, ctx.getPropertyAccessors().size());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) PropertyAccessor(org.springframework.expression.PropertyAccessor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 32 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class PropertyAccessTests method testAccessingOnNullObject.

/**
	 * The standard reflection resolver cannot find properties on null objects but some
	 * supplied resolver might be able to - so null shouldn't crash the reflection resolver.
	 */
@Test
public void testAccessingOnNullObject() throws Exception {
    SpelExpression expr = (SpelExpression) parser.parseExpression("madeup");
    EvaluationContext context = new StandardEvaluationContext(null);
    try {
        expr.getValue(context);
        fail("Should have failed - default property resolver cannot resolve on null");
    } catch (Exception ex) {
        checkException(ex, SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL);
    }
    assertFalse(expr.isWritable(context));
    try {
        expr.setValue(context, "abc");
        fail("Should have failed - default property resolver cannot resolve on null");
    } catch (Exception ex) {
        checkException(ex, SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationException(org.springframework.expression.EvaluationException) AccessException(org.springframework.expression.AccessException) Test(org.junit.Test)

Example 33 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class PropertyAccessTests method testAddingSpecificPropertyAccessor.

@Test
public // Adding a new property accessor just for a particular type
void testAddingSpecificPropertyAccessor() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    // Even though this property accessor is added after the reflection one, it specifically
    // names the String class as the type it is interested in so is chosen in preference to
    // any 'default' ones
    ctx.addPropertyAccessor(new StringyPropertyAccessor());
    Expression expr = parser.parseRaw("new String('hello').flibbles");
    Integer i = expr.getValue(ctx, Integer.class);
    assertEquals((int) i, 7);
    // The reflection one will be used for other properties...
    expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
    Object o = expr.getValue(ctx);
    assertNotNull(o);
    expr = parser.parseRaw("new String('hello').flibbles");
    expr.setValue(ctx, 99);
    i = expr.getValue(ctx, Integer.class);
    assertEquals((int) i, 99);
    // Cannot set it to a string value
    try {
        expr.setValue(ctx, "not allowed");
        fail("Should not have been allowed");
    } catch (EvaluationException ex) {
    // success - message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
    // 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
    // System.out.println(e.getMessage());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 34 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class ScenariosForSpringSecurity method testScenario02_ComparingNames.

@Test
public void testScenario02_ComparingNames() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new SecurityPrincipalAccessor());
    // Multiple options for supporting this expression: "p.name == principal.name"
    // (1) If the right person is the root context object then "name==principal.name" is good enough
    Expression expr = parser.parseRaw("name == principal.name");
    ctx.setRootObject(new Person("Andy"));
    Boolean value = expr.getValue(ctx, Boolean.class);
    assertTrue(value);
    ctx.setRootObject(new Person("Christian"));
    value = expr.getValue(ctx, Boolean.class);
    assertFalse(value);
    // (2) Or register an accessor that can understand 'p' and return the right person
    expr = parser.parseRaw("p.name == principal.name");
    PersonAccessor pAccessor = new PersonAccessor();
    ctx.addPropertyAccessor(pAccessor);
    ctx.setRootObject(null);
    pAccessor.setPerson(new Person("Andy"));
    value = expr.getValue(ctx, Boolean.class);
    assertTrue(value);
    pAccessor.setPerson(new Person("Christian"));
    value = expr.getValue(ctx, Boolean.class);
    assertFalse(value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 35 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class ScenariosForSpringSecurity method testScenario04_ControllingWhichMethodsRun.

// Here i'm going to change which hasRole() executes and make it one of my own Java methods
@Test
public void testScenario04_ControllingWhichMethodsRun() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);
    ctx.setRootObject(new Supervisor("Ben"));
    // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
    ctx.addMethodResolver(new MyMethodResolver());
    // Might be better with a as a variable although it would work as a property too...
    // Variable references using a '#'
    //		SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
    Expression expr = parser.parseRaw("(hasRole(3) or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
    Boolean value = null;
    // referenced as #a in the expression
    ctx.setVariable("a", 1.0d);
    value = expr.getValue(ctx, Boolean.class);
    assertTrue(value);
//			ctx.setRootObject(new Manager("Luke"));
//			ctx.setVariable("a",1.043d);
//			value = (Boolean)expr.getValue(ctx,Boolean.class);
//			assertFalse(value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Aggregations

StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)251 Test (org.junit.Test)211 Expression (org.springframework.expression.Expression)157 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)104 ExpressionParser (org.springframework.expression.ExpressionParser)81 EvaluationContext (org.springframework.expression.EvaluationContext)43 ArrayList (java.util.ArrayList)27 List (java.util.List)14 HashMap (java.util.HashMap)12 EvaluationException (org.springframework.expression.EvaluationException)12 TypedValue (org.springframework.expression.TypedValue)12 Map (java.util.Map)11 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 BigInteger (java.math.BigInteger)7 LinkedHashMap (java.util.LinkedHashMap)7 ExpressionState (org.springframework.expression.spel.ExpressionState)7 AccessException (org.springframework.expression.AccessException)6 Inventor (org.springframework.expression.spel.testresources.Inventor)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5