View Javadoc

1   /*
2    * $Id: MessagePropertiesTransformerScopesTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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.junit4.AbstractMuleContextTestCase;
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  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNull;
27  
28  public class MessagePropertiesTransformerScopesTestCase extends AbstractMuleContextTestCase
29  {
30  
31      @Test
32      public void testPropertyScopes() throws Exception
33      {
34          MuleMessage msg = new DefaultMuleMessage("message", muleContext);
35          
36          // Add properties to scope
37          
38          MessagePropertiesTransformer add = new MessagePropertiesTransformer();
39          add.setScope(PropertyScope.INVOCATION);
40          Map<String, Object> addProps = new HashMap<String, Object>();
41          addProps.put("foo", "bar");
42          addProps.put("foo2", "baz");
43          add.setAddProperties(addProps);
44          add.setMuleContext(muleContext);
45          add.initialise();
46  
47          msg = (DefaultMuleMessage) add.transform(msg, (String)null);
48  
49          assertEquals("bar", msg.getInvocationProperty("foo"));
50          assertNull(msg.getOutboundProperty("foo"));
51          assertNull(msg.getSessionProperty("foo"));
52  
53          // Remove property from the wrong scope
54          
55          MessagePropertiesTransformer deleteWrongScope = new MessagePropertiesTransformer();
56          deleteWrongScope.setScope(PropertyScope.OUTBOUND);
57          deleteWrongScope.setDeleteProperties("foo");
58          deleteWrongScope.setMuleContext(muleContext);
59          deleteWrongScope.initialise();
60  
61          msg = (DefaultMuleMessage) deleteWrongScope.transform(msg, (String)null);
62          assertEquals("bar", msg.getInvocationProperty("foo"));
63  
64          // Remove property from the correct scope
65          
66          MessagePropertiesTransformer delete = new MessagePropertiesTransformer();
67          delete.setScope(PropertyScope.INVOCATION);
68          delete.setDeleteProperties(Collections.singletonList("foo"));
69          delete.setMuleContext(muleContext);
70          delete.initialise();
71  
72          msg = (DefaultMuleMessage) delete.transform(msg, (String)null);
73          assertNull(msg.getInvocationProperty("foo"));
74          assertEquals("baz", msg.getInvocationProperty("foo2"));
75      }
76  }