use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class LiteralTests method testNotWritable.
@Test
public void testNotWritable() throws Exception {
SpelExpression expr = (SpelExpression) parser.parseExpression("37");
assertThat(expr.isWritable(new StandardEvaluationContext())).isFalse();
expr = (SpelExpression) parser.parseExpression("37L");
assertThat(expr.isWritable(new StandardEvaluationContext())).isFalse();
expr = (SpelExpression) parser.parseExpression("true");
assertThat(expr.isWritable(new StandardEvaluationContext())).isFalse();
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class MapTests method testMapKeysThatAreAlsoSpELKeywords.
@Test
public void testMapKeysThatAreAlsoSpELKeywords() {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = null;
Object o = null;
// expression = (SpelExpression) parser.parseExpression("foo['NEW']");
// o = expression.getValue(new MapHolder());
// assertEquals("VALUE",o);
expression = (SpelExpression) parser.parseExpression("foo[T]");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("TV");
expression = (SpelExpression) parser.parseExpression("foo[t]");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("tv");
expression = (SpelExpression) parser.parseExpression("foo[NEW]");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("VALUE");
expression = (SpelExpression) parser.parseExpression("foo[new]");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("value");
expression = (SpelExpression) parser.parseExpression("foo['abc.def']");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("value");
expression = (SpelExpression) parser.parseExpression("foo[foo[NEW]]");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("37");
expression = (SpelExpression) parser.parseExpression("foo[foo[new]]");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("38");
expression = (SpelExpression) parser.parseExpression("foo[foo[foo[T]]]");
o = expression.getValue(new MapHolder());
assertThat(o).isEqualTo("value");
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class OperatorOverloaderTests method testSimpleOperations.
@Test
public void testSimpleOperations() throws Exception {
// no built in support for this:
evaluateAndCheckError("'abc'-true", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
eContext.setOperatorOverloader(new StringAndBooleanAddition());
SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'+true");
assertThat(expr.getValue(eContext)).isEqualTo("abctrue");
expr = (SpelExpression) parser.parseExpression("'abc'-true");
assertThat(expr.getValue(eContext)).isEqualTo("abc");
expr = (SpelExpression) parser.parseExpression("'abc'+null");
assertThat(expr.getValue(eContext)).isEqualTo("abcnull");
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method mixingItUp_propertyAccessIndexerOpLtTernaryRootNull.
@Test
void mixingItUp_propertyAccessIndexerOpLtTernaryRootNull() {
Payload payload = new Payload();
expression = parser.parseExpression("DR[0].three");
Object v = expression.getValue(payload);
assertThat(getAst().getExitDescriptor()).isEqualTo("Lorg/springframework/expression/spel/SpelCompilationCoverageTests$Three");
Expression expression = parser.parseExpression("DR[0].three.four lt 0.1d?#root:null");
v = expression.getValue(payload);
SpelExpression sExpr = (SpelExpression) expression;
Ternary ternary = (Ternary) sExpr.getAST();
OpLT oplt = (OpLT) ternary.getChild(0);
CompoundExpression cExpr = (CompoundExpression) oplt.getLeftOperand();
String cExprExitDescriptor = cExpr.getExitDescriptor();
assertThat(cExprExitDescriptor).isEqualTo("D");
assertThat(oplt.getExitDescriptor()).isEqualTo("Z");
assertCanCompile(expression);
Object vc = expression.getValue(payload);
assertThat(v).isEqualTo(payload);
assertThat(vc).isEqualTo(payload);
payload.DR[0].three.four = 0.13d;
vc = expression.getValue(payload);
assertThat(vc).isNull();
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs.
@Test
public void compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs() throws Exception {
StandardEvaluationContext context = null;
// Here the target method takes Object... and we are passing a string
expression = parser.parseExpression("#doFormat('hey %s', 'there')");
context = new StandardEvaluationContext();
context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
((SpelExpression) expression).setEvaluationContext(context);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
expression = parser.parseExpression("#doFormat([0], 'there')");
context = new StandardEvaluationContext(new Object[] { "hey %s" });
context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
((SpelExpression) expression).setEvaluationContext(context);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
expression = parser.parseExpression("#doFormat([0], #arg)");
context = new StandardEvaluationContext(new Object[] { "hey %s" });
context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
context.setVariable("arg", "there");
((SpelExpression) expression).setEvaluationContext(context);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
}
Aggregations