@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2012 the original author or authors .
* Copyright 2002 - 2013 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -22,6 +22,7 @@ import java.util.Enumeration;
@@ -22,6 +22,7 @@ import java.util.Enumeration;
import java.util.HashMap ;
import java.util.Iterator ;
import java.util.LinkedHashMap ;
import java.util.LinkedHashSet ;
import java.util.Map ;
import javax.servlet.ServletContext ;
import javax.servlet.http.HttpSession ;
@ -34,7 +35,7 @@ import org.springframework.util.Assert;
@@ -34,7 +35,7 @@ import org.springframework.util.Assert;
/ * *
* Mock implementation of the { @link javax . servlet . http . HttpSession } interface .
*
* < p > Compatible with Servlet 2 . 5 as well as Servlet 3 . 0 .
* < p > As of Spring 4 . 0 , this set of mocks is designed on a Servlet 3 . 0 baseline .
*
* < p > Used for testing the web framework ; also useful for testing application
* controllers .
@ -50,6 +51,7 @@ public class MockHttpSession implements HttpSession {
@@ -50,6 +51,7 @@ public class MockHttpSession implements HttpSession {
public static final String SESSION_COOKIE_NAME = "JSESSION" ;
private static int nextId = 1 ;
private final String id ;
@ -100,6 +102,7 @@ public class MockHttpSession implements HttpSession {
@@ -100,6 +102,7 @@ public class MockHttpSession implements HttpSession {
@Override
public long getCreationTime ( ) {
assertIsValid ( ) ;
return this . creationTime ;
}
@ -115,6 +118,7 @@ public class MockHttpSession implements HttpSession {
@@ -115,6 +118,7 @@ public class MockHttpSession implements HttpSession {
@Override
public long getLastAccessedTime ( ) {
assertIsValid ( ) ;
return this . lastAccessedTime ;
}
@ -140,6 +144,7 @@ public class MockHttpSession implements HttpSession {
@@ -140,6 +144,7 @@ public class MockHttpSession implements HttpSession {
@Override
public Object getAttribute ( String name ) {
assertIsValid ( ) ;
Assert . notNull ( name , "Attribute name must not be null" ) ;
return this . attributes . get ( name ) ;
}
@ -151,16 +156,19 @@ public class MockHttpSession implements HttpSession {
@@ -151,16 +156,19 @@ public class MockHttpSession implements HttpSession {
@Override
public Enumeration < String > getAttributeNames ( ) {
return Collections . enumeration ( this . attributes . keySet ( ) ) ;
assertIsValid ( ) ;
return Collections . enumeration ( new LinkedHashSet < String > ( this . attributes . keySet ( ) ) ) ;
}
@Override
public String [ ] getValueNames ( ) {
assertIsValid ( ) ;
return this . attributes . keySet ( ) . toArray ( new String [ this . attributes . size ( ) ] ) ;
}
@Override
public void setAttribute ( String name , Object value ) {
assertIsValid ( ) ;
Assert . notNull ( name , "Attribute name must not be null" ) ;
if ( value ! = null ) {
this . attributes . put ( name , value ) ;
@ -180,6 +188,7 @@ public class MockHttpSession implements HttpSession {
@@ -180,6 +188,7 @@ public class MockHttpSession implements HttpSession {
@Override
public void removeAttribute ( String name ) {
assertIsValid ( ) ;
Assert . notNull ( name , "Attribute name must not be null" ) ;
Object value = this . attributes . remove ( name ) ;
if ( value instanceof HttpSessionBindingListener ) {
@ -214,11 +223,7 @@ public class MockHttpSession implements HttpSession {
@@ -214,11 +223,7 @@ public class MockHttpSession implements HttpSession {
* /
@Override
public void invalidate ( ) {
if ( this . invalid ) {
throw new IllegalStateException ( "The session has already been invalidated" ) ;
}
// else
assertIsValid ( ) ;
this . invalid = true ;
clearAttributes ( ) ;
}
@ -227,12 +232,25 @@ public class MockHttpSession implements HttpSession {
@@ -227,12 +232,25 @@ public class MockHttpSession implements HttpSession {
return this . invalid ;
}
/ * *
* Convenience method for asserting that this session has not been
* { @linkplain # invalidate ( ) invalidated } .
*
* @throws IllegalStateException if this session has been invalidated
* /
private void assertIsValid ( ) {
if ( isInvalid ( ) ) {
throw new IllegalStateException ( "The session has already been invalidated" ) ;
}
}
public void setNew ( boolean value ) {
this . isNew = value ;
}
@Override
public boolean isNew ( ) {
assertIsValid ( ) ;
return this . isNew ;
}