Package org.springframework.expression

Examples of org.springframework.expression.Expression


    evaluateAndCheckError("new Integer[3]{'3','ghi','5'}", SpelMessage.INCORRECT_ELEMENT_TYPE_FOR_ARRAY, 4);
  }

  private String evaluateArrayBuildingExpression(String expression, String expectedToString) {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression e = parser.parseExpression(expression);
    Object o = e.getValue();
    assertNotNull(o);
    assertTrue(o.getClass().isArray());
    StringBuilder s = new StringBuilder();
    s.append('[');
    if (o instanceof int[]) {
View Full Code Here


    // On 2 it will throw a RuntimeException
    // On 3 it will exit normally
    // In each case it increments the Tester field 'counter' when invoked

    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("new org.springframework.expression.spel.ConstructorInvocationTests$Tester(#bar).i");

    // Normal exit
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    eContext.setRootObject(new Tester());
    eContext.setVariable("bar", 3);
    Object o = expr.getValue(eContext);
    assertEquals(o, 3);
    assertEquals(1, parser.parseExpression("counter").getValue(eContext));

    // Now the expression has cached that throwException(int) is the right thing to
    // call. Let's change 'bar' to be a PlaceOfBirth which indicates the cached
    // reference is out of date.
    eContext.setVariable("bar", new PlaceOfBirth("London"));
    o = expr.getValue(eContext);
    assertEquals(0, o);
    // That confirms the logic to mark the cached reference stale and retry is working

    // Now let's cause the method to exit via exception and ensure it doesn't cause
    // a retry.

    // First, switch back to throwException(int)
    eContext.setVariable("bar", 3);
    o = expr.getValue(eContext);
    assertEquals(3, o);
    assertEquals(2, parser.parseExpression("counter").getValue(eContext));

    // 4 will make it throw a checked exception - this will be wrapped by spel on the
    // way out
    eContext.setVariable("bar", 4);
    try {
      o = expr.getValue(eContext);
      fail("Should have failed");
    }
    catch (Exception e) {
      // A problem occurred whilst attempting to construct an object of type
      // 'org.springframework.expression.spel.ConstructorInvocationTests$Tester'
      // using arguments '(java.lang.Integer)'
      int idx = e.getMessage().indexOf("Tester");
      if (idx == -1) {
        fail("Expected reference to Tester in :" + e.getMessage());
      }
      // normal
    }
    // If counter is 4 then the method got called twice!
    assertEquals(3, parser.parseExpression("counter").getValue(eContext));

    // 1 will make it throw a RuntimeException - SpEL will let this through
    eContext.setVariable("bar", 1);
    try {
      o = expr.getValue(eContext);
      fail("Should have failed");
    }
    catch (Exception e) {
      // A problem occurred whilst attempting to construct an object of type
      // 'org.springframework.expression.spel.ConstructorInvocationTests$Tester'
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);
  }
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);
  }
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));
  }
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));
  }
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);
    }
    s.stop();
    assertThat(s.getTotalTimeMillis(), lessThan(200L));
  }
View Full Code Here


  @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

  }

  @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

  }

  @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

TOP

Related Classes of org.springframework.expression.Expression

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.