From 4e15d705aafc1568d4288320287811ba4fe3f124 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 7 Jul 2013 01:42:43 -0700 Subject: [PATCH] Remove servet/filter class tangle Remove class tangle between ServletRegistrationBean and FilterRegistrationBean. --- .../EmbeddedWebApplicationContext.java | 3 - .../embedded/ServletRegistrationBean.java | 35 ------------ .../EmbeddedWebApplicationContextTests.java | 5 -- .../ServletRegistrationBeanTests.java | 57 +++++-------------- 4 files changed, 14 insertions(+), 86 deletions(-) diff --git a/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContext.java b/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContext.java index d3030fe970..b319ab9691 100644 --- a/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContext.java +++ b/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContext.java @@ -206,9 +206,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext if (initializer instanceof RegistrationBean) { targets.add(((RegistrationBean) initializer).getRegistrationTarget()); } - if (initializer instanceof ServletRegistrationBean) { - targets.addAll(((ServletRegistrationBean) initializer).getFilters()); - } initializers.add(initializer); } diff --git a/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/ServletRegistrationBean.java b/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/ServletRegistrationBean.java index 53bc37433d..0756ba0185 100644 --- a/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/ServletRegistrationBean.java +++ b/spring-zero-core/src/main/java/org/springframework/zero/context/embedded/ServletRegistrationBean.java @@ -21,7 +21,6 @@ import java.util.Collection; import java.util.LinkedHashSet; import java.util.Set; -import javax.servlet.Filter; import javax.servlet.MultipartConfigElement; import javax.servlet.Servlet; import javax.servlet.ServletContext; @@ -55,8 +54,6 @@ public class ServletRegistrationBean extends RegistrationBean { private int loadOnStartup = 1; - private Set filters = new LinkedHashSet(); - private MultipartConfigElement multipartConfig; /** @@ -121,32 +118,6 @@ public class ServletRegistrationBean extends RegistrationBean { this.loadOnStartup = loadOnStartup; } - /** - * Sets any Filters that should be registered to this servlet. Any previously - * specified Filters will be replaced. - * @param filters the Filters to set - */ - public void setFilters(Collection filters) { - Assert.notNull(filters, "Filters must not be null"); - this.filters = new LinkedHashSet(filters); - } - - /** - * Returns a mutable collection of the Filters being registered with this servlet. - */ - public Collection getFilters() { - return this.filters; - } - - /** - * Add Filters that will be registered with this servlet. - * @param filters the Filters to add - */ - public void addFilters(Filter... filters) { - Assert.notNull(filters, "Filters must not be null"); - this.filters.addAll(Arrays.asList(filters)); - } - /** * Set the the {@link MultipartConfigElement multi-part configuration}. * @param multipartConfig the muti-part configuration to set or {@code null} @@ -179,12 +150,6 @@ public class ServletRegistrationBean extends RegistrationBean { public void onStartup(ServletContext servletContext) throws ServletException { Assert.notNull(this.servlet, "Servlet must not be null"); configure(servletContext.addServlet(getServletName(), this.servlet)); - for (Filter filter : this.filters) { - FilterRegistrationBean filterRegistration = new FilterRegistrationBean( - filter, this); - filterRegistration.setAsyncSupported(isAsyncSupported()); - filterRegistration.onStartup(servletContext); - } } /** diff --git a/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContextTests.java b/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContextTests.java index b2e00e6b91..adc48e3a4e 100644 --- a/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContextTests.java +++ b/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/EmbeddedWebApplicationContextTests.java @@ -40,10 +40,6 @@ import org.springframework.core.Ordered; import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.request.SessionScope; -import org.springframework.zero.context.embedded.EmbeddedWebApplicationContext; -import org.springframework.zero.context.embedded.FilterRegistrationBean; -import org.springframework.zero.context.embedded.ServletContextInitializer; -import org.springframework.zero.context.embedded.ServletRegistrationBean; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -310,7 +306,6 @@ public class EmbeddedWebApplicationContextTests { Servlet servlet = mock(Servlet.class); Filter filter = mock(Filter.class); ServletRegistrationBean initializer = new ServletRegistrationBean(servlet, "/foo"); - initializer.addFilters(filter); this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer)); this.context.registerBeanDefinition("servletBean", beanDefinition(servlet)); diff --git a/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/ServletRegistrationBeanTests.java b/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/ServletRegistrationBeanTests.java index 18f0008ded..cfaa3a374c 100644 --- a/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/ServletRegistrationBeanTests.java +++ b/spring-zero-core/src/test/java/org/springframework/zero/context/embedded/ServletRegistrationBeanTests.java @@ -18,12 +18,10 @@ package org.springframework.zero.context.embedded; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; -import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterRegistration; import javax.servlet.Servlet; @@ -36,11 +34,11 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.springframework.zero.context.embedded.ServletRegistrationBean; -import static org.mockito.BDDMockito.*; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.verify; /** * Tests for {@link ServletRegistrationBean}. @@ -123,38 +121,38 @@ public class ServletRegistrationBeanTests { @Test public void setServletMustNotBeNull() throws Exception { ServletRegistrationBean bean = new ServletRegistrationBean(); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Servlet must not be null"); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("Servlet must not be null"); bean.onStartup(this.servletContext); } @Test public void createServletMustNotBeNull() throws Exception { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Servlet must not be null"); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("Servlet must not be null"); new ServletRegistrationBean(null); } @Test public void setMappingMustNotBeNull() throws Exception { ServletRegistrationBean bean = new ServletRegistrationBean(this.servlet); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("UrlMappings must not be null"); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("UrlMappings must not be null"); bean.setUrlMappings(null); } @Test public void createMappingMustNotBeNull() throws Exception { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("UrlMappings must not be null"); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("UrlMappings must not be null"); new ServletRegistrationBean(this.servlet, (String[]) null); } @Test public void addMappingMustNotBeNull() throws Exception { ServletRegistrationBean bean = new ServletRegistrationBean(this.servlet); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("UrlMappings must not be null"); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("UrlMappings must not be null"); bean.addUrlMappings((String[]) null); } @@ -177,31 +175,4 @@ public class ServletRegistrationBeanTests { verify(this.registration).setInitParameters(Collections.singletonMap("a", "c")); } - @Test - public void filters() throws Exception { - ServletRegistrationBean bean = new ServletRegistrationBean(this.servlet); - Filter filter = new MockFilter(); - bean.addFilters(filter); - bean.onStartup(this.servletContext); - verify(servletContext).addFilter("mockFilter", filter); - verify(filterRegistration).setAsyncSupported(true); - verify(filterRegistration).addMappingForServletNames( - EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, - DispatcherType.INCLUDE, DispatcherType.ASYNC), false, - "mockServlet"); - } - - @Test - public void filtersNoAsync() throws Exception { - ServletRegistrationBean bean = new ServletRegistrationBean(this.servlet); - Filter filter = new MockFilter(); - bean.addFilters(filter); - bean.setAsyncSupported(false); - bean.onStartup(this.servletContext); - verify(servletContext).addFilter("mockFilter", filter); - verify(filterRegistration).setAsyncSupported(false); - verify(filterRegistration).addMappingForServletNames( - EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, - DispatcherType.INCLUDE), false, "mockServlet"); - } }