|
16 | 16 |
|
17 | 17 | package org.springframework.beans.factory.generator;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URL; |
| 21 | +import java.util.Enumeration; |
19 | 22 | import java.util.List;
|
20 | 23 | import java.util.function.BiPredicate;
|
21 | 24 |
|
|
28 | 31 | import org.springframework.aot.generator.GeneratedType;
|
29 | 32 | import org.springframework.aot.generator.GeneratedTypeContext;
|
30 | 33 | import org.springframework.beans.factory.config.BeanDefinition;
|
| 34 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
31 | 35 | import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
32 | 36 | import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
33 | 37 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
34 | 38 | import org.springframework.javapoet.ClassName;
|
35 | 39 | import org.springframework.javapoet.support.CodeSnippet;
|
| 40 | +import org.springframework.lang.Nullable; |
| 41 | +import org.springframework.util.Assert; |
36 | 42 |
|
37 | 43 | import static org.assertj.core.api.Assertions.assertThat;
|
38 | 44 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
44 | 50 | */
|
45 | 51 | class BeanDefinitionsContributionTests {
|
46 | 52 |
|
| 53 | + @Test |
| 54 | + void loadContributorWithConstructorArgumentOnBeanFactory() { |
| 55 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 56 | + beanFactory.setBeanClassLoader(new TestSpringFactoriesClassLoader( |
| 57 | + "bean-registration-contribution-provider-constructor.factories")); |
| 58 | + BeanDefinitionsContribution contribution = new BeanDefinitionsContribution(beanFactory); |
| 59 | + assertThat(contribution).extracting("contributionProviders").asList() |
| 60 | + .anySatisfy(provider -> assertThat(provider).isInstanceOfSatisfying(TestConstructorBeanRegistrationContributionProvider.class, |
| 61 | + testProvider -> assertThat(testProvider.beanFactory).isSameAs(beanFactory))) |
| 62 | + .anySatisfy(provider -> assertThat(provider).isInstanceOf(DefaultBeanRegistrationContributionProvider.class)) |
| 63 | + .hasSize(2); |
| 64 | + } |
| 65 | + |
47 | 66 | @Test
|
48 | 67 | void contributeThrowsContributionNotFoundIfNoContributionIsAvailable() {
|
49 | 68 | DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
@@ -158,4 +177,40 @@ public BeanFactoryContribution getContributionFor(String beanName, RootBeanDefin
|
158 | 177 | }
|
159 | 178 | }
|
160 | 179 |
|
| 180 | + static class TestConstructorBeanRegistrationContributionProvider implements BeanRegistrationContributionProvider { |
| 181 | + |
| 182 | + private final ConfigurableListableBeanFactory beanFactory; |
| 183 | + |
| 184 | + TestConstructorBeanRegistrationContributionProvider(ConfigurableListableBeanFactory beanFactory) { |
| 185 | + Assert.notNull(beanFactory, "BeanFactory must not be null"); |
| 186 | + this.beanFactory = beanFactory; |
| 187 | + } |
| 188 | + |
| 189 | + @Nullable |
| 190 | + @Override |
| 191 | + public BeanFactoryContribution getContributionFor(String beanName, RootBeanDefinition beanDefinition) { |
| 192 | + return null; |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + static class TestSpringFactoriesClassLoader extends ClassLoader { |
| 198 | + |
| 199 | + private final String factoriesName; |
| 200 | + |
| 201 | + TestSpringFactoriesClassLoader(String factoriesName) { |
| 202 | + super(BeanDefinitionsContributionTests.class.getClassLoader()); |
| 203 | + this.factoriesName = factoriesName; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public Enumeration<URL> getResources(String name) throws IOException { |
| 208 | + if ("META-INF/spring.factories".equals(name)) { |
| 209 | + return super.getResources("org/springframework/beans/factory/generator/" + this.factoriesName); |
| 210 | + } |
| 211 | + return super.getResources(name); |
| 212 | + } |
| 213 | + |
| 214 | + } |
| 215 | + |
161 | 216 | }
|
0 commit comments