001: /*
002: * $Id: PortletApplicationMapTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet;
022:
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Collections;
026: import java.util.Enumeration;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import javax.portlet.PortletContext;
033:
034: import org.jmock.Mock;
035: import org.jmock.MockObjectTestCase;
036: import org.jmock.core.Constraint;
037:
038: /**
039: */
040: public class PortletApplicationMapTest extends MockObjectTestCase {
041:
042: Mock mockPortletContext;
043:
044: PortletContext portletContext;
045:
046: public void setUp() throws Exception {
047: super .setUp();
048: mockPortletContext = mock(PortletContext.class);
049: portletContext = (PortletContext) mockPortletContext.proxy();
050: }
051:
052: public void testGetFromAttributes() {
053: mockPortletContext.stubs().method("getAttribute").with(
054: eq("dummyKey")).will(returnValue("dummyValue"));
055:
056: PortletApplicationMap map = new PortletApplicationMap(
057: (PortletContext) mockPortletContext.proxy());
058:
059: assertEquals("dummyValue", map.get("dummyKey"));
060: }
061:
062: public void testGetFromInitParameters() {
063: mockPortletContext.stubs().method("getAttribute").with(
064: eq("dummyKey"));
065: mockPortletContext.stubs().method("getInitParameter").with(
066: eq("dummyKey")).will(returnValue("dummyValue"));
067:
068: PortletApplicationMap map = new PortletApplicationMap(
069: (PortletContext) mockPortletContext.proxy());
070:
071: assertEquals("dummyValue", map.get("dummyKey"));
072: }
073:
074: public void testPut() {
075: mockPortletContext.expects(once()).method("setAttribute").with(
076: new Constraint[] { eq("dummyKey"), eq("dummyValue") });
077: mockPortletContext.expects(once()).method("getAttribute").with(
078: eq("dummyKey")).will(returnValue("dummyValue"));
079: PortletApplicationMap map = new PortletApplicationMap(
080: portletContext);
081: Object val = map.put("dummyKey", "dummyValue");
082: assertEquals("dummyValue", val);
083: }
084:
085: public void testRemove() {
086: mockPortletContext.expects(once()).method("getAttribute").with(
087: eq("dummyKey")).will(returnValue("dummyValue"));
088: mockPortletContext.expects(once()).method("removeAttribute")
089: .with(eq("dummyKey"));
090: PortletApplicationMap map = new PortletApplicationMap(
091: portletContext);
092: Object val = map.remove("dummyKey");
093: assertEquals("dummyValue", val);
094: }
095:
096: public void testEntrySet() {
097:
098: Enumeration names = new Enumeration() {
099:
100: List keys = Arrays.asList(new Object[] { "key1", "key2" });
101:
102: Iterator it = keys.iterator();
103:
104: public boolean hasMoreElements() {
105: return it.hasNext();
106: }
107:
108: public Object nextElement() {
109: return it.next();
110: }
111:
112: };
113: Enumeration initParamNames = new Enumeration() {
114:
115: List keys = Arrays.asList(new Object[] { "key3" });
116:
117: Iterator it = keys.iterator();
118:
119: public boolean hasMoreElements() {
120: return it.hasNext();
121: }
122:
123: public Object nextElement() {
124: return it.next();
125: }
126:
127: };
128:
129: mockPortletContext.stubs().method("getAttributeNames").will(
130: returnValue(names));
131: mockPortletContext.stubs().method("getInitParameterNames")
132: .will(returnValue(initParamNames));
133: mockPortletContext.stubs().method("getAttribute").with(
134: eq("key1")).will(returnValue("value1"));
135: mockPortletContext.stubs().method("getAttribute").with(
136: eq("key2")).will(returnValue("value2"));
137: mockPortletContext.stubs().method("getInitParameter").with(
138: eq("key3")).will(returnValue("value3"));
139:
140: PortletApplicationMap map = new PortletApplicationMap(
141: portletContext);
142: Set entries = map.entrySet();
143:
144: assertEquals(3, entries.size());
145: Iterator it = entries.iterator();
146: Map.Entry entry = (Map.Entry) it.next();
147: assertEquals("key2", entry.getKey());
148: assertEquals("value2", entry.getValue());
149: entry = (Map.Entry) it.next();
150: assertEquals("key1", entry.getKey());
151: assertEquals("value1", entry.getValue());
152: entry = (Map.Entry) it.next();
153: assertEquals("key3", entry.getKey());
154: assertEquals("value3", entry.getValue());
155:
156: }
157:
158: public void testClear() {
159:
160: mockPortletContext.expects(once()).method("removeAttribute")
161: .with(eq("key1"));
162: mockPortletContext.expects(once()).method("removeAttribute")
163: .with(eq("key2"));
164:
165: ArrayList dummy = new ArrayList();
166: dummy.add("key1");
167: dummy.add("key2");
168:
169: mockPortletContext.expects(once()).method("getAttributeNames")
170: .will(returnValue(Collections.enumeration(dummy)));
171:
172: PortletApplicationMap map = new PortletApplicationMap(
173: portletContext);
174: map.clear();
175: }
176: }
|