Test status quo for private init/destroy methods in AOT mode

This commit only tests which init/destroy methods are "registered" in
AOT mode.

This commit does NOT test that the registered methods can actually be
invoked in AOT mode: that will be addressed in a separate commit.

See gh-30692
This commit is contained in:
Sam Brannen 2023-06-21 13:47:21 +02:00
parent 2fd83aa764
commit 4879b56bb9
1 changed files with 80 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@ package org.springframework.beans.factory.annotation;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
@ -29,12 +31,15 @@ import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.I
import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.MultiInitDestroyBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
/**
* Tests for {@link InitDestroyAnnotationBeanPostProcessor}.
*
* @since 6.0
* @author Stephane Nicoll
* @author Phillip Webb
* @author Sam Brannen
*/
class InitDestroyAnnotationBeanPostProcessorTests {
@ -109,6 +114,35 @@ class InitDestroyAnnotationBeanPostProcessorTests {
assertThat(mergedBeanDefinition.getDestroyMethodNames()).containsExactly("anotherDestroyMethod", "destroyMethod");
}
@Test
void processAheadOfTimeWithMultipleLevelsOfPublicAndPrivateInitAndDestroyMethods() {
RootBeanDefinition beanDefinition = new RootBeanDefinition(CustomAnnotatedPrivateSameNameInitDestroyBean.class);
// We explicitly define "afterPropertiesSet" as a "custom init method"
// to ensure that it will be tracked as such even though it has the same
// name as InitializingBean#afterPropertiesSet().
beanDefinition.setInitMethodNames("afterPropertiesSet", "customInit");
// We explicitly define "destroy" as a "custom destroy method"
// to ensure that it will be tracked as such even though it has the same
// name as DisposableBean#destroy().
beanDefinition.setDestroyMethodNames("destroy", "customDestroy");
processAheadOfTime(beanDefinition);
RootBeanDefinition mergedBeanDefinition = getMergedBeanDefinition();
assertSoftly(softly -> {
softly.assertThat(mergedBeanDefinition.getInitMethodNames()).containsExactly(
"afterPropertiesSet",
"customInit",
CustomAnnotatedPrivateInitDestroyBean.class.getName() + ".privateInit", // fully-qualified private method
CustomAnnotatedPrivateSameNameInitDestroyBean.class.getName() + ".privateInit" // fully-qualified private method
);
softly.assertThat(mergedBeanDefinition.getDestroyMethodNames()).containsExactly(
"destroy",
"customDestroy",
CustomAnnotatedPrivateSameNameInitDestroyBean.class.getName() + ".privateDestroy", // fully-qualified private method
CustomAnnotatedPrivateInitDestroyBean.class.getName() + ".privateDestroy" // fully-qualified private method
);
});
}
private void processAheadOfTime(RootBeanDefinition beanDefinition) {
RegisteredBean registeredBean = registerBean(beanDefinition);
assertThat(createAotBeanPostProcessor().processAheadOfTime(registeredBean)).isNull();
@ -133,4 +167,49 @@ class InitDestroyAnnotationBeanPostProcessorTests {
static class NoInitDestroyBean {}
static class CustomInitDestroyBean {
public void customInit() {
}
public void customDestroy() {
}
}
static class CustomInitializingDisposableBean extends CustomInitDestroyBean
implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() {
}
@Override
public void destroy() {
}
}
static class CustomAnnotatedPrivateInitDestroyBean extends CustomInitializingDisposableBean {
@Init
private void privateInit() {
}
@Destroy
private void privateDestroy() {
}
}
static class CustomAnnotatedPrivateSameNameInitDestroyBean extends CustomAnnotatedPrivateInitDestroyBean {
@Init
@SuppressWarnings("unused")
private void privateInit() {
}
@Destroy
@SuppressWarnings("unused")
private void privateDestroy() {
}
}
}