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 3b30a83e32b..39d771b2f5d 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -39,6 +39,7 @@ import org.springframework.util.Assert; * @author Juergen Hoeller * @author Rod Johnson * @author Mark Fisher + * @author Sam Brannen * @since 1.0.2 */ @SuppressWarnings("deprecation") @@ -186,7 +187,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() { + if (this.invalid) { + throw new IllegalStateException("The session has already been invalidated"); + } + + // else this.invalid = true; clearAttributes(); } 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 new file mode 100644 index 00000000000..0379ea78d0e --- /dev/null +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2002-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.mock.web; + +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Unit tests for {@link MockHttpSession}. + * + * @author Sam Brannen + * @since 3.2 + */ +public class MockHttpSessionTests { + + private MockHttpSession session = new MockHttpSession(); + + + @Test + public void invalidateOnce() { + assertFalse(session.isInvalid()); + session.invalidate(); + assertTrue(session.isInvalid()); + } + + @Test(expected = IllegalStateException.class) + public void invalidateTwice() { + session.invalidate(); + session.invalidate(); + } + +}