Throw ISEs in MockHttpSession for invalid session
The Javadoc for several methods in HttpSession specifies that an IllegalStateException must be thrown if the method is called on an invalidated session; however, Spring's MockHttpSession did not implement this behavior consistently prior to this commit. This commit therefore ensures that the following methods in MockHttpSession properly throw an IllegalStateException as defined in the Servlet specification. - long getCreationTime() - long getLastAccessedTime() - Object getAttribute(String) - Object getValue(String) - Enumeration<String> getAttributeNames() - String[] getValueNames() - void setAttribute(String, Object) - void putValue(String , Object) - void removeAttribute(String) - void removeValue(String) - void invalidate() - boolean isNew() Issue: SPR-7659
This commit is contained in:
parent
ec5d81e78e
commit
51d828816d
|
@ -102,6 +102,7 @@ public class MockHttpSession implements HttpSession {
|
|||
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
assertIsValid();
|
||||
return this.creationTime;
|
||||
}
|
||||
|
||||
|
@ -117,6 +118,7 @@ public class MockHttpSession implements HttpSession {
|
|||
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
assertIsValid();
|
||||
return this.lastAccessedTime;
|
||||
}
|
||||
|
||||
|
@ -142,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);
|
||||
}
|
||||
|
@ -153,16 +156,19 @@ public class MockHttpSession implements HttpSession {
|
|||
|
||||
@Override
|
||||
public Enumeration<String> getAttributeNames() {
|
||||
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);
|
||||
|
@ -182,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) {
|
||||
|
@ -216,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();
|
||||
}
|
||||
|
@ -229,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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
@ -28,7 +28,7 @@ import org.junit.Test;
|
|||
*/
|
||||
public class MockHttpSessionTests {
|
||||
|
||||
private MockHttpSession session = new MockHttpSession();
|
||||
private final MockHttpSession session = new MockHttpSession();
|
||||
|
||||
|
||||
@Test
|
||||
|
@ -44,4 +44,103 @@ public class MockHttpSessionTests {
|
|||
session.invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getCreationTimeOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.getCreationTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getLastAccessedTimeOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.getLastAccessedTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getAttributeOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.getAttribute("foo");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getAttributeNamesOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.getAttributeNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getValueOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.getValue("foo");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getValueNamesOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.getValueNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void setAttributeOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.setAttribute("name", "value");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void putValueOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.putValue("name", "value");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void removeAttributeOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.removeAttribute("name");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void removeValueOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.removeValue("name");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void isNewOnInvalidatedSession() {
|
||||
session.invalidate();
|
||||
session.isNew();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue