use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class IndexingTests method setPropertyContainingList.
@Test
public void setPropertyContainingList() {
List<Integer> property = new ArrayList<>();
property.add(3);
this.parameterizedList = property;
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("parameterizedList");
assertEquals("java.util.ArrayList<java.lang.Integer>", expression.getValueTypeDescriptor(this).toString());
assertEquals(property, expression.getValue(this));
expression = parser.parseExpression("parameterizedList[0]");
assertEquals(3, expression.getValue(this));
expression.setValue(this, "4");
assertEquals(4, expression.getValue(this));
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class MethodInvocationTests method invokeMethodWithoutConversion.
@Test
public void invokeMethodWithoutConversion() throws Exception {
final BytesService service = new BytesService();
byte[] bytes = new byte[100];
StandardEvaluationContext context = new StandardEvaluationContext(bytes);
context.setBeanResolver(new BeanResolver() {
@Override
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
if ("service".equals(beanName)) {
return service;
}
return null;
}
});
Expression expression = parser.parseExpression("@service.handleBytes(#root)");
byte[] outBytes = expression.getValue(context, byte[].class);
assertSame(bytes, outBytes);
}
use of org.springframework.expression.Expression 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.Expression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method methodReference_literalArguments_int.
@Test
public void methodReference_literalArguments_int() throws Exception {
Expression expression = parser.parseExpression("'abcd'.substring(1,3)");
String resultI = expression.getValue(new TestClass1(), String.class);
assertCanCompile(expression);
String resultC = expression.getValue(new TestClass1(), String.class);
assertEquals("bc", resultI);
assertEquals("bc", resultC);
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method checkCalc.
private void checkCalc(PayloadX p, String expression, double expectedResult) {
Expression expr = parse(expression);
assertEquals(expectedResult, expr.getValue(p));
assertCanCompile(expr);
assertEquals(expectedResult, expr.getValue(p));
}
Aggregations