@Override public boolean isWritable(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); return this.ast.isWritable(new ExpressionState(context, this.configuration)); }
@Override public void setValue(EvaluationContext context, @Nullable Object value) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); this.ast.setValue(new ExpressionState(context, this.configuration), value); }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
@Override public void setValue(EvaluationContext context, Object rootObject, @Nullable Object value) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); this.ast.setValue(new ExpressionState(context, toTypedValue(rootObject), this.configuration), value); }
@Override public boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); return this.ast.isWritable(new ExpressionState(context, toTypedValue(rootObject), this.configuration)); }
@Override @Nullable public Class<?> getValueType(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, this.configuration); TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor(); return (typeDescriptor != null ? typeDescriptor.getType() : null); }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
@Override public void setValue(Object rootObject, @Nullable Object value) throws EvaluationException { this.ast.setValue( new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration), value); }
/** * @return a new ExpressionState */ private ExpressionState getState() { EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); ExpressionState state = new ExpressionState(context); return state; }
@Override @Nullable public Class<?> getValueType(EvaluationContext context, Object rootObject) throws EvaluationException { ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor(); return (typeDescriptor != null ? typeDescriptor.getType() : null); }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException { ExpressionState expressionState = new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
@Override public boolean isWritable(Object rootObject) throws EvaluationException { return this.ast.isWritable( new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration)); }
@Test public void testConstruction() { EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); ExpressionState state = new ExpressionState(context); assertEquals(context, state.getEvaluationContext()); }
@Test(expected = SpelEvaluationException.class) public void test_unaryPlusWithStringLiteral() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); StringLiteral str = new StringLiteral("word", -1, -1, "word"); OpPlus o = new OpPlus(-1, -1, str); o.getValueInternal(expressionState); }
@Test public void test_binaryPlusWithTime_ToString() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); Time time = new Time(new Date().getTime()); VariableReference var = new VariableReference("timeVar", -1, -1); var.setValue(expressionState, time); StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\""); OpPlus o = new OpPlus(-1, -1, var, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals(time + " is now", value.getValue()); }
@Test public void testRootObjectConstructor() { EvaluationContext ctx = getContext(); // TypedValue root = ctx.getRootObject(); // supplied should override root on context ExpressionState state = new ExpressionState(ctx,new TypedValue("i am a string")); TypedValue stateRoot = state.getRootContextObject(); assertEquals(String.class, stateRoot.getTypeDescriptor().getType()); assertEquals("i am a string", stateRoot.getValue()); }
@Test public void testRootContextObject() { ExpressionState state = getState(); assertEquals(Inventor.class, state.getRootContextObject().getValue().getClass()); // although the root object is being set on the evaluation context, the value in the 'state' remains what it was when constructed ((StandardEvaluationContext) state.getEvaluationContext()).setRootObject(null); assertEquals(Inventor.class, state.getRootContextObject().getValue().getClass()); // assertEquals(null, state.getRootContextObject().getValue()); state = new ExpressionState(new StandardEvaluationContext()); assertEquals(TypedValue.NULL, state.getRootContextObject()); ((StandardEvaluationContext) state.getEvaluationContext()).setRootObject(null); assertEquals(null, state.getRootContextObject().getValue()); }
@Test public void test_binaryPlusWithStringOperands() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); StringLiteral n1 = new StringLiteral("\"foo\"", -1, -1, "\"foo\""); StringLiteral n2 = new StringLiteral("\"bar\"", -1, -1, "\"bar\""); OpPlus o = new OpPlus(-1, -1, n1, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals("foobar", value.getValue()); }
@Test public void test_binaryPlusWithRightStringOperand() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); LongLiteral n1 = new LongLiteral("123", -1, -1, 123); StringLiteral n2 = new StringLiteral("\" is a number\"", -1, -1, "\" is a number\""); OpPlus o = new OpPlus(-1, -1, n1, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals("123 is a number", value.getValue()); }
@Test public void test_binaryPlusWithLeftStringOperand() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); StringLiteral n1 = new StringLiteral("\"number is \"", -1, -1, "\"number is \""); LongLiteral n2 = new LongLiteral("123", -1, -1, 123); OpPlus o = new OpPlus(-1, -1, n1, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals("number is 123", value.getValue()); }