- 
                Notifications
    You must be signed in to change notification settings 
- Fork 41.6k
Apply TomcatConnectorCustomizer and TomcatContextCustomizer beans automatically #15492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -21,11 +21,15 @@ | |
|  | ||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
| import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; | ||
| import org.springframework.boot.test.context.runner.WebApplicationContextRunner; | ||
| import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; | ||
| import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; | ||
| import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; | ||
| import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; | ||
| import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory; | ||
| import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; | ||
| import org.springframework.boot.web.server.WebServerFactoryCustomizer; | ||
| import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; | ||
| import org.springframework.context.ApplicationContextException; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|  | @@ -37,6 +41,7 @@ | |
| * Tests for {@link ReactiveWebServerFactoryAutoConfiguration}. | ||
| * | ||
| * @author Brian Clozel | ||
| * @author Raheela Aslam | ||
| */ | ||
| public class ReactiveWebServerFactoryAutoConfigurationTests { | ||
|  | ||
|  | @@ -97,6 +102,36 @@ public void defaultWebServerIsTomcat() { | |
| .isInstanceOf(TomcatReactiveWebServerFactory.class)); | ||
| } | ||
|  | ||
| @Test | ||
| public void tomcatConnectorCustomizerBeanIsAddedToFactory() { | ||
| WebApplicationContextRunner runner = new WebApplicationContextRunner( | ||
| AnnotationConfigServletWebServerApplicationContext::new) | ||
|         
                  wilkinsona marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| .withConfiguration(AutoConfigurations | ||
| .of(ReactiveWebServerFactoryAutoConfiguration.class)) | ||
| .withUserConfiguration( | ||
| TomcatConnectorCustomizerConfiguration.class); | ||
| runner.run((context) -> { | ||
| TomcatServletWebServerFactory factory = context | ||
| .getBean(TomcatServletWebServerFactory.class); | ||
| assertThat(factory.getTomcatConnectorCustomizers()).hasSize(1); | ||
| }); | ||
| } | ||
|  | ||
| @Test | ||
| public void tomcatContextBeanIsAddedToFactory() { | ||
| WebApplicationContextRunner runner = new WebApplicationContextRunner( | ||
| AnnotationConfigServletWebServerApplicationContext::new) | ||
|         
                  wilkinsona marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| .withConfiguration(AutoConfigurations | ||
| .of(ReactiveWebServerFactoryAutoConfiguration.class)) | ||
| .withUserConfiguration( | ||
| TomcatContextCustomizerConfiguration.class); | ||
| runner.run((context) -> { | ||
| TomcatServletWebServerFactory factory = context | ||
| .getBean(TomcatServletWebServerFactory.class); | ||
| assertThat(factory.getTomcatContextCustomizers()).hasSize(1); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this test passes then this assertion isn't quite right as  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
| }); | ||
| } | ||
|  | ||
| @Configuration | ||
| protected static class HttpHandlerConfiguration { | ||
|  | ||
|  | @@ -137,4 +172,19 @@ public MockReactiveWebServerFactory mockReactiveWebServerFactory() { | |
|  | ||
| } | ||
|  | ||
| @Configuration | ||
| static class TomcatConnectorCustomizerConfiguration { | ||
|  | ||
| @Bean | ||
| public TomcatConnectorCustomizer connectorCustomizer() { | ||
| return (connector) -> connector.setPort(9001); | ||
|          | ||
| } | ||
|  | ||
| } | ||
|  | ||
| @Configuration | ||
| static class TomcatContextCustomizerConfiguration { | ||
|         
                  wilkinsona marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| } | ||
|  | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -30,6 +30,8 @@ | |
| import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; | ||
| import org.springframework.boot.test.context.runner.ContextConsumer; | ||
| import org.springframework.boot.test.context.runner.WebApplicationContextRunner; | ||
| import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; | ||
| import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; | ||
| import org.springframework.boot.web.server.WebServerFactoryCustomizer; | ||
| import org.springframework.boot.web.servlet.ServletRegistrationBean; | ||
| import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; | ||
|  | @@ -51,6 +53,7 @@ | |
| * @author Dave Syer | ||
| * @author Phillip Webb | ||
| * @author Stephane Nicoll | ||
| * @author Raheela Aslam | ||
| */ | ||
| public class ServletWebServerFactoryAutoConfigurationTests { | ||
|  | ||
|  | @@ -138,6 +141,36 @@ public void initParametersAreConfiguredOnTheServletContext() { | |
| }); | ||
| } | ||
|  | ||
| @Test | ||
| public void tomcatConnectorCustomizerBeanIsAddedToFactory() { | ||
| WebApplicationContextRunner runner = new WebApplicationContextRunner( | ||
| AnnotationConfigServletWebServerApplicationContext::new) | ||
| .withConfiguration(AutoConfigurations | ||
| .of(ServletWebServerFactoryAutoConfiguration.class)) | ||
| .withUserConfiguration( | ||
| TomcatConnectorCustomizerConfiguration.class); | ||
| runner.run((context) -> { | ||
| TomcatServletWebServerFactory factory = context | ||
| .getBean(TomcatServletWebServerFactory.class); | ||
| assertThat(factory.getTomcatConnectorCustomizers()).hasSize(1); | ||
| }); | ||
| } | ||
|  | ||
| @Test | ||
| public void tomcatContextCustomizerBeanIsAddedToFactory() { | ||
| WebApplicationContextRunner runner = new WebApplicationContextRunner( | ||
| AnnotationConfigServletWebServerApplicationContext::new) | ||
| .withConfiguration(AutoConfigurations | ||
| .of(ServletWebServerFactoryAutoConfiguration.class)) | ||
| .withUserConfiguration( | ||
| TomcatContextCustomizerConfiguration.class); | ||
| runner.run((context) -> { | ||
| TomcatServletWebServerFactory factory = context | ||
| .getBean(TomcatServletWebServerFactory.class); | ||
| assertThat(factory.getTomcatContextCustomizers()).hasSize(1); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this test passes then this assertion isn't quite right as  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 We have an option that add assert for size 2 because one is already there and other is added by our configuration class. If i am thinking wrong you can guide me towards correcting the assertion. | ||
| }); | ||
| } | ||
|  | ||
| private ContextConsumer<AssertableWebApplicationContext> verifyContext() { | ||
| return this::verifyContext; | ||
| } | ||
|  | @@ -253,4 +286,19 @@ public void customize(ConfigurableServletWebServerFactory serverFactory) { | |
|  | ||
| } | ||
|  | ||
| @Configuration | ||
| static class TomcatConnectorCustomizerConfiguration { | ||
|  | ||
| @Bean | ||
| public TomcatConnectorCustomizer connectorCustomizer() { | ||
| return (connector) -> connector.setPort(9001); | ||
|          | ||
| } | ||
|  | ||
| } | ||
|  | ||
| @Configuration | ||
| static class TomcatContextCustomizerConfiguration { | ||
|         
                  wilkinsona marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| } | ||
|  | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.