View Javadoc

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