use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class MapTests method checkConstantMap.
private void checkConstantMap(String expressionText, boolean expectedToBeConstant) {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
SpelNode node = expression.getAST();
boolean condition = node instanceof InlineMap;
assertThat(condition).isTrue();
InlineMap inlineMap = (InlineMap) node;
if (expectedToBeConstant) {
assertThat(inlineMap.isConstant()).isTrue();
} else {
assertThat(inlineMap.isConstant()).isFalse();
}
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelReproTests method greaterThanWithNulls_SPR7840.
@Test
void greaterThanWithNulls_SPR7840() {
List<D> list = new ArrayList<>();
list.add(new D("aaa"));
list.add(new D("bbb"));
list.add(new D(null));
list.add(new D("ccc"));
list.add(new D(null));
list.add(new D("zzz"));
StandardEvaluationContext context = new StandardEvaluationContext(list);
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "#root.?[a < 'hhh']";
SpelExpression exp = parser.parseRaw(el1);
Object value = exp.getValue(context);
assertThat(value.toString()).isEqualTo("[D(aaa), D(bbb), D(null), D(ccc), D(null)]");
String el2 = "#root.?[a > 'hhh']";
SpelExpression exp2 = parser.parseRaw(el2);
Object value2 = exp2.getValue(context);
assertThat(value2.toString()).isEqualTo("[D(zzz)]");
// trim out the nulls first
String el3 = "#root.?[a!=null].?[a < 'hhh']";
SpelExpression exp3 = parser.parseRaw(el3);
Object value3 = exp3.getValue(context);
assertThat(value3.toString()).isEqualTo("[D(aaa), D(bbb), D(ccc)]");
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelReproTests method projectionTypeDescriptors_1.
@Test
void projectionTypeDescriptors_1() {
StandardEvaluationContext context = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ls.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List<?> value = (List<?>) exp.getValue(context);
// value is list containing [true,false]
assertThat(value.get(0).getClass()).isEqualTo(Boolean.class);
TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
assertThat(evaluated.getElementTypeDescriptor()).isNull();
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method opNe_SPR14863.
@Test
public void opNe_SPR14863() throws Exception {
SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.MIXED, ClassLoader.getSystemClassLoader());
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("data['my-key'] != 'my-value'");
Map<String, String> data = new HashMap<>();
data.put("my-key", new String("my-value"));
StandardEvaluationContext context = new StandardEvaluationContext(new MyContext(data));
assertThat(expression.getValue(context, Boolean.class)).isFalse();
assertCanCompile(expression);
((SpelExpression) expression).compileExpression();
assertThat(expression.getValue(context, Boolean.class)).isFalse();
List<String> ls = new ArrayList<String>();
ls.add(new String("foo"));
context = new StandardEvaluationContext(ls);
expression = parse("get(0) != 'foo'");
assertThat(expression.getValue(context, Boolean.class)).isFalse();
assertCanCompile(expression);
assertThat(expression.getValue(context, Boolean.class)).isFalse();
ls.remove(0);
ls.add("goo");
assertThat(expression.getValue(context, Boolean.class)).isTrue();
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method methodReferenceMissingCastAndRootObjectAccessing_SPR12326.
@Test
public void methodReferenceMissingCastAndRootObjectAccessing_SPR12326() {
// Need boxing code on the 1 so that toString() can be called
expression = parser.parseExpression("1.toString()");
assertThat(expression.getValue()).isEqualTo("1");
assertCanCompile(expression);
assertThat(expression.getValue()).isEqualTo("1");
expression = parser.parseExpression("#it?.age.equals([0])");
Person person = new Person(1);
StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { person.getAge() });
context.setVariable("it", person);
assertThat(expression.getValue(context, Boolean.class)).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(context, Boolean.class)).isTrue();
// Variant of above more like what was in the bug report:
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, getClass().getClassLoader()));
SpelExpression ex = parser.parseRaw("#it?.age.equals([0])");
context = new StandardEvaluationContext(new Object[] { person.getAge() });
context.setVariable("it", person);
assertThat(ex.getValue(context, Boolean.class)).isTrue();
assertThat(ex.getValue(context, Boolean.class)).isTrue();
PersonInOtherPackage person2 = new PersonInOtherPackage(1);
ex = parser.parseRaw("#it?.age.equals([0])");
context = new StandardEvaluationContext(new Object[] { person2.getAge() });
context.setVariable("it", person2);
assertThat(ex.getValue(context, Boolean.class)).isTrue();
assertThat(ex.getValue(context, Boolean.class)).isTrue();
ex = parser.parseRaw("#it?.age.equals([0])");
context = new StandardEvaluationContext(new Object[] { person2.getAge() });
context.setVariable("it", person2);
assertThat((boolean) (Boolean) ex.getValue(context)).isTrue();
assertThat((boolean) (Boolean) ex.getValue(context)).isTrue();
}
Aggregations