Merge pull request #1210 from apache/WW-5516-attrmap-npe-67

This commit is contained in:
Kusal Kithul-Godage
2025-02-04 01:19:16 +11:00
committed by GitHub
2 changed files with 24 additions and 2 deletions
@@ -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);
@@ -40,6 +40,11 @@ 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 {
@@ -360,4 +365,21 @@ public class AttributeMapTest {
assertEquals("value", value);
}
}
@Test
public void get_whenPageContextHasNoRequest() {
PageContext pageContext = mock(PageContext.class);
when(pageContext.getRequest()).thenReturn(null);
HttpServletRequest req = new MockHttpServletRequest();
req.setAttribute("attr", "reqValue");
AttributeMap attributeMap = new AttributeMap(new HashMap<String, Object>() {{
put(StrutsStatics.PAGE_CONTEXT, pageContext);
put(DispatcherConstants.REQUEST, new RequestMap(req));
}});
assertEquals("reqValue", attributeMap.get("attr"));
verify(pageContext, never()).findAttribute(anyString());
}
}