Search in sources :

Example 6 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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 SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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 SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class EvaluationTests method testRogueTrailingDotCausesNPE_SPR6866.

@Test
public void testRogueTrailingDotCausesNPE_SPR6866() {
    try {
        new SpelExpressionParser().parseExpression("placeOfBirth.foo.");
        fail("Should have failed to parse");
    } catch (ParseException e) {
        assertTrue(e instanceof SpelParseException);
        SpelParseException spe = (SpelParseException) e;
        assertEquals(SpelMessage.OOD, spe.getMessageCode());
        assertEquals(16, spe.getPosition());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ParseException(org.springframework.expression.ParseException) Test(org.junit.Test)

Example 9 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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 10 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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

SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)210 Test (org.junit.Test)190 Expression (org.springframework.expression.Expression)172 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)151 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)101 ExpressionParser (org.springframework.expression.ExpressionParser)87 EvaluationContext (org.springframework.expression.EvaluationContext)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)16 List (java.util.List)14 EvaluationException (org.springframework.expression.EvaluationException)13 Map (java.util.Map)11 LinkedHashMap (java.util.LinkedHashMap)8 BigInteger (java.math.BigInteger)6 AccessException (org.springframework.expression.AccessException)6 CompositeStringExpression (org.springframework.expression.common.CompositeStringExpression)6 ParseException (org.springframework.expression.ParseException)5 BigDecimal (java.math.BigDecimal)4 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)4 TypedValue (org.springframework.expression.TypedValue)4