Search in sources :

Example 86 with Expression

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

the class SelectionAndProjectionTests method selectLastItemInSet.

@Test
public void selectLastItemInSet() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof Integer);
    assertEquals(4, value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 87 with Expression

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

the class SelectionAndProjectionTests method selectFirstItemInArray.

@Test
public void selectFirstItemInArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof Integer);
    assertEquals(0, value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 88 with Expression

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

the class SetValueTests method testSetGenericMapElementRequiresCoercion.

/*
	 * Testing the coercion of both the keys and the values to the correct type
	 */
@Test
public void testSetGenericMapElementRequiresCoercion() throws Exception {
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    Expression e = parse("mapOfStringToBoolean[42]");
    assertNull(e.getValue(eContext));
    // Key should be coerced to string representation of 42
    e.setValue(eContext, "true");
    // All keys should be strings
    Set<?> ks = parse("mapOfStringToBoolean.keySet()").getValue(eContext, Set.class);
    for (Object o : ks) {
        assertEquals(String.class, o.getClass());
    }
    // All values should be booleans
    Collection<?> vs = parse("mapOfStringToBoolean.values()").getValue(eContext, Collection.class);
    for (Object o : vs) {
        assertEquals(Boolean.class, o.getClass());
    }
    // One final test check coercion on the key for a map lookup
    Object o = e.getValue(eContext);
    assertEquals(Boolean.TRUE, o);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 89 with Expression

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

the class SetValueTests method testIsWritableForInvalidExpressions_SPR10610.

@Test
public void testIsWritableForInvalidExpressions_SPR10610() {
    Expression e = null;
    StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
    // PROPERTYORFIELDREFERENCE
    // Non existent field (or property):
    e = parser.parseExpression("arrayContainer.wibble");
    assertFalse("Should not be writable!", e.isWritable(lContext));
    e = parser.parseExpression("arrayContainer.wibble.foo");
    try {
        assertFalse("Should not be writable!", e.isWritable(lContext));
        fail("Should have had an error because wibble does not really exist");
    } catch (SpelEvaluationException see) {
    //			org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 15): Property or field 'wibble' cannot be found on object of type 'org.springframework.expression.spel.testresources.ArrayContainer' - maybe not public?
    //					at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:225)
    // success!
    }
    // VARIABLE
    // the variable does not exist (but that is OK, we should be writable)
    e = parser.parseExpression("#madeup1");
    assertTrue("Should be writable!", e.isWritable(lContext));
    // compound expression
    e = parser.parseExpression("#madeup2.bar");
    assertFalse("Should not be writable!", e.isWritable(lContext));
    // INDEXER
    // non existent indexer (wibble made up)
    e = parser.parseExpression("arrayContainer.wibble[99]");
    try {
        assertFalse("Should not be writable!", e.isWritable(lContext));
        fail("Should have had an error because wibble does not really exist");
    } catch (SpelEvaluationException see) {
    // success!
    }
    // non existent indexer (index via a string)
    e = parser.parseExpression("arrayContainer.ints['abc']");
    try {
        assertFalse("Should not be writable!", e.isWritable(lContext));
        fail("Should have had an error because wibble does not really exist");
    } catch (SpelEvaluationException see) {
    // success!
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 90 with Expression

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

the class SetValueTests method testAssign.

@Test
public void testAssign() throws Exception {
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    Expression e = parse("publicName='Andy'");
    assertFalse(e.isWritable(eContext));
    assertEquals("Andy", e.getValue(eContext));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Aggregations

Expression (org.springframework.expression.Expression)299 Test (org.junit.Test)228 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)159 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)114 ExpressionParser (org.springframework.expression.ExpressionParser)78 EvaluationContext (org.springframework.expression.EvaluationContext)53 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)19 CompoundExpression (org.springframework.expression.spel.ast.CompoundExpression)18 EvaluationException (org.springframework.expression.EvaluationException)17 Authentication (org.springframework.security.core.Authentication)16 Map (java.util.Map)14 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 List (java.util.List)13 LinkedHashMap (java.util.LinkedHashMap)11 ParseException (org.springframework.expression.ParseException)11 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9