Search in sources :

Example 41 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class SelectionAndProjectionTests method selectLastItemInMap.

@Test
@SuppressWarnings("unchecked")
public void selectLastItemInMap() {
    EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("colors.$[key.startsWith('b')]");
    Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
    assertEquals(1, colorsMap.size());
    assertEquals("brown", colorsMap.keySet().iterator().next());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 42 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class SelectionAndProjectionTests method projectionWithArray.

@Test
public void projectionWithArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testArray", IntegerTestBean.createArray());
    Object value = expression.getValue(context);
    assertTrue(value.getClass().isArray());
    TypedValue typedValue = new TypedValue(value);
    assertEquals(Number.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType());
    Number[] array = (Number[]) value;
    assertEquals(3, array.length);
    assertEquals(new Integer(5), array[0]);
    assertEquals(5.9f, array[1]);
    assertEquals(new Integer(7), array[2]);
}
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) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 43 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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 44 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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 45 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class IndexingTests method setGenericPropertyContainingListAutogrow.

@Test
public void setGenericPropertyContainingListAutogrow() {
    List<Integer> property = new ArrayList<>();
    this.property = property;
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression expression = parser.parseExpression("property");
    assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString());
    assertEquals(property, expression.getValue(this));
    expression = parser.parseExpression("property[0]");
    try {
        expression.setValue(this, "4");
    } catch (EvaluationException ex) {
        assertTrue(ex.getMessage().startsWith("EL1053E"));
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Aggregations

SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)218 Test (org.junit.Test)190 Expression (org.springframework.expression.Expression)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)101 ExpressionParser (org.springframework.expression.ExpressionParser)89 EvaluationContext (org.springframework.expression.EvaluationContext)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)16 List (java.util.List)14 EvaluationException (org.springframework.expression.EvaluationException)13 Map (java.util.Map)11 LinkedHashMap (java.util.LinkedHashMap)8 BigInteger (java.math.BigInteger)6 AccessException (org.springframework.expression.AccessException)6 CompositeStringExpression (org.springframework.expression.common.CompositeStringExpression)6 ParseException (org.springframework.expression.ParseException)5 BigDecimal (java.math.BigDecimal)4 TypedValue (org.springframework.expression.TypedValue)4 TreeMap (java.util.TreeMap)3