Search in sources :

Example 1 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project pinpoint by naver.

the class HbaseApiMetaDataDaoTest method getApiMetaDataCachable.

@Test
public void getApiMetaDataCachable() {
    // cacheable key - spring expression language
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("agentId", "foo");
    context.setVariable("time", (long) 1);
    context.setVariable("apiId", (int) 2);
    String key = (String) parser.parseExpression(HbaseApiMetaDataDao.SPEL_KEY).getValue(context);
    assertEquals("foo.1.2", key);
}
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 2 with SpelExpressionParser

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

the class BindingPreparationTests method testExpressionLists.

@Test
@Ignore("Work in progress")
public void testExpressionLists() throws Exception {
    TargetWithNestedMapOfListOfString target = new TargetWithNestedMapOfListOfString();
    LinkedHashMap<String, List<String>> map = new LinkedHashMap<>();
    // map.put("foo", Arrays.asList("bar"));
    target.setNested(map);
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext(target);
    context.addPropertyAccessor(new MapAccessor());
    Expression expression = parser.parseExpression("nested.foo");
    assertThat(expression.getValue(context)).isNotNull();
}
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) MapAccessor(org.springframework.context.expression.MapAccessor) LinkedHashMap(java.util.LinkedHashMap) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with SpelExpressionParser

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

the class ExpressionEvaluatorTests method unavailableReturnValue.

@Test
public void unavailableReturnValue() throws Exception {
    EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE);
    try {
        new SpelExpressionParser().parseExpression("#result").getValue(context);
        fail("Should have failed to parse expression, result not available");
    } catch (VariableNotAvailableException e) {
        assertEquals("wrong variable name", "result", e.getName());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Example 4 with SpelExpressionParser

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

the class MapAccessorTests method mapAccessorCompilable.

@Test
public void mapAccessorCompilable() {
    Map<String, Object> testMap = getSimpleTestMap();
    StandardEvaluationContext sec = new StandardEvaluationContext();
    sec.addPropertyAccessor(new MapAccessor());
    SpelExpressionParser sep = new SpelExpressionParser();
    // basic
    Expression ex = sep.parseExpression("foo");
    assertEquals("bar", ex.getValue(sec, testMap));
    assertTrue(SpelCompiler.compile(ex));
    assertEquals("bar", ex.getValue(sec, testMap));
    // compound expression
    ex = sep.parseExpression("foo.toUpperCase()");
    assertEquals("BAR", ex.getValue(sec, testMap));
    assertTrue(SpelCompiler.compile(ex));
    assertEquals("BAR", ex.getValue(sec, testMap));
    // nested map
    Map<String, Map<String, Object>> nestedMap = getNestedTestMap();
    ex = sep.parseExpression("aaa.foo.toUpperCase()");
    assertEquals("BAR", ex.getValue(sec, nestedMap));
    assertTrue(SpelCompiler.compile(ex));
    assertEquals("BAR", ex.getValue(sec, nestedMap));
    // avoiding inserting checkcast because first part of expression returns a Map
    ex = sep.parseExpression("getMap().foo");
    MapGetter mapGetter = new MapGetter();
    assertEquals("bar", ex.getValue(sec, mapGetter));
    assertTrue(SpelCompiler.compile(ex));
    assertEquals("bar", ex.getValue(sec, mapGetter));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with SpelExpressionParser

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

the class ArrayConstructorTests method evaluateArrayBuildingExpression.

private String evaluateArrayBuildingExpression(String expression, String expectedToString) {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression e = parser.parseExpression(expression);
    Object o = e.getValue();
    assertNotNull(o);
    assertTrue(o.getClass().isArray());
    StringBuilder s = new StringBuilder();
    s.append('[');
    if (o instanceof int[]) {
        int[] array = (int[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof boolean[]) {
        boolean[] array = (boolean[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof char[]) {
        char[] array = (char[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof long[]) {
        long[] array = (long[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof short[]) {
        short[] array = (short[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof double[]) {
        double[] array = (double[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof float[]) {
        float[] array = (float[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof byte[]) {
        byte[] array = (byte[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else {
        fail("Not supported " + o.getClass());
    }
    s.append(']');
    assertEquals(expectedToString, s.toString());
    return s.toString();
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression)

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