Search in sources :

Example 91 with SpelExpressionParser

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

the class SpelReproTests method SPR9486_floatDivideByFloat.

@Test
public void SPR9486_floatDivideByFloat() {
    Number expectedNumber = -10.21f / -10.2f;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("-10.21f / -10.2f");
    Number result = expression.getValue(context, null, Number.class);
    assertEquals(expectedNumber, result);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) 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 92 with SpelExpressionParser

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

the class SpelReproTests method SPR5899.

@Test
public void SPR5899() throws Exception {
    StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
    Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
    assertEquals(12, expr.getValue(eContext));
    expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
    assertEquals(null, expr.getValue(eContext));
    try {
        expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
        expr.getValue();
        fail("Should have failed to find a method to which it could pass null");
    } catch (EvaluationException see) {
    // success
    }
    eContext.setTypeLocator(new MyTypeLocator());
    // varargs
    expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
    assertEquals("ab", expr.getValue(eContext));
    // varargs 2 - null is packed into the varargs
    expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
    assertEquals("anullc", expr.getValue(eContext));
    // check we can find the ctor ok
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
    assertEquals("instance", expr.getValue(eContext));
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
    assertEquals("instance", expr.getValue(eContext));
    // ctor varargs
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
    assertEquals("instance", expr.getValue(eContext));
    // ctor varargs 2
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
    assertEquals("instance", expr.getValue(eContext));
}
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) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 93 with SpelExpressionParser

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

the class SpelReproTests method SPR10091_primitiveTestValueType.

@Test
public void SPR10091_primitiveTestValueType() {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new BooleanHolder());
    Class<?> valueType = parser.parseExpression("primitiveProperty").getValueType(evaluationContext);
    assertNotNull(valueType);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 94 with SpelExpressionParser

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

the class SpelReproTests method SPR10091_simpleTestValue.

@Test
public void SPR10091_simpleTestValue() {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new BooleanHolder());
    Object value = parser.parseExpression("simpleProperty").getValue(evaluationContext);
    assertNotNull(value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 95 with SpelExpressionParser

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

the class SpelReproTests method indexingAsAPropertyAccess_SPR6968_4.

/**
	 * Should be accessing (setting) Goo.wibble field because 'bar' variable evaluates to
	 * "wibble"
	 */
@Test
public void indexingAsAPropertyAccess_SPR6968_4() {
    Goo g = Goo.instance;
    StandardEvaluationContext eContext = new StandardEvaluationContext(g);
    eContext.setVariable("bar", "wibble");
    Expression expr = null;
    expr = new SpelExpressionParser().parseRaw("instance[#bar]='world'");
    // will access the field 'wibble' and not use a getter
    expr.getValue(eContext, String.class);
    assertEquals("world", g.wibble);
    // will be using the cached accessor this time
    expr.getValue(eContext, String.class);
    assertEquals("world", g.wibble);
}
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)

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