Search in sources :

Example 81 with ExpressionParser

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

the class SpelReproTests method SPR9486_floatPowerFloat.

@Test
public void SPR9486_floatPowerFloat() {
    Number expectedResult = Math.pow(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(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 82 with ExpressionParser

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

the class SpelReproTests method propertyAccessorOrder_8211.

/**
	 * We add property accessors in the order:
	 * First, Second, Third, Fourth.
	 * They are not utilized in this order; preventing a priority or order of operations
	 * in evaluation of SPEL expressions for a given context.
	 */
@Test
public void propertyAccessorOrder_8211() {
    ExpressionParser expressionParser = new SpelExpressionParser();
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new ContextObject());
    evaluationContext.addPropertyAccessor(new TestPropertyAccessor("firstContext"));
    evaluationContext.addPropertyAccessor(new TestPropertyAccessor("secondContext"));
    evaluationContext.addPropertyAccessor(new TestPropertyAccessor("thirdContext"));
    evaluationContext.addPropertyAccessor(new TestPropertyAccessor("fourthContext"));
    assertEquals("first", expressionParser.parseExpression("shouldBeFirst").getValue(evaluationContext));
    assertEquals("second", expressionParser.parseExpression("shouldBeSecond").getValue(evaluationContext));
    assertEquals("third", expressionParser.parseExpression("shouldBeThird").getValue(evaluationContext));
    assertEquals("fourth", expressionParser.parseExpression("shouldBeFourth").getValue(evaluationContext));
}
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 83 with ExpressionParser

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

the class MapAccessTests method testGetValuePerformance.

@Test
public void testGetValuePerformance() throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    Map<String, String> map = new HashMap<>();
    map.put("key", "value");
    EvaluationContext context = new StandardEvaluationContext(map);
    ExpressionParser spelExpressionParser = new SpelExpressionParser();
    Expression expr = spelExpressionParser.parseExpression("#root['key']");
    StopWatch s = new StopWatch();
    s.start();
    for (int i = 0; i < 10000; i++) {
        expr.getValue(context);
    }
    s.stop();
    assertThat(s.getTotalTimeMillis(), lessThan(200L));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) HashMap(java.util.HashMap) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 84 with ExpressionParser

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

the class EvaluationTests method increment04.

@Test
public void increment04() {
    Integer i = 42;
    StandardEvaluationContext ctx = new StandardEvaluationContext(i);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    try {
        Expression e = parser.parseExpression("++1");
        e.getValue(ctx, Integer.class);
        fail();
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode());
    }
    try {
        Expression e = parser.parseExpression("1++");
        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 85 with ExpressionParser

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

the class EvaluationTests method testCreateListsOnAttemptToIndexNull01.

@Test
public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException {
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression expression = parser.parseExpression("list[0]");
    TestClass testClass = new TestClass();
    Object o = null;
    o = expression.getValue(new StandardEvaluationContext(testClass));
    assertEquals("", o);
    o = parser.parseExpression("list[3]").getValue(new StandardEvaluationContext(testClass));
    assertEquals("", o);
    assertEquals(4, testClass.list.size());
    try {
        o = parser.parseExpression("list2[3]").getValue(new StandardEvaluationContext(testClass));
        fail();
    } catch (EvaluationException ee) {
        ee.printStackTrace();
    // success!
    }
    o = parser.parseExpression("foo[3]").getValue(new StandardEvaluationContext(testClass));
    assertEquals("", o);
    assertEquals(4, testClass.getFoo().size());
}
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) EvaluationException(org.springframework.expression.EvaluationException) 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