Search in sources :

Example 76 with ExpressionParser

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

the class SpelReproTests method SPR9486_floatGreaterThanOrEqualFloat.

@Test
public void SPR9486_floatGreaterThanOrEqualFloat() {
    Boolean expectedNumber = -10.21f >= -10.2f;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("-10.21f >= -10.2f");
    Boolean result = expression.getValue(context, null, Boolean.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 77 with ExpressionParser

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

the class SpelReproTests method SPR9486_floatEqFloatUnaryMinus.

@Test
public void SPR9486_floatEqFloatUnaryMinus() {
    Boolean expectedResult = -10.21f == -10.2f;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("-10.21f == -10.2f");
    Boolean result = expression.getValue(context, null, Boolean.class);
    assertEquals(expectedResult, 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 78 with ExpressionParser

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

the class SpelDocumentationTests method testSpecialVariables.

@SuppressWarnings("unchecked")
@Test
public void testSpecialVariables() throws Exception {
    // create an array of integers
    List<Integer> primes = new ArrayList<>();
    primes.addAll(Arrays.asList(2, 3, 5, 7, 11, 13, 17));
    // create parser and set variable 'primes' as the array of integers
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("primes", primes);
    // all prime numbers > 10 from the list (using selection ?{...})
    List<Integer> primesGreaterThanTen = (List<Integer>) parser.parseExpression("#primes.?[#this>10]").getValue(context);
    assertEquals("[11, 13, 17]", primesGreaterThanTen.toString());
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ArrayList(java.util.ArrayList) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 79 with ExpressionParser

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

the class SpelDocumentationTests method testFunctions.

// 7.5.9
@Test
public void testFunctions() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.registerFunction("reverseString", StringUtils.class.getDeclaredMethod("reverseString", new Class[] { String.class }));
    String helloWorldReversed = parser.parseExpression("#reverseString('hello world')").getValue(context, String.class);
    assertEquals("dlrow olleh", helloWorldReversed);
}
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 80 with ExpressionParser

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

the class SpelDocumentationTests method testRootObject.

@Test
public void testRootObject() throws Exception {
    GregorianCalendar c = new GregorianCalendar();
    c.set(1856, 7, 9);
    //  The constructor arguments are name, birthday, and nationaltiy.
    Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("name");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(tesla);
    String name = (String) exp.getValue(context);
    assertEquals("Nikola Tesla", name);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) GregorianCalendar(java.util.GregorianCalendar) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Inventor(org.springframework.expression.spel.testresources.Inventor) Test(org.junit.Test)

Aggregations

ExpressionParser (org.springframework.expression.ExpressionParser)92 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)89 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)81 Expression (org.springframework.expression.Expression)78 Test (org.junit.Test)76 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)51 EvaluationContext (org.springframework.expression.EvaluationContext)10 BigInteger (java.math.BigInteger)6 Map (java.util.Map)5 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 ParseException (org.springframework.expression.ParseException)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 SyslogRuntimeException (org.graylog2.syslog4j.SyslogRuntimeException)2 EvaluationException (org.springframework.expression.EvaluationException)2