From 51d828816d3a50004863964d8b9c92b2a32fdc33 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 9 Jun 2013 16:38:04 +0200 Subject: [PATCH] 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 getAttributeNames() - String[] getValueNames() - void setAttribute(String, Object) - void putValue(String , Object) - void removeAttribute(String) - void removeValue(String) - void invalidate() - boolean isNew() Issue: SPR-7659 --- .../mock/web/MockHttpSession.java | 26 ++++- .../mock/web/MockHttpSessionTests.java | 103 +++++++++++++++++- 2 files changed, 122 insertions(+), 7 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java index 5daf16d36e..513f64ad56 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java @@ -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 getAttributeNames() { + assertIsValid(); return Collections.enumeration(new LinkedHashSet(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; } diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java index 0379ea78d0..41b735c4bd 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java @@ -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(); + } + }