001: /*
002: * Copyright 2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web.portlet;
017:
018: import org.apache.commons.chain.web.MockEnumeration;
019:
020: import javax.portlet.PortletContext;
021: import javax.portlet.PortletSession;
022: import javax.portlet.PortletContext;
023: import java.util.Enumeration;
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.Date;
027:
028: // Mock Object for PortletSession
029: public class MockPortletSession implements PortletSession {
030:
031: private Date creationTime = new Date();
032: private Date lastAccessedTime = creationTime;
033:
034: private PortletContext context = null;
035: private int maxInactiveInterval = 100;
036: private boolean newSession = true;
037: private String id = "mockId" + creationTime.getTime();
038: private Map portletScope = new HashMap();
039: private Map applicationScope = new HashMap();
040:
041: public MockPortletSession() {
042: this (null);
043: }
044:
045: public MockPortletSession(PortletContext context) {
046: this .context = (context == null ? new MockPortletContext()
047: : context);
048: }
049:
050: // --------------------------------------------------------- Public Methods
051:
052: public void setPortletContext(PortletContext context) {
053: this .context = context;
054: }
055:
056: public void setNew(boolean newSession) {
057: this .newSession = newSession;
058: }
059:
060: public void setNew(String id) {
061: this .id = id;
062: }
063:
064: // ---------------------------------------------------- PortletSession Methods
065:
066: public Object getAttribute(String name) {
067: accessed();
068: return getAttribute(name, PortletSession.PORTLET_SCOPE);
069: }
070:
071: public Object getAttribute(String name, int scope) {
072: accessed();
073: return getScope(scope).get(name);
074: }
075:
076: public Enumeration getAttributeNames() {
077: accessed();
078: return getAttributeNames(PortletSession.PORTLET_SCOPE);
079: }
080:
081: public Enumeration getAttributeNames(int scope) {
082: accessed();
083: return new MockEnumeration(getScope(scope).keySet().iterator());
084: }
085:
086: public long getCreationTime() {
087: accessed();
088: return creationTime.getTime();
089: }
090:
091: public String getId() {
092: accessed();
093: return id;
094: }
095:
096: public long getLastAccessedTime() {
097: return lastAccessedTime.getTime();
098: }
099:
100: public int getMaxInactiveInterval() {
101: accessed();
102: return maxInactiveInterval;
103: }
104:
105: public PortletContext getPortletContext() {
106: accessed();
107: return context;
108: }
109:
110: public void invalidate() {
111: throw new UnsupportedOperationException();
112: }
113:
114: public boolean isNew() {
115: accessed();
116: return newSession;
117: }
118:
119: public void removeAttribute(String name) {
120: accessed();
121: removeAttribute(name, PortletSession.PORTLET_SCOPE);
122: }
123:
124: public void removeAttribute(String name, int scope) {
125: accessed();
126: getScope(scope).remove(name);
127: }
128:
129: public void setAttribute(String name, Object value) {
130: accessed();
131: setAttribute(name, value, PortletSession.PORTLET_SCOPE);
132: }
133:
134: public void setAttribute(String name, Object value, int scope) {
135: accessed();
136: getScope(scope).put(name, value);
137: }
138:
139: public void setMaxInactiveInterval(int interval) {
140: accessed();
141: this .maxInactiveInterval = interval;
142: }
143:
144: private void accessed() {
145: lastAccessedTime = new Date();
146: }
147:
148: private Map getScope(int scope) {
149: if (scope == PortletSession.PORTLET_SCOPE) {
150: return portletScope;
151: } else if (scope == PortletSession.APPLICATION_SCOPE) {
152: return applicationScope;
153: } else {
154: throw new IllegalArgumentException("Invalid scope: "
155: + scope);
156: }
157: }
158:
159: }
|