Search in sources :

Example 31 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ExpressionStateTests method testNoVariableInteference.

@Test
public void testNoVariableInteference() {
    ExpressionState state = getState();
    TypedValue typedValue = state.lookupVariable("foo");
    assertEquals(TypedValue.NULL, typedValue);
    state.setLocalVariable("foo", 34);
    typedValue = state.lookupVariable("foo");
    assertEquals(TypedValue.NULL, typedValue);
    state.setVariable("goo", "hello");
    assertNull(state.lookupLocalVariable("goo"));
}
Also used : TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 32 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ExpressionStateTests method testActiveContextObject.

@Test
public void testActiveContextObject() {
    ExpressionState state = getState();
    assertEquals(state.getRootContextObject().getValue(), state.getActiveContextObject().getValue());
    try {
        state.popActiveContextObject();
        fail("stack should be empty...");
    } catch (EmptyStackException ese) {
    // success
    }
    state.pushActiveContextObject(new TypedValue(34));
    assertEquals(34, state.getActiveContextObject().getValue());
    state.pushActiveContextObject(new TypedValue("hello"));
    assertEquals("hello", state.getActiveContextObject().getValue());
    state.popActiveContextObject();
    assertEquals(34, state.getActiveContextObject().getValue());
    state.popActiveContextObject();
    assertEquals(state.getRootContextObject().getValue(), state.getActiveContextObject().getValue());
    state = new ExpressionState(new StandardEvaluationContext());
    assertEquals(TypedValue.NULL, state.getActiveContextObject());
}
Also used : EmptyStackException(java.util.EmptyStackException) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 33 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ExpressionStateTests method testTypeConversion.

@Test
public void testTypeConversion() throws EvaluationException {
    ExpressionState state = getState();
    String s = (String) state.convertValue(34, TypeDescriptor.valueOf(String.class));
    assertEquals("34", s);
    s = (String) state.convertValue(new TypedValue(34), TypeDescriptor.valueOf(String.class));
    assertEquals("34", s);
}
Also used : TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 34 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class SelectionAndProjectionTests method selectionWithArray.

@Test
public void selectionWithArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
    Object value = expression.getValue(context);
    assertTrue(value.getClass().isArray());
    TypedValue typedValue = new TypedValue(value);
    assertEquals(Integer.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType());
    Integer[] array = (Integer[]) value;
    assertEquals(5, array.length);
    assertEquals(new Integer(0), array[0]);
    assertEquals(new Integer(1), array[1]);
    assertEquals(new Integer(2), array[2]);
    assertEquals(new Integer(3), array[3]);
    assertEquals(new Integer(4), array[4]);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 35 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class SelectionAndProjectionTests method selectionWithPrimitiveArray.

@Test
public void selectionWithPrimitiveArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("ints.?[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
    Object value = expression.getValue(context);
    assertTrue(value.getClass().isArray());
    TypedValue typedValue = new TypedValue(value);
    assertEquals(Integer.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType());
    Integer[] array = (Integer[]) value;
    assertEquals(5, array.length);
    assertEquals(new Integer(0), array[0]);
    assertEquals(new Integer(1), array[1]);
    assertEquals(new Integer(2), array[2]);
    assertEquals(new Integer(3), array[3]);
    assertEquals(new Integer(4), array[4]);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Aggregations

TypedValue (org.springframework.expression.TypedValue)63 Test (org.junit.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)18 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)13 ExpressionState (org.springframework.expression.spel.ExpressionState)11 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 AccessException (org.springframework.expression.AccessException)4 Expression (org.springframework.expression.Expression)4 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)4 MethodParameter (org.springframework.core.MethodParameter)3 EvaluationException (org.springframework.expression.EvaluationException)3 SpelNode (org.springframework.expression.spel.SpelNode)3 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2