From 802967fc98480eeb86fff62b9ea7fdeb34731a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 8 Apr 2024 11:09:44 +0200 Subject: [PATCH] Add reproducer See gh-32489 --- .../context/annotation/Gh32489Tests.java | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/Gh32489Tests.java diff --git a/spring-context/src/test/java/org/springframework/context/annotation/Gh32489Tests.java b/spring-context/src/test/java/org/springframework/context/annotation/Gh32489Tests.java new file mode 100644 index 000000000000..4151f397461b --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/Gh32489Tests.java @@ -0,0 +1,194 @@ +/* + * Copyright 2002-2024 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.context.annotation; + +import java.util.List; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.core.ResolvableType; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for gh-32489 + * + * @author Stephane Nicoll + */ +public class Gh32489Tests { + + @Test + void resolveFactoryBeansWithWildcard() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.register(SimpleRepositoryFactoriesBeanHolder.class); + context.refresh(); + assertThat(context.getBean(SimpleRepositoryFactoriesBeanHolder.class).repositoryFactoryies) + .containsOnly(context.getBean("&repositoryFactoryBean", SimpleRepositoryFactoryBean.class)); + } + } + + @Test + void resolveFactoryBeansParentInterfaceWithWildcard() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.register(RepositoryFactoriesInformationHolder.class); + context.refresh(); + assertThat(context.getBean(RepositoryFactoriesInformationHolder.class).repositoryFactoresInformation) + .containsOnly(context.getBean("&repositoryFactoryBean", SimpleRepositoryFactoryBean.class)); + } + } + + @Test + void resolveFactoryBeanWithMatchingGenerics() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.register(RepositoryFactoryHolder.class); + context.refresh(); + assertThat(context.getBean(RepositoryFactoryHolder.class).repositoryFactory) + .isEqualTo(context.getBean("&repositoryFactoryBean")); + } + } + + @Test + @Disabled + void resolveFactoryBeanWithMatchingGeneric() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.refresh(); + ResolvableType requiredType = ResolvableType.forClassWithGenerics(SimpleRepositoryFactoryBean.class, + EmployeeRepository.class, Long.class); + assertThat(context.getBeanProvider(requiredType)).containsOnly(context.getBean("&repositoryFactoryBean")); + } + } + + @Test + void resolveFactoryBeanWithFirstNonMatchingGeneric() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.refresh(); + ResolvableType requiredType = ResolvableType.forClassWithGenerics(SimpleRepositoryFactoryBean.class, + TestBean.class, Long.class); + assertThat(context.getBeanProvider(requiredType)).hasSize(0); + } + } + + @Test + void resolveFactoryBeanWithSecondNonMatchingGeneric() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.refresh(); + ResolvableType requiredType = ResolvableType.forClassWithGenerics(SimpleRepositoryFactoryBean.class, + EmployeeRepository.class, String.class); + assertThat(context.getBeanProvider(requiredType)).hasSize(0); + } + } + + @Test + void resolveFactoryBeanTargetTypeWithMatchingGeneric() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.refresh(); + ResolvableType requiredType = ResolvableType.forClassWithGenerics(Repository.class, + Employee.class, Long.class); + assertThat(context.getBeanProvider(requiredType)). + containsOnly(context.getBean("repositoryFactoryBean")); + } + } + + @Test + void resolveFactoryBeanTargetTypeWithFirstNonMatchingGeneric() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.refresh(); + ResolvableType requiredType = ResolvableType.forClassWithGenerics(Repository.class, + TestBean.class, Long.class); + assertThat(context.getBeanProvider(requiredType)).hasSize(0); + } + } + + @Test + void resolveFactoryBeanTargetTypeWithSecondNonMatchingGeneric() { + try (AnnotationConfigApplicationContext context = prepareContext()) { + context.refresh(); + ResolvableType requiredType = ResolvableType.forClassWithGenerics(Repository.class, + Employee.class, String.class); + assertThat(context.getBeanProvider(requiredType)).hasSize(0); + } + } + + private AnnotationConfigApplicationContext prepareContext() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + RootBeanDefinition rbd = new RootBeanDefinition(SimpleRepositoryFactoryBean.class); + rbd.setTargetType(ResolvableType.forClassWithGenerics(SimpleRepositoryFactoryBean.class, + EmployeeRepository.class, Long.class)); + rbd.getConstructorArgumentValues().addIndexedArgumentValue(0, EmployeeRepository.class); + context.registerBeanDefinition("repositoryFactoryBean", rbd); + return context; + } + + + static class SimpleRepositoryFactoriesBeanHolder { + + @Autowired + List> repositoryFactoryies; + + } + + static class RepositoryFactoriesInformationHolder { + + @Autowired + List> repositoryFactoresInformation; + + } + + static class RepositoryFactoryHolder { + + @Autowired + SimpleRepositoryFactoryBean repositoryFactory; + + } + + static class SimpleRepositoryFactoryBean extends RepositoryFactoryBeanSupport { + + private final Class repositoryType; + + public SimpleRepositoryFactoryBean(Class repositoryType) { + this.repositoryType = repositoryType; + } + + @Override + public T getObject() throws Exception { + return BeanUtils.instantiateClass(this.repositoryType); + } + + @Override + public Class getObjectType() { + return this.repositoryType; + } + } + + abstract static class RepositoryFactoryBeanSupport implements FactoryBean, RepositoryFactoryInformation { + } + + interface RepositoryFactoryInformation { + } + + interface Repository {} + + static class EmployeeRepository implements Repository {} + + record Employee(Long id, String name) {} + +}