|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2016 the original author or authors. |
| 2 | + * Copyright 2012-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.boot.web.support;
|
18 | 18 |
|
19 | 19 | import javax.servlet.ServletContext;
|
| 20 | +import javax.servlet.ServletException; |
20 | 21 |
|
21 | 22 | import org.junit.Rule;
|
22 | 23 | import org.junit.Test;
|
|
25 | 26 | import org.springframework.beans.DirectFieldAccessor;
|
26 | 27 | import org.springframework.boot.SpringApplication;
|
27 | 28 | import org.springframework.boot.builder.SpringApplicationBuilder;
|
28 |
| -import org.springframework.boot.web.support.SpringBootServletInitializer.ErrorPageFilterConfiguration; |
| 29 | +import org.springframework.boot.context.embedded.EmbeddedServletContainer; |
| 30 | +import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; |
| 31 | +import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory; |
| 32 | +import org.springframework.boot.web.servlet.ServletContextInitializer; |
| 33 | +import org.springframework.context.ConfigurableApplicationContext; |
| 34 | +import org.springframework.context.annotation.Bean; |
29 | 35 | import org.springframework.context.annotation.Configuration;
|
| 36 | +import org.springframework.context.support.AbstractApplicationContext; |
30 | 37 | import org.springframework.mock.web.MockServletContext;
|
31 | 38 | import org.springframework.web.context.WebApplicationContext;
|
32 | 39 |
|
@@ -88,11 +95,43 @@ public void mainClassHasSensibleDefault() throws Exception {
|
88 | 95 | }
|
89 | 96 |
|
90 | 97 | @Test
|
91 |
| - public void withErrorPageFilterNotRegistered() throws Exception { |
92 |
| - new WithErrorPageFilterNotRegistered() |
93 |
| - .createRootApplicationContext(this.servletContext); |
94 |
| - assertThat(this.application.getSources()) |
95 |
| - .containsOnly(WithErrorPageFilterNotRegistered.class); |
| 98 | + public void errorPageFilterRegistrationCanBeDisabled() throws Exception { |
| 99 | + EmbeddedServletContainer container = new UndertowEmbeddedServletContainerFactory( |
| 100 | + 0).getEmbeddedServletContainer(new ServletContextInitializer() { |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onStartup(ServletContext servletContext) |
| 104 | + throws ServletException { |
| 105 | + AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilterNotRegistered() |
| 106 | + .createRootApplicationContext(servletContext); |
| 107 | + try { |
| 108 | + assertThat(context.getBeansOfType(ErrorPageFilter.class)) |
| 109 | + .hasSize(0); |
| 110 | + } |
| 111 | + finally { |
| 112 | + context.close(); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + try { |
| 117 | + container.start(); |
| 118 | + } |
| 119 | + finally { |
| 120 | + container.stop(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured() |
| 126 | + throws Exception { |
| 127 | + ConfigurableApplicationContext context = new SpringApplication( |
| 128 | + ExecutableWar.class).run(); |
| 129 | + try { |
| 130 | + assertThat(context.getBeansOfType(ErrorPageFilter.class)).hasSize(0); |
| 131 | + } |
| 132 | + finally { |
| 133 | + context.close(); |
| 134 | + } |
96 | 135 | }
|
97 | 136 |
|
98 | 137 | @Test
|
@@ -146,15 +185,25 @@ protected SpringApplicationBuilder configure(
|
146 | 185 | }
|
147 | 186 |
|
148 | 187 | @Configuration
|
149 |
| - public class WithErrorPageFilterNotRegistered |
150 |
| - extends MockSpringBootServletInitializer { |
| 188 | + public static class WithErrorPageFilterNotRegistered |
| 189 | + extends SpringBootServletInitializer { |
151 | 190 |
|
152 | 191 | public WithErrorPageFilterNotRegistered() {
|
153 | 192 | setRegisterErrorPageFilter(false);
|
154 | 193 | }
|
155 | 194 |
|
156 | 195 | }
|
157 | 196 |
|
| 197 | + @Configuration |
| 198 | + public static class ExecutableWar extends SpringBootServletInitializer { |
| 199 | + |
| 200 | + @Bean |
| 201 | + public EmbeddedServletContainerFactory containerFactory() { |
| 202 | + return new UndertowEmbeddedServletContainerFactory(0); |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + |
158 | 207 | @Configuration
|
159 | 208 | public static class Config {
|
160 | 209 |
|
|
0 commit comments