Merge pull request #9095 from hengyunabc
* gh-9095: Polish "Fail startup when Tomcat's context fails to start" Fail startup when Tomcat's context fails to start
This commit is contained in:
		
						commit
						d058ed38a8
					
				| 
						 | 
				
			
			@ -163,6 +163,9 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
 | 
			
		|||
					throw exception;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			if (!LifecycleState.STARTED.equals(container.getState())) {
 | 
			
		||||
				throw new IllegalStateException(container + " failed to start");
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,7 +23,13 @@ import java.util.Locale;
 | 
			
		|||
import java.util.Map;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.Filter;
 | 
			
		||||
import javax.servlet.FilterChain;
 | 
			
		||||
import javax.servlet.FilterConfig;
 | 
			
		||||
import javax.servlet.ServletContext;
 | 
			
		||||
import javax.servlet.ServletException;
 | 
			
		||||
import javax.servlet.ServletRequest;
 | 
			
		||||
import javax.servlet.ServletResponse;
 | 
			
		||||
import javax.servlet.http.HttpServlet;
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
| 
						 | 
				
			
			@ -45,8 +51,10 @@ import org.mockito.InOrder;
 | 
			
		|||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
 | 
			
		||||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
 | 
			
		||||
import org.springframework.boot.context.embedded.Compression;
 | 
			
		||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
 | 
			
		||||
import org.springframework.boot.context.embedded.PortInUseException;
 | 
			
		||||
import org.springframework.boot.context.embedded.Ssl;
 | 
			
		||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
 | 
			
		||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
 | 
			
		||||
import org.springframework.http.HttpHeaders;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -282,6 +290,38 @@ public class JettyEmbeddedServletContainerFactoryTests
 | 
			
		|||
				.getThreadPool()).isSameAs(threadPool);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void faultyFilterCausesStartFailure() throws Exception {
 | 
			
		||||
		AbstractEmbeddedServletContainerFactory factory = getFactory();
 | 
			
		||||
		factory.addInitializers(new ServletContextInitializer() {
 | 
			
		||||
 | 
			
		||||
			@Override
 | 
			
		||||
			public void onStartup(ServletContext servletContext) throws ServletException {
 | 
			
		||||
				servletContext.addFilter("faulty", new Filter() {
 | 
			
		||||
 | 
			
		||||
					@Override
 | 
			
		||||
					public void init(FilterConfig filterConfig) throws ServletException {
 | 
			
		||||
						throw new ServletException("Faulty filter");
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					@Override
 | 
			
		||||
					public void doFilter(ServletRequest request, ServletResponse response,
 | 
			
		||||
							FilterChain chain) throws IOException, ServletException {
 | 
			
		||||
						chain.doFilter(request, response);
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					@Override
 | 
			
		||||
					public void destroy() {
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
				});
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		});
 | 
			
		||||
		this.thrown.expect(EmbeddedServletContainerException.class);
 | 
			
		||||
		factory.getEmbeddedServletContainer().start();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	@SuppressWarnings("serial")
 | 
			
		||||
	// Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,7 +26,13 @@ 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.ServletContext;
 | 
			
		||||
import javax.servlet.ServletException;
 | 
			
		||||
import javax.servlet.ServletRequest;
 | 
			
		||||
import javax.servlet.ServletResponse;
 | 
			
		||||
 | 
			
		||||
import org.apache.catalina.Container;
 | 
			
		||||
import org.apache.catalina.Context;
 | 
			
		||||
| 
						 | 
				
			
			@ -53,6 +59,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.ServletContextInitializer;
 | 
			
		||||
import org.springframework.test.util.ReflectionTestUtils;
 | 
			
		||||
import org.springframework.util.SocketUtils;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -460,6 +467,38 @@ public class TomcatEmbeddedServletContainerFactoryTests
 | 
			
		|||
		assertThat(sessionIdGenerator.getJvmRoute()).isEqualTo("test");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void faultyFilterCausesStartFailure() throws Exception {
 | 
			
		||||
		AbstractEmbeddedServletContainerFactory factory = getFactory();
 | 
			
		||||
		factory.addInitializers(new ServletContextInitializer() {
 | 
			
		||||
 | 
			
		||||
			@Override
 | 
			
		||||
			public void onStartup(ServletContext servletContext) throws ServletException {
 | 
			
		||||
				servletContext.addFilter("faulty", new Filter() {
 | 
			
		||||
 | 
			
		||||
					@Override
 | 
			
		||||
					public void init(FilterConfig filterConfig) throws ServletException {
 | 
			
		||||
						throw new ServletException("Faulty filter");
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					@Override
 | 
			
		||||
					public void doFilter(ServletRequest request, ServletResponse response,
 | 
			
		||||
							FilterChain chain) throws IOException, ServletException {
 | 
			
		||||
						chain.doFilter(request, response);
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					@Override
 | 
			
		||||
					public void destroy() {
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
				});
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		});
 | 
			
		||||
		this.thrown.expect(EmbeddedServletContainerException.class);
 | 
			
		||||
		factory.getEmbeddedServletContainer().start();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	protected JspServlet getJspServlet() throws ServletException {
 | 
			
		||||
		Container context = ((TomcatEmbeddedServletContainer) this.container).getTomcat()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue