|
26 | 26 | import org.springframework.aop.framework.Advised;
|
27 | 27 | import org.springframework.aot.hint.RuntimeHints;
|
28 | 28 | import org.springframework.beans.factory.ListableBeanFactory;
|
| 29 | +import org.springframework.beans.factory.parsing.BeanComponentDefinition; |
29 | 30 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
30 | 31 | import org.springframework.context.annotation.AnnotationBeanNameGenerator;
|
31 | 32 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
32 | 33 | import org.springframework.context.annotation.ComponentScan;
|
33 | 34 | import org.springframework.context.annotation.ComponentScan.Filter;
|
34 | 35 | import org.springframework.context.annotation.FilterType;
|
35 | 36 | import org.springframework.context.support.GenericApplicationContext;
|
| 37 | +import org.springframework.core.ResolvableType; |
36 | 38 | import org.springframework.core.env.StandardEnvironment;
|
37 | 39 | import org.springframework.core.metrics.ApplicationStartup;
|
38 | 40 | import org.springframework.core.type.AnnotationMetadata;
|
|
46 | 48 | import org.springframework.data.repository.core.support.DummyRepositoryFactoryBean;
|
47 | 49 | import org.springframework.data.repository.sample.AddressRepository;
|
48 | 50 | import org.springframework.data.repository.sample.AddressRepositoryClient;
|
| 51 | +import org.springframework.data.repository.sample.Product; |
49 | 52 | import org.springframework.data.repository.sample.ProductRepository;
|
| 53 | +import org.springframework.data.repository.sample.SampleAnnotatedRepository; |
| 54 | + |
| 55 | +import java.io.Serializable; |
| 56 | +import java.util.List; |
50 | 57 |
|
51 | 58 | /**
|
52 | 59 | * Unit tests for {@link RepositoryConfigurationDelegate}.
|
53 | 60 | *
|
54 | 61 | * @author Oliver Gierke
|
55 | 62 | * @author Mark Paluch
|
| 63 | + * @author Yanming Zhou |
56 | 64 | * @soundtrack Richard Spaven - Tribute (Whole Other*)
|
57 | 65 | */
|
58 | 66 | @ExtendWith(MockitoExtension.class)
|
@@ -87,6 +95,38 @@ void registersRepositoryBeanNameAsAttribute() {
|
87 | 95 | }
|
88 | 96 | }
|
89 | 97 |
|
| 98 | + @Test |
| 99 | + void registersAccurateTargetTypeOfBeanDefinition() { |
| 100 | + |
| 101 | + var environment = new StandardEnvironment(); |
| 102 | + var context = new GenericApplicationContext(); |
| 103 | + |
| 104 | + RepositoryConfigurationSource configSource = new AnnotationRepositoryConfigurationSource( |
| 105 | + new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment, |
| 106 | + context.getDefaultListableBeanFactory(), null); |
| 107 | + |
| 108 | + var delegate = new RepositoryConfigurationDelegate(configSource, context, environment); |
| 109 | + List<BeanComponentDefinition> bcds = delegate.registerRepositoriesIn(context, extension); |
| 110 | + var productRepository = bcds.stream().filter(bcd -> |
| 111 | + bcd.getName().equals("productRepository")).map(BeanComponentDefinition::getBeanDefinition).findFirst(); |
| 112 | + var sampleAnnotatedRepository = bcds.stream().filter(bcd -> |
| 113 | + bcd.getName().equals("sampleAnnotatedRepository")).map(BeanComponentDefinition::getBeanDefinition).findFirst(); |
| 114 | + |
| 115 | + assertThat(productRepository).isPresent().get().isInstanceOfSatisfying(RootBeanDefinition.class, it -> { |
| 116 | + ResolvableType[] generics = it.getResolvableType().getGenerics(); |
| 117 | + assertThat(generics[0].resolve()).isSameAs(ProductRepository.class); |
| 118 | + assertThat(generics[1].resolve()).isSameAs(Product.class); |
| 119 | + assertThat(generics[2].resolve()).isSameAs(Long.class); |
| 120 | + }); |
| 121 | + |
| 122 | + assertThat(sampleAnnotatedRepository).isPresent().get().isInstanceOfSatisfying(RootBeanDefinition.class, it -> { |
| 123 | + ResolvableType[] generics = it.getResolvableType().getGenerics(); |
| 124 | + assertThat(generics[0].resolve()).isSameAs(SampleAnnotatedRepository.class); |
| 125 | + assertThat(generics[1].resolve()).isSameAs(Object.class); |
| 126 | + assertThat(generics[2].resolve()).isSameAs(Serializable.class); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
90 | 130 | @Test // DATACMNS-1368
|
91 | 131 | void registersLazyAutowireCandidateResolver() {
|
92 | 132 | assertLazyRepositoryBeanSetup(LazyConfig.class);
|
|
0 commit comments