View Javadoc

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