View Javadoc

1   /*
2    * $Id: PortletRequestMapTest.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.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import javax.portlet.PortletRequest;
30  
31  import org.jmock.Mock;
32  import org.jmock.MockObjectTestCase;
33  import org.jmock.core.Constraint;
34  
35  
36  /***
37   * PortletRequestMapTest. Insert description.
38   * 
39   */
40  public class PortletRequestMapTest extends MockObjectTestCase {
41  
42      public void testSetAttribute() {
43          
44      }
45      
46      public void testGet() {
47          Mock mockRequest = mock(PortletRequest.class, "testGet");
48          mockRequest.expects(once()).method("getAttribute").with(eq("testAttribute")).will(returnValue("testValue"));
49          PortletRequestMap map = new PortletRequestMap((PortletRequest)mockRequest.proxy());
50          String value = (String)map.get("testAttribute");
51          mockRequest.verify();
52          assertEquals("testValue", value);
53      }
54      
55      public void testPut() {
56          Mock mockRequest = mock(PortletRequest.class, "testPut");
57          Object value = new String("testValue");
58          Constraint[] params = new Constraint[]{eq("testAttribute"), eq(value)};
59          mockRequest.expects(once()).method("setAttribute").with(params);
60          mockRequest.expects(once()).method("getAttribute").with(eq("testAttribute")).will(returnValue(value));
61          PortletRequestMap map = new PortletRequestMap((PortletRequest)mockRequest.proxy());
62          Object obj = map.put("testAttribute", value);
63          mockRequest.verify();
64          assertEquals(obj, value);
65      }
66      
67      public void testClear() {
68          Mock mockRequest = mock(PortletRequest.class, "testClear");
69  
70          mockRequest.expects(once()).method("removeAttribute").with(eq("a"));
71          mockRequest.expects(once()).method("removeAttribute").with(eq("b"));
72          
73          ArrayList dummy = new ArrayList();
74          dummy.add("a");
75          dummy.add("b");
76          
77          mockRequest.expects(once()).method("getAttributeNames").will(returnValue(Collections.enumeration(dummy)));
78          
79          PortletRequestMap map = new PortletRequestMap((PortletRequest)mockRequest.proxy());
80          map.clear();
81          mockRequest.verify();
82      }
83      
84      public void testRemove() {
85          Mock mockRequest = mock(PortletRequest.class);
86          
87          PortletRequest req = (PortletRequest)mockRequest.proxy();
88          
89          
90          mockRequest.expects(once()).method("getAttribute").with(eq("dummyKey")).will(returnValue("dummyValue"));
91          
92          mockRequest.expects(once()).method("removeAttribute").with(eq("dummyKey"));
93          
94          PortletRequestMap map = new PortletRequestMap(req);
95          Object ret = map.remove("dummyKey");
96          assertEquals("dummyValue", ret);
97      }
98      
99      public void testEntrySet() {
100         Mock mockRequest = mock(PortletRequest.class);
101         
102         PortletRequest req = (PortletRequest)mockRequest.proxy();
103         
104         Enumeration names = new Enumeration() {
105 
106             List keys = Arrays.asList(new Object[]{"key1", "key2"});
107             Iterator it = keys.iterator();
108             
109             public boolean hasMoreElements() {
110                 return it.hasNext();
111             }
112 
113             public Object nextElement() {
114                 return it.next();
115             }
116             
117         };
118         
119         mockRequest.stubs().method("getAttributeNames").will(returnValue(names));
120         mockRequest.stubs().method("getAttribute").with(eq("key1")).will(returnValue("value1"));
121         mockRequest.stubs().method("getAttribute").with(eq("key2")).will(returnValue("value2"));
122         
123         PortletRequestMap map = new PortletRequestMap(req);
124         Set entries = map.entrySet();
125         
126         assertEquals(2, entries.size());
127         Iterator it = entries.iterator();
128         Map.Entry entry = (Map.Entry)it.next();
129         assertEquals("key2", entry.getKey());
130         assertEquals("value2", entry.getValue());
131         entry = (Map.Entry)it.next();
132         assertEquals("key1", entry.getKey());
133         assertEquals("value1", entry.getValue());
134         
135     }
136     
137 }