@Test
 public void withUrlExpression() {
   HttpRequestExecutingMessageHandler handler =
       (HttpRequestExecutingMessageHandler)
           new DirectFieldAccessor(this.withUrlExpressionEndpoint).getPropertyValue("handler");
   MessageChannel requestChannel =
       (MessageChannel)
           new DirectFieldAccessor(this.withUrlExpressionEndpoint)
               .getPropertyValue("inputChannel");
   assertEquals(this.applicationContext.getBean("requests"), requestChannel);
   DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
   Object replyChannel = handlerAccessor.getPropertyValue("outputChannel");
   assertNull(replyChannel);
   DirectFieldAccessor templateAccessor =
       new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate"));
   ClientHttpRequestFactory requestFactory =
       (ClientHttpRequestFactory) templateAccessor.getPropertyValue("requestFactory");
   assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
   SpelExpression expression = (SpelExpression) handlerAccessor.getPropertyValue("uriExpression");
   assertNotNull(expression);
   assertEquals("'http://localhost/test1'", expression.getExpressionString());
   assertEquals(
       HttpMethod.POST.name(),
       TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class)
           .getExpressionString());
   assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
   assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
   assertEquals(false, handlerAccessor.getPropertyValue("transferCookies"));
 }
예제 #2
0
  @Test
  public void testPlus() throws Exception {
    evaluate("7 + 2", "9", Integer.class);
    evaluate("3.0f + 5.0f", 8.0f, Float.class);
    evaluate("3.0d + 5.0d", 8.0d, Double.class);

    evaluate("'ab' + 2", "ab2", String.class);
    evaluate("2 + 'a'", "2a", String.class);
    evaluate("'ab' + null", "abnull", String.class);
    evaluate("null + 'ab'", "nullab", String.class);

    // AST:
    SpelExpression expr = (SpelExpression) parser.parseExpression("+3");
    Assert.assertEquals("+3", expr.toStringAST());
    expr = (SpelExpression) parser.parseExpression("2+3");
    Assert.assertEquals("(2 + 3)", expr.toStringAST());

    // use as a unary operator
    evaluate("+5d", 5d, Double.class);
    evaluate("+5L", 5L, Long.class);
    evaluate("+5", 5, Integer.class);
    evaluateAndCheckError("+'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);

    // string concatenation
    evaluate("'abc'+'def'", "abcdef", String.class);

    //
    evaluate("5 + new Integer('37')", 42, Integer.class);
  }
  /*
   * (non-Javadoc)
   * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
   */
  @Override
  public int compare(T arg1, T arg2) {

    SpelExpression expressionToUse = getExpression();

    expressionToUse.getEvaluationContext().setVariable("arg1", arg1);
    expressionToUse.getEvaluationContext().setVariable("arg2", arg2);

    return expressionToUse.getValue(Integer.class) * (asc ? 1 : -1);
  }
예제 #4
0
  @Test
  public void testMinus() throws Exception {
    evaluate("'c' - 2", "a", String.class);
    evaluate("3.0f - 5.0f", -2.0f, Float.class);
    evaluateAndCheckError("'ab' - 2", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    evaluateAndCheckError("2-'ab'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    SpelExpression expr = (SpelExpression) parser.parseExpression("-3");
    Assert.assertEquals("-3", expr.toStringAST());
    expr = (SpelExpression) parser.parseExpression("2-3");
    Assert.assertEquals("(2 - 3)", expr.toStringAST());

    evaluate("-5d", -5d, Double.class);
    evaluate("-5L", -5L, Long.class);
    evaluate("-5", -5, Integer.class);
    evaluateAndCheckError("-'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
  }
  @Test
  public void testMethodFiltering_SPR6764() {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(new TestObject());
    LocalFilter filter = new LocalFilter();
    context.registerMethodFilter(TestObject.class, filter);

    // Filter will be called but not do anything, so first doit() will be invoked
    SpelExpression expr = (SpelExpression) parser.parseExpression("doit(1)");
    String result = expr.getValue(context, String.class);
    Assert.assertEquals("1", result);
    Assert.assertTrue(filter.filterCalled);

    // Filter will now remove non @Anno annotated methods
    filter.removeIfNotAnnotated = true;
    filter.filterCalled = false;
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    Assert.assertEquals("double 1.0", result);
    Assert.assertTrue(filter.filterCalled);

    // check not called for other types
    filter.filterCalled = false;
    context.setRootObject(new String("abc"));
    expr = (SpelExpression) parser.parseExpression("charAt(0)");
    result = expr.getValue(context, String.class);
    Assert.assertEquals("a", result);
    Assert.assertFalse(filter.filterCalled);

    // check de-registration works
    filter.filterCalled = false;
    context.registerMethodFilter(TestObject.class, null); // clear filter
    context.setRootObject(new TestObject());
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    Assert.assertEquals("1", result);
    Assert.assertFalse(filter.filterCalled);
  }
예제 #6
0
 private Operator getOperatorNode(SpelExpression e) {
   SpelNode node = e.getAST();
   return (Operator) findNode(node, Operator.class);
 }