use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelReproTests method propertyAccessOnNullTarget_SPR5663.
@Test
void propertyAccessOnNullTarget_SPR5663() throws AccessException {
PropertyAccessor accessor = new ReflectivePropertyAccessor();
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
assertThat(accessor.canRead(context, null, "abc")).isFalse();
assertThat(accessor.canWrite(context, null, "abc")).isFalse();
assertThatIllegalStateException().isThrownBy(() -> accessor.read(context, null, "abc"));
assertThatIllegalStateException().isThrownBy(() -> accessor.write(context, null, "abc", "foo"));
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelReproTests method SPR13055.
@Test
@SuppressWarnings("rawtypes")
void SPR13055() {
List<Map<String, Object>> myPayload = new ArrayList<>();
Map<String, Object> v1 = new HashMap<>();
Map<String, Object> v2 = new HashMap<>();
v1.put("test11", "test11");
v1.put("test12", "test12");
v2.put("test21", "test21");
v2.put("test22", "test22");
myPayload.add(v1);
myPayload.add(v2);
EvaluationContext context = new StandardEvaluationContext(myPayload);
ExpressionParser parser = new SpelExpressionParser();
String ex = "#root.![T(cn.taketoday.util.StringUtils).collectionToCommaDelimitedString(#this.values())]";
List res = parser.parseExpression(ex).getValue(context, List.class);
assertThat(res.toString()).isEqualTo("[test12,test11, test22,test21]");
res = parser.parseExpression("#root.![#this.values()]").getValue(context, List.class);
assertThat(res.toString()).isEqualTo("[[test12, test11], [test22, test21]]");
res = parser.parseExpression("#root.![values()]").getValue(context, List.class);
assertThat(res.toString()).isEqualTo("[[test12, test11], [test22, test21]]");
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelReproTests method wideningPrimitiveConversion_SPR8224.
/**
* Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
* method accepting 'long' is ok.
*/
@Test
void wideningPrimitiveConversion_SPR8224() throws Exception {
class WideningPrimitiveConversion {
public int getX(long i) {
return 10;
}
}
final Integer INTEGER_VALUE = 7;
WideningPrimitiveConversion target = new WideningPrimitiveConversion();
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.fromObject(INTEGER_VALUE));
MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue();
final int compiler = target.getX(INTEGER_VALUE);
assertThat(actual).isEqualTo(compiler);
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelReproTests method SPR13918.
@Test
void SPR13918() {
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("encoding", "UTF-8");
Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
Object result = ex.getValue(context);
assertThat(result).isEqualTo(StandardCharsets.UTF_8);
}
use of cn.taketoday.expression.EvaluationContext in project today-infrastructure by TAKETODAY.
the class SpelCompilationCoverageTests method functionReference.
@Test
public void functionReference() throws Exception {
EvaluationContext ctx = new StandardEvaluationContext();
Method m = getClass().getDeclaredMethod("concat", String.class, String.class);
ctx.setVariable("concat", m);
expression = parser.parseExpression("#concat('a','b')");
assertThat(expression.getValue(ctx)).isEqualTo("ab");
assertCanCompile(expression);
assertThat(expression.getValue(ctx)).isEqualTo("ab");
expression = parser.parseExpression("#concat(#concat('a','b'),'c').charAt(1)");
assertThat(expression.getValue(ctx)).isEqualTo('b');
assertCanCompile(expression);
assertThat(expression.getValue(ctx)).isEqualTo('b');
expression = parser.parseExpression("#concat(#a,#b)");
ctx.setVariable("a", "foo");
ctx.setVariable("b", "bar");
assertThat(expression.getValue(ctx)).isEqualTo("foobar");
assertCanCompile(expression);
assertThat(expression.getValue(ctx)).isEqualTo("foobar");
ctx.setVariable("b", "boo");
assertThat(expression.getValue(ctx)).isEqualTo("fooboo");
m = Math.class.getDeclaredMethod("pow", Double.TYPE, Double.TYPE);
ctx.setVariable("kapow", m);
expression = parser.parseExpression("#kapow(2.0d,2.0d)");
assertThat(expression.getValue(ctx).toString()).isEqualTo("4.0");
assertCanCompile(expression);
assertThat(expression.getValue(ctx).toString()).isEqualTo("4.0");
}
Aggregations