View Javadoc

1   /*
2    * $Id: PortletSessionMapTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.portlet;
23  
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.portlet.PortletSession;
29  
30  import junit.framework.TestCase;
31  
32  import org.springframework.mock.web.portlet.MockPortletRequest;
33  
34  
35  /***
36   * PortletSessionMapTest. Insert description.
37   *
38   */
39  public class PortletSessionMapTest extends TestCase {
40  
41      public void testPut() {
42  
43      	MockPortletRequest request = new MockPortletRequest();
44  
45          PortletSessionMap map = new PortletSessionMap(request);
46          assertEquals("testValue1", map.put("testAttribute1", "testValue1"));
47          assertEquals("testValue2", map.put("testAttribute2", "testValue2"));
48  
49          PortletSession session = request.getPortletSession();
50          // Assert that the values has been propagated to the session
51          assertEquals("testValue1", session.getAttribute("testAttribute1"));
52          assertEquals("testValue2", session.getAttribute("testAttribute2"));
53      }
54  
55      public void testGet() {
56      	MockPortletRequest request = new MockPortletRequest();
57      	PortletSession session = request.getPortletSession();
58      	session.setAttribute("testAttribute1", "testValue1");
59      	session.setAttribute("testAttribute2", "testValue2");
60          PortletSessionMap map = new PortletSessionMap(request);
61          Object val1 = map.get("testAttribute1");
62          Object val2 = map.get("testAttribute2");
63          // Assert that the values from the session is in the map
64          assertEquals("testValue1", val1);
65          assertEquals("testValue2", val2);
66      }
67  
68      public void testClear() {
69          MockPortletRequest req = new MockPortletRequest();
70          PortletSession session = req.getPortletSession();
71      	session.setAttribute("testAttribute1", "testValue1");
72      	session.setAttribute("testAttribute2", "testValue2");
73          
74          PortletSessionMap map = new PortletSessionMap(req);
75          map.clear();
76          
77          // Assert that there are no elements in the portlet session
78          assertFalse(req.getPortletSession().getAttributeNames().hasMoreElements());
79      }
80  
81      public void testRemove() {
82      	MockPortletRequest request = new MockPortletRequest();
83      	PortletSession session = request.getPortletSession();
84      	session.setAttribute("testAttribute1", "testValue1");
85  
86          PortletSessionMap map = new PortletSessionMap(request);
87          Object ret = map.remove("testAttribute1");
88          // Assert that the element that was removed was returned and the key is no longer in the
89          // portlet session
90          assertEquals("testValue1", ret);
91          assertNull(session.getAttribute("testAttribute1"));
92      }
93  
94      public void testEntrySet() {
95      	MockPortletRequest request = new MockPortletRequest();
96      	PortletSession session = request.getPortletSession();
97      	session.setAttribute("testAttribute1", "testValue1");
98      	session.setAttribute("testAttribute2", "testValue2");
99  
100         PortletSessionMap map = new PortletSessionMap(request);
101         Set entries = map.entrySet();
102 
103         assertEquals(2, entries.size());
104         Iterator it = entries.iterator();
105         Map.Entry entry = (Map.Entry)it.next();
106         checkEntry(entry);
107         entry = (Map.Entry)it.next();
108         checkEntry(entry);
109 
110     }
111 
112 	private void checkEntry(Map.Entry entry) {
113 		if(entry.getKey().equals("testAttribute1")) {
114         	assertEquals("testValue1", entry.getValue());
115         }
116         else if(entry.getKey().equals("testAttribute2")) {
117         	assertEquals("testValue2", entry.getValue());
118         }
119         else {
120         	fail("Unexpected entry in etry set: " + entry);
121         }
122 	}
123 
124 
125 }