diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java index b9a59b4fc2..07960d126a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java @@ -192,6 +192,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer if (connector != null && this.autoStart) { startConnector(connector); } + checkThatContextHaveStarted(); checkThatConnectorsHaveStarted(); this.started = true; TomcatEmbeddedServletContainer.logger @@ -221,6 +222,13 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer } } + private void checkThatContextHaveStarted() { + LifecycleState state = this.findContext().getState(); + if (!LifecycleState.STARTED.equals(state)) { + throw new EmbeddedServletContainerException("Context state expect STARTED, but " + state, null); + } + } + private void stopSilently() { try { stopTomcat(); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java index ed5ea62394..f699c3872b 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java @@ -26,7 +26,12 @@ import java.util.concurrent.TimeUnit; import javax.naming.InitialContext; import javax.naming.NamingException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import org.apache.catalina.Container; import org.apache.catalina.Context; @@ -53,6 +58,7 @@ import org.springframework.boot.context.embedded.AbstractEmbeddedServletContaine import org.springframework.boot.context.embedded.EmbeddedServletContainerException; import org.springframework.boot.context.embedded.Ssl; import org.springframework.boot.testutil.InternalOutputCapture; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.SocketUtils; @@ -509,4 +515,29 @@ public class TomcatEmbeddedServletContainerFactoryTests assertThat(((ConnectorStartFailedException) ex).getPort()).isEqualTo(blockedPort); } + @Test(expected = EmbeddedServletContainerException.class) + public void startServletExceptionFilter() throws Exception { + AbstractEmbeddedServletContainerFactory factory = getFactory(); + + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); + filterRegistrationBean.setFilter(new Filter() { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + throw new ServletException(); + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + } + + @Override + public void destroy() { + } + }); + filterRegistrationBean.setUrlPatterns(Arrays.asList("/test")); + + this.container = factory.getEmbeddedServletContainer(filterRegistrationBean); + this.container.start(); + } }