View Javadoc

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