View Javadoc

1   /*
2    * $Id: PortletApplicationMapTest.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.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import javax.portlet.PortletContext;
34  
35  import org.jmock.Mock;
36  import org.jmock.MockObjectTestCase;
37  import org.jmock.core.Constraint;
38  
39  /***
40   */
41  public class PortletApplicationMapTest extends MockObjectTestCase {
42  
43      Mock mockPortletContext;
44  
45      PortletContext portletContext;
46  
47      public void setUp() throws Exception {
48          super.setUp();
49          mockPortletContext = mock(PortletContext.class);
50          portletContext = (PortletContext) mockPortletContext.proxy();
51      }
52  
53      public void testGetFromAttributes() {
54          mockPortletContext.stubs().method("getAttribute").with(eq("dummyKey"))
55                  .will(returnValue("dummyValue"));
56  
57          PortletApplicationMap map = new PortletApplicationMap(
58                  (PortletContext) mockPortletContext.proxy());
59  
60          assertEquals("dummyValue", map.get("dummyKey"));
61      }
62  
63      public void testGetFromInitParameters() {
64          mockPortletContext.stubs().method("getAttribute").with(eq("dummyKey"));
65          mockPortletContext.stubs().method("getInitParameter").with(
66                  eq("dummyKey")).will(returnValue("dummyValue"));
67  
68          PortletApplicationMap map = new PortletApplicationMap(
69                  (PortletContext) mockPortletContext.proxy());
70  
71          assertEquals("dummyValue", map.get("dummyKey"));
72      }
73  
74      public void testPut() {
75          mockPortletContext.expects(once()).method("setAttribute").with(
76                  new Constraint[] { eq("dummyKey"), eq("dummyValue") });
77          mockPortletContext.expects(once()).method("getAttribute").with(
78                  eq("dummyKey")).will(returnValue("dummyValue"));
79          PortletApplicationMap map = new PortletApplicationMap(portletContext);
80          Object val = map.put("dummyKey", "dummyValue");
81          assertEquals("dummyValue", val);
82      }
83  
84      public void testRemove() {
85          mockPortletContext.expects(once()).method("getAttribute").with(
86                  eq("dummyKey")).will(returnValue("dummyValue"));
87          mockPortletContext.expects(once()).method("removeAttribute").with(
88                  eq("dummyKey"));
89          PortletApplicationMap map = new PortletApplicationMap(portletContext);
90          Object val = map.remove("dummyKey");
91          assertEquals("dummyValue", val);
92      }
93  
94      public void testEntrySet() {
95  
96          Enumeration names = new Enumeration() {
97  
98              List keys = Arrays.asList(new Object[] { "key1", "key2" });
99  
100             Iterator it = keys.iterator();
101 
102             public boolean hasMoreElements() {
103                 return it.hasNext();
104             }
105 
106             public Object nextElement() {
107                 return it.next();
108             }
109 
110         };
111         Enumeration initParamNames = new Enumeration() {
112 
113             List keys = Arrays.asList(new Object[] { "key3" });
114 
115             Iterator it = keys.iterator();
116 
117             public boolean hasMoreElements() {
118                 return it.hasNext();
119             }
120 
121             public Object nextElement() {
122                 return it.next();
123             }
124 
125         };
126 
127         mockPortletContext.stubs().method("getAttributeNames").will(
128                 returnValue(names));
129         mockPortletContext.stubs().method("getInitParameterNames").will(
130                 returnValue(initParamNames));
131         mockPortletContext.stubs().method("getAttribute").with(eq("key1"))
132                 .will(returnValue("value1"));
133         mockPortletContext.stubs().method("getAttribute").with(eq("key2"))
134                 .will(returnValue("value2"));
135         mockPortletContext.stubs().method("getInitParameter").with(eq("key3"))
136                 .will(returnValue("value3"));
137 
138         PortletApplicationMap map = new PortletApplicationMap(portletContext);
139         Set entries = map.entrySet();
140 
141         assertEquals(3, entries.size());
142         Iterator it = entries.iterator();
143         Map.Entry entry = (Map.Entry) it.next();
144         assertEquals("key2", entry.getKey());
145         assertEquals("value2", entry.getValue());
146         entry = (Map.Entry) it.next();
147         assertEquals("key1", entry.getKey());
148         assertEquals("value1", entry.getValue());
149         entry = (Map.Entry) it.next();
150         assertEquals("key3", entry.getKey());
151         assertEquals("value3", entry.getValue());
152 
153     }
154 
155     public void testClear() {
156 
157         mockPortletContext.expects(once()).method("removeAttribute").with(eq("key1"));
158         mockPortletContext.expects(once()).method("removeAttribute").with(eq("key2"));
159 
160         ArrayList dummy = new ArrayList();
161         dummy.add("key1");
162         dummy.add("key2");
163 
164         mockPortletContext.expects(once()).method("getAttributeNames").will(
165                 returnValue(Collections.enumeration(dummy)));
166 
167         PortletApplicationMap map = new PortletApplicationMap(portletContext);
168         map.clear();
169     }
170 }