Search in sources :

Example 61 with SpelExpressionParser

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

the class SpelReproTests method beanResolution.

// bean resolver tests
@Test
public void beanResolution() {
    StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
    Expression expr = null;
    // no resolver registered == exception
    try {
        expr = new SpelExpressionParser().parseRaw("@foo");
        assertEquals("custard", expr.getValue(eContext, String.class));
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.NO_BEAN_RESOLVER_REGISTERED, see.getMessageCode());
        assertEquals("foo", see.getInserts()[0]);
    }
    eContext.setBeanResolver(new MyBeanResolver());
    // bean exists
    expr = new SpelExpressionParser().parseRaw("@foo");
    assertEquals("custard", expr.getValue(eContext, String.class));
    // bean does not exist
    expr = new SpelExpressionParser().parseRaw("@bar");
    assertEquals(null, expr.getValue(eContext, String.class));
    // bean name will cause AccessException
    expr = new SpelExpressionParser().parseRaw("@goo");
    try {
        assertEquals(null, expr.getValue(eContext, String.class));
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION, see.getMessageCode());
        assertEquals("goo", see.getInserts()[0]);
        assertTrue(see.getCause() instanceof AccessException);
        assertTrue(see.getCause().getMessage().startsWith("DONT"));
    }
    // bean exists
    expr = new SpelExpressionParser().parseRaw("@'foo.bar'");
    assertEquals("trouble", expr.getValue(eContext, String.class));
    // bean exists
    try {
        expr = new SpelExpressionParser().parseRaw("@378");
        assertEquals("trouble", expr.getValue(eContext, String.class));
    } catch (SpelParseException spe) {
        assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) AccessException(org.springframework.expression.AccessException) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 62 with SpelExpressionParser

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

the class SpelReproTests method reservedWordProperties_9862.

@Test
public void reservedWordProperties_9862() throws Exception {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
    Object value = expression.getValue(ctx);
    assertEquals(value, Reserver.CONST);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 63 with SpelExpressionParser

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

the class SpelReproTests method SPR5847.

@Test
public void SPR5847() throws Exception {
    StandardEvaluationContext eContext = new StandardEvaluationContext(new TestProperties());
    String name = null;
    Expression expr = null;
    expr = new SpelExpressionParser().parseRaw("jdbcProperties['username']");
    name = expr.getValue(eContext, String.class);
    assertEquals("Dave", name);
    expr = new SpelExpressionParser().parseRaw("jdbcProperties[username]");
    name = expr.getValue(eContext, String.class);
    assertEquals("Dave", name);
    // MapAccessor required for this to work
    expr = new SpelExpressionParser().parseRaw("jdbcProperties.username");
    eContext.addPropertyAccessor(new MapAccessor());
    name = expr.getValue(eContext, String.class);
    assertEquals("Dave", name);
    // --- dotted property names
    // lookup foo on the root, then bar on that, then use that as the key into
    // jdbcProperties
    expr = new SpelExpressionParser().parseRaw("jdbcProperties[foo.bar]");
    eContext.addPropertyAccessor(new MapAccessor());
    name = expr.getValue(eContext, String.class);
    assertEquals("Dave2", name);
    // key is foo.bar
    expr = new SpelExpressionParser().parseRaw("jdbcProperties['foo.bar']");
    eContext.addPropertyAccessor(new MapAccessor());
    name = expr.getValue(eContext, String.class);
    assertEquals("Elephant", name);
}
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 64 with SpelExpressionParser

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

the class SpelReproTests method SPR9495.

@Test
public void SPR9495() throws Exception {
    SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
    ExpressionParser parser = new SpelExpressionParser(configuration);
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression spel = parser.parseExpression("#enumType.values()");
    context.setVariable("enumType", ABC.class);
    Object result = spel.getValue(context);
    assertNotNull(result);
    assertTrue(result.getClass().isArray());
    assertEquals(ABC.A, Array.get(result, 0));
    assertEquals(ABC.B, Array.get(result, 1));
    assertEquals(ABC.C, Array.get(result, 2));
    context.addMethodResolver(new MethodResolver() {

        @Override
        public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
            return new MethodExecutor() {

                @Override
                public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
                    try {
                        Method method = XYZ.class.getMethod("values");
                        Object value = method.invoke(target, arguments);
                        return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
                    } catch (Exception ex) {
                        throw new AccessException(ex.getMessage(), ex);
                    }
                }
            };
        }
    });
    result = spel.getValue(context);
    assertNotNull(result);
    assertTrue(result.getClass().isArray());
    assertEquals(XYZ.X, Array.get(result, 0));
    assertEquals(XYZ.Y, Array.get(result, 1));
    assertEquals(XYZ.Z, Array.get(result, 2));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodResolver(org.springframework.expression.MethodResolver) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) Method(java.lang.reflect.Method) ExpressionException(org.springframework.expression.ExpressionException) EvaluationException(org.springframework.expression.EvaluationException) ExpectedException(org.junit.rules.ExpectedException) AccessException(org.springframework.expression.AccessException) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) MethodExecutor(org.springframework.expression.MethodExecutor) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodParameter(org.springframework.core.MethodParameter) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 65 with SpelExpressionParser

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

the class SpelReproTests method SPR9486_floatNotEqDouble.

@Test
public void SPR9486_floatNotEqDouble() {
    Boolean expectedResult = 10.215f != 10.2109;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("10.215f != 10.2109");
    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)

Aggregations

SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)218 Test (org.junit.Test)190 Expression (org.springframework.expression.Expression)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)101 ExpressionParser (org.springframework.expression.ExpressionParser)89 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 TypedValue (org.springframework.expression.TypedValue)4 TreeMap (java.util.TreeMap)3