Package org.springframework.expression.spel.standard

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


  };


  @Test
  public void testParsingSimpleTemplateExpression01() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    Object o = expr.getValue();
    assertEquals("hello world", o.toString());
  }
View Full Code Here


    assertEquals("hello world", o.toString());
  }

  @Test
  public void testParsingSimpleTemplateExpression02() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("hello ${'to'} you", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    Object o = expr.getValue();
    assertEquals("hello to you", o.toString());
  }
View Full Code Here

    assertEquals("hello to you", o.toString());
  }

  @Test
  public void testParsingSimpleTemplateExpression03() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("The quick ${'brown'} fox jumped over the ${'lazy'} dog",
        DEFAULT_TEMPLATE_PARSER_CONTEXT);
    Object o = expr.getValue();
    assertEquals("The quick brown fox jumped over the lazy dog", o.toString());
  }
View Full Code Here

    assertEquals("The quick brown fox jumped over the lazy dog", o.toString());
  }

  @Test
  public void testParsingSimpleTemplateExpression04() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    Object o = expr.getValue();
    assertEquals("hello world", o.toString());

    expr = parser.parseExpression("", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    o = expr.getValue();
    assertEquals("", o.toString());

    expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    o = expr.getValue();
    assertEquals("abc", o.toString());

    expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    o = expr.getValue((Object)null);
    assertEquals("abc", o.toString());
  }
View Full Code Here

    assertEquals("abc", o.toString());
  }

  @Test
  public void testCompositeStringExpression() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression ex = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    checkString("hello world", ex.getValue());
    checkString("hello world", ex.getValue(String.class));
    checkString("hello world", ex.getValue((Object)null, String.class));
    checkString("hello world", ex.getValue(new Rooty()));
    checkString("hello world", ex.getValue(new Rooty(), String.class));
View Full Code Here

  static class Rooty {}

  @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);
    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());
    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);
      fail("Should have failed");
    }
    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

    assertEquals(1.0f,expression.getValue());
  }

  @Test
  public void failsWhenSettingContextForExpression_SPR12326() {
      SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this
              .getClass().getClassLoader()));
      Person3 person = new Person3("foo", 1);
      SpelExpression expression = parser.parseRaw("#it?.age?.equals([0])");
      StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { 1 });
      context.setVariable("it", person);
      expression.setEvaluationContext(context);
      assertTrue(expression.getValue(Boolean.class));
      assertTrue(expression.getValue(Boolean.class));      
View Full Code Here

    assertTrue(expression.getValue(context, Boolean.class));
    assertCanCompile(expression);
    assertTrue(expression.getValue(context, Boolean.class));

    // Variant of above more like what was in the bug report:
    SpelExpressionParser parser = new SpelExpressionParser(
        new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
            this.getClass().getClassLoader()));

    SpelExpression ex = parser.parseRaw("#it?.age.equals([0])");
    context = new StandardEvaluationContext(new Object[] { person.getAge() });
    context.setVariable("it", person);
    assertTrue(ex.getValue(context, Boolean.class));
    assertTrue(ex.getValue(context, Boolean.class));
   
    PersonInOtherPackage person2 = new PersonInOtherPackage(1);
    ex = parser.parseRaw("#it?.age.equals([0])");
    context =
        new StandardEvaluationContext(new Object[] { person2.getAge() });
    context.setVariable("it", person2);
    assertTrue(ex.getValue(context, Boolean.class));
    assertTrue(ex.getValue(context, Boolean.class));
   
    ex = parser.parseRaw("#it?.age.equals([0])");
    context =
        new StandardEvaluationContext(new Object[] { person2.getAge() });
    context.setVariable("it", person2);
    assertTrue((Boolean)ex.getValue(context));
    assertTrue((Boolean)ex.getValue(context));
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)
    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));
    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
  public void testScenario_UsingStandardInfrastructure() {
    try {
      // Create a parser
      SpelExpressionParser parser = new SpelExpressionParser();
      // Parse an expression
      Expression expr = parser.parseRaw("new String('hello world')");
      // Evaluate it using a 'standard' context
      Object value = expr.getValue();
      // They are reusable
      value = expr.getValue();

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.