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:
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user