Browse Source

Implement invalidate() properly in MockHttpSession

The invalidate() method in MockHttpSession is currently implemented
incorrectly. According to the Servlet specification, the method should
throw an IllegalStateException if it is invoked on an already
invalidated session. However, invoking invalidate() on the same
MockHttpSession instance multiple times does not throw an exception.

This commits addresses this issue by checking the invalid field and
throwing an IllegalStateException if it has already been set to true.

Issue: SPR-9686
pull/120/head
Sam Brannen 14 years ago
parent
commit
8059625670
  1. 13
      spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java
  2. 47
      spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java

13
spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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();
}

47
spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java

@ -0,0 +1,47 @@ @@ -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();
}
}
Loading…
Cancel
Save