001: /*
002: * $Id: PortletRequestMapTest.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.PortletRequest;
033:
034: import org.jmock.Mock;
035: import org.jmock.MockObjectTestCase;
036: import org.jmock.core.Constraint;
037:
038: /**
039: * PortletRequestMapTest. Insert description.
040: *
041: */
042: public class PortletRequestMapTest extends MockObjectTestCase {
043:
044: public void testSetAttribute() {
045:
046: }
047:
048: public void testGet() {
049: Mock mockRequest = mock(PortletRequest.class, "testGet");
050: mockRequest.expects(once()).method("getAttribute").with(
051: eq("testAttribute")).will(returnValue("testValue"));
052: PortletRequestMap map = new PortletRequestMap(
053: (PortletRequest) mockRequest.proxy());
054: String value = (String) map.get("testAttribute");
055: mockRequest.verify();
056: assertEquals("testValue", value);
057: }
058:
059: public void testPut() {
060: Mock mockRequest = mock(PortletRequest.class, "testPut");
061: Object value = new String("testValue");
062: Constraint[] params = new Constraint[] { eq("testAttribute"),
063: eq(value) };
064: mockRequest.expects(once()).method("setAttribute").with(params);
065: mockRequest.expects(once()).method("getAttribute").with(
066: eq("testAttribute")).will(returnValue(value));
067: PortletRequestMap map = new PortletRequestMap(
068: (PortletRequest) mockRequest.proxy());
069: Object obj = map.put("testAttribute", value);
070: mockRequest.verify();
071: assertEquals(obj, value);
072: }
073:
074: public void testClear() {
075: Mock mockRequest = mock(PortletRequest.class, "testClear");
076:
077: mockRequest.expects(once()).method("removeAttribute").with(
078: eq("a"));
079: mockRequest.expects(once()).method("removeAttribute").with(
080: eq("b"));
081:
082: ArrayList dummy = new ArrayList();
083: dummy.add("a");
084: dummy.add("b");
085:
086: mockRequest.expects(once()).method("getAttributeNames").will(
087: returnValue(Collections.enumeration(dummy)));
088:
089: PortletRequestMap map = new PortletRequestMap(
090: (PortletRequest) mockRequest.proxy());
091: map.clear();
092: mockRequest.verify();
093: }
094:
095: public void testRemove() {
096: Mock mockRequest = mock(PortletRequest.class);
097:
098: PortletRequest req = (PortletRequest) mockRequest.proxy();
099:
100: mockRequest.expects(once()).method("getAttribute").with(
101: eq("dummyKey")).will(returnValue("dummyValue"));
102:
103: mockRequest.expects(once()).method("removeAttribute").with(
104: eq("dummyKey"));
105:
106: PortletRequestMap map = new PortletRequestMap(req);
107: Object ret = map.remove("dummyKey");
108: assertEquals("dummyValue", ret);
109: }
110:
111: public void testEntrySet() {
112: Mock mockRequest = mock(PortletRequest.class);
113:
114: PortletRequest req = (PortletRequest) mockRequest.proxy();
115:
116: Enumeration names = new Enumeration() {
117:
118: List keys = Arrays.asList(new Object[] { "key1", "key2" });
119: Iterator it = keys.iterator();
120:
121: public boolean hasMoreElements() {
122: return it.hasNext();
123: }
124:
125: public Object nextElement() {
126: return it.next();
127: }
128:
129: };
130:
131: mockRequest.stubs().method("getAttributeNames").will(
132: returnValue(names));
133: mockRequest.stubs().method("getAttribute").with(eq("key1"))
134: .will(returnValue("value1"));
135: mockRequest.stubs().method("getAttribute").with(eq("key2"))
136: .will(returnValue("value2"));
137:
138: PortletRequestMap map = new PortletRequestMap(req);
139: Set entries = map.entrySet();
140:
141: assertEquals(2, 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:
150: }
151:
152: }
|