|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.beans.factory.annotation; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.generator.BeanInstantiationContribution; |
| 22 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 23 | +import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.Destroy; |
| 24 | +import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.Init; |
| 25 | +import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.InitDestroyBean; |
| 26 | +import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.MultiInitDestroyBean; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link InitDestroyAnnotationBeanPostProcessor}. |
| 35 | + * |
| 36 | + * @author Stephane Nicoll |
| 37 | + */ |
| 38 | +class InitDestroyAnnotationBeanPostProcessorTests { |
| 39 | + |
| 40 | + @Test |
| 41 | + void contributeWithNoCallbackDoesNotMutateRootBeanDefinition() { |
| 42 | + RootBeanDefinition beanDefinition = mock(RootBeanDefinition.class); |
| 43 | + assertThat(createAotContributingBeanPostProcessor().contribute( |
| 44 | + beanDefinition, String.class, "test")).isNull(); |
| 45 | + verifyNoInteractions(beanDefinition); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void contributeWithInitDestroyCallback() { |
| 50 | + RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyBean.class); |
| 51 | + assertThat(createContribution(beanDefinition)).isNull(); |
| 52 | + assertThat(beanDefinition.getInitMethodNames()).containsExactly("initMethod"); |
| 53 | + assertThat(beanDefinition.getDestroyMethodNames()).containsExactly("destroyMethod"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void contributeWithInitDestroyCallbackRetainCustomMethods() { |
| 58 | + RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyBean.class); |
| 59 | + beanDefinition.setInitMethodName("customInitMethod"); |
| 60 | + beanDefinition.setDestroyMethodNames("customDestroyMethod"); |
| 61 | + assertThat(createContribution(beanDefinition)).isNull(); |
| 62 | + assertThat(beanDefinition.getInitMethodNames()) |
| 63 | + .containsExactly("customInitMethod", "initMethod"); |
| 64 | + assertThat(beanDefinition.getDestroyMethodNames()) |
| 65 | + .containsExactly("customDestroyMethod", "destroyMethod"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void contributeWithInitDestroyCallbackFilterDuplicates() { |
| 70 | + RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyBean.class); |
| 71 | + beanDefinition.setInitMethodName("initMethod"); |
| 72 | + beanDefinition.setDestroyMethodNames("destroyMethod"); |
| 73 | + assertThat(createContribution(beanDefinition)).isNull(); |
| 74 | + assertThat(beanDefinition.getInitMethodNames()).containsExactly("initMethod"); |
| 75 | + assertThat(beanDefinition.getDestroyMethodNames()).containsExactly("destroyMethod"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void contributeWithMultipleInitDestroyCallbacks() { |
| 80 | + RootBeanDefinition beanDefinition = new RootBeanDefinition(MultiInitDestroyBean.class); |
| 81 | + assertThat(createContribution(beanDefinition)).isNull(); |
| 82 | + assertThat(beanDefinition.getInitMethodNames()) |
| 83 | + .containsExactly("initMethod", "anotherInitMethod"); |
| 84 | + assertThat(beanDefinition.getDestroyMethodNames()) |
| 85 | + .containsExactly("anotherDestroyMethod", "destroyMethod"); |
| 86 | + } |
| 87 | + |
| 88 | + @Nullable |
| 89 | + private BeanInstantiationContribution createContribution(RootBeanDefinition beanDefinition) { |
| 90 | + InitDestroyAnnotationBeanPostProcessor bpp = createAotContributingBeanPostProcessor(); |
| 91 | + return bpp.contribute(beanDefinition, beanDefinition.getResolvableType().toClass(), "test"); |
| 92 | + } |
| 93 | + |
| 94 | + private InitDestroyAnnotationBeanPostProcessor createAotContributingBeanPostProcessor() { |
| 95 | + InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor(); |
| 96 | + bpp.setInitAnnotationType(Init.class); |
| 97 | + bpp.setDestroyAnnotationType(Destroy.class); |
| 98 | + return bpp; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments