|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.aot |
| 18 | + |
| 19 | +import org.assertj.core.api.Assertions |
| 20 | +import org.junit.jupiter.api.Test |
| 21 | +import org.mockito.Mockito |
| 22 | +import org.springframework.aot.generate.GenerationContext |
| 23 | +import org.springframework.aot.hint.MemberCategory |
| 24 | +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates |
| 25 | +import org.springframework.aot.test.generate.TestGenerationContext |
| 26 | +import org.springframework.beans.factory.aot.* |
| 27 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory |
| 28 | +import org.springframework.beans.factory.support.RegisteredBean |
| 29 | +import org.springframework.beans.factory.support.RootBeanDefinition |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for [KotlinReflectionBeanRegistrationAotProcessor]. |
| 33 | + * |
| 34 | + * @author Sebastien Deleuze |
| 35 | + */ |
| 36 | +class KotlinReflectionBeanRegistrationAotProcessorTests { |
| 37 | + |
| 38 | + private val processor = KotlinReflectionBeanRegistrationAotProcessor() |
| 39 | + |
| 40 | + private val generationContext = TestGenerationContext() |
| 41 | + |
| 42 | + @Test |
| 43 | + fun processorIsRegistered() { |
| 44 | + Assertions.assertThat( |
| 45 | + AotServices.factories(javaClass.classLoader).load(BeanRegistrationAotProcessor::class.java)) |
| 46 | + .anyMatch(KotlinReflectionBeanRegistrationAotProcessor::class.java::isInstance) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun shouldProcessKotlinBean() { |
| 51 | + process(SampleKotlinBean::class.java) |
| 52 | + Assertions.assertThat( |
| 53 | + RuntimeHintsPredicates.reflection() |
| 54 | + .onType(SampleKotlinBean::class.java) |
| 55 | + .withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS) |
| 56 | + ).accepts(generationContext.runtimeHints) |
| 57 | + Assertions.assertThat( |
| 58 | + RuntimeHintsPredicates.reflection() |
| 59 | + .onType(BaseKotlinBean::class.java) |
| 60 | + .withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS) |
| 61 | + ).accepts(generationContext.runtimeHints) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun shouldNotProcessJavaBean() { |
| 66 | + process(SampleJavaBean::class.java) |
| 67 | + Assertions.assertThat(generationContext.runtimeHints.reflection().typeHints()).isEmpty() |
| 68 | + } |
| 69 | + |
| 70 | + private fun process(beanClass: Class<*>) { |
| 71 | + createContribution(beanClass)?.applyTo(generationContext, Mockito.mock(BeanRegistrationCode::class.java)) |
| 72 | + } |
| 73 | + |
| 74 | + private fun createContribution(beanClass: Class<*>): BeanRegistrationAotContribution? { |
| 75 | + val beanFactory = DefaultListableBeanFactory() |
| 76 | + beanFactory.registerBeanDefinition(beanClass.name, RootBeanDefinition(beanClass)) |
| 77 | + return processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.name)) |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + class SampleKotlinBean : BaseKotlinBean() { |
| 82 | + fun sample() { |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + open class BaseKotlinBean { |
| 87 | + fun base() { |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments