|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2017 the original author or authors. |
| 2 | + * Copyright 2012-2019 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.
|
|
38 | 38 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
39 | 39 | import org.springframework.boot.test.context.runner.ContextConsumer;
|
40 | 40 | import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
| 41 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
41 | 42 | import org.springframework.context.annotation.Bean;
|
42 | 43 | import org.springframework.context.annotation.Configuration;
|
43 | 44 | import org.springframework.context.annotation.Primary;
|
@@ -159,6 +160,32 @@ public void openEntityManagerInViewInterceptorIsNotRegisteredWhenFilterPresent()
|
159 | 160 | .doesNotHaveBean(OpenEntityManagerInViewInterceptor.class));
|
160 | 161 | }
|
161 | 162 |
|
| 163 | + @Test |
| 164 | + public void openEntityManagerInViewInterceptorIsNotRegisteredWhenFilterRegistrationPresent() { |
| 165 | + new WebApplicationContextRunner() |
| 166 | + .withPropertyValues("spring.datasource.generate-unique-name=true") |
| 167 | + .withUserConfiguration(TestFilterRegistrationConfiguration.class) |
| 168 | + .withConfiguration(AutoConfigurations.of( |
| 169 | + DataSourceAutoConfiguration.class, |
| 170 | + TransactionAutoConfiguration.class, this.autoConfiguredClass)) |
| 171 | + .run((context) -> assertThat(context) |
| 172 | + .doesNotHaveBean(OpenEntityManagerInViewInterceptor.class)); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void openEntityManagerInViewInterceptorAutoConfigurationBacksOffWhenManuallyRegistered() { |
| 177 | + new WebApplicationContextRunner() |
| 178 | + .withPropertyValues("spring.datasource.generate-unique-name=true") |
| 179 | + .withUserConfiguration(TestInterceptorManualConfiguration.class) |
| 180 | + .withConfiguration(AutoConfigurations.of( |
| 181 | + DataSourceAutoConfiguration.class, |
| 182 | + TransactionAutoConfiguration.class, this.autoConfiguredClass)) |
| 183 | + .run((context) -> assertThat(context) |
| 184 | + .getBean(OpenEntityManagerInViewInterceptor.class) |
| 185 | + .isExactlyInstanceOf( |
| 186 | + TestInterceptorManualConfiguration.ManualOpenEntityManagerInViewInterceptor.class)); |
| 187 | + } |
| 188 | + |
162 | 189 | @Test
|
163 | 190 | public void openEntityManagerInViewInterceptorISNotRegisteredWhenExplicitlyOff() {
|
164 | 191 | new WebApplicationContextRunner()
|
@@ -298,6 +325,33 @@ public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() {
|
298 | 325 |
|
299 | 326 | }
|
300 | 327 |
|
| 328 | + @Configuration |
| 329 | + @TestAutoConfigurationPackage(City.class) |
| 330 | + protected static class TestFilterRegistrationConfiguration { |
| 331 | + |
| 332 | + @Bean |
| 333 | + public FilterRegistrationBean<OpenEntityManagerInViewFilter> OpenEntityManagerInViewFilterFilterRegistrationBean() { |
| 334 | + return new FilterRegistrationBean<>(); |
| 335 | + } |
| 336 | + |
| 337 | + } |
| 338 | + |
| 339 | + @Configuration |
| 340 | + @TestAutoConfigurationPackage(City.class) |
| 341 | + protected static class TestInterceptorManualConfiguration { |
| 342 | + |
| 343 | + @Bean |
| 344 | + public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() { |
| 345 | + return new ManualOpenEntityManagerInViewInterceptor(); |
| 346 | + } |
| 347 | + |
| 348 | + protected static class ManualOpenEntityManagerInViewInterceptor |
| 349 | + extends OpenEntityManagerInViewInterceptor { |
| 350 | + |
| 351 | + } |
| 352 | + |
| 353 | + } |
| 354 | + |
301 | 355 | @Configuration
|
302 | 356 | protected static class TestConfigurationWithLocalContainerEntityManagerFactoryBean
|
303 | 357 | extends TestConfiguration {
|
|
0 commit comments