Package org.springframework.expression

Examples of org.springframework.expression.ExpressionParser.parseExpression()


  public static Boolean evaluateExpression(Object obj, String expression) {
    if (StringUtil.isEmpty(expression))
      return false;
    try {
      ExpressionParser parser = new SpelExpressionParser();
      Expression exp = parser.parseExpression(expression);
      return exp.getValue(obj, Boolean.class);    
    } catch (Exception e) {
      _log.debug("Failed to evaluate expression ["+expression+"] for object ["+obj.getClass()+"].");
      _log.debug(e.getMessage());
      return false;
View Full Code Here


  public void testCustomMapAccessor() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
    ctx.addPropertyAccessor(new MapAccessor());

    Expression expr = parser.parseExpression("testMap.monday");
    Object value = expr.getValue(ctx, String.class);
    assertEquals("montag", value);
  }

  @Test
View Full Code Here

  public void testVariableMapAccess() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
    ctx.setVariable("day", "saturday");

    Expression expr = parser.parseExpression("testMap[#day]");
    Object value = expr.getValue(ctx, String.class);
    assertEquals("samstag", value);
  }

  @Test
View Full Code Here

    props1.put("key3", "value3");

    Object bean = new TestBean("name1", new TestBean("name2", null, "Description 2", 15, props1), "description 1", 6, props1);

    ExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("testBean.properties['key2']");
    assertEquals("value2", expr.getValue(bean));
  }

  @Test
  public void testGetValueFromRootMap() {
View Full Code Here

  public void testGetValueFromRootMap() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key", "value");

    ExpressionParser spelExpressionParser = new SpelExpressionParser();
    Expression expr = spelExpressionParser.parseExpression("#root['key']");
    assertEquals("value", expr.getValue(map));
  }

  @Test
  public void testGetValuePerformance() throws Exception {
View Full Code Here

    Map<String, String> map = new HashMap<String, String>();
    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);
View Full Code Here

  @Test
  @SuppressWarnings("unchecked")
  public void selectionWithMap() {
    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(3, colorsMap.size());
    assertTrue(colorsMap.containsKey("beige"));
    assertTrue(colorsMap.containsKey("blue"));
View Full Code Here

  @SuppressWarnings("unchecked")
  public void selectFirstItemInMap() {
    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("beige", colorsMap.keySet().iterator().next());
  }
View Full Code Here

  @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());
  }
View Full Code Here

public class EvaluationTests extends AbstractExpressionTests {

  @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));
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.