View Javadoc

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