Package org.springframework.expression.spel.support

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


    assertCanCompile(expression);
    assertEquals("1", expression.getValue());

    expression = parser.parseExpression("#it?.age.equals([0])");
    Person person = new Person(1);
    StandardEvaluationContext context =
        new StandardEvaluationContext(new Object[] { person.getAge() });
    context.setVariable("it", person);
    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


    assertEquals("value4",expression.getValue(tc));
  }

  @Test
  public void propertyReferenceVisibility() { // SPR-12771
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("httpServletRequest", HttpServlet3RequestFactory.getOne());
    // Without a fix compilation was inserting a checkcast to a private type
    expression = parser.parseExpression("#httpServletRequest.servletPath");
    assertEquals("wibble",expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals("wibble",expression.getValue(ctx));
View Full Code Here

    assertEquals("c",stringify(expression.getValue(mapToLists)));
    assertEquals("Ljava/lang/Object",getAst().getExitDescriptor());
   
    // Map to array
    Map<String,int[]> mapToIntArray = new HashMap<String,int[]>();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new CompilableMapAccessor());
    mapToIntArray.put("foo",new int[]{1,2,3});
    expression = parser.parseExpression("['foo']");
    assertEquals("1 2 3",stringify(expression.getValue(mapToIntArray)));
    assertCanCompile(expression);
    assertEquals("Ljava/lang/Object",getAst().getExitDescriptor());
View Full Code Here

  }

  @Test
  public void variantGetter() throws Exception {
    Payload2Holder holder = new Payload2Holder();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new MyAccessor());
    expression = parser.parseExpression("payload2.var1");
    Object v = expression.getValue(ctx,holder);
    assertEquals("abc",v);
   
//    // time it interpreted
View Full Code Here

    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) {
View Full Code Here

    assertEquals(4, parser.parseExpression("counter").getValue(eContext));
  }

  @Test
  public void testAddingConstructorResolvers() {
    StandardEvaluationContext ctx = new StandardEvaluationContext();

    // reflective constructor accessor is the only one by default
    List<ConstructorResolver> constructorResolvers = ctx.getConstructorResolvers();
    assertEquals(1, constructorResolvers.size());

    ConstructorResolver dummy = new DummyConstructorResolver();
    ctx.addConstructorResolver(dummy);
    assertEquals(2, ctx.getConstructorResolvers().size());

    List<ConstructorResolver> copy = new ArrayList<ConstructorResolver>();
    copy.addAll(ctx.getConstructorResolvers());
    assertTrue(ctx.removeConstructorResolver(dummy));
    assertFalse(ctx.removeConstructorResolver(dummy));
    assertEquals(1, ctx.getConstructorResolvers().size());

    ctx.setConstructorResolvers(copy);
    assertEquals(2, ctx.getConstructorResolvers().size());
  }
View Full Code Here

  }

  @Test
  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

  }

  @Test
  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

  @Test
  public void testGetValuePerformance() throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    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();
View Full Code Here

    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));

    EvaluationContext ctx = new StandardEvaluationContext();
    checkString("hello world", ex.getValue(ctx));
    checkString("hello world", ex.getValue(ctx, String.class));
    checkString("hello world", ex.getValue(ctx, null, String.class));
    checkString("hello world", ex.getValue(ctx, new Rooty()));
    checkString("hello world", ex.getValue(ctx, new Rooty(), String.class));
    checkString("hello world", ex.getValue(ctx, new Rooty(), String.class));
    assertEquals("hello ${'world'}", ex.getExpressionString());
    assertFalse(ex.isWritable(new StandardEvaluationContext()));
    assertFalse(ex.isWritable(new Rooty()));
    assertFalse(ex.isWritable(new StandardEvaluationContext(), new Rooty()));

    assertEquals(String.class,ex.getValueType());
    assertEquals(String.class,ex.getValueType(ctx));
    assertEquals(String.class,ex.getValueTypeDescriptor().getType());
    assertEquals(String.class,ex.getValueTypeDescriptor(ctx).getType());
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.