|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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 | + * http://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.boot.autoconfigure; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import org.springframework.beans.BeansException; |
| 24 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 25 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 26 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 27 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 28 | +import org.springframework.boot.SpringApplication; |
| 29 | +import org.springframework.boot.WebApplicationType; |
| 30 | +import org.springframework.context.ApplicationContextInitializer; |
| 31 | +import org.springframework.context.support.GenericApplicationContext; |
| 32 | +import org.springframework.test.util.ReflectionTestUtils; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link SharedMetadataReaderFactoryContextInitializer}. |
| 38 | + * |
| 39 | + * @author Dave Syer |
| 40 | + */ |
| 41 | +public class SharedMetadataReaderFactoryContextInitializerTests { |
| 42 | + |
| 43 | + @Test |
| 44 | + public void checkOrderOfInitializer() { |
| 45 | + SpringApplication application = new SpringApplication(TestConfig.class); |
| 46 | + application.setWebApplicationType(WebApplicationType.NONE); |
| 47 | + @SuppressWarnings("unchecked") |
| 48 | + List<ApplicationContextInitializer<?>> initializers = (List<ApplicationContextInitializer<?>>) ReflectionTestUtils |
| 49 | + .getField(application, "initializers"); |
| 50 | + // Simulate what would happen if an initializer was added using spring.factories |
| 51 | + // and happened to be loaded first |
| 52 | + initializers.add(0, new Initializer()); |
| 53 | + GenericApplicationContext context = (GenericApplicationContext) application.run(); |
| 54 | + BeanDefinition definition = context.getBeanDefinition( |
| 55 | + SharedMetadataReaderFactoryContextInitializer.BEAN_NAME); |
| 56 | + assertThat(definition.getAttribute("seen")).isEqualTo(true); |
| 57 | + } |
| 58 | + |
| 59 | + protected static class TestConfig { |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + static class Initializer |
| 64 | + implements ApplicationContextInitializer<GenericApplicationContext> { |
| 65 | + |
| 66 | + @Override |
| 67 | + public void initialize(GenericApplicationContext applicationContext) { |
| 68 | + applicationContext.addBeanFactoryPostProcessor(new PostProcessor()); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + static class PostProcessor implements BeanDefinitionRegistryPostProcessor { |
| 74 | + |
| 75 | + @Override |
| 76 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) |
| 77 | + throws BeansException { |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) |
| 82 | + throws BeansException { |
| 83 | + for (String name : registry.getBeanDefinitionNames()) { |
| 84 | + BeanDefinition definition = registry.getBeanDefinition(name); |
| 85 | + definition.setAttribute("seen", true); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments