|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2009 the original author or authors. |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -33,23 +33,25 @@ import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Mock implementation of the {@link javax.servlet.http.HttpSession} interface. |
|
|
|
* Mock implementation of the {@link javax.servlet.http.HttpSession} interface. |
|
|
|
* Supports the Servlet 2.4 API level. |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Used for testing the web framework; also useful for testing |
|
|
|
* <p>Compatible with Servlet 2.5 as well as Servlet 3.0. |
|
|
|
* application controllers. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Used for testing the web framework; also useful for testing application |
|
|
|
|
|
|
|
* controllers. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Rod Johnson |
|
|
|
* @author Rod Johnson |
|
|
|
* @author Mark Fisher |
|
|
|
* @author Mark Fisher |
|
|
|
|
|
|
|
* @author Sam Brannen |
|
|
|
* @since 1.0.2 |
|
|
|
* @since 1.0.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
public class MockHttpSession implements HttpSession { |
|
|
|
public class MockHttpSession implements HttpSession { |
|
|
|
|
|
|
|
|
|
|
|
public static final String SESSION_COOKIE_NAME = "JSESSION"; |
|
|
|
public static final String SESSION_COOKIE_NAME = "JSESSION"; |
|
|
|
|
|
|
|
|
|
|
|
private static int nextId = 1; |
|
|
|
private static int nextId = 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String id; |
|
|
|
private final String id; |
|
|
|
|
|
|
|
|
|
|
|
private final long creationTime = System.currentTimeMillis(); |
|
|
|
private final long creationTime = System.currentTimeMillis(); |
|
|
|
@ -68,8 +70,9 @@ public class MockHttpSession implements HttpSession { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a new MockHttpSession with a default {@link org.springframework.mock.web.MockServletContext}. |
|
|
|
* Create a new MockHttpSession with a default {@link MockServletContext}. |
|
|
|
* @see org.springframework.mock.web.MockServletContext |
|
|
|
* |
|
|
|
|
|
|
|
* @see MockServletContext |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockHttpSession() { |
|
|
|
public MockHttpSession() { |
|
|
|
this(null); |
|
|
|
this(null); |
|
|
|
@ -77,6 +80,7 @@ public class MockHttpSession implements HttpSession { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a new MockHttpSession. |
|
|
|
* Create a new MockHttpSession. |
|
|
|
|
|
|
|
* |
|
|
|
* @param servletContext the ServletContext that the session runs in |
|
|
|
* @param servletContext the ServletContext that the session runs in |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockHttpSession(ServletContext servletContext) { |
|
|
|
public MockHttpSession(ServletContext servletContext) { |
|
|
|
@ -85,6 +89,7 @@ public class MockHttpSession implements HttpSession { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a new MockHttpSession. |
|
|
|
* Create a new MockHttpSession. |
|
|
|
|
|
|
|
* |
|
|
|
* @param servletContext the ServletContext that the session runs in |
|
|
|
* @param servletContext the ServletContext that the session runs in |
|
|
|
* @param id a unique identifier for this session |
|
|
|
* @param id a unique identifier for this session |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -93,7 +98,6 @@ public class MockHttpSession implements HttpSession { |
|
|
|
this.id = (id != null ? id : Integer.toString(nextId++)); |
|
|
|
this.id = (id != null ? id : Integer.toString(nextId++)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public long getCreationTime() { |
|
|
|
public long getCreationTime() { |
|
|
|
return this.creationTime; |
|
|
|
return this.creationTime; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -188,7 +192,17 @@ public class MockHttpSession implements HttpSession { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Invalidates this session then unbinds any objects bound to it. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException if this method is called on an already invalidated session |
|
|
|
|
|
|
|
*/ |
|
|
|
public void invalidate() { |
|
|
|
public void invalidate() { |
|
|
|
|
|
|
|
if (this.invalid) { |
|
|
|
|
|
|
|
throw new IllegalStateException("The session has already been invalidated"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// else
|
|
|
|
this.invalid = true; |
|
|
|
this.invalid = true; |
|
|
|
clearAttributes(); |
|
|
|
clearAttributes(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -205,10 +219,10 @@ public class MockHttpSession implements HttpSession { |
|
|
|
return this.isNew; |
|
|
|
return this.isNew; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Serialize the attributes of this session into an object that can |
|
|
|
* Serialize the attributes of this session into an object that can be |
|
|
|
* be turned into a byte array with standard Java serialization. |
|
|
|
* turned into a byte array with standard Java serialization. |
|
|
|
|
|
|
|
* |
|
|
|
* @return a representation of this session's serialized state |
|
|
|
* @return a representation of this session's serialized state |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public Serializable serializeState() { |
|
|
|
public Serializable serializeState() { |
|
|
|
@ -233,8 +247,9 @@ public class MockHttpSession implements HttpSession { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Deserialize the attributes of this session from a state object |
|
|
|
* Deserialize the attributes of this session from a state object created by |
|
|
|
* created by {@link #serializeState()}. |
|
|
|
* {@link #serializeState()}. |
|
|
|
|
|
|
|
* |
|
|
|
* @param state a representation of this session's serialized state |
|
|
|
* @param state a representation of this session's serialized state |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|