blob: 3031b31e2df487e7e02b9948cbf1cbc1d645d8e8 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.chain2.web.portlet;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.portlet.PortletContext;
import javax.portlet.PortletSession;
import org.apache.commons.chain2.web.MockEnumeration;
// Mock Object for PortletSession
public class MockPortletSession implements PortletSession {
private Date creationTime = new Date();
private Date lastAccessedTime = creationTime;
private PortletContext context = null;
private int maxInactiveInterval = 100;
private boolean newSession = true;
private String id = "mockId" + creationTime.getTime();
private Map<String, Object> portletScope = new HashMap<String, Object>();
private Map<String, Object> applicationScope = new HashMap<String, Object>();
public MockPortletSession() {
this(null);
}
public MockPortletSession(PortletContext context) {
this.context = (context == null ? new MockPortletContext() : context);
}
// --------------------------------------------------------- Public Methods
public void setPortletContext(PortletContext context) {
this.context = context;
}
public void setNew(boolean newSession) {
this.newSession = newSession;
}
public void setNew(String id) {
this.id = id;
}
// ---------------------------------------------------- PortletSession Methods
public Object getAttribute(String name) {
accessed();
return getAttribute(name, PortletSession.PORTLET_SCOPE);
}
public Object getAttribute(String name, int scope) {
accessed();
return getScope(scope).get(name);
}
public Enumeration<String> getAttributeNames() {
accessed();
return getAttributeNames(PortletSession.PORTLET_SCOPE);
}
public Enumeration<String> getAttributeNames(int scope) {
accessed();
return new MockEnumeration<String>(getScope(scope).keySet().iterator());
}
public long getCreationTime() {
accessed();
return creationTime.getTime();
}
public String getId() {
accessed();
return id;
}
public long getLastAccessedTime() {
return lastAccessedTime.getTime();
}
public int getMaxInactiveInterval() {
accessed();
return maxInactiveInterval;
}
public PortletContext getPortletContext() {
accessed();
return context;
}
public void invalidate() {
throw new UnsupportedOperationException();
}
public boolean isNew() {
accessed();
return newSession;
}
public void removeAttribute(String name) {
accessed();
removeAttribute(name, PortletSession.PORTLET_SCOPE);
}
public void removeAttribute(String name, int scope) {
accessed();
getScope(scope).remove(name);
}
public void setAttribute(String name, Object value) {
accessed();
setAttribute(name, value, PortletSession.PORTLET_SCOPE);
}
public void setAttribute(String name, Object value, int scope) {
accessed();
getScope(scope).put(name, value);
}
public void setMaxInactiveInterval(int interval) {
accessed();
this.maxInactiveInterval = interval;
}
private void accessed() {
lastAccessedTime = new Date();
}
private Map<String, Object> getScope(int scope) {
if (scope == PortletSession.PORTLET_SCOPE) {
return portletScope;
} else if (scope == PortletSession.APPLICATION_SCOPE) {
return applicationScope;
} else {
throw new IllegalArgumentException("Invalid scope: " + scope);
}
}
public Map<String, Object> getAttributeMap() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Map<String, Object> getAttributeMap(int scope) {
throw new UnsupportedOperationException("Not supported yet.");
}
}