|
36 | 36 | import org.junit.rules.ExpectedException;
|
37 | 37 | import org.mockito.InOrder;
|
38 | 38 | import org.springframework.beans.MutablePropertyValues;
|
| 39 | +import org.springframework.beans.factory.DisposableBean; |
39 | 40 | import org.springframework.beans.factory.config.BeanDefinition;
|
40 | 41 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
41 | 42 | import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
42 | 43 | import org.springframework.beans.factory.config.Scope;
|
| 44 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
43 | 45 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
| 46 | +import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory.MockEmbeddedServletContainer; |
44 | 47 | import org.springframework.context.ApplicationContextException;
|
45 | 48 | import org.springframework.context.ApplicationListener;
|
46 | 49 | import org.springframework.context.support.AbstractApplicationContext;
|
|
54 | 57 |
|
55 | 58 | import static org.hamcrest.Matchers.equalTo;
|
56 | 59 | import static org.hamcrest.Matchers.instanceOf;
|
| 60 | +import static org.hamcrest.Matchers.is; |
57 | 61 | import static org.hamcrest.Matchers.nullValue;
|
58 | 62 | import static org.hamcrest.Matchers.sameInstance;
|
59 | 63 | import static org.junit.Assert.assertEquals;
|
@@ -429,6 +433,22 @@ public void doesNotReplaceExistingScopes() throws Exception { // gh-2082
|
429 | 433 | sameInstance(scope));
|
430 | 434 | }
|
431 | 435 |
|
| 436 | + @Test |
| 437 | + public void containerIsStoppedBeforeContextIsClosed() { |
| 438 | + addEmbeddedServletContainerFactoryBean(); |
| 439 | + this.context.registerBeanDefinition("shutdownOrderingValidator", |
| 440 | + BeanDefinitionBuilder.rootBeanDefinition(ShutdownOrderingValidator.class) |
| 441 | + .addConstructorArgReference("embeddedServletContainerFactory") |
| 442 | + .getBeanDefinition()); |
| 443 | + this.context.refresh(); |
| 444 | + ShutdownOrderingValidator validator = this.context |
| 445 | + .getBean(ShutdownOrderingValidator.class); |
| 446 | + this.context.close(); |
| 447 | + assertThat(validator.destroyed, is(true)); |
| 448 | + assertThat(validator.containerStoppedFirst, is(true)); |
| 449 | + |
| 450 | + } |
| 451 | + |
432 | 452 | private void addEmbeddedServletContainerFactoryBean() {
|
433 | 453 | this.context.registerBeanDefinition("embeddedServletContainerFactory",
|
434 | 454 | new RootBeanDefinition(MockEmbeddedServletContainerFactory.class));
|
@@ -478,4 +498,25 @@ public void doFilter(ServletRequest request, ServletResponse response,
|
478 | 498 |
|
479 | 499 | }
|
480 | 500 |
|
| 501 | + protected static class ShutdownOrderingValidator implements DisposableBean { |
| 502 | + |
| 503 | + private final MockEmbeddedServletContainer servletContainer; |
| 504 | + |
| 505 | + private boolean destroyed = false; |
| 506 | + |
| 507 | + private boolean containerStoppedFirst = false; |
| 508 | + |
| 509 | + ShutdownOrderingValidator( |
| 510 | + MockEmbeddedServletContainerFactory servletContainerFactory) { |
| 511 | + this.servletContainer = servletContainerFactory.getContainer(); |
| 512 | + } |
| 513 | + |
| 514 | + @Override |
| 515 | + public void destroy() { |
| 516 | + this.destroyed = true; |
| 517 | + this.containerStoppedFirst = this.servletContainer.isStopped(); |
| 518 | + } |
| 519 | + |
| 520 | + } |
| 521 | + |
481 | 522 | }
|
0 commit comments