use of org.springframework.expression.spel.standard.SpelExpressionParser 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.spel.standard.SpelExpressionParser 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.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class MapTests method testMapKeysThatAreAlsoSpELKeywords.
@Test
public void testMapKeysThatAreAlsoSpELKeywords() {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = null;
Object o = null;
// expression = (SpelExpression) parser.parseExpression("foo['NEW']");
// o = expression.getValue(new MapHolder());
// assertEquals("VALUE",o);
expression = (SpelExpression) parser.parseExpression("foo[T]");
o = expression.getValue(new MapHolder());
assertEquals("TV", o);
expression = (SpelExpression) parser.parseExpression("foo[t]");
o = expression.getValue(new MapHolder());
assertEquals("tv", o);
expression = (SpelExpression) parser.parseExpression("foo[NEW]");
o = expression.getValue(new MapHolder());
assertEquals("VALUE", o);
expression = (SpelExpression) parser.parseExpression("foo[new]");
o = expression.getValue(new MapHolder());
assertEquals("value", o);
expression = (SpelExpression) parser.parseExpression("foo['abc.def']");
o = expression.getValue(new MapHolder());
assertEquals("value", o);
expression = (SpelExpression) parser.parseExpression("foo[foo[NEW]]");
o = expression.getValue(new MapHolder());
assertEquals("37", o);
expression = (SpelExpression) parser.parseExpression("foo[foo[new]]");
o = expression.getValue(new MapHolder());
assertEquals("38", o);
expression = (SpelExpression) parser.parseExpression("foo[foo[foo[T]]]");
o = expression.getValue(new MapHolder());
assertEquals("value", o);
}
use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class MethodInvocationTests method testMethodThrowingException_SPR6941_2.
@Test
public void testMethodThrowingException_SPR6941_2() {
// Test method on inventor: throwException()
// On 1 it will throw an IllegalArgumentException
// On 2 it will throw a RuntimeException
// On 3 it will exit normally
// In each case it increments the Inventor field 'counter' when invoked
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("throwException(#bar)");
eContext.setVariable("bar", 4);
try {
expr.getValue(eContext);
fail();
} catch (ExpressionInvocationTargetException ex) {
Throwable cause = ex.getCause();
assertEquals("org.springframework.expression.spel.testresources.Inventor$TestException", cause.getClass().getName());
}
}
use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method projectionWithIterable.
@Test
public void projectionWithIterable() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testList", IntegerTestBean.createIterable());
Object value = expression.getValue(context);
assertTrue(value instanceof List);
List<?> list = (List<?>) value;
assertEquals(3, list.size());
assertEquals(5, list.get(0));
assertEquals(6, list.get(1));
assertEquals(7, list.get(2));
}
Aggregations