Search in sources :

Example 21 with StandardEvaluationContext

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

the class SpelCompilationCoverageTests method variantGetter.

@Test
public void variantGetter() throws Exception {
    Payload2Holder holder = new Payload2Holder();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new MyAccessor());
    expression = parser.parseExpression("payload2.var1");
    Object v = expression.getValue(ctx, holder);
    assertEquals("abc", v);
    //	// time it interpreted
    //	long stime = System.currentTimeMillis();
    //	for (int i = 0; i < 100000; i++) {
    //		v = expression.getValue(ctx,holder);
    //	}
    //	System.out.println((System.currentTimeMillis() - stime));
    assertCanCompile(expression);
    v = expression.getValue(ctx, holder);
    assertEquals("abc", v);
//	// time it compiled
//	stime = System.currentTimeMillis();
//	for (int i = 0; i < 100000; i++) {
//		v = expression.getValue(ctx,holder);
//	}
//	System.out.println((System.currentTimeMillis() - stime));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 22 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 23 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 24 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 25 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)

Aggregations

StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)241 Test (org.junit.Test)207 Expression (org.springframework.expression.Expression)153 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)151 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)103 ExpressionParser (org.springframework.expression.ExpressionParser)79 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 AccessException (org.springframework.expression.AccessException)6 ExpressionState (org.springframework.expression.spel.ExpressionState)6 Inventor (org.springframework.expression.spel.testresources.Inventor)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5