View Javadoc

1   /*
2    * $Id: MessageProcessorExpressionEvaluatorTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.expression;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.RequestContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.expression.ExpressionManager;
19  import org.mule.api.expression.ExpressionRuntimeException;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.api.transport.PropertyScope;
22  import org.mule.tck.junit4.AbstractMuleContextTestCase;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  public class MessageProcessorExpressionEvaluatorTestCase extends AbstractMuleContextTestCase
29  {
30  
31      private ExpressionManager expressionManager;
32  
33      @Override
34      protected void doSetUp() throws Exception
35      {
36          super.doSetUp();
37          expressionManager = muleContext.getExpressionManager();
38          RequestContext.setEvent(getTestEvent(""));
39  
40          muleContext.getRegistry().registerObject("processor", new MessageProcessor()
41          {
42              public MuleEvent process(MuleEvent event) throws MuleException
43              {
44                  event.getMessage().setPayload(event.getMessageAsString() + "123");
45                  return event;
46              }
47          });
48      }
49  
50      @Test
51      public void testNameOnly() throws Exception
52      {
53          MessageProcessorExpressionEvaluator evaluator = new MessageProcessorExpressionEvaluator();
54          assertEquals("0123",
55              ((MuleMessage) evaluator.evaluate("processor", createTestMessage())).getPayloadAsString());
56      }
57  
58      @Test
59      public void testNameOnlyExpressionManager() throws ExpressionRuntimeException, Exception
60      {
61          assertEquals("0123", ((MuleMessage) expressionManager.evaluate("#[process:processor]",
62              createTestMessage())).getPayloadAsString());
63      }
64  
65      @Test
66      public void testNestedPayloadExpression() throws Exception
67      {
68          MessageProcessorExpressionEvaluator evaluator = new MessageProcessorExpressionEvaluator();
69          assertEquals("0123",
70              ((MuleMessage) evaluator.evaluate("processor:payload", createTestMessage())).getPayloadAsString());
71      }
72  
73      @Test
74      public void testNestedPayloadExpressionExpressionManager() throws ExpressionRuntimeException, Exception
75      {
76          assertEquals("0123", ((MuleMessage) expressionManager.evaluate("#[process:processor:#[payload]]",
77              createTestMessage())).getPayloadAsString());
78      }
79  
80      @Test
81      public void testNestedHeaderExpression() throws Exception
82      {
83          MessageProcessorExpressionEvaluator evaluator = new MessageProcessorExpressionEvaluator();
84          assertEquals("value123", ((MuleMessage) evaluator.evaluate("processor:header:one",
85              createTestMessage())).getPayloadAsString());
86      }
87  
88      @Test
89      public void testNestedHeaderExpressionExpressionManager() throws ExpressionRuntimeException, Exception
90      {
91          assertEquals("value123", ((MuleMessage) expressionManager.evaluate(
92              "#[process:processor:#[header:one]]", createTestMessage())).getPayloadAsString());
93      }
94  
95      private MuleMessage createTestMessage()
96      {
97          MuleMessage message = new DefaultMuleMessage("0", muleContext);
98          message.setProperty("one", "value", PropertyScope.OUTBOUND);
99          return message;
100     }
101 
102 }