|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.aop.aspectj; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.aop.aspectj.annotation.AspectJAdvisorBeanRegistrationAotProcessor; |
| 22 | +import org.springframework.aot.generate.GenerationContext; |
| 23 | +import org.springframework.aot.hint.MemberCategory; |
| 24 | +import org.springframework.aot.hint.RuntimeHints; |
| 25 | +import org.springframework.aot.test.generate.TestGenerationContext; |
| 26 | +import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; |
| 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 | +import org.springframework.lang.Nullable; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link AspectJAdvisorBeanRegistrationAotProcessor}. |
| 38 | + * |
| 39 | + * @author Sebastien Deleuze |
| 40 | + */ |
| 41 | +public class AspectJAdvisorBeanRegistrationAotProcessorTests { |
| 42 | + |
| 43 | + private final GenerationContext generationContext = new TestGenerationContext(); |
| 44 | + |
| 45 | + private final RuntimeHints runtimeHints = this.generationContext.getRuntimeHints(); |
| 46 | + |
| 47 | + @Test |
| 48 | + void shouldProcessesAspectJClass() { |
| 49 | + process(AspectJClass.class); |
| 50 | + assertThat(reflection().onType(AspectJClass.class).withMemberCategory(MemberCategory.DECLARED_FIELDS)) |
| 51 | + .accepts(this.runtimeHints); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldSkipRegularClass() { |
| 56 | + process(RegularClass.class); |
| 57 | + assertThat(this.runtimeHints.reflection().typeHints()).isEmpty(); |
| 58 | + } |
| 59 | + |
| 60 | + void process(Class<?> beanClass) { |
| 61 | + BeanRegistrationAotContribution contribution = createContribution(beanClass); |
| 62 | + if (contribution != null) { |
| 63 | + contribution.applyTo(this.generationContext, mock()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Nullable |
| 68 | + private static BeanRegistrationAotContribution createContribution(Class<?> beanClass) { |
| 69 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 70 | + beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass)); |
| 71 | + return new AspectJAdvisorBeanRegistrationAotProcessor() |
| 72 | + .processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName())); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + static class AspectJClass { |
| 77 | + private static java.lang.Throwable ajc$initFailureCause; |
| 78 | + } |
| 79 | + |
| 80 | + static class RegularClass { |
| 81 | + private static java.lang.Throwable initFailureCause; |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments