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.juneau.rest.mock; 018 019import static org.apache.juneau.commons.utils.CollectionUtils.*; 020 021import java.util.*; 022 023import jakarta.servlet.*; 024import jakarta.servlet.http.*; 025 026/** 027 * An implementation of {@link HttpSession} for mocking purposes. 028 * 029 * <p> 030 * Session-based tests can use this API to create customized instances of {@link HttpSession} objects 031 * that can be passed to the 032 * {@link org.apache.juneau.rest.mock.classic.MockRestRequest#httpSession(HttpSession)} method. 033 * 034 * <h5 class='section'>See Also:</h5><ul> 035 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestMock">juneau-rest-mock</a> 036 * </ul> 037 */ 038public class MockHttpSession implements HttpSession { 039 040 /** 041 * Creates a new HTTP session. 042 * 043 * @return A new HTTP session. 044 */ 045 public static MockHttpSession create() { 046 return new MockHttpSession(); 047 } 048 049 private final Map<String,Object> attributes = map(); 050 private long creationTime; 051 private long lastAccessedTime; 052 private int maxInactiveInterval; 053 private String id; 054 private ServletContext servletContext; 055 056 private boolean isNew; 057 058 /** 059 * Sets the creation time on this session. 060 * 061 * <p> 062 * Affects the results of calling {@link HttpSession#getCreationTime()}. 063 * 064 * @param value The new value for this setting. 065 * @return This object. 066 */ 067 public MockHttpSession creationTime(long value) { 068 creationTime = value; 069 return this; 070 } 071 072 @Override /* Overridden from HttpSession */ 073 public Object getAttribute(String name) { 074 return attributes.get(name); 075 } 076 077 @Override /* Overridden from HttpSession */ 078 public Enumeration<String> getAttributeNames() { return Collections.enumeration(attributes.keySet()); } 079 080 @Override /* Overridden from HttpSession */ 081 public long getCreationTime() { return creationTime; } 082 083 @Override /* Overridden from HttpSession */ 084 public String getId() { return id; } 085 086 @Override /* Overridden from HttpSession */ 087 public long getLastAccessedTime() { return lastAccessedTime; } 088 089 @Override /* Overridden from HttpSession */ 090 public int getMaxInactiveInterval() { return maxInactiveInterval; } 091 092 @Override /* Overridden from HttpSession */ 093 public ServletContext getServletContext() { return servletContext; } 094 095 /** 096 * Sets the id on this session. 097 * 098 * <p> 099 * Affects the results of calling {@link HttpSession#getId()}. 100 * 101 * @param value The new value for this setting. 102 * @return This object. 103 */ 104 public MockHttpSession id(String value) { 105 id = value; 106 return this; 107 } 108 109 @Override /* Overridden from HttpSession */ 110 public void invalidate() { 111 // No-op: Mock implementation - full functionality not required 112 } 113 114 @Override /* Overridden from HttpSession */ 115 public boolean isNew() { return isNew; } 116 117 /** 118 * Sets the is-new value on this session. 119 * 120 * <p> 121 * Affects the results of calling {@link HttpSession#isNew()}. 122 * 123 * @param value The new value for this setting. 124 * @return This object. 125 */ 126 public MockHttpSession isNew(boolean value) { 127 isNew = value; 128 return this; 129 } 130 131 /** 132 * Sets the last-accessed time on this session. 133 * 134 * <p> 135 * Affects the results of calling {@link HttpSession#getLastAccessedTime()}. 136 * 137 * @param value The new value for this setting. 138 * @return This object. 139 */ 140 public MockHttpSession lastAccessedTime(long value) { 141 lastAccessedTime = value; 142 return this; 143 } 144 145 /** 146 * Sets the max-inactive interval time on this session. 147 * 148 * <p> 149 * Affects the results of calling {@link HttpSession#getMaxInactiveInterval()}. 150 * 151 * @param value The new value for this setting. 152 * @return This object. 153 */ 154 public MockHttpSession maxInactiveInterval(int value) { 155 maxInactiveInterval = value; 156 return this; 157 } 158 159 @Override /* Overridden from HttpSession */ 160 public void removeAttribute(String name) { 161 attributes.remove(name); 162 } 163 164 /** 165 * Sets the servlet context on this session. 166 * 167 * <p> 168 * Affects the results of calling {@link HttpSession#getServletContext()}. 169 * 170 * @param value The new value for this setting. 171 * @return This object. 172 */ 173 public MockHttpSession servletContext(ServletContext value) { 174 servletContext = value; 175 return this; 176 } 177 178 @Override /* Overridden from HttpSession */ 179 public void setAttribute(String name, Object value) { 180 attributes.put(name, value); 181 } 182 183 @Override /* Overridden from HttpSession */ 184 public void setMaxInactiveInterval(int value) { this.maxInactiveInterval = value; } 185}