use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method failsWhenSettingContextForExpression_SPR12326.
@Test
public void failsWhenSettingContextForExpression_SPR12326() {
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.OFF, getClass().getClassLoader()));
Person3 person = new Person3("foo", 1);
SpelExpression expression = parser.parseRaw("#it?.age?.equals([0])");
StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { 1 });
context.setVariable("it", person);
expression.setEvaluationContext(context);
assertThat(expression.getValue(Boolean.class)).isTrue();
// This will trigger compilation (second usage)
assertThat(expression.getValue(Boolean.class)).isTrue();
context.setVariable("it", null);
assertThat(expression.getValue(Boolean.class)).isNull();
assertCanCompile(expression);
context.setVariable("it", person);
assertThat(expression.getValue(Boolean.class)).isTrue();
context.setVariable("it", null);
assertThat(expression.getValue(Boolean.class)).isNull();
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method getAst.
// Helper methods
private SpelNodeImpl getAst() {
SpelExpression spelExpression = (SpelExpression) expression;
SpelNode ast = spelExpression.getAST();
return (SpelNodeImpl) ast;
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method compilationOfBasicNullSafeMethodReference.
@Test
public void compilationOfBasicNullSafeMethodReference() {
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.OFF, getClass().getClassLoader()));
SpelExpression expression = parser.parseRaw("#it?.equals(3)");
StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { 1 });
context.setVariable("it", 3);
expression.setEvaluationContext(context);
assertThat(expression.getValue(Boolean.class)).isTrue();
context.setVariable("it", null);
assertThat(expression.getValue(Boolean.class)).isNull();
assertCanCompile(expression);
context.setVariable("it", 3);
assertThat(expression.getValue(Boolean.class)).isTrue();
context.setVariable("it", null);
assertThat(expression.getValue(Boolean.class)).isNull();
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method verifyCompilationAndBehaviourWithNull2.
private void verifyCompilationAndBehaviourWithNull2(String expressionText, SpelExpressionParser parser, StandardEvaluationContext ctx) {
SpelExpression fast = (SpelExpression) parser.parseExpression(expressionText);
SpelExpression slow = (SpelExpression) parser.parseExpression(expressionText);
fast.getValue(ctx);
assertThat(fast.compileExpression()).isTrue();
Reg r = (Reg) ctx.getRootObject().getValue();
// try the numbers 0,1,2,null
for (int i = 0; i < 4; i++) {
r.setValue(i < 3 ? i : null);
boolean slowResult = (Boolean) slow.getValue(ctx);
boolean fastResult = (Boolean) fast.getValue(ctx);
assertThat(fastResult).as("Differing results: expression=" + expressionText + " value=" + r.getValue() + " slow=" + slowResult + " fast=" + fastResult).isEqualTo(slowResult);
}
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method verifyCompilationAndBehaviourWithNull.
private void verifyCompilationAndBehaviourWithNull(String expressionText, SpelExpressionParser parser, StandardEvaluationContext ctx) {
Reg r = (Reg) ctx.getRootObject().getValue();
// having a value in value2 fields will enable compilation to succeed, then can switch it to null
r.setValue2(1);
SpelExpression fast = (SpelExpression) parser.parseExpression(expressionText);
SpelExpression slow = (SpelExpression) parser.parseExpression(expressionText);
fast.getValue(ctx);
assertThat(fast.compileExpression()).isTrue();
r.setValue2(null);
// try the numbers 0,1,2,null
for (int i = 0; i < 4; i++) {
r.setValue(i < 3 ? i : null);
boolean slowResult = (Boolean) slow.getValue(ctx);
boolean fastResult = (Boolean) fast.getValue(ctx);
assertThat(fastResult).as("Differing results: expression=" + expressionText + " value=" + r.getValue() + " slow=" + slowResult + " fast=" + fastResult).isEqualTo(slowResult);
}
}
Aggregations