use of org.springframework.expression.ExpressionParser in project spring-framework by spring-projects.
the class MapAccessTests method testGetValueFromRootMap.
@Test
public void testGetValueFromRootMap() {
Map<String, String> map = new HashMap<>();
map.put("key", "value");
ExpressionParser spelExpressionParser = new SpelExpressionParser();
Expression expr = spelExpressionParser.parseExpression("#root['key']");
assertEquals("value", expr.getValue(map));
}
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);
}
Aggregations