|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.annotation; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.BeanDefinitionStoreException; |
| 22 | +import org.springframework.context.ApplicationContextException; |
| 23 | +import org.springframework.context.annotation.componentscan.simple.SimpleComponent; |
| 24 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for gh-23206. |
| 30 | + * |
| 31 | + * @author Stephane Nicoll |
| 32 | + */ |
| 33 | +public class Gh23206Tests { |
| 34 | + |
| 35 | + @Test |
| 36 | + void componentScanShouldFailWithRegisterBeanCondition() { |
| 37 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 38 | + context.register(ConditionalComponentScanConfiguration.class); |
| 39 | + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(context::refresh) |
| 40 | + .withMessageContaining(ConditionalComponentScanConfiguration.class.getName()) |
| 41 | + .havingCause().isInstanceOf(ApplicationContextException.class) |
| 42 | + .withMessageContaining("Component scan could not be used with conditions in REGISTER_BEAN phase"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void componentScanShouldFailWithRegisterBeanConditionOnClasThatImportedIt() { |
| 47 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 48 | + context.register(ConditionalConfiguration.class); |
| 49 | + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(context::refresh) |
| 50 | + .withMessageContaining(ConditionalConfiguration.class.getName()) |
| 51 | + .havingCause().isInstanceOf(ApplicationContextException.class) |
| 52 | + .withMessageContaining("Component scan could not be used with conditions in REGISTER_BEAN phase"); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Configuration(proxyBeanMethods = false) |
| 57 | + @Conditional(NeverRegisterBeanCondition.class) |
| 58 | + @ComponentScan(basePackageClasses = SimpleComponent.class) |
| 59 | + static class ConditionalComponentScanConfiguration { |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Configuration(proxyBeanMethods = false) |
| 65 | + @Conditional(NeverRegisterBeanCondition.class) |
| 66 | + static class ConditionalConfiguration { |
| 67 | + |
| 68 | + @Configuration(proxyBeanMethods = false) |
| 69 | + @ComponentScan(basePackageClasses = SimpleComponent.class) |
| 70 | + static class NestedConfiguration { |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + static class NeverRegisterBeanCondition implements ConfigurationCondition { |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public ConfigurationPhase getConfigurationPhase() { |
| 85 | + return ConfigurationPhase.REGISTER_BEAN; |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | +} |
0 commit comments