View Javadoc

1   /*
2    * $Id: PortletRequestMapTest.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.PortletRequest;
29  
30  import org.jmock.MockObjectTestCase;
31  import org.springframework.mock.web.portlet.MockPortletRequest;
32  
33  
34  /***
35   * PortletRequestMapTest. Insert description.
36   *
37   */
38  public class PortletRequestMapTest extends MockObjectTestCase {
39  
40      public void testGet() {
41      	PortletRequest request = new MockPortletRequest();
42      	request.setAttribute("testAttribute", "testValue");
43  
44      	PortletRequestMap map = new PortletRequestMap(request);
45          String value = (String)map.get("testAttribute");
46          assertEquals("testValue", value);
47      }
48  
49      public void testPut() {
50      	PortletRequest request = new MockPortletRequest();
51      	PortletRequestMap map = new PortletRequestMap(request);
52          Object obj = map.put("testAttribute", "testValue1");
53          
54          assertEquals(obj, "testValue1");
55          assertEquals("testValue1", request.getAttribute("testAttribute"));
56      }
57  
58      public void testClear() {
59      	MockPortletRequest request = new MockPortletRequest();
60      	request.setAttribute("testAttribute1", "testValue1");
61      	request.setAttribute("testAttribute2", "testValue2");
62  
63  
64          PortletRequestMap map = new PortletRequestMap(request);
65          map.clear();
66  
67          assertFalse(request.getAttributeNames().hasMoreElements());
68      }
69  
70      public void testRemove() {
71          MockPortletRequest request = new MockPortletRequest();
72          request.setAttribute("testAttribute1", "testValue1");
73          
74          PortletRequestMap map = new PortletRequestMap(request);
75          assertEquals("testValue1", map.remove("testAttribute1"));
76          assertNull(request.getAttribute("testAttribute1"));
77      }
78  
79      public void testEntrySet() {
80      	MockPortletRequest request = new MockPortletRequest();
81      	request.setAttribute("testAttribute1", "testValue1");
82      	request.setAttribute("testAttribute2", "testValue2");
83  
84          PortletRequestMap map = new PortletRequestMap(request);
85          Set entries = map.entrySet();
86  
87          assertEquals(2, entries.size());
88          Iterator it = entries.iterator();
89          Map.Entry entry = (Map.Entry)it.next();
90          checkEntry(entry);
91          entry = (Map.Entry)it.next();
92          checkEntry(entry);
93  
94      }
95      
96  	private void checkEntry(Map.Entry entry) {
97  		if(entry.getKey().equals("testAttribute1")) {
98          	assertEquals("testValue1", entry.getValue());
99          }
100         else if(entry.getKey().equals("testAttribute2")) {
101         	assertEquals("testValue2", entry.getValue());
102         }
103         else {
104         	fail("Unexpected entry in etry set: " + entry);
105         }
106 	}
107 
108 }