View Javadoc

1   /*
2    * $Id: MuleSessionHandlerTestCase.java 23054 2011-10-02 05:31:18Z 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;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.MuleSession;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.security.Authentication;
17  import org.mule.api.security.Credentials;
18  import org.mule.api.security.SecurityContext;
19  import org.mule.api.transport.SessionHandler;
20  import org.mule.security.DefaultMuleAuthentication;
21  import org.mule.security.DefaultSecurityContextFactory;
22  import org.mule.security.MuleCredentials;
23  import org.mule.session.DefaultMuleSession;
24  import org.mule.session.LegacySessionHandler;
25  import org.mule.session.SerializeAndEncodeSessionHandler;
26  import org.mule.tck.junit4.AbstractMuleContextTestCase;
27  
28  import java.util.ArrayList;
29  import java.util.Date;
30  import java.util.List;
31  
32  import org.apache.commons.lang.SerializationException;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertNull;
37  import static org.junit.Assert.assertTrue;
38  import static org.junit.Assert.fail;
39  
40  public class MuleSessionHandlerTestCase extends AbstractMuleContextTestCase
41  {
42      /** see EE-1705/MULE-4567 */
43      @Test
44      public void testSessionProperties() throws Exception 
45      {
46          DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
47          SessionHandler handler = new SerializeAndEncodeSessionHandler();
48          MuleSession session = new DefaultMuleSession(muleContext);
49          
50          String string = "bar";
51          session.setProperty("fooString", string);
52  
53          Date date = new Date(0);
54          session.setProperty("fooDate", date);
55          
56          List<String> list = createList();
57          session.setProperty("fooList", list);
58          
59          handler.storeSessionInfoToMessage(session, message);
60          // store save session to outbound, move it to the inbound
61          // for retrieve to deserialize
62          Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
63          message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
64          session = handler.retrieveSessionInfoFromMessage(message);
65          
66          Object obj = session.getProperty("fooString");
67          assertTrue(obj instanceof String);
68          assertEquals(string, obj);
69          
70          obj = session.getProperty("fooDate");
71          assertTrue("Object should be a Date but is " + obj.getClass().getName(), obj instanceof Date);
72          assertEquals(date, obj);
73  
74          obj = session.getProperty("fooList");
75          assertTrue("Object should be a List but is " + obj.getClass().getName(), obj instanceof List);
76          assertEquals(list, obj);
77      }
78  
79      /** see EE-1774 */
80      @Test
81      public void testNonSerializableSessionProperties() throws Exception 
82      {
83          DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
84          MuleSession session = new DefaultMuleSession(muleContext);
85          SessionHandler handler = new SerializeAndEncodeSessionHandler();
86          
87          NotSerializableClass clazz = new NotSerializableClass();
88          session.setProperty("foo", clazz);
89          handler.storeSessionInfoToMessage(session, message);
90          // store save session to outbound, move it to the inbound
91          // for retrieve to deserialize
92          Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
93          message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
94          session = handler.retrieveSessionInfoFromMessage(message);
95          // Property was removed because it could not be serialized
96          assertNull(session.getProperty("foo"));
97      }    
98  
99      /** see EE-1820 */
100     @Test
101     public void testBackwardsCompatibility() throws Exception 
102     {
103         MuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
104         SessionHandler legacyHandler = new LegacySessionHandler();
105         MuleSession session = new DefaultMuleSession(muleContext);
106         
107         String string = "bar";
108         session.setProperty("fooString", string);
109 
110         Date date = new Date(0);
111         session.setProperty("fooDate", date);
112         
113         List<String> list = createList();
114         session.setProperty("fooList", list);
115         
116         legacyHandler.storeSessionInfoToMessage(session, message);
117         try
118         {
119             // Try to deserialize legacy format with new session handler
120             session = new SerializeAndEncodeSessionHandler().retrieveSessionInfoFromMessage(message);
121         }
122         catch (SerializationException e)
123         {
124             // expected
125         }
126         session = legacyHandler.retrieveSessionInfoFromMessage(message);
127     }    
128     
129     /** see EE-1820 */
130     @Test
131     public void testSessionPropertiesLegacyFormat() throws Exception 
132     {
133         DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
134         SessionHandler handler = new LegacySessionHandler();
135         MuleSession session = new DefaultMuleSession(muleContext);
136         
137         String string = "bar";
138         session.setProperty("fooString", string);
139 
140         Date date = new Date(0);
141         session.setProperty("fooDate", date);
142         
143         List<String> list = createList();
144         session.setProperty("fooList", list);
145 
146         handler.storeSessionInfoToMessage(session, message);
147         // store save session to outbound, move it to the inbound
148         // for retrieve to deserialize
149         Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
150         message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
151         session = handler.retrieveSessionInfoFromMessage(message);
152         
153         Object obj = session.getProperty("fooString");
154         assertTrue(obj instanceof String);
155         assertEquals(string, obj);
156         
157         obj = session.getProperty("fooDate");
158         // MULE-4567 / EE-1705 
159         assertTrue(obj instanceof String);
160 
161         obj = session.getProperty("fooList");
162         // MULE-4567 / EE-1705 
163         assertTrue(obj instanceof String);
164     }
165 
166     /** see MULE-4720 */
167     @Test
168     public void testSecurityContext() throws Exception 
169     {
170         DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
171         SessionHandler handler = new SerializeAndEncodeSessionHandler();
172         MuleSession session = new DefaultMuleSession(muleContext);
173         
174         Credentials credentials = new MuleCredentials("joe", "secret".toCharArray());
175         SecurityContext sc = new DefaultSecurityContextFactory().create(new DefaultMuleAuthentication(credentials));
176         session.setSecurityContext(sc);
177 
178         handler.storeSessionInfoToMessage(session, message);
179         // store save session to outbound, move it to the inbound
180         // for retrieve to deserialize
181         Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
182         message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
183         session = handler.retrieveSessionInfoFromMessage(message);
184 
185         sc = session.getSecurityContext();
186         assertEquals("joe", sc.getAuthentication().getPrincipal());
187     }
188     
189     /** see EE-1774 */
190     @Test
191     public void testNotSerializableSecurityContext() throws Exception 
192     {
193         MuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
194         SessionHandler handler = new SerializeAndEncodeSessionHandler();
195         MuleSession session = new DefaultMuleSession(muleContext);
196         
197         session.setSecurityContext(new NotSerializableSecurityContext());
198         
199         try
200         {
201             handler.storeSessionInfoToMessage(session, message);
202             fail("Should throw a SerializationException");
203         }
204         catch (SerializationException e)
205         {
206             // expected
207         }
208     }
209 
210     private List<String> createList()
211     {
212         List<String> list = new ArrayList<String>();
213         list.add("bar1");
214         list.add("bar2");
215         return list;
216     }
217 
218     private class NotSerializableClass
219     {
220         public NotSerializableClass()
221         {
222             super();
223         }
224     }
225     
226     private class NotSerializableSecurityContext implements SecurityContext
227     {
228         public NotSerializableSecurityContext()
229         {
230             super();
231         }
232         
233         public void setAuthentication(Authentication authentication)
234         {
235             // nothing to do
236         }
237 
238         public Authentication getAuthentication() 
239         {
240             return null;
241         }
242     }
243     
244 }