Package org.springframework.expression.spel.standard

Examples of org.springframework.expression.spel.standard.SpelExpressionParser.parseExpression()


  @Test
  public void testNestedExpressions() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    // treat the nested ${..} as a part of the expression
    Expression ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
    String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class);
    assertEquals("hello 4 world",s);

    // not a useful expression but tests nested expression syntax that clashes with template prefix/suffix
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
View Full Code Here


    Expression ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
    String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class);
    assertEquals("hello 4 world",s);

    // not a useful expression but tests nested expression syntax that clashes with template prefix/suffix
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
    assertEquals(CompositeStringExpression.class,ex.getClass());
    CompositeStringExpression cse = (CompositeStringExpression)ex;
    Expression[] exprs = cse.getExpressions();
    assertEquals(3,exprs.length);
    assertEquals("listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]",exprs[1].getExpressionString());
View Full Code Here

    assertEquals(3,exprs.length);
    assertEquals("listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]",exprs[1].getExpressionString());
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class);
    assertEquals("hello  world",s);

    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class);
    assertEquals("hello 4 10 world",s);

    try {
      ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
View Full Code Here

    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class);
    assertEquals("hello 4 10 world",s);

    try {
      ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
      fail("Should have failed");
    }
    catch (ParseException pe) {
      assertEquals("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world", pe.getSimpleMessage());
    }
View Full Code Here

    catch (ParseException pe) {
      assertEquals("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world", pe.getSimpleMessage());
    }

    try {
      ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
      fail("Should have failed");
    }
    catch (ParseException pe) {
      assertEquals("Found closing '}' at position 74 but most recent opening is '[' at position 30", pe.getSimpleMessage());
    }
View Full Code Here

  @Test
  public void indexerMapAccessor_12045() throws Exception {
    SpelParserConfiguration spc = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,this.getClass().getClassLoader());
    SpelExpressionParser sep = new SpelExpressionParser(spc);
    expression=sep.parseExpression("headers[command]");
    MyMessage root = new MyMessage();
    assertEquals("wibble",expression.getValue(root));
    // This next call was failing because the isCompilable check in Indexer did not check on the key being compilable
    // (and also generateCode in the Indexer was missing the optimization that it didn't need necessarily need to call
    // generateCode for that accessor)
View Full Code Here

    // generateCode for that accessor)
    assertEquals("wibble",expression.getValue(root));
    assertCanCompile(expression);

    // What about a map key that is an expression - ensure the getKey() is evaluated in the right scope
    expression=sep.parseExpression("headers[getKey()]");
    assertEquals("wobble",expression.getValue(root));
    assertEquals("wobble",expression.getValue(root));
   
    expression=sep.parseExpression("list[getKey2()]");
    assertEquals("wobble",expression.getValue(root));
View Full Code Here

    // What about a map key that is an expression - ensure the getKey() is evaluated in the right scope
    expression=sep.parseExpression("headers[getKey()]");
    assertEquals("wobble",expression.getValue(root));
    assertEquals("wobble",expression.getValue(root));
   
    expression=sep.parseExpression("list[getKey2()]");
    assertEquals("wobble",expression.getValue(root));
    assertEquals("wobble",expression.getValue(root));
   
    expression = sep.parseExpression("ia[getKey2()]");
    assertEquals(3,expression.getValue(root));
View Full Code Here

   
    expression=sep.parseExpression("list[getKey2()]");
    assertEquals("wobble",expression.getValue(root));
    assertEquals("wobble",expression.getValue(root));
   
    expression = sep.parseExpression("ia[getKey2()]");
    assertEquals(3,expression.getValue(root));
    assertEquals(3,expression.getValue(root));
  }

  // ---
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

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.