Issue #4505 Ensure get/setInitParam with null arg throws NPE (#4509)

* Issue #4505 Ensure get/setInitParam with null arg throws NPE
This commit is contained in:
Jan Bartel 2020-01-22 10:48:34 +01:00 committed by GitHub
parent d74caab345
commit 158789b766
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -1287,6 +1288,14 @@ public class ServletContextHandler extends ContextHandler
return null; //existing completed registration for servlet name
}
@Override
public String getInitParameter(String name)
{
//since servlet spec 4.0
Objects.requireNonNull(name);
return super.getInitParameter(name);
}
@Override
public boolean setInitParameter(String name, String value)
{
@ -1296,6 +1305,9 @@ public class ServletContextHandler extends ContextHandler
if (!_enabled)
throw new UnsupportedOperationException();
//since servlet spec 4.0
Objects.requireNonNull(name);
return super.setInitParameter(name, value);
}

View File

@ -87,6 +87,7 @@ import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
@ -97,6 +98,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@ -470,6 +472,32 @@ public class ServletContextHandlerTest
_server.join();
}
@Test
public void testInitParams() throws Exception
{
//Test get/setInitParam with null throws NPE
ServletContextHandler root = new ServletContextHandler(_server, "/", ServletContextHandler.SESSIONS);
_server.setHandler(root);
ListenerHolder initialListener = new ListenerHolder();
initialListener.setListener(new ServletContextListener()
{
public void contextInitialized(ServletContextEvent sce)
{
sce.getServletContext().setInitParameter("foo", "bar");
assertEquals("bar", sce.getServletContext().getInitParameter("foo"));
assertThrows(NullPointerException.class,
() -> sce.getServletContext().setInitParameter(null, "bad")
);
assertThrows(NullPointerException.class,
() -> sce.getServletContext().getInitParameter(null)
);
}
});
root.getServletHandler().addListener(initialListener);
_server.start();
}
@Test
public void testGetSetSessionTimeout() throws Exception
{