|
41 | 41 | import org.springframework.beans.factory.support.RootBeanDefinition; |
42 | 42 | import org.springframework.context.annotation.Bean; |
43 | 43 | import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.core.env.Environment; |
44 | 45 | import org.springframework.data.aot.ManagedTypesRegistrationAotContribution.ManagedTypesInstanceCodeFragment; |
45 | 46 | import org.springframework.data.domain.ManagedTypes; |
46 | 47 | import org.springframework.javapoet.MethodSpec; |
47 | 48 | import org.springframework.javapoet.MethodSpec.Builder; |
| 49 | +import org.springframework.mock.env.MockEnvironment; |
48 | 50 | import org.springframework.test.util.ReflectionTestUtils; |
49 | 51 |
|
50 | 52 | /** |
@@ -198,15 +200,15 @@ void generatesInstanceSupplierCodeFragmentToAvoidDuplicateInvocations() { |
198 | 200 | @Test // GH-2680 |
199 | 201 | void generatesInstanceSupplierCodeFragmentToAvoidDuplicateInvocationsForEmptyManagedTypes() { |
200 | 202 |
|
201 | | - beanFactory.registerBeanDefinition("commons.managed-types", BeanDefinitionBuilder.rootBeanDefinition(EmptyManagedTypes.class).getBeanDefinition()); |
| 203 | + beanFactory.registerBeanDefinition("commons.managed-types", |
| 204 | + BeanDefinitionBuilder.rootBeanDefinition(EmptyManagedTypes.class).getBeanDefinition()); |
202 | 205 | RegisteredBean registeredBean = RegisteredBean.of(beanFactory, "commons.managed-types"); |
203 | 206 |
|
204 | 207 | BeanRegistrationAotContribution contribution = createPostProcessor("commons") |
205 | 208 | .processAheadOfTime(RegisteredBean.of(beanFactory, "commons.managed-types")); |
206 | 209 |
|
207 | 210 | AotTestCodeContributionBuilder.withContextFor(this.getClass()).writeContentFor(contribution).compile(it -> { |
208 | 211 |
|
209 | | - |
210 | 212 | InstanceSupplier<ManagedTypes> types = ReflectionTestUtils |
211 | 213 | .invokeMethod(it.getAllCompiledClasses().iterator().next(), "instance"); |
212 | 214 | try { |
@@ -266,6 +268,40 @@ void canGenerateCodeReturnsFalseIfNoFactoryMethodPresent() { |
266 | 268 | assertThat(fragment.canGenerateCode()).isFalse(); |
267 | 269 | } |
268 | 270 |
|
| 271 | + @Test // GH-3414 |
| 272 | + void usesConfiguredEnvironment() { |
| 273 | + |
| 274 | + MockEnvironment env = spy(new MockEnvironment()); |
| 275 | + ManagedTypesBeanRegistrationAotProcessor processor = createPostProcessor("commons"); |
| 276 | + processor.setEnvironment(env); |
| 277 | + |
| 278 | + beanFactory.registerBeanDefinition("commons.managed-types", managedTypesDefinition); |
| 279 | + |
| 280 | + BeanRegistrationAotContribution contribution = processor |
| 281 | + .processAheadOfTime(RegisteredBean.of(beanFactory, "commons.managed-types")); |
| 282 | + |
| 283 | + contribution.applyTo(new TestGenerationContext(Object.class), null); |
| 284 | + |
| 285 | + verify(env).getProperty(eq("spring.aot.data.accessors.enabled"), eq(Boolean.class), eq(true)); |
| 286 | + } |
| 287 | + |
| 288 | + @Test // GH-3414 |
| 289 | + void usesUsesEnvironmentFromBeanIfNotSet() { |
| 290 | + |
| 291 | + MockEnvironment env = spy(new MockEnvironment()); |
| 292 | + ManagedTypesBeanRegistrationAotProcessor processor = createPostProcessor("commons"); |
| 293 | + |
| 294 | + beanFactory.registerBeanDefinition("commons.managed-types", managedTypesDefinition); |
| 295 | + beanFactory.registerBeanDefinition("environment", new RootBeanDefinition(Environment.class, () -> env)); |
| 296 | + |
| 297 | + BeanRegistrationAotContribution contribution = processor |
| 298 | + .processAheadOfTime(RegisteredBean.of(beanFactory, "commons.managed-types")); |
| 299 | + |
| 300 | + contribution.applyTo(new TestGenerationContext(Object.class), null); |
| 301 | + |
| 302 | + verify(env).getProperty(eq("spring.aot.data.accessors.enabled"), eq(Boolean.class), eq(true)); |
| 303 | + } |
| 304 | + |
269 | 305 | private ManagedTypesBeanRegistrationAotProcessor createPostProcessor(String moduleIdentifier) { |
270 | 306 | ManagedTypesBeanRegistrationAotProcessor postProcessor = new ManagedTypesBeanRegistrationAotProcessor(); |
271 | 307 | postProcessor.setModuleIdentifier(moduleIdentifier); |
|
0 commit comments