Backported proper updateAccessedAttributes test

Issue: SPR-11738
(cherry picked from commit 6188550)
This commit is contained in:
Juergen Hoeller
2014-05-19 16:00:29 +02:00
parent c70a81a805
commit 1bbc032071

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,10 +17,11 @@
package org.springframework.web.context.request; package org.springframework.web.context.request;
import java.io.Serializable; import java.io.Serializable;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpSession; import org.springframework.mock.web.test.MockHttpSession;
@@ -39,23 +40,12 @@ public class ServletRequestAttributesTests {
private static final Serializable VALUE = new Serializable() { private static final Serializable VALUE = new Serializable() {
}; };
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void ctorRejectsNullArg() throws Exception { public void ctorRejectsNullArg() throws Exception {
new ServletRequestAttributes(null); new ServletRequestAttributes(null);
} }
@Test
public void updateAccessedAttributes() throws Exception {
MockHttpSession session = new MockHttpSession();
session.setAttribute(KEY, VALUE);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(session);
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
Object value = attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION);
assertSame(VALUE, value);
attrs.requestCompleted();
}
@Test @Test
public void setRequestScopedAttribute() throws Exception { public void setRequestScopedAttribute() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
@@ -162,4 +152,20 @@ public class ServletRequestAttributesTests {
verify(request).getSession(false); verify(request).getSession(false);
} }
@Test
public void updateAccessedAttributes() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpSession session = mock(HttpSession.class);
when(request.getSession(anyBoolean())).thenReturn(session);
when(session.getAttribute(KEY)).thenReturn(VALUE);
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
assertSame(VALUE, attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION));
attrs.requestCompleted();
verify(session, times(2)).getAttribute(KEY);
verify(session).setAttribute(KEY, VALUE);
verifyNoMoreInteractions(session);
}
} }