Search in sources :

Example 61 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext 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 62 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext 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 63 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext 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 64 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext 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)

Example 65 with StandardEvaluationContext

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

the class SetValueTests method setValueExpectError.

/**
	 * Call setValue() but expect it to fail.
	 */
protected void setValueExpectError(String expression, Object value) {
    try {
        Expression e = parser.parseExpression(expression);
        if (e == null) {
            fail("Parser returned null for expression");
        }
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        e.setValue(lContext, value);
        fail("expected an error");
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail("Unexpected Exception: " + pe.getMessage());
    } catch (EvaluationException ee) {
    // success!
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ParseException(org.springframework.expression.ParseException) EvaluationException(org.springframework.expression.EvaluationException)

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