use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class MethodInvocationTests method testMethodThrowingException_SPR6941.
/**
* Check on first usage (when the cachedExecutor in MethodReference is null) that the exception is not wrapped.
*/
@Test
public void testMethodThrowingException_SPR6941() {
// 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", 2);
try {
expr.getValue(eContext);
fail();
} catch (Exception ex) {
if (ex instanceof SpelEvaluationException) {
fail("Should not be a SpelEvaluationException: " + ex);
}
// normal
}
}
use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class MethodInvocationTests method testMethodFiltering_SPR6764.
@Test
public void testMethodFiltering_SPR6764() {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setRootObject(new TestObject());
LocalFilter filter = new LocalFilter();
context.registerMethodFilter(TestObject.class, filter);
// Filter will be called but not do anything, so first doit() will be invoked
SpelExpression expr = (SpelExpression) parser.parseExpression("doit(1)");
String result = expr.getValue(context, String.class);
assertEquals("1", result);
assertTrue(filter.filterCalled);
// Filter will now remove non @Anno annotated methods
filter.removeIfNotAnnotated = true;
filter.filterCalled = false;
expr = (SpelExpression) parser.parseExpression("doit(1)");
result = expr.getValue(context, String.class);
assertEquals("double 1.0", result);
assertTrue(filter.filterCalled);
// check not called for other types
filter.filterCalled = false;
context.setRootObject(new String("abc"));
expr = (SpelExpression) parser.parseExpression("charAt(0)");
result = expr.getValue(context, String.class);
assertEquals("a", result);
assertFalse(filter.filterCalled);
// check de-registration works
filter.filterCalled = false;
//clear filter
context.registerMethodFilter(TestObject.class, null);
context.setRootObject(new TestObject());
expr = (SpelExpression) parser.parseExpression("doit(1)");
result = expr.getValue(context, String.class);
assertEquals("1", result);
assertFalse(filter.filterCalled);
}
use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method indexerMapAccessor_12045.
@Test
public void indexerMapAccessor_12045() throws Exception {
SpelParserConfiguration spc = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, getClass().getClassLoader());
SpelExpressionParser sep = new SpelExpressionParser(spc);
expression = sep.parseExpression("headers[command]");
MyMessage root = new MyMessage();
assertEquals("wibble", expression.getValue(root));
// This next call was failing because the isCompilable check in Indexer did not check on the key being compilable
// (and also generateCode in the Indexer was missing the optimization that it didn't need necessarily need to call
// generateCode for that accessor)
assertEquals("wibble", expression.getValue(root));
assertCanCompile(expression);
// What about a map key that is an expression - ensure the getKey() is evaluated in the right scope
expression = sep.parseExpression("headers[getKey()]");
assertEquals("wobble", expression.getValue(root));
assertEquals("wobble", expression.getValue(root));
expression = sep.parseExpression("list[getKey2()]");
assertEquals("wobble", expression.getValue(root));
assertEquals("wobble", expression.getValue(root));
expression = sep.parseExpression("ia[getKey2()]");
assertEquals(3, expression.getValue(root));
assertEquals(3, expression.getValue(root));
}
use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class MapAccessTests method testVariableMapAccess.
@Test
public void testVariableMapAccess() throws Exception {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
ctx.setVariable("day", "saturday");
Expression expr = parser.parseExpression("testMap[#day]");
Object value = expr.getValue(ctx, String.class);
assertEquals("samstag", value);
}
use of org.springframework.expression.spel.standard.SpelExpressionParser 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));
}
Aggregations