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.wicket.protocol.http.mock; 018 019import java.io.Serializable; 020import java.util.Collections; 021import java.util.Enumeration; 022import java.util.UUID; 023 024import javax.servlet.ServletContext; 025import javax.servlet.http.HttpSession; 026 027import org.apache.wicket.Session; 028import org.apache.wicket.util.tester.BaseWicketTester; 029import org.apache.wicket.util.value.ValueMap; 030 031 032/** 033 * Mock implementation of the <code>WebSession</code> interface for use by the test harnesses. 034 * 035 * @author Chris Turner 036 */ 037public class MockHttpSession implements HttpSession, Serializable 038{ 039 private static final long serialVersionUID = 1L; 040 041 private final ValueMap attributes = new ValueMap(); 042 043 private final transient ServletContext context; 044 045 private final long creationTime = System.currentTimeMillis(); 046 047 private String id = generateSessionId(); 048 049 private long lastAccessedTime = 0; 050 051 private boolean temporary = true; 052 053 /** 054 * Create the session. 055 * 056 * @param context 057 */ 058 public MockHttpSession(final ServletContext context) 059 { 060 this.context = context; 061 } 062 063 /** 064 * Get the attribute with the given name. 065 * 066 * @param name 067 * The attribute name 068 * @return The value or null 069 */ 070 @Override 071 public Object getAttribute(final String name) 072 { 073 return attributes.get(name); 074 } 075 076 /** 077 * Get the names of the attributes in the session. 078 * 079 * @return The attribute names 080 */ 081 @Override 082 public Enumeration<String> getAttributeNames() 083 { 084 return Collections.enumeration(attributes.keySet()); 085 086 } 087 088 /** 089 * Get the creation time of the session. 090 * 091 * @return The creation time 092 */ 093 @Override 094 public long getCreationTime() 095 { 096 return creationTime; 097 } 098 099 /** 100 * Return the id of this session. 101 * 102 * @return The id 103 */ 104 @Override 105 public String getId() 106 { 107 return id; 108 } 109 110 /** 111 * Get the time the session was last accessed. 112 * 113 * @return The last accessed time 114 */ 115 @Override 116 public long getLastAccessedTime() 117 { 118 return lastAccessedTime; 119 } 120 121 /** 122 * NOT USED. Sessions never expire in the test harness. 123 * 124 * @return Always returns 0 125 */ 126 @Override 127 public int getMaxInactiveInterval() 128 { 129 return 0; 130 } 131 132 /** 133 * Return the servlet context for the session. 134 * 135 * @return The servlet context 136 */ 137 @Override 138 public ServletContext getServletContext() 139 { 140 return context; 141 } 142 143 /** 144 * NOT USED. 145 * 146 * @return Always null 147 * @deprecated 148 */ 149 @Override 150 @Deprecated 151 public javax.servlet.http.HttpSessionContext getSessionContext() 152 { 153 return null; 154 } 155 156 /** 157 * Get the value for the given name. 158 * 159 * @param name 160 * The name 161 * @return The value or null 162 * @deprecated use getAttribute(String) instead 163 */ 164 @Override 165 @Deprecated 166 public Object getValue(final String name) 167 { 168 return getAttribute(name); 169 } 170 171 /** 172 * Get the names of the values in the session. 173 * 174 * @return The names of the attributes 175 * @deprecated use getAttributeNames() instead 176 */ 177 @Override 178 @Deprecated 179 public String[] getValueNames() 180 { 181 String[] result = new String[attributes.size()]; 182 return attributes.keySet().toArray(result); 183 } 184 185 /** 186 * Invalidate the session. 187 */ 188 @Override 189 public void invalidate() 190 { 191 Session session = (Session) attributes.get("wicket:" + BaseWicketTester.TestFilterConfig.class.getName() + ":session"); 192 if (session != null) 193 { 194 session.onInvalidate(); 195 } 196 attributes.clear(); 197 id = generateSessionId(); 198 } 199 200 /** 201 * Check if the session is new. 202 * 203 * @return Always false 204 */ 205 @Override 206 public boolean isNew() 207 { 208 return false; 209 } 210 211 /** 212 * Set a value. 213 * 214 * @param name 215 * The name of the value 216 * @param o 217 * The value 218 * @deprecated Use setAttribute(String, Object) instead 219 */ 220 @Override 221 @Deprecated 222 public void putValue(final String name, final Object o) 223 { 224 setAttribute(name, o); 225 } 226 227 /** 228 * Remove an attribute. 229 * 230 * @param name 231 * The name of the attribute 232 */ 233 @Override 234 public void removeAttribute(final String name) 235 { 236 attributes.remove(name); 237 } 238 239 /** 240 * Remove a value. 241 * 242 * @param name 243 * The name of the value 244 * @deprecated Use removeAttribute(String) instead 245 */ 246 @Override 247 @Deprecated 248 public void removeValue(String name) 249 { 250 removeAttribute(name); 251 } 252 253 /** 254 * Set an attribute. 255 * 256 * @param name 257 * The name of the attribute to set 258 * @param o 259 * The value to set 260 */ 261 @Override 262 public void setAttribute(final String name, final Object o) 263 { 264 attributes.put(name, o); 265 } 266 267 /** 268 * NOT USED. Sessions never expire in the test harness. 269 * 270 * @param i 271 * The value 272 */ 273 @Override 274 public void setMaxInactiveInterval(final int i) 275 { 276 } 277 278 /** 279 * Set the last accessed time for the session. 280 */ 281 public void timestamp() 282 { 283 lastAccessedTime = System.currentTimeMillis(); 284 } 285 286 /** 287 * Indicates the state of the session. Temporary or persisted. 288 * 289 * @return true if this is a temporary session, false otherwise 290 */ 291 public final boolean isTemporary() 292 { 293 return temporary; 294 } 295 296 /** 297 * Changes the state of this session. Temporary or persisted. Upon creation all sessions are 298 * temporary. 299 * 300 * @param temporary 301 * true, for a temporary session, false for a persisted session 302 */ 303 public final void setTemporary(boolean temporary) 304 { 305 this.temporary = temporary; 306 } 307 308 private static String generateSessionId() 309 { 310 return UUID.randomUUID().toString().replace(':', '_').replace('-', '_'); 311 } 312}