Package org.springframework.expression.spel.standard

Examples of org.springframework.expression.spel.standard.SpelExpressionParser


   * Scenario: using the standard context but adding your own variables
   */
  @Test
  public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception {
    // Create a parser
    SpelExpressionParser parser = new SpelExpressionParser();
    // Use the standard evaluation context
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("favouriteColour","blue");
    List<Integer> primes = new ArrayList<Integer>();
    primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
    ctx.setVariable("primes",primes);

    Expression expr = parser.parseRaw("#favouriteColour");
    Object value = expr.getValue(ctx);
    assertEquals("blue", value);

    expr = parser.parseRaw("#primes.get(1)");
    value = expr.getValue(ctx);
    assertEquals(3, value);

    // all prime numbers > 10 from the list (using selection ?{...})
    expr = parser.parseRaw("#primes.?[#this>10]");
    value = expr.getValue(ctx);
    assertEquals("[11, 13, 17]", value.toString());
  }
View Full Code Here


   * Scenario: using your own root context object
   */
  @Test
  public void testScenario_UsingADifferentRootContextObject() throws Exception {
    // Create a parser
    SpelExpressionParser parser = new SpelExpressionParser();
    // Use the standard evaluation context
    StandardEvaluationContext ctx = new StandardEvaluationContext();

    TestClass tc = new TestClass();
    tc.setProperty(42);
    tc.str = "wibble";
    ctx.setRootObject(tc);

    // read it, set it, read it again
    Expression expr = parser.parseRaw("str");
    Object value = expr.getValue(ctx);
    assertEquals("wibble", value);
    expr = parser.parseRaw("str");
    expr.setValue(ctx, "wobble");
    expr = parser.parseRaw("str");
    value = expr.getValue(ctx);
    assertEquals("wobble", value);
    // or using assignment within the expression
    expr = parser.parseRaw("str='wabble'");
    value = expr.getValue(ctx);
    expr = parser.parseRaw("str");
    value = expr.getValue(ctx);
    assertEquals("wabble", value);

    // private property will be accessed through getter()
    expr = parser.parseRaw("property");
    value = expr.getValue(ctx);
    assertEquals(42, value);

    // ... and set through setter
    expr = parser.parseRaw("property=4");
    value = expr.getValue(ctx);
    expr = parser.parseRaw("property");
    value = expr.getValue(ctx);
    assertEquals(4,value);
  }
View Full Code Here

   */
  @Test
  public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException {
    try {
      // Create a parser
      SpelExpressionParser parser = new SpelExpressionParser();
      // Use the standard evaluation context
      StandardEvaluationContext ctx = new StandardEvaluationContext();
      ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class));

      Expression expr = parser.parseRaw("#repeat('hello')");
      Object value = expr.getValue(ctx);
      assertEquals("hellohello", value);

    } catch (EvaluationException ee) {
      ee.printStackTrace();
View Full Code Here

   * Scenario: add a property resolver that will get called in the resolver chain, this one only supports reading.
   */
  @Test
  public void testScenario_AddingYourOwnPropertyResolvers_1() throws Exception {
    // Create a parser
    SpelExpressionParser parser = new SpelExpressionParser();
    // Use the standard evaluation context
    StandardEvaluationContext ctx = new StandardEvaluationContext();

    ctx.addPropertyAccessor(new FruitColourAccessor());
    Expression expr = parser.parseRaw("orange");
    Object value = expr.getValue(ctx);
    assertEquals(Color.orange, value);

    try {
      expr.setValue(ctx, Color.blue);
View Full Code Here

  }

  @Test
  public void testScenario_AddingYourOwnPropertyResolvers_2() throws Exception {
    // Create a parser
    SpelExpressionParser parser = new SpelExpressionParser();
    // Use the standard evaluation context
    StandardEvaluationContext ctx = new StandardEvaluationContext();

    ctx.addPropertyAccessor(new VegetableColourAccessor());
    Expression expr = parser.parseRaw("pea");
    Object value = expr.getValue(ctx);
    assertEquals(Color.green, value);

    try {
      expr.setValue(ctx, Color.blue);
View Full Code Here

*/
public class SelectionAndProjectionTests {

  @Test
  public void selectionWithList() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof List);
    List<?> list = (List<?>) value;
    assertEquals(5, list.size());
View Full Code Here

    assertEquals(4, list.get(4));
  }

  @Test
  public void selectFirstItemInList() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof Integer);
    assertEquals(0, value);
  }
View Full Code Here

    assertEquals(0, value);
  }

  @Test
  public void selectLastItemInList() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof Integer);
    assertEquals(4, value);
  }
View Full Code Here

    assertEquals(4, value);
  }

  @Test
  public void selectionWithSet() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof List);
    List<?> list = (List<?>) value;
    assertEquals(5, list.size());
View Full Code Here

    assertEquals(4, list.get(4));
  }

  @Test
  public void selectFirstItemInSet() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
    Object value = expression.getValue(context);
    assertTrue(value instanceof Integer);
    assertEquals(0, value);
  }
View Full Code Here

TOP

Related Classes of org.springframework.expression.spel.standard.SpelExpressionParser

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.