Search in sources :

Example 81 with Expression

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

the class MethodInvocationTests method testMethodOfClass.

@Test
public void testMethodOfClass() throws Exception {
    Expression expression = parser.parseExpression("getName()");
    Object value = expression.getValue(new StandardEvaluationContext(String.class));
    assertEquals(value, "java.lang.String");
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 82 with Expression

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

the class MethodInvocationTests method testMethodThrowingException_SPR6941_2.

@Test
public void testMethodThrowingException_SPR6941_2() {
    // Test method on inventor: throwException()
    // On 1 it will throw an IllegalArgumentException
    // On 2 it will throw a RuntimeException
    // On 3 it will exit normally
    // In each case it increments the Inventor field 'counter' when invoked
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("throwException(#bar)");
    eContext.setVariable("bar", 4);
    try {
        expr.getValue(eContext);
        fail();
    } catch (ExpressionInvocationTargetException ex) {
        Throwable cause = ex.getCause();
        assertEquals("org.springframework.expression.spel.testresources.Inventor$TestException", cause.getClass().getName());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionInvocationTargetException(org.springframework.expression.ExpressionInvocationTargetException) Test(org.junit.Test)

Example 83 with Expression

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

the class SelectionAndProjectionTests method projectionWithIterable.

@Test
public void projectionWithIterable() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testList", IntegerTestBean.createIterable());
    Object value = expression.getValue(context);
    assertTrue(value instanceof List);
    List<?> list = (List<?>) value;
    assertEquals(3, list.size());
    assertEquals(5, list.get(0));
    assertEquals(6, list.get(1));
    assertEquals(7, list.get(2));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 84 with Expression

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

the class SelectionAndProjectionTests method selectLastItemInMap.

@Test
@SuppressWarnings("unchecked")
public void selectLastItemInMap() {
    EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("colors.$[key.startsWith('b')]");
    Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
    assertEquals(1, colorsMap.size());
    assertEquals("brown", colorsMap.keySet().iterator().next());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) 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) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 85 with Expression

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

the class SelectionAndProjectionTests method projectionWithArray.

@Test
public void projectionWithArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testArray", IntegerTestBean.createArray());
    Object value = expression.getValue(context);
    assertTrue(value.getClass().isArray());
    TypedValue typedValue = new TypedValue(value);
    assertEquals(Number.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType());
    Number[] array = (Number[]) value;
    assertEquals(3, array.length);
    assertEquals(new Integer(5), array[0]);
    assertEquals(5.9f, array[1]);
    assertEquals(new Integer(7), array[2]);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Aggregations

Expression (org.springframework.expression.Expression)299 Test (org.junit.Test)228 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)159 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)114 ExpressionParser (org.springframework.expression.ExpressionParser)78 EvaluationContext (org.springframework.expression.EvaluationContext)53 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)19 CompoundExpression (org.springframework.expression.spel.ast.CompoundExpression)18 EvaluationException (org.springframework.expression.EvaluationException)17 Authentication (org.springframework.security.core.Authentication)16 Map (java.util.Map)14 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 List (java.util.List)13 LinkedHashMap (java.util.LinkedHashMap)11 ParseException (org.springframework.expression.ParseException)11 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9