use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class PropertyAccessTests method addingSpecificPropertyAccessor.
@Test
// Adding a new property accessor just for a particular type
void addingSpecificPropertyAccessor() {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
// Even though this property accessor is added after the reflection one, it specifically
// names the String class as the type it is interested in so is chosen in preference to
// any 'default' ones
ctx.addPropertyAccessor(new StringyPropertyAccessor());
Expression expr = parser.parseRaw("new String('hello').flibbles");
Integer i = expr.getValue(ctx, Integer.class);
assertThat((int) i).isEqualTo(7);
// The reflection one will be used for other properties...
expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
Object o = expr.getValue(ctx);
assertThat(o).isNotNull();
SpelExpression flibbleexpr = parser.parseRaw("new String('hello').flibbles");
flibbleexpr.setValue(ctx, 99);
i = flibbleexpr.getValue(ctx, Integer.class);
assertThat((int) i).isEqualTo(99);
// Cannot set it to a string value
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() -> flibbleexpr.setValue(ctx, "not allowed"));
// message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
// System.out.println(e.getMessage());
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class PropertyAccessTests method accessingOnNullObject.
/**
* The standard reflection resolver cannot find properties on null objects but some
* supplied resolver might be able to - so null shouldn't crash the reflection resolver.
*/
@Test
void accessingOnNullObject() {
SpelExpression expr = (SpelExpression) parser.parseExpression("madeup");
EvaluationContext context = new StandardEvaluationContext(null);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expr.getValue(context)).extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL);
assertThat(expr.isWritable(context)).isFalse();
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expr.setValue(context, "abc")).extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class OperatorTests method testMinus.
@Test
void testMinus() {
evaluate("'c' - 2", "a", String.class);
evaluate("3.0f - 5.0f", -2.0f, Float.class);
evaluateAndCheckError("'ab' - 2", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
evaluateAndCheckError("2-'ab'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
SpelExpression expr = (SpelExpression) parser.parseExpression("-3");
assertThat(expr.toStringAST()).isEqualTo("-3");
expr = (SpelExpression) parser.parseExpression("2-3");
assertThat(expr.toStringAST()).isEqualTo("(2 - 3)");
evaluate("-5d", -5d, Double.class);
evaluate("-5L", -5L, Long.class);
evaluate("-5", -5, Integer.class);
evaluate("-new java.math.BigDecimal('5')", new BigDecimal("-5"), BigDecimal.class);
evaluateAndCheckError("-'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class InProgressTests method testSelectionAST.
@Test
public void testSelectionAST() throws Exception {
SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'.^[true]");
assertThat(expr.toStringAST()).isEqualTo("'abc'.^[true]");
expr = (SpelExpression) parser.parseExpression("'abc'.?[true]");
assertThat(expr.toStringAST()).isEqualTo("'abc'.?[true]");
expr = (SpelExpression) parser.parseExpression("'abc'.$[true]");
assertThat(expr.toStringAST()).isEqualTo("'abc'.$[true]");
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class ListTests method checkConstantList.
private void checkConstantList(String expressionText, boolean expectedToBeConstant) {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
SpelNode node = expression.getAST();
boolean condition = node instanceof InlineList;
assertThat(condition).isTrue();
InlineList inlineList = (InlineList) node;
if (expectedToBeConstant) {
assertThat(inlineList.isConstant()).isTrue();
} else {
assertThat(inlineList.isConstant()).isFalse();
}
}
Aggregations