use of cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.
the class MethodInvocationTests method testMethodFiltering_SPR6764.
@Test
public void testMethodFiltering_SPR6764() {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setRootObject(new TestObject());
LocalFilter filter = new LocalFilter();
context.registerMethodFilter(TestObject.class, filter);
// Filter will be called but not do anything, so first doit() will be invoked
SpelExpression expr = (SpelExpression) parser.parseExpression("doit(1)");
String result = expr.getValue(context, String.class);
assertThat(result).isEqualTo("1");
assertThat(filter.filterCalled).isTrue();
// Filter will now remove non @Anno annotated methods
filter.removeIfNotAnnotated = true;
filter.filterCalled = false;
expr = (SpelExpression) parser.parseExpression("doit(1)");
result = expr.getValue(context, String.class);
assertThat(result).isEqualTo("double 1.0");
assertThat(filter.filterCalled).isTrue();
// check not called for other types
filter.filterCalled = false;
context.setRootObject("abc");
expr = (SpelExpression) parser.parseExpression("charAt(0)");
result = expr.getValue(context, String.class);
assertThat(result).isEqualTo("a");
assertThat(filter.filterCalled).isFalse();
// check de-registration works
filter.filterCalled = false;
// clear filter
context.registerMethodFilter(TestObject.class, null);
context.setRootObject(new TestObject());
expr = (SpelExpression) parser.parseExpression("doit(1)");
result = expr.getValue(context, String.class);
assertThat(result).isEqualTo("1");
assertThat(filter.filterCalled).isFalse();
}
use of cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.
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 cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.
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 cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.
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 cn.taketoday.expression.spel.standard.SpelExpression in project today-infrastructure by TAKETODAY.
the class OperatorTests method testPlus.
@Test
void testPlus() {
evaluate("7 + 2", "9", Integer.class);
evaluate("3.0f + 5.0f", 8.0f, Float.class);
evaluate("3.0d + 5.0d", 8.0d, Double.class);
evaluate("3 + new java.math.BigDecimal('5')", new BigDecimal("8"), BigDecimal.class);
evaluate("'ab' + 2", "ab2", String.class);
evaluate("2 + 'a'", "2a", String.class);
evaluate("'ab' + null", "abnull", String.class);
evaluate("null + 'ab'", "nullab", String.class);
// AST:
SpelExpression expr = (SpelExpression) parser.parseExpression("+3");
assertThat(expr.toStringAST()).isEqualTo("+3");
expr = (SpelExpression) parser.parseExpression("2+3");
assertThat(expr.toStringAST()).isEqualTo("(2 + 3)");
// use as a unary operator
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);
// string concatenation
evaluate("'abc'+'def'", "abcdef", String.class);
evaluate("5 + new Integer('37')", 42, Integer.class);
}
Aggregations