提交 80596256 编写于 作者: S Sam Brannen

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
上级 e65b930e
/*
* 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();
}
......
/*
* 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();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册