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.jakarta.servlet;
018
019import java.util.Enumeration;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.commons.chain.web.MockEnumeration;
024
025import jakarta.servlet.ServletContext;
026import jakarta.servlet.http.HttpSession;
027
028/**
029 * Mock Object for {@code HttpSession} (Version 2.3)
030 */
031public class MockHttpSession implements HttpSession {
032    public MockHttpSession() {
033    }
034
035    public MockHttpSession(ServletContext servletContext) {
036        setServletContext(servletContext);
037    }
038
039    protected Map<String, Object> attributes = new HashMap<>();
040    protected ServletContext servletContext = null;
041
042    // --------------------------------------------------------- Public Methods
043
044    public void setServletContext(ServletContext servletContext) {
045        this.servletContext = servletContext;
046    }
047
048    // ---------------------------------------------------- HttpSession Methods
049
050    @Override
051    public Object getAttribute(String name) {
052        return attributes.get(name);
053    }
054
055    @Override
056    public Enumeration<String> getAttributeNames() {
057        return new MockEnumeration<>(attributes.keySet().iterator());
058    }
059
060    @Override
061    public long getCreationTime() {
062        throw new UnsupportedOperationException();
063    }
064
065    @Override
066    public String getId() {
067        throw new UnsupportedOperationException();
068    }
069
070    @Override
071    public long getLastAccessedTime() {
072        throw new UnsupportedOperationException();
073    }
074
075    @Override
076    public int getMaxInactiveInterval() {
077        throw new UnsupportedOperationException();
078    }
079
080    @Override
081    public ServletContext getServletContext() {
082        return this.servletContext;
083    }
084
085    @Override
086    @SuppressWarnings("deprecation")
087    public jakarta.servlet.http.HttpSessionContext getSessionContext() {
088        throw new UnsupportedOperationException();
089    }
090
091    @Deprecated
092    @Override
093    public Object getValue(String name) {
094        throw new UnsupportedOperationException();
095    }
096
097    @Deprecated
098    @Override
099    public String[] getValueNames() {
100        throw new UnsupportedOperationException();
101    }
102
103    @Override
104    public void invalidate() {
105        throw new UnsupportedOperationException();
106    }
107
108    @Override
109    public boolean isNew() {
110        throw new UnsupportedOperationException();
111    }
112
113    @Deprecated
114    @Override
115    public void putValue(String name, Object value) {
116        throw new UnsupportedOperationException();
117    }
118
119    @Override
120    public void removeAttribute(String name) {
121        attributes.remove(name);
122    }
123
124    @Deprecated
125    @Override
126    public void removeValue(String name) {
127        throw new UnsupportedOperationException();
128    }
129
130    @Override
131    public void setAttribute(String name, Object value) {
132        if (value == null) {
133            attributes.remove(name);
134        } else {
135            attributes.put(name, value);
136        }
137    }
138
139    @Override
140    public void setMaxInactiveInterval(int interval) {
141        throw new UnsupportedOperationException();
142    }
143}