Package org.springframework.expression.spel.support

Examples of org.springframework.expression.spel.support.StandardEvaluationContext


    Properties props = new Properties();
    props.setProperty("x", "1");
    props.setProperty("y", "2");
    props.setProperty("z", "3");
    Expression expression = parser.parseExpression("foo(#props)");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("props", props);
    String result = expression.getValue(context, new TestBean(), String.class);
    assertEquals("123", result);
  }
View Full Code Here


    Map<String, Object> map = new HashMap<String, Object>();
    map.put("x", "1");
    map.put("y", "2");
    map.put("z", "3");
    Expression expression = parser.parseExpression("foo(#props)");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("props", map);
    String result = expression.getValue(context, new TestBean(), String.class);
    assertEquals("123", result);
  }
View Full Code Here

    map.put("x", "1");
    map.put("y", 2);
    map.put("z", "3");
    map.put("a", new UUID(1, 1));
    Expression expression = parser.parseExpression("foo(#props)");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("props", map);
    String result = expression.getValue(context, new TestBean(), String.class);
    assertEquals("1null3", result);
  }
View Full Code Here

    CustomMap map = new CustomMap();
    map.put("x", "1");
    map.put("y", 2);
    map.put("z", "3");
    Expression expression = parser.parseExpression("foo(#props)");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("props", map);
    String result = expression.getValue(context, new TestBean(), String.class);
    assertEquals("1null3", result);
  }
View Full Code Here

    assertCanCompile(expression);
    String resultC = expression.getValue(new TestClass1(),String.class);
    assertEquals("abcde",resultC);
    assertEquals("abcde",expression.getValue(String.class));
    assertEquals("abcde",expression.getValue());
    assertEquals("abcde",expression.getValue(new StandardEvaluationContext()));
    expression = parser.parseExpression("\"abcde\"");
    assertCanCompile(expression);
    assertEquals("abcde",expression.getValue(String.class));
  }
View Full Code Here

    return buf.toString();
  }
 
  @Test
  public void compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs() throws Exception {
    StandardEvaluationContext context = null;

    // Here the target method takes Object... and we are passing a string
    expression = parser.parseExpression("#doFormat('hey %s', 'there')");
    context = new StandardEvaluationContext();
    context.registerFunction("doFormat",
        DelegatingStringFormat.class.getDeclaredMethod("format", String.class,
            Object[].class));
    ((SpelExpression) expression).setEvaluationContext(context);

    assertEquals("hey there", expression.getValue(String.class));
    assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
    assertCanCompile(expression);
    assertEquals("hey there", expression.getValue(String.class));

    expression = parser.parseExpression("#doFormat([0], 'there')");
    context = new StandardEvaluationContext(new Object[] { "hey %s" });
    context.registerFunction("doFormat",
        DelegatingStringFormat.class.getDeclaredMethod("format", String.class,
            Object[].class));
    ((SpelExpression) expression).setEvaluationContext(context);

    assertEquals("hey there", expression.getValue(String.class));
    assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
    assertCanCompile(expression);
    assertEquals("hey there", expression.getValue(String.class));

    expression = parser.parseExpression("#doFormat([0], #arg)");
    context = new StandardEvaluationContext(new Object[] { "hey %s" });
    context.registerFunction("doFormat",
        DelegatingStringFormat.class.getDeclaredMethod("format", String.class,
            Object[].class));
    context.setVariable("arg", "there");
    ((SpelExpression) expression).setEvaluationContext(context);

    assertEquals("hey there", expression.getValue(String.class));
    assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
    assertCanCompile(expression);
View Full Code Here

    assertEquals("hey there", expression.getValue(String.class));
  }
  
  @Test
  public void functionReference() throws Exception {
    EvaluationContext ctx = new StandardEvaluationContext();
    Method m = this.getClass().getDeclaredMethod("concat",String.class,String.class);
    ctx.setVariable("concat",m);
   
    expression = parser.parseExpression("#concat('a','b')");
    assertEquals("ab",expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals("ab",expression.getValue(ctx));
   
    expression = parser.parseExpression("#concat(#concat('a','b'),'c').charAt(1)");
    assertEquals('b',expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals('b',expression.getValue(ctx));
   
    expression = parser.parseExpression("#concat(#a,#b)");
    ctx.setVariable("a", "foo");
    ctx.setVariable("b", "bar");
    assertEquals("foobar",expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals("foobar",expression.getValue(ctx));
    ctx.setVariable("b", "boo");
    assertEquals("fooboo",expression.getValue(ctx));
   
    m = Math.class.getDeclaredMethod("pow",Double.TYPE,Double.TYPE);
    ctx.setVariable("kapow",m);
    expression = parser.parseExpression("#kapow(2.0d,2.0d)");
    assertEquals("4.0",expression.getValue(ctx).toString());
    assertCanCompile(expression);
    assertEquals("4.0",expression.getValue(ctx).toString());
  }
View Full Code Here

  }

  // Confirms visibility of what is being called.
  @Test
  public void functionReferenceVisibility_SPR12359() throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext(new  Object[] { "1" });
    context.registerFunction("doCompare", SomeCompareMethod.class.getDeclaredMethod(
        "compare", Object.class, Object.class));
    context.setVariable("arg", "2");
    // type nor method are public
    expression = parser.parseExpression("#doCompare([0],#arg)");
    assertEquals("-1",expression.getValue(context, Integer.class).toString());
    assertCantCompile(expression);
   
    // type not public but method is
    context = new StandardEvaluationContext(new  Object[] { "1" });
    context.registerFunction("doCompare", SomeCompareMethod.class.getDeclaredMethod(
        "compare2", Object.class, Object.class));
    context.setVariable("arg", "2");
    expression = parser.parseExpression("#doCompare([0],#arg)");
    assertEquals("-1",expression.getValue(context, Integer.class).toString());
    assertCantCompile(expression);
  }
View Full Code Here

    assertCantCompile(expression);
  }
 
  @Test
  public void functionReferenceNonCompilableArguments_SPR12359() throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext(new  Object[] { "1" });
    context.registerFunction("negate", SomeCompareMethod2.class.getDeclaredMethod(
        "negate", Integer.TYPE));
    context.setVariable("arg", "2");
    int[] ints = new int[]{1,2,3};
    context.setVariable("ints",ints);

    expression = parser.parseExpression("#negate(#ints.?[#this<2][0])");
    assertEquals("-1",expression.getValue(context, Integer.class).toString());
    // Selection isn't compilable.
    assertFalse(((SpelNodeImpl)((SpelExpression)expression).getAST()).isCompilable());
View Full Code Here

    assertFalse(((SpelNodeImpl)((SpelExpression)expression).getAST()).isCompilable());
  }
 
  @Test 
  public void functionReferenceVarargs_SPR12359() throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.registerFunction("append",
        SomeCompareMethod2.class.getDeclaredMethod("append", String[].class));
    context.registerFunction("append2",
        SomeCompareMethod2.class.getDeclaredMethod("append2", Object[].class));
    context.registerFunction("append3",
        SomeCompareMethod2.class.getDeclaredMethod("append3", String[].class));
    context.registerFunction("append4",
        SomeCompareMethod2.class.getDeclaredMethod("append4", String.class, String[].class));
    context.registerFunction("appendChar",
        SomeCompareMethod2.class.getDeclaredMethod("appendChar", char[].class));
    context.registerFunction("sum",
        SomeCompareMethod2.class.getDeclaredMethod("sum", int[].class));
    context.registerFunction("sumDouble",
        SomeCompareMethod2.class.getDeclaredMethod("sumDouble", double[].class));
    context.registerFunction("sumFloat",
        SomeCompareMethod2.class.getDeclaredMethod("sumFloat", float[].class));
    context.setVariable("stringArray", new String[]{"x","y","z"});
    context.setVariable("intArray", new int[]{5,6,9});
    context.setVariable("doubleArray", new double[]{5.0d,6.0d,9.0d});
    context.setVariable("floatArray", new float[]{5.0f,6.0f,9.0f});

    expression = parser.parseExpression("#append('a','b','c')");
    assertEquals("abc",expression.getValue(context).toString());
    assertTrue(((SpelNodeImpl)((SpelExpression)expression).getAST()).isCompilable());
    assertCanCompile(expression);
View Full Code Here

TOP

Related Classes of org.springframework.expression.spel.support.StandardEvaluationContext

Copyright © 2018 www.massapicom. 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.