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
This commit is contained in:
parent
c0dedec1d6
commit
25d13ac41e
|
|
@ -63,7 +63,7 @@ import org.springframework.web.util.WebUtils;
|
||||||
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
|
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
|
||||||
* Note that Servlet 3.0 support is limited: servlet, filter and listener
|
* Note that Servlet 3.0 support is limited: servlet, filter and listener
|
||||||
* registration methods are not supported; neither is JSP configuration.
|
* 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.
|
* WebApplicationInitializers which is where those registration methods would be used.
|
||||||
*
|
*
|
||||||
* <p>Used for testing the Spring web framework; only rarely necessary for testing
|
* <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();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method always returns {@code null}.
|
||||||
|
* @see javax.servlet.ServletContext#getServletRegistration(java.lang.String)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ServletRegistration getServletRegistration(String servletName) {
|
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
|
@Override
|
||||||
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
|
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
|
||||||
throw new UnsupportedOperationException();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -635,14 +643,22 @@ public class MockServletContext implements ServletContext {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method always returns {@code null}.
|
||||||
|
* @see javax.servlet.ServletContext#getFilterRegistration(java.lang.String)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public FilterRegistration getFilterRegistration(String filterName) {
|
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
|
@Override
|
||||||
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
|
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
|
||||||
throw new UnsupportedOperationException();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,18 @@
|
||||||
|
|
||||||
package org.springframework.mock.web;
|
package org.springframework.mock.web;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.activation.FileTypeMap;
|
import javax.activation.FileTypeMap;
|
||||||
import javax.activation.MimetypesFileTypeMap;
|
import javax.activation.MimetypesFileTypeMap;
|
||||||
|
import javax.servlet.FilterRegistration;
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -147,4 +149,40 @@ public class MockServletContextTests {
|
||||||
assertEquals(newDefault, response.getForwardedUrl());
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue