Search in sources :

Example 6 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class EvaluationTests method increment02prefix.

@Test
public void increment02prefix() {
    Spr9751 helper = new Spr9751();
    StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = null;
    // BigDecimal
    e = parser.parseExpression("++bd");
    assertTrue(new BigDecimal("2").equals(helper.bd));
    BigDecimal return_bd = e.getValue(ctx, BigDecimal.class);
    assertTrue(new BigDecimal("3").equals(return_bd));
    assertTrue(new BigDecimal("3").equals(helper.bd));
    // double
    e = parser.parseExpression("++ddd");
    assertEquals(2.0d, helper.ddd, 0d);
    double return_ddd = e.getValue(ctx, Double.TYPE);
    assertEquals(3.0d, return_ddd, 0d);
    assertEquals(3.0d, helper.ddd, 0d);
    // float
    e = parser.parseExpression("++fff");
    assertEquals(3.0f, helper.fff, 0d);
    float return_fff = e.getValue(ctx, Float.TYPE);
    assertEquals(4.0f, return_fff, 0d);
    assertEquals(4.0f, helper.fff, 0d);
    // long
    e = parser.parseExpression("++lll");
    assertEquals(66666L, helper.lll);
    long return_lll = e.getValue(ctx, Long.TYPE);
    assertEquals(66667L, return_lll);
    assertEquals(66667L, helper.lll);
    // int
    e = parser.parseExpression("++iii");
    assertEquals(42, helper.iii);
    int return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(43, return_iii);
    assertEquals(43, helper.iii);
    return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(44, return_iii);
    assertEquals(44, helper.iii);
    // short
    e = parser.parseExpression("++sss");
    assertEquals(15, helper.sss);
    int return_sss = (Integer) e.getValue(ctx);
    assertEquals(16, return_sss);
    assertEquals(16, helper.sss);
}
Also used : BigInteger(java.math.BigInteger) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 7 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class EvaluationTests method limitCollectionGrowing.

@Test
public void limitCollectionGrowing() throws Exception {
    TestClass instance = new TestClass();
    StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true, 3));
    Expression expression = parser.parseExpression("foo[2]");
    expression.setValue(ctx, "2");
    assertThat(instance.getFoo().size(), equalTo(3));
    expression = parser.parseExpression("foo[3]");
    try {
        expression.setValue(ctx, "3");
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.UNABLE_TO_GROW_COLLECTION, see.getMessageCode());
        assertThat(instance.getFoo().size(), equalTo(3));
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 8 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class EvaluationTests method increment01root.

// For now I am making #this not assignable
@Test
public void increment01root() {
    Integer i = 42;
    StandardEvaluationContext ctx = new StandardEvaluationContext(i);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = parser.parseExpression("#this++");
    assertEquals(42, i.intValue());
    try {
        e.getValue(ctx, Integer.class);
        fail();
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode());
    }
}
Also used : BigInteger(java.math.BigInteger) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 9 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class EvaluationTests method operatorVariants.

@Test
public void operatorVariants() throws Exception {
    SpelExpression expr = (SpelExpression) parser.parseExpression("#a < #b");
    EvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("a", (short) 3);
    ctx.setVariable("b", (short) 6);
    assertTrue(expr.getValue(ctx, Boolean.class));
    ctx.setVariable("b", (byte) 6);
    assertTrue(expr.getValue(ctx, Boolean.class));
    ctx.setVariable("a", (byte) 9);
    ctx.setVariable("b", (byte) 6);
    assertFalse(expr.getValue(ctx, Boolean.class));
    ctx.setVariable("a", 10L);
    ctx.setVariable("b", (short) 30);
    assertTrue(expr.getValue(ctx, Boolean.class));
    ctx.setVariable("a", (byte) 3);
    ctx.setVariable("b", (short) 30);
    assertTrue(expr.getValue(ctx, Boolean.class));
    ctx.setVariable("a", (byte) 3);
    ctx.setVariable("b", 30L);
    assertTrue(expr.getValue(ctx, Boolean.class));
    ctx.setVariable("a", (byte) 3);
    ctx.setVariable("b", 30f);
    assertTrue(expr.getValue(ctx, Boolean.class));
    ctx.setVariable("a", new BigInteger("10"));
    ctx.setVariable("b", new BigInteger("20"));
    assertTrue(expr.getValue(ctx, Boolean.class));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) BigInteger(java.math.BigInteger) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 10 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class EvaluationTests method collectionGrowingViaIndexer.

/**
	 * This test is checking that with the changes for 9751 that the refactoring in Indexer is
	 * coping correctly for references beyond collection boundaries.
	 */
@Test
public void collectionGrowingViaIndexer() {
    Spr9751 instance = new Spr9751();
    // Add a new element to the list
    StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = parser.parseExpression("listOfStrings[++index3]='def'");
    e.getValue(ctx);
    assertEquals(2, instance.listOfStrings.size());
    assertEquals("def", instance.listOfStrings.get(1));
    // Check reference beyond end of collection
    ctx = new StandardEvaluationContext(instance);
    parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    e = parser.parseExpression("listOfStrings[0]");
    String value = e.getValue(ctx, String.class);
    assertEquals("abc", value);
    e = parser.parseExpression("listOfStrings[1]");
    value = e.getValue(ctx, String.class);
    assertEquals("def", value);
    e = parser.parseExpression("listOfStrings[2]");
    value = e.getValue(ctx, String.class);
    assertEquals("", value);
    // Now turn off growing and reference off the end
    ctx = new StandardEvaluationContext(instance);
    parser = new SpelExpressionParser(new SpelParserConfiguration(false, false));
    e = parser.parseExpression("listOfStrings[3]");
    try {
        e.getValue(ctx, String.class);
        fail();
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, see.getMessageCode());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Aggregations

StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)241 Test (org.junit.Test)207 Expression (org.springframework.expression.Expression)153 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)151 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)103 ExpressionParser (org.springframework.expression.ExpressionParser)79 EvaluationContext (org.springframework.expression.EvaluationContext)43 ArrayList (java.util.ArrayList)27 List (java.util.List)14 HashMap (java.util.HashMap)12 EvaluationException (org.springframework.expression.EvaluationException)12 TypedValue (org.springframework.expression.TypedValue)12 Map (java.util.Map)11 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 BigInteger (java.math.BigInteger)7 LinkedHashMap (java.util.LinkedHashMap)7 AccessException (org.springframework.expression.AccessException)6 ExpressionState (org.springframework.expression.spel.ExpressionState)6 Inventor (org.springframework.expression.spel.testresources.Inventor)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5