001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.mock2; 014 015import java.util.*; 016 017import javax.servlet.*; 018import javax.servlet.http.*; 019 020/** 021 * An implementation of {@link HttpSession} for mocking purposes. 022 * 023 * <p> 024 * Session-based tests can use this API to create customized instances of {@link HttpSession} objects 025 * that can be passed to the {@link MockRestRequest#httpSession(HttpSession)} method. 026 * 027 * <ul class='seealso'> 028 * <li class='link'>{@doc juneau-rest-mock} 029 * </ul> 030 */ 031public class MockHttpSession implements HttpSession { 032 033 private Map<String,Object> attributes = new LinkedHashMap<>(), values = new LinkedHashMap<>(); 034 035 private long creationTime, lastAccessedTime; 036 private int maxInactiveInterval; 037 private String id; 038 private ServletContext servletContext; 039 private boolean isNew = false; 040 041 /** 042 * Creates a new HTTP session. 043 * 044 * @return A new HTTP session. 045 */ 046 public static MockHttpSession create() { 047 return new MockHttpSession(); 048 } 049 050 //------------------------------------------------------------------------------------------------------------------ 051 // Setter methods 052 //------------------------------------------------------------------------------------------------------------------ 053 054 /** 055 * Sets the creation time on this session. 056 * 057 * <p> 058 * Affects the results of calling {@link HttpSession#getCreationTime()}. 059 * 060 * @param value The new value for this setting. 061 * @return This object (for method chaining). 062 */ 063 public MockHttpSession creationTime(long value) { 064 this.creationTime = value; 065 return this; 066 } 067 068 /** 069 * Sets the last-accessed time on this session. 070 * 071 * <p> 072 * Affects the results of calling {@link HttpSession#getLastAccessedTime()}. 073 * 074 * @param value The new value for this setting. 075 * @return This object (for method chaining). 076 */ 077 public MockHttpSession lastAccessedTime(long value) { 078 this.lastAccessedTime = value; 079 return this; 080 } 081 082 /** 083 * Sets the max-inactive interval time on this session. 084 * 085 * <p> 086 * Affects the results of calling {@link HttpSession#getMaxInactiveInterval()}. 087 * 088 * @param value The new value for this setting. 089 * @return This object (for method chaining). 090 */ 091 public MockHttpSession maxInactiveInterval(int value) { 092 this.maxInactiveInterval = value; 093 return this; 094 } 095 096 /** 097 * Sets the id on this session. 098 * 099 * <p> 100 * Affects the results of calling {@link HttpSession#getId()}. 101 * 102 * @param value The new value for this setting. 103 * @return This object (for method chaining). 104 */ 105 public MockHttpSession id(String value) { 106 this.id = value; 107 return this; 108 } 109 110 /** 111 * Sets the servlet context on this session. 112 * 113 * <p> 114 * Affects the results of calling {@link HttpSession#getServletContext()}. 115 * 116 * @param value The new value for this setting. 117 * @return This object (for method chaining). 118 */ 119 public MockHttpSession servletContext(ServletContext value) { 120 this.servletContext = value; 121 return this; 122 } 123 124 /** 125 * Sets the is-new value on this session. 126 * 127 * <p> 128 * Affects the results of calling {@link HttpSession#isNew()}. 129 * 130 * @param value The new value for this setting. 131 * @return This object (for method chaining). 132 */ 133 public MockHttpSession isNew(boolean value) { 134 this.isNew = value; 135 return this; 136 } 137 138 //------------------------------------------------------------------------------------------------------------------ 139 // HttpSession methods 140 //------------------------------------------------------------------------------------------------------------------ 141 142 @Override /* HttpSession */ 143 public long getCreationTime() { 144 return creationTime; 145 } 146 147 @Override /* HttpSession */ 148 public String getId() { 149 return id; 150 } 151 152 @Override /* HttpSession */ 153 public long getLastAccessedTime() { 154 return lastAccessedTime; 155 } 156 157 @Override /* HttpSession */ 158 public ServletContext getServletContext() { 159 return servletContext; 160 } 161 162 @Override /* HttpSession */ 163 public void setMaxInactiveInterval(int value) { 164 this.maxInactiveInterval = value; 165 } 166 167 @Override /* HttpSession */ 168 public int getMaxInactiveInterval() { 169 return maxInactiveInterval; 170 } 171 172 @SuppressWarnings("deprecation") 173 @Override /* HttpSession */ 174 public HttpSessionContext getSessionContext() { 175 return null; 176 } 177 178 @Override /* HttpSession */ 179 public Object getAttribute(String name) { 180 return attributes.get(name); 181 } 182 183 @Override /* HttpSession */ 184 public Object getValue(String name) { 185 return values.get(name); 186 } 187 188 @Override /* HttpSession */ 189 public Enumeration<String> getAttributeNames() { 190 return Collections.enumeration(attributes.keySet()); 191 } 192 193 @Override /* HttpSession */ 194 public String[] getValueNames() { 195 return values.keySet().toArray(new String[0]); 196 } 197 198 @Override /* HttpSession */ 199 public void setAttribute(String name, Object value) { 200 attributes.put(name, value); 201 } 202 203 @Override /* HttpSession */ 204 public void putValue(String name, Object value) { 205 values.put(name, value); 206 } 207 208 @Override /* HttpSession */ 209 public void removeAttribute(String name) { 210 attributes.remove(name); 211 } 212 213 @Override /* HttpSession */ 214 public void removeValue(String name) { 215 values.remove(name); 216 } 217 218 @Override /* HttpSession */ 219 public void invalidate() { 220 } 221 222 @Override /* HttpSession */ 223 public boolean isNew() { 224 return isNew; 225 } 226}