View Javadoc

1   /*
2    * $Id: PortletSessionMapTest.java 418521 2006-07-01 23:36:50Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.portlet;
19  
20  import java.util.Arrays;
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import javax.portlet.PortletRequest;
28  import javax.portlet.PortletSession;
29  
30  import org.jmock.Mock;
31  import org.jmock.MockObjectTestCase;
32  import org.jmock.core.Constraint;
33  
34  
35  /***
36   * PortletSessionMapTest. Insert description.
37   * 
38   */
39  public class PortletSessionMapTest extends MockObjectTestCase {
40  
41      public void testPut() {
42          
43          Mock mockSession = mock(PortletSession.class);
44          Mock mockRequest = mock(PortletRequest.class);
45          
46          PortletRequest req = (PortletRequest)mockRequest.proxy();
47          PortletSession session = (PortletSession)mockSession.proxy();
48          
49          mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
50          Constraint[] params = new Constraint[]{eq("testAttribute1"), eq("testValue1")};
51          mockSession.expects(once()).method("setAttribute").with(params);
52          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute1")).will(returnValue("testValue1"));
53          params = new Constraint[]{eq("testAttribute2"), eq("testValue2")};
54          mockSession.expects(once()).method("setAttribute").with(params);
55          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute2")).will(returnValue("testValue2"));
56          
57          PortletSessionMap map = new PortletSessionMap(req);
58          map.put("testAttribute1", "testValue1");
59          map.put("testAttribute2", "testValue2");
60          
61      }
62      
63      public void testGet() {
64          Mock mockSession = mock(PortletSession.class);
65          Mock mockRequest = mock(PortletRequest.class);
66          
67          PortletRequest req = (PortletRequest)mockRequest.proxy();
68          PortletSession session = (PortletSession)mockSession.proxy();
69          
70          mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
71          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute1")).will(returnValue("testValue1"));
72          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute2")).will(returnValue("testValue2"));
73          
74          PortletSessionMap map = new PortletSessionMap(req);
75          Object val1 = map.get("testAttribute1");
76          Object val2 = map.get("testAttribute2");
77          assertEquals("testValue1", val1);
78          assertEquals("testValue2", val2);
79      }
80      
81      public void testClear() {
82          Mock mockSession = mock(PortletSession.class);
83          Mock mockRequest = mock(PortletRequest.class);
84          
85          PortletRequest req = (PortletRequest)mockRequest.proxy();
86          PortletSession session = (PortletSession)mockSession.proxy();
87          
88          mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
89          mockSession.expects(once()).method("invalidate");
90          
91          PortletSessionMap map = new PortletSessionMap(req);
92          map.clear();
93      }
94      
95      public void testRemove() {
96          Mock mockSession = mock(PortletSession.class);
97          Mock mockRequest = mock(PortletRequest.class);
98          
99          PortletRequest req = (PortletRequest)mockRequest.proxy();
100         PortletSession session = (PortletSession)mockSession.proxy();
101         
102         
103         mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
104         mockSession.stubs().method("getAttribute").with(eq("dummyKey")).will(returnValue("dummyValue"));
105         mockSession.expects(once()).method("removeAttribute").with(eq("dummyKey"));
106         
107         PortletSessionMap map = new PortletSessionMap(req);
108         Object ret = map.remove("dummyKey");
109         assertEquals("dummyValue", ret);
110     }
111     
112     public void testEntrySet() {
113         Mock mockSession = mock(PortletSession.class);
114         Mock mockRequest = mock(PortletRequest.class);
115         
116         PortletRequest req = (PortletRequest)mockRequest.proxy();
117         PortletSession session = (PortletSession)mockSession.proxy();
118         
119         Enumeration names = new Enumeration() {
120 
121             List keys = Arrays.asList(new Object[]{"key1", "key2"});
122             Iterator it = keys.iterator();
123             
124             public boolean hasMoreElements() {
125                 return it.hasNext();
126             }
127 
128             public Object nextElement() {
129                 return it.next();
130             }
131             
132         };
133         
134         mockSession.stubs().method("getAttributeNames").will(returnValue(names));
135         mockSession.stubs().method("getAttribute").with(eq("key1")).will(returnValue("value1"));
136         mockSession.stubs().method("getAttribute").with(eq("key2")).will(returnValue("value2"));
137         
138         mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
139         
140         PortletSessionMap map = new PortletSessionMap(req);
141         Set entries = map.entrySet();
142         
143         assertEquals(2, entries.size());
144         Iterator it = entries.iterator();
145         Map.Entry entry = (Map.Entry)it.next();
146         assertEquals("key2", entry.getKey());
147         assertEquals("value2", entry.getValue());
148         entry = (Map.Entry)it.next();
149         assertEquals("key1", entry.getKey());
150         assertEquals("value1", entry.getValue());
151         
152     }
153     
154 }