|
20 | 20 |
|
21 | 21 | import javax.validation.constraints.Min;
|
22 | 22 |
|
23 |
| -import org.junit.After; |
24 | 23 | import org.junit.Test;
|
25 | 24 |
|
| 25 | +import org.springframework.boot.test.context.FilteredClassLoader; |
| 26 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
26 | 27 | import org.springframework.context.ApplicationContext;
|
27 |
| -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
28 | 28 | import org.springframework.context.annotation.Bean;
|
29 | 29 | import org.springframework.context.annotation.Configuration;
|
| 30 | +import org.springframework.core.io.ClassPathResource; |
30 | 31 | import org.springframework.validation.MapBindingResult;
|
31 | 32 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
32 | 33 |
|
|
41 | 42 | * Tests for {@link ValidatorAdapter}.
|
42 | 43 | *
|
43 | 44 | * @author Stephane Nicoll
|
| 45 | + * @author Madhura Bhave |
44 | 46 | */
|
45 | 47 | public class ValidatorAdapterTests {
|
46 | 48 |
|
47 |
| - private AnnotationConfigApplicationContext context; |
48 |
| - |
49 |
| - @After |
50 |
| - public void close() { |
51 |
| - if (this.context != null) { |
52 |
| - this.context.close(); |
53 |
| - } |
54 |
| - } |
| 49 | + private ApplicationContextRunner contextRunner = new ApplicationContextRunner(); |
55 | 50 |
|
56 | 51 | @Test
|
57 | 52 | public void wrapLocalValidatorFactoryBean() {
|
58 |
| - ValidatorAdapter wrapper = load(LocalValidatorFactoryBeanConfig.class); |
59 |
| - assertThat(wrapper.supports(SampleData.class)).isTrue(); |
60 |
| - MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), |
61 |
| - "test"); |
62 |
| - wrapper.validate(new SampleData(40), errors); |
63 |
| - assertThat(errors.getErrorCount()).isEqualTo(1); |
| 53 | + this.contextRunner.withUserConfiguration(LocalValidatorFactoryBeanConfig.class) |
| 54 | + .run((context) -> { |
| 55 | + ValidatorAdapter wrapper = context.getBean(ValidatorAdapter.class); |
| 56 | + assertThat(wrapper.supports(SampleData.class)).isTrue(); |
| 57 | + MapBindingResult errors = new MapBindingResult( |
| 58 | + new HashMap<String, Object>(), "test"); |
| 59 | + wrapper.validate(new SampleData(40), errors); |
| 60 | + assertThat(errors.getErrorCount()).isEqualTo(1); |
| 61 | + }); |
64 | 62 | }
|
65 | 63 |
|
66 | 64 | @Test
|
67 | 65 | public void wrapperInvokesCallbackOnNonManagedBean() {
|
68 |
| - load(NonManagedBeanConfig.class); |
69 |
| - LocalValidatorFactoryBean validator = this.context |
70 |
| - .getBean(NonManagedBeanConfig.class).validator; |
71 |
| - verify(validator, times(1)).setApplicationContext(any(ApplicationContext.class)); |
72 |
| - verify(validator, times(1)).afterPropertiesSet(); |
73 |
| - verify(validator, never()).destroy(); |
74 |
| - this.context.close(); |
75 |
| - this.context = null; |
76 |
| - verify(validator, times(1)).destroy(); |
| 66 | + this.contextRunner.withUserConfiguration(NonManagedBeanConfig.class) |
| 67 | + .run((context) -> { |
| 68 | + LocalValidatorFactoryBean validator = context |
| 69 | + .getBean(NonManagedBeanConfig.class).validator; |
| 70 | + verify(validator, times(1)) |
| 71 | + .setApplicationContext(any(ApplicationContext.class)); |
| 72 | + verify(validator, times(1)).afterPropertiesSet(); |
| 73 | + verify(validator, never()).destroy(); |
| 74 | + context.close(); |
| 75 | + verify(validator, times(1)).destroy(); |
| 76 | + }); |
77 | 77 | }
|
78 | 78 |
|
79 | 79 | @Test
|
80 | 80 | public void wrapperDoesNotInvokeCallbackOnManagedBean() {
|
81 |
| - load(ManagedBeanConfig.class); |
82 |
| - LocalValidatorFactoryBean validator = this.context |
83 |
| - .getBean(ManagedBeanConfig.class).validator; |
84 |
| - verify(validator, never()).setApplicationContext(any(ApplicationContext.class)); |
85 |
| - verify(validator, never()).afterPropertiesSet(); |
86 |
| - verify(validator, never()).destroy(); |
87 |
| - this.context.close(); |
88 |
| - this.context = null; |
89 |
| - verify(validator, never()).destroy(); |
| 81 | + this.contextRunner.withUserConfiguration(ManagedBeanConfig.class) |
| 82 | + .run((context) -> { |
| 83 | + LocalValidatorFactoryBean validator = context |
| 84 | + .getBean(ManagedBeanConfig.class).validator; |
| 85 | + verify(validator, never()) |
| 86 | + .setApplicationContext(any(ApplicationContext.class)); |
| 87 | + verify(validator, never()).afterPropertiesSet(); |
| 88 | + verify(validator, never()).destroy(); |
| 89 | + context.close(); |
| 90 | + verify(validator, never()).destroy(); |
| 91 | + }); |
90 | 92 | }
|
91 | 93 |
|
92 |
| - private ValidatorAdapter load(Class<?> config) { |
93 |
| - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
94 |
| - ctx.register(config); |
95 |
| - ctx.refresh(); |
96 |
| - this.context = ctx; |
97 |
| - return this.context.getBean(ValidatorAdapter.class); |
| 94 | + @Test |
| 95 | + public void wrapperWhenValidationProviderNotPresentShouldNotThrowException() { |
| 96 | + ClassPathResource hibernateValidator = new ClassPathResource( |
| 97 | + "META-INF/services/javax.validation.spi.ValidationProvider"); |
| 98 | + this.contextRunner |
| 99 | + .withClassLoader(new FilteredClassLoader( |
| 100 | + FilteredClassLoader.ClassPathResourceFilter |
| 101 | + .of(hibernateValidator), |
| 102 | + FilteredClassLoader.PackageFilter.of("org.hibernate.validator"))) |
| 103 | + .run((context) -> ValidatorAdapter.get(context, null)); |
98 | 104 | }
|
99 | 105 |
|
100 | 106 | @Configuration
|
|
0 commit comments