001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet;
006:
007: import java.util.ArrayList;
008: import java.util.Arrays;
009: import java.util.Collections;
010: import java.util.Enumeration;
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Map;
014: import java.util.Set;
015:
016: import javax.portlet.PortletRequest;
017: import javax.portlet.PortletSession;
018:
019: import org.jmock.Mock;
020: import org.jmock.MockObjectTestCase;
021: import org.jmock.core.Constraint;
022:
023: /**
024: * PortletRequestMapTest. Insert description.
025: *
026: * @author Nils-Helge Garli
027: * @version $Revision: 2350 $ $Date: 2006-03-10 10:39:24 +0100 (Fri, 10 Mar 2006) $
028: */
029: public class PortletRequestMapTest extends MockObjectTestCase {
030:
031: public void testSetAttribute() {
032:
033: }
034:
035: public void testGet() {
036: Mock mockRequest = mock(PortletRequest.class, "testGet");
037: mockRequest.expects(once()).method("getAttribute").with(
038: eq("testAttribute")).will(returnValue("testValue"));
039: PortletRequestMap map = new PortletRequestMap(
040: (PortletRequest) mockRequest.proxy());
041: String value = (String) map.get("testAttribute");
042: mockRequest.verify();
043: assertEquals("testValue", value);
044: }
045:
046: public void testPut() {
047: Mock mockRequest = mock(PortletRequest.class, "testPut");
048: Object value = new String("testValue");
049: Constraint[] params = new Constraint[] { eq("testAttribute"),
050: eq(value) };
051: mockRequest.expects(once()).method("setAttribute").with(params);
052: mockRequest.expects(once()).method("getAttribute").with(
053: eq("testAttribute")).will(returnValue(value));
054: PortletRequestMap map = new PortletRequestMap(
055: (PortletRequest) mockRequest.proxy());
056: Object obj = map.put("testAttribute", value);
057: mockRequest.verify();
058: assertEquals(obj, value);
059: }
060:
061: public void testClear() {
062: Mock mockRequest = mock(PortletRequest.class, "testClear");
063:
064: mockRequest.expects(once()).method("removeAttribute").with(
065: eq("a"));
066: mockRequest.expects(once()).method("removeAttribute").with(
067: eq("b"));
068:
069: ArrayList dummy = new ArrayList();
070: dummy.add("a");
071: dummy.add("b");
072:
073: mockRequest.expects(once()).method("getAttributeNames").will(
074: returnValue(Collections.enumeration(dummy)));
075:
076: PortletRequestMap map = new PortletRequestMap(
077: (PortletRequest) mockRequest.proxy());
078: map.clear();
079: mockRequest.verify();
080: }
081:
082: public void testRemove() {
083: Mock mockRequest = mock(PortletRequest.class);
084:
085: PortletRequest req = (PortletRequest) mockRequest.proxy();
086:
087: mockRequest.expects(once()).method("getAttribute").with(
088: eq("dummyKey")).will(returnValue("dummyValue"));
089:
090: mockRequest.expects(once()).method("removeAttribute").with(
091: eq("dummyKey"));
092:
093: PortletRequestMap map = new PortletRequestMap(req);
094: Object ret = map.remove("dummyKey");
095: assertEquals("dummyValue", ret);
096: }
097:
098: public void testEntrySet() {
099: Mock mockRequest = mock(PortletRequest.class);
100:
101: PortletRequest req = (PortletRequest) mockRequest.proxy();
102:
103: Enumeration names = new Enumeration() {
104:
105: List keys = Arrays.asList(new Object[] { "key1", "key2" });
106: Iterator it = keys.iterator();
107:
108: public boolean hasMoreElements() {
109: return it.hasNext();
110: }
111:
112: public Object nextElement() {
113: return it.next();
114: }
115:
116: };
117:
118: mockRequest.stubs().method("getAttributeNames").will(
119: returnValue(names));
120: mockRequest.stubs().method("getAttribute").with(eq("key1"))
121: .will(returnValue("value1"));
122: mockRequest.stubs().method("getAttribute").with(eq("key2"))
123: .will(returnValue("value2"));
124:
125: PortletRequestMap map = new PortletRequestMap(req);
126: Set entries = map.entrySet();
127:
128: assertEquals(2, entries.size());
129: Iterator it = entries.iterator();
130: Map.Entry entry = (Map.Entry) it.next();
131: assertEquals("key2", entry.getKey());
132: assertEquals("value2", entry.getValue());
133: entry = (Map.Entry) it.next();
134: assertEquals("key1", entry.getKey());
135: assertEquals("value1", entry.getValue());
136:
137: }
138:
139: }
|