|
| 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.cache.annotation; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.aop.config.AopConfigUtils; |
| 22 | +import org.springframework.aop.framework.AopProxyUtils; |
| 23 | +import org.springframework.aot.generate.GenerationContext; |
| 24 | +import org.springframework.aot.hint.RuntimeHints; |
| 25 | +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; |
| 26 | +import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; |
| 27 | +import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; |
| 28 | +import org.springframework.beans.factory.aot.BeanRegistrationCode; |
| 29 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 30 | +import org.springframework.beans.factory.support.RegisteredBean; |
| 31 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 32 | +import org.springframework.core.testfixture.aot.generate.TestGenerationContext; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for {@link CachingBeanRegistrationAotProcessor}. |
| 40 | + * |
| 41 | + * @author Sebastien Deleuze |
| 42 | + */ |
| 43 | +public class CachingBeanRegistrationAotProcessorTests { |
| 44 | + |
| 45 | + BeanRegistrationAotProcessor processor = new CachingBeanRegistrationAotProcessor(); |
| 46 | + |
| 47 | + GenerationContext generationContext = new TestGenerationContext(); |
| 48 | + |
| 49 | + |
| 50 | + @Test |
| 51 | + void ignoresNonCachingBean() { |
| 52 | + assertThat(createContribution(NonCaching.class, false)).isNull(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void contributesProxyForCacheableInterface() { |
| 57 | + process(CacheableServiceImpl.class, false); |
| 58 | + RuntimeHints runtimeHints = this.generationContext.getRuntimeHints(); |
| 59 | + assertThat(RuntimeHintsPredicates.proxies().forInterfaces(AopProxyUtils.completeJdkProxyInterfaces(CacheableServiceInterface.class))).accepts(runtimeHints); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void ignoresProxyForCacheableInterfaceWithClassProxying() { |
| 64 | + assertThat(createContribution(CacheableServiceImpl.class, true)).isNull(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void ignoresProxyForCacheableClass() { |
| 69 | + assertThat(createContribution(CacheableService.class, true)).isNull(); |
| 70 | + } |
| 71 | + |
| 72 | + @Nullable |
| 73 | + private BeanRegistrationAotContribution createContribution(Class<?> beanClass, boolean forceClassProxying) { |
| 74 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 75 | + if (forceClassProxying) { |
| 76 | + AopConfigUtils.registerAutoProxyCreatorIfNecessary(beanFactory); |
| 77 | + AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(beanFactory); |
| 78 | + } |
| 79 | + beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass)); |
| 80 | + return this.processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName())); |
| 81 | + } |
| 82 | + |
| 83 | + private void process(Class<?> beanClass, boolean forceClassProxying) { |
| 84 | + BeanRegistrationAotContribution contribution = createContribution(beanClass, forceClassProxying); |
| 85 | + assertThat(contribution).isNotNull(); |
| 86 | + contribution.applyTo(this.generationContext, mock(BeanRegistrationCode.class)); |
| 87 | + } |
| 88 | + |
| 89 | + static class NonCaching { |
| 90 | + } |
| 91 | + |
| 92 | + interface CacheableServiceInterface { |
| 93 | + |
| 94 | + @Cacheable |
| 95 | + void invoke(); |
| 96 | + } |
| 97 | + |
| 98 | + class CacheableServiceImpl implements CacheableServiceInterface { |
| 99 | + |
| 100 | + @Override |
| 101 | + public void invoke() { |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + class CacheableService { |
| 106 | + |
| 107 | + @Cacheable |
| 108 | + public void invoke() { |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments