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