View Javadoc

1   /*
2    * $Id: MessagePropertiesTransformerScopesTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transformers.simple;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.transport.PropertyScope;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.transformer.simple.MessagePropertiesTransformer;
18  
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  public class MessagePropertiesTransformerScopesTestCase extends AbstractMuleTestCase
24  {
25      public void testPropertyScopes() throws Exception
26      {
27          MuleMessage msg = new DefaultMuleMessage("message", muleContext);
28          
29          // Add properties to scope
30          
31          MessagePropertiesTransformer add = new MessagePropertiesTransformer();
32          add.setScope(PropertyScope.INVOCATION);
33          Map<String, Object> addProps = new HashMap<String, Object>();
34          addProps.put("foo", "bar");
35          addProps.put("foo2", "baz");
36          add.setAddProperties(addProps);
37          add.setMuleContext(muleContext);
38          add.initialise();
39  
40          msg = (DefaultMuleMessage) add.transform(msg, (String)null);
41  
42          assertEquals("bar", msg.getInvocationProperty("foo"));
43          assertNull(msg.getOutboundProperty("foo"));
44          assertNull(msg.getSessionProperty("foo"));
45  
46          // Remove property from the wrong scope
47          
48          MessagePropertiesTransformer deleteWrongScope = new MessagePropertiesTransformer();
49          deleteWrongScope.setScope(PropertyScope.OUTBOUND);
50          deleteWrongScope.setDeleteProperties("foo");
51          deleteWrongScope.setMuleContext(muleContext);
52          deleteWrongScope.initialise();
53  
54          msg = (DefaultMuleMessage) deleteWrongScope.transform(msg, (String)null);
55          assertEquals("bar", msg.getInvocationProperty("foo"));
56  
57          // Remove property from the correct scope
58          
59          MessagePropertiesTransformer delete = new MessagePropertiesTransformer();
60          delete.setScope(PropertyScope.INVOCATION);
61          delete.setDeleteProperties(Collections.singletonList("foo"));
62          delete.setMuleContext(muleContext);
63          delete.initialise();
64  
65          msg = (DefaultMuleMessage) delete.transform(msg, (String)null);
66          assertNull(msg.getInvocationProperty("foo"));
67          assertEquals("baz", msg.getInvocationProperty("foo2"));
68      }
69  }