mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
Merge pull request #1209 from apache/WW-5516-attrmap-npe
WW-5526 Fix AttributeMap NPE when PageContext has no request
This commit is contained in:
@@ -18,9 +18,9 @@
|
||||
*/
|
||||
package org.apache.struts2.dispatcher;
|
||||
|
||||
import jakarta.servlet.jsp.PageContext;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
|
||||
import jakarta.servlet.jsp.PageContext;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -86,7 +86,7 @@ public class AttributeMap extends AbstractMap<String, Object> {
|
||||
|
||||
PageContext pc = getPageContext();
|
||||
|
||||
if (pc == null) {
|
||||
if (pc == null || pc.getRequest() == null) {
|
||||
RequestMap request = (RequestMap) context.get(DispatcherConstants.REQUEST);
|
||||
SessionMap session = (SessionMap) context.get(DispatcherConstants.SESSION);
|
||||
ApplicationMap application = (ApplicationMap) context.get(DispatcherConstants.APPLICATION);
|
||||
|
||||
@@ -18,18 +18,6 @@
|
||||
*/
|
||||
package org.apache.struts2.dispatcher;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -41,6 +29,22 @@ import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import jakarta.servlet.jsp.PageContext;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class AttributeMapTest {
|
||||
|
||||
@@ -361,4 +365,21 @@ public class AttributeMapTest {
|
||||
assertEquals("value", value);
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void get_whenPageContextHasNoRequest() {
|
||||
PageContext pageContext = mock(PageContext.class);
|
||||
when(pageContext.getRequest()).thenReturn(null);
|
||||
|
||||
var req = new MockHttpServletRequest();
|
||||
req.setAttribute("attr", "reqValue");
|
||||
|
||||
var attributeMap = new AttributeMap(Map.of(
|
||||
StrutsStatics.PAGE_CONTEXT, pageContext,
|
||||
DispatcherConstants.REQUEST, new RequestMap(req)
|
||||
));
|
||||
|
||||
assertEquals("reqValue", attributeMap.get("attr"));
|
||||
verify(pageContext, never()).findAttribute(anyString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user