提交 25d13ac4 编写于 作者: S Sam Brannen

Sensible defaults for Servlet & Filter registrations in mock

Prior to this commit, the getter methods in MockServletContext threw an
UnsupportedOperationException when trying to retrieve Servlet and
Filter registrations.

This commit improves the behavior of these methods by returning null
when a single registration is requested and an empty map when all
registrations are requested. This is now in line with the Javadoc for
ServletContext. Note, however, that the corresponding setter methods
still throw UnsupportedOperationExceptions which is suitable behavior
for a mock.

Issue: SPR-12290
上级 c0dedec1
......@@ -63,7 +63,7 @@ import org.springframework.web.util.WebUtils;
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
* Note that Servlet 3.0 support is limited: servlet, filter and listener
* registration methods are not supported; neither is JSP configuration.
* We generally do not recommend to unit-test your ServletContainerInitializers and
* We generally do not recommend to unit test your ServletContainerInitializers and
* WebApplicationInitializers which is where those registration methods would be used.
*
* <p>Used for testing the Spring web framework; only rarely necessary for testing
......@@ -605,14 +605,22 @@ public class MockServletContext implements ServletContext {
throw new UnsupportedOperationException();
}
/**
* This method always returns {@code null}.
* @see javax.servlet.ServletContext#getServletRegistration(java.lang.String)
*/
@Override
public ServletRegistration getServletRegistration(String servletName) {
throw new UnsupportedOperationException();
return null;
}
/**
* This method always returns an {@linkplain Collections#emptyMap empty map}.
* @see javax.servlet.ServletContext#getServletRegistrations()
*/
@Override
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
throw new UnsupportedOperationException();
return Collections.emptyMap();
}
@Override
......@@ -635,14 +643,22 @@ public class MockServletContext implements ServletContext {
throw new UnsupportedOperationException();
}
/**
* This method always returns {@code null}.
* @see javax.servlet.ServletContext#getFilterRegistration(java.lang.String)
*/
@Override
public FilterRegistration getFilterRegistration(String filterName) {
throw new UnsupportedOperationException();
return null;
}
/**
* This method always returns an {@linkplain Collections#emptyMap empty map}.
* @see javax.servlet.ServletContext#getFilterRegistrations()
*/
@Override
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
throw new UnsupportedOperationException();
return Collections.emptyMap();
}
@Override
......
......@@ -16,16 +16,18 @@
package org.springframework.mock.web;
import java.util.Map;
import java.util.Set;
import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.FilterRegistration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRegistration;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
......@@ -147,4 +149,40 @@ public class MockServletContextTests {
assertEquals(newDefault, response.getForwardedUrl());
}
/**
* @since 4.1.2
*/
@Test
public void getServletRegistration() {
assertNull(sc.getServletRegistration("servlet"));
}
/**
* @since 4.1.2
*/
@Test
public void getServletRegistrations() {
Map<String, ? extends ServletRegistration> servletRegistrations = sc.getServletRegistrations();
assertNotNull(servletRegistrations);
assertEquals(0, servletRegistrations.size());
}
/**
* @since 4.1.2
*/
@Test
public void getFilterRegistration() {
assertNull(sc.getFilterRegistration("filter"));
}
/**
* @since 4.1.2
*/
@Test
public void getFilterRegistrations() {
Map<String, ? extends FilterRegistration> filterRegistrations = sc.getFilterRegistrations();
assertNotNull(filterRegistrations);
assertEquals(0, filterRegistrations.size());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册