use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method nullsafeFieldPropertyDereferencing_SPR16489.
@Test
public void nullsafeFieldPropertyDereferencing_SPR16489() throws Exception {
FooObjectHolder foh = new FooObjectHolder();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setRootObject(foh);
// First non compiled:
SpelExpression expression = (SpelExpression) parser.parseExpression("foo?.object");
assertThat(expression.getValue(context)).isEqualTo("hello");
foh.foo = null;
assertThat(expression.getValue(context)).isNull();
// Now revert state of foh and try compiling it:
foh.foo = new FooObject();
assertThat(expression.getValue(context)).isEqualTo("hello");
assertCanCompile(expression);
assertThat(expression.getValue(context)).isEqualTo("hello");
foh.foo = null;
assertThat(expression.getValue(context)).isNull();
// Static references
expression = (SpelExpression) parser.parseExpression("#var?.propertya");
context.setVariable("var", StaticsHelper.class);
assertThat(expression.getValue(context).toString()).isEqualTo("sh");
context.setVariable("var", null);
assertThat(expression.getValue(context)).isNull();
assertCanCompile(expression);
context.setVariable("var", StaticsHelper.class);
assertThat(expression.getValue(context).toString()).isEqualTo("sh");
context.setVariable("var", null);
assertThat(expression.getValue(context)).isNull();
// Single size primitive (boolean)
expression = (SpelExpression) parser.parseExpression("#var?.a");
context.setVariable("var", new TestClass4());
assertThat((boolean) (Boolean) expression.getValue(context)).isFalse();
context.setVariable("var", null);
assertThat(expression.getValue(context)).isNull();
assertCanCompile(expression);
context.setVariable("var", new TestClass4());
assertThat((boolean) (Boolean) expression.getValue(context)).isFalse();
context.setVariable("var", null);
assertThat(expression.getValue(context)).isNull();
// Double slot primitives
expression = (SpelExpression) parser.parseExpression("#var?.four");
context.setVariable("var", new Three());
assertThat(expression.getValue(context).toString()).isEqualTo("0.04");
context.setVariable("var", null);
assertThat(expression.getValue(context)).isNull();
assertCanCompile(expression);
context.setVariable("var", new Three());
assertThat(expression.getValue(context).toString()).isEqualTo("0.04");
context.setVariable("var", null);
assertThat(expression.getValue(context)).isNull();
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelReproTests method reservedWords_SPR8228.
@Test
void reservedWords_SPR8228() {
// "DIV","EQ","GE","GT","LE","LT","MOD","NE","NOT"
@SuppressWarnings("unused")
class Reserver {
public Reserver getReserver() {
return this;
}
public String NE = "abc";
public String ne = "def";
public int DIV = 1;
public int div = 3;
public Map<String, String> m = new HashMap<>();
Reserver() {
m.put("NE", "xyz");
}
}
StandardEvaluationContext context = new StandardEvaluationContext(new Reserver());
SpelExpressionParser parser = new SpelExpressionParser();
String ex = "getReserver().NE";
SpelExpression exp = parser.parseRaw(ex);
String value = (String) exp.getValue(context);
assertThat(value).isEqualTo("abc");
ex = "getReserver().ne";
exp = parser.parseRaw(ex);
value = (String) exp.getValue(context);
assertThat(value).isEqualTo("def");
ex = "getReserver().m[NE]";
exp = parser.parseRaw(ex);
value = (String) exp.getValue(context);
assertThat(value).isEqualTo("xyz");
ex = "getReserver().DIV";
exp = parser.parseRaw(ex);
assertThat(exp.getValue(context)).isEqualTo(1);
ex = "getReserver().div";
exp = parser.parseRaw(ex);
assertThat(exp.getValue(context)).isEqualTo(3);
exp = parser.parseRaw("NE");
assertThat(exp.getValue(context)).isEqualTo("abc");
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelReproTests method array.
@Test
void array() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Expression expression = null;
Object result = null;
expression = parser.parseExpression("new java.lang.Long[0].class");
result = expression.getValue(context, "");
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [Ljava.lang.Long;");
expression = parser.parseExpression("T(java.lang.Long[])");
result = expression.getValue(context, "");
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [Ljava.lang.Long;");
expression = parser.parseExpression("T(java.lang.String[][][])");
result = expression.getValue(context, "");
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [[[Ljava.lang.String;");
assertThat(((SpelExpression) expression).toStringAST()).isEqualTo("T(java.lang.String[][][])");
expression = parser.parseExpression("new int[0].class");
result = expression.getValue(context, "");
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [I");
expression = parser.parseExpression("T(int[][])");
result = expression.getValue(context, "");
assertThat(result.toString()).isEqualTo("class [[I");
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelReproTests method projectionTypeDescriptors_3.
@Test
void projectionTypeDescriptors_3() {
StandardEvaluationContext context = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ms.![key.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 SpelReproTests method projectionTypeDescriptors_2.
@Test
void projectionTypeDescriptors_2() {
StandardEvaluationContext context = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "as.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
Object[] value = (Object[]) exp.getValue(context);
// value is array containing [true,false]
assertThat(value[0].getClass()).isEqualTo(Boolean.class);
TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
assertThat(evaluated.getElementTypeDescriptor().getType()).isEqualTo(Boolean.class);
}
Aggregations