Search in sources :

Example 41 with StandardEvaluationContext

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

the class ExpressionWithConversionTests method testCoercionToCollectionOfPrimitive.

@Test
public void testCoercionToCollectionOfPrimitive() throws Exception {
    class TestTarget {

        @SuppressWarnings("unused")
        public int sum(Collection<Integer> numbers) {
            int total = 0;
            for (int i : numbers) {
                total += i;
            }
            return total;
        }
    }
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    TypeDescriptor collectionType = new TypeDescriptor(new MethodParameter(TestTarget.class.getDeclaredMethod("sum", Collection.class), 0));
    // The type conversion is possible
    assertTrue(evaluationContext.getTypeConverter().canConvert(TypeDescriptor.valueOf(String.class), collectionType));
    // ... and it can be done successfully
    assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType).toString());
    evaluationContext.setVariable("target", new TestTarget());
    // OK up to here, so the evaluation should be fine...
    // ... but this fails
    int result = (Integer) parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4");
    assertEquals("Wrong result: " + result, 10, result);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Collection(java.util.Collection) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 42 with StandardEvaluationContext

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

the class ExpressionWithConversionTests method testConvert.

@Test
public void testConvert() {
    Foo root = new Foo("bar");
    Collection<String> foos = Collections.singletonList("baz");
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    // property access
    Expression expression = parser.parseExpression("foos");
    expression.setValue(context, foos);
    Foo baz = root.getFoos().iterator().next();
    assertEquals("baz", baz.value);
    // method call
    expression = parser.parseExpression("setFoos(#foos)");
    context.setVariable("foos", foos);
    expression.getValue(context);
    baz = root.getFoos().iterator().next();
    assertEquals("baz", baz.value);
    // method call with result from method call
    expression = parser.parseExpression("setFoos(getFoosAsStrings())");
    expression.getValue(context);
    baz = root.getFoos().iterator().next();
    assertEquals("baz", baz.value);
    // method call with result from method call
    expression = parser.parseExpression("setFoos(getFoosAsObjects())");
    expression.getValue(context);
    baz = root.getFoos().iterator().next();
    assertEquals("baz", baz.value);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 43 with StandardEvaluationContext

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

the class ExpressionWithConversionTests method testSetParameterizedList.

@Test
public void testSetParameterizedList() throws Exception {
    StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    Expression e = parser.parseExpression("listOfInteger.size()");
    assertEquals(0, e.getValue(context, Integer.class).intValue());
    context.setTypeConverter(new TypeConvertorUsingConversionService());
    // Assign a List<String> to the List<Integer> field - the component elements should be converted
    parser.parseExpression("listOfInteger").setValue(context, listOfString);
    // size now 3
    assertEquals(3, e.getValue(context, Integer.class).intValue());
    // element type correctly Integer
    Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class);
    assertEquals(Integer.class, clazz);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 44 with StandardEvaluationContext

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

the class IndexingTests method indexIntoGenericPropertyContainingMapObject.

@Test
public void indexIntoGenericPropertyContainingMapObject() {
    Map<String, Map<String, String>> property = new HashMap<>();
    Map<String, String> map = new HashMap<>();
    map.put("foo", "bar");
    property.put("property", map);
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new MapAccessor());
    context.setRootObject(property);
    Expression expression = parser.parseExpression("property");
    assertEquals("java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(context).toString());
    assertEquals(map, expression.getValue(context));
    assertEquals(map, expression.getValue(context, Map.class));
    expression = parser.parseExpression("property['foo']");
    assertEquals("bar", expression.getValue(context));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) Expression(org.springframework.expression.Expression) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 45 with StandardEvaluationContext

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

the class MethodInvocationTests method invokeMethodWithoutConversion.

@Test
public void invokeMethodWithoutConversion() throws Exception {
    final BytesService service = new BytesService();
    byte[] bytes = new byte[100];
    StandardEvaluationContext context = new StandardEvaluationContext(bytes);
    context.setBeanResolver(new BeanResolver() {

        @Override
        public Object resolve(EvaluationContext context, String beanName) throws AccessException {
            if ("service".equals(beanName)) {
                return service;
            }
            return null;
        }
    });
    Expression expression = parser.parseExpression("@service.handleBytes(#root)");
    byte[] outBytes = expression.getValue(context, byte[].class);
    assertSame(bytes, outBytes);
}
Also used : BeanResolver(org.springframework.expression.BeanResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) AccessException(org.springframework.expression.AccessException) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) 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