001: /*
002: * Created on Mar 10, 2006
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package com.opensymphony.webwork.portlet;
008:
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.Collections;
012: import java.util.Enumeration;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Map;
016: import java.util.Set;
017:
018: import javax.portlet.PortletContext;
019: import javax.portlet.PortletRequest;
020:
021: import org.jmock.Mock;
022: import org.jmock.MockObjectTestCase;
023: import org.jmock.core.Constraint;
024:
025: /**
026: * @author Nils-Helge Garli
027: *
028: * TODO To change the template for this generated type comment go to Window -
029: * Preferences - Java - Code Style - Code Templates
030: */
031: public class PortletApplicationMapTest extends MockObjectTestCase {
032:
033: Mock mockPortletContext;
034:
035: PortletContext portletContext;
036:
037: public void setUp() throws Exception {
038: super .setUp();
039: mockPortletContext = mock(PortletContext.class);
040: portletContext = (PortletContext) mockPortletContext.proxy();
041: }
042:
043: public void testGetFromAttributes() {
044: mockPortletContext.stubs().method("getAttribute").with(
045: eq("dummyKey")).will(returnValue("dummyValue"));
046:
047: PortletApplicationMap map = new PortletApplicationMap(
048: (PortletContext) mockPortletContext.proxy());
049:
050: assertEquals("dummyValue", map.get("dummyKey"));
051: }
052:
053: public void testGetFromInitParameters() {
054: mockPortletContext.stubs().method("getAttribute").with(
055: eq("dummyKey"));
056: mockPortletContext.stubs().method("getInitParameter").with(
057: eq("dummyKey")).will(returnValue("dummyValue"));
058:
059: PortletApplicationMap map = new PortletApplicationMap(
060: (PortletContext) mockPortletContext.proxy());
061:
062: assertEquals("dummyValue", map.get("dummyKey"));
063: }
064:
065: public void testPut() {
066: mockPortletContext.expects(once()).method("setAttribute").with(
067: new Constraint[] { eq("dummyKey"), eq("dummyValue") });
068: mockPortletContext.expects(once()).method("getAttribute").with(
069: eq("dummyKey")).will(returnValue("dummyValue"));
070: PortletApplicationMap map = new PortletApplicationMap(
071: portletContext);
072: Object val = map.put("dummyKey", "dummyValue");
073: assertEquals("dummyValue", val);
074: }
075:
076: public void testRemove() {
077: mockPortletContext.expects(once()).method("getAttribute").with(
078: eq("dummyKey")).will(returnValue("dummyValue"));
079: mockPortletContext.expects(once()).method("removeAttribute")
080: .with(eq("dummyKey"));
081: PortletApplicationMap map = new PortletApplicationMap(
082: portletContext);
083: Object val = map.remove("dummyKey");
084: assertEquals("dummyValue", val);
085: }
086:
087: public void testEntrySet() {
088:
089: Enumeration names = new Enumeration() {
090:
091: List keys = Arrays.asList(new Object[] { "key1", "key2" });
092:
093: Iterator it = keys.iterator();
094:
095: public boolean hasMoreElements() {
096: return it.hasNext();
097: }
098:
099: public Object nextElement() {
100: return it.next();
101: }
102:
103: };
104: Enumeration initParamNames = new Enumeration() {
105:
106: List keys = Arrays.asList(new Object[] { "key3" });
107:
108: Iterator it = keys.iterator();
109:
110: public boolean hasMoreElements() {
111: return it.hasNext();
112: }
113:
114: public Object nextElement() {
115: return it.next();
116: }
117:
118: };
119:
120: mockPortletContext.stubs().method("getAttributeNames").will(
121: returnValue(names));
122: mockPortletContext.stubs().method("getInitParameterNames")
123: .will(returnValue(initParamNames));
124: mockPortletContext.stubs().method("getAttribute").with(
125: eq("key1")).will(returnValue("value1"));
126: mockPortletContext.stubs().method("getAttribute").with(
127: eq("key2")).will(returnValue("value2"));
128: mockPortletContext.stubs().method("getInitParameter").with(
129: eq("key3")).will(returnValue("value3"));
130:
131: PortletApplicationMap map = new PortletApplicationMap(
132: portletContext);
133: Set entries = map.entrySet();
134:
135: assertEquals(3, entries.size());
136: Iterator it = entries.iterator();
137: Map.Entry entry = (Map.Entry) it.next();
138: assertEquals("key2", entry.getKey());
139: assertEquals("value2", entry.getValue());
140: entry = (Map.Entry) it.next();
141: assertEquals("key1", entry.getKey());
142: assertEquals("value1", entry.getValue());
143: entry = (Map.Entry) it.next();
144: assertEquals("key3", entry.getKey());
145: assertEquals("value3", entry.getValue());
146:
147: }
148:
149: public void testClear() {
150:
151: mockPortletContext.expects(once()).method("removeAttribute")
152: .with(eq("key1"));
153: mockPortletContext.expects(once()).method("removeAttribute")
154: .with(eq("key2"));
155:
156: ArrayList dummy = new ArrayList();
157: dummy.add("key1");
158: dummy.add("key2");
159:
160: mockPortletContext.expects(once()).method("getAttributeNames")
161: .will(returnValue(Collections.enumeration(dummy)));
162:
163: PortletApplicationMap map = new PortletApplicationMap(
164: portletContext);
165: map.clear();
166: }
167: }
|