View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.message;
8   
9   import org.mule.MessagePropertiesContext;
10  import org.mule.RequestContext;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.transport.PropertyScope;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  
15  import java.util.Set;
16  
17  import org.apache.commons.lang.SerializationUtils;
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  public class MessagePropertiesContextTestCase extends AbstractMuleContextTestCase
26  {
27      @Override
28      public void doTearDown()
29      {
30          RequestContext.clear();
31      }
32      
33      @Test
34      public void testPropertiesCase() throws Exception
35      {
36          //Default scope
37          MessagePropertiesContext mpc = new MessagePropertiesContext();
38          mpc.setProperty("FOO", "BAR", PropertyScope.OUTBOUND);
39          mpc.setProperty("ABC", "abc", PropertyScope.OUTBOUND);
40          mpc.setProperty("DOO", "DAR", PropertyScope.INVOCATION);
41          doTest(mpc);
42      }
43  
44      @Test
45      public void testSessionScope() throws Exception
46      {
47          MuleEvent e = getTestEvent("testing");
48          e.getSession().setProperty("SESSION_PROP", "Value1");
49          RequestContext.setEvent(e);
50  
51          MessagePropertiesContext mpc = new MessagePropertiesContext();
52  
53          assertEquals("Value1", mpc.getProperty("SESSION_PROP", PropertyScope.SESSION));
54          //test case insensitivity
55          assertEquals("Value1", mpc.getProperty("SESSION_prop", PropertyScope.SESSION));
56          assertNull(mpc.getProperty("SESSION_X", PropertyScope.SESSION));
57      }
58  
59      @Test
60      public void testPropertyScopeOrder() throws Exception
61      {
62          MuleEvent e = getTestEvent("testing");
63          e.getSession().setProperty("Prop", "session");
64          RequestContext.setEvent(e);
65  
66          MessagePropertiesContext mpc = new MessagePropertiesContext();
67          //Note that we cannot write to the Inbound scope, its read only
68          mpc.setProperty("Prop", "invocation", PropertyScope.INVOCATION);
69          mpc.setProperty("Prop", "outbound", PropertyScope.OUTBOUND);
70  
71          assertEquals("outbound", mpc.getProperty("Prop", PropertyScope.OUTBOUND));
72          mpc.removeProperty("Prop", PropertyScope.OUTBOUND);
73  
74          assertEquals("invocation", mpc.getProperty("Prop", PropertyScope.INVOCATION));
75          mpc.removeProperty("Prop", PropertyScope.INVOCATION);
76  
77          assertEquals("session", mpc.getProperty("Prop", PropertyScope.SESSION));
78          assertNull(mpc.getProperty("Prop", PropertyScope.INBOUND));
79          assertNull(mpc.getProperty("Prop", PropertyScope.INVOCATION));
80          assertNull(mpc.getProperty("Prop", PropertyScope.OUTBOUND));
81      }
82  
83      @Test
84      public void testPropertiesCaseAfterSerialization() throws Exception
85      {
86          //Default scope
87          MessagePropertiesContext mpc = new MessagePropertiesContext();
88          mpc.setProperty("FOO", "BAR", PropertyScope.OUTBOUND);
89          mpc.setProperty("ABC", "abc", PropertyScope.OUTBOUND);
90          mpc.setProperty("DOO", "DAR", PropertyScope.INVOCATION);
91          doTest(mpc);
92  
93          //Serialize and deserialize
94          byte[] bytes = SerializationUtils.serialize(mpc);
95          mpc = (MessagePropertiesContext) SerializationUtils.deserialize(bytes);
96          doTest(mpc);
97      }
98  
99      /*@Test
100     public void testInboundScopeIsImmutable() throws Exception
101     {        
102         MessagePropertiesContext mpc = new MessagePropertiesContext();
103         try
104         {
105             mpc.setProperty("key", "value", PropertyScope.INBOUND);
106             fail("Inbound scope should be read-only");
107         }
108         catch (IllegalArgumentException iae)
109         {
110             // this exception was expected
111         }
112     }*/
113         
114     protected void doTest(MessagePropertiesContext mpc)
115     {
116         //Look in all scopes
117         assertEquals("BAR", mpc.getProperty("foo", PropertyScope.OUTBOUND));
118         assertEquals("DAR", mpc.getProperty("doo", PropertyScope.INVOCATION));
119         assertEquals("abc", mpc.getProperty("abc", PropertyScope.OUTBOUND));
120 
121         //Look in specific scope
122         assertEquals("BAR", mpc.getProperty("foO", PropertyScope.OUTBOUND)); //default scope
123         assertEquals("DAR", mpc.getProperty("doO", PropertyScope.INVOCATION));
124 
125         //Not found using other specific scopes
126         assertNull(mpc.getProperty("doo", PropertyScope.INBOUND));
127         assertNull(mpc.getProperty("doo", PropertyScope.OUTBOUND));
128         assertNull(mpc.getProperty("doo", PropertyScope.SESSION));
129 
130         Set<String> keys = mpc.getPropertyNames();
131         assertEquals(3, keys.size());
132 
133         for (String key : keys)
134         {
135             assertTrue(key.equals("FOO") || key.equals("DOO") || key.equals("ABC"));
136             assertFalse(key.equals("foo") || key.equals("doo") || key.equals("abc"));
137         }
138     }
139 }