use of org.springframework.expression.ExpressionParser in project spring-framework by spring-projects.
the class MapAccessTests method testGetValue.
@Test
public void testGetValue() {
Map<String, String> props1 = new HashMap<>();
props1.put("key1", "value1");
props1.put("key2", "value2");
props1.put("key3", "value3");
Object bean = new TestBean("name1", new TestBean("name2", null, "Description 2", 15, props1), "description 1", 6, props1);
ExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("testBean.properties['key2']");
assertEquals("value2", expr.getValue(bean));
}
use of org.springframework.expression.ExpressionParser in project spring-framework by spring-projects.
the class MapAccessTests method testCustomMapAccessor.
@Test
public void testCustomMapAccessor() throws Exception {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
ctx.addPropertyAccessor(new MapAccessor());
Expression expr = parser.parseExpression("testMap.monday");
Object value = expr.getValue(ctx, String.class);
assertEquals("montag", value);
}
use of org.springframework.expression.ExpressionParser 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());
}
use of org.springframework.expression.ExpressionParser in project spring-framework by spring-projects.
the class SpelDocumentationTests method testEqualityCheck.
@Test
public void testEqualityCheck() throws Exception {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setRootObject(tesla);
Expression exp = parser.parseExpression("name == 'Nikola Tesla'");
// evaluates to true
boolean isEqual = exp.getValue(context, Boolean.class);
assertTrue(isEqual);
}
use of org.springframework.expression.ExpressionParser in project spring-framework by spring-projects.
the class SpelReproTests method SPR13055_maps.
@Test
public void SPR13055_maps() {
EvaluationContext context = new StandardEvaluationContext();
ExpressionParser parser = new SpelExpressionParser();
Expression ex = parser.parseExpression("{'a':'y','b':'n'}.![value=='y'?key:null]");
assertEquals("[a, null]", ex.getValue(context).toString());
ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.key) + 5]");
assertEquals("[7, 8]", ex.getValue(context).toString());
ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.value) + 5]");
assertEquals("[9, 11]", ex.getValue(context).toString());
}
Aggregations