|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 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.boot.autoconfigure; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.BeanFactory; |
| 22 | +import org.springframework.beans.factory.FactoryBean; |
| 23 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 24 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 25 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 26 | +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; |
| 27 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 28 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link AbstractDependsOnBeanFactoryPostProcessor}. |
| 36 | + * |
| 37 | + * @author Dmytro Nosan |
| 38 | + */ |
| 39 | +public class AbstractDependsOnBeanFactoryPostProcessorTests { |
| 40 | + |
| 41 | + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 42 | + .withUserConfiguration(FooBarConfiguration.class); |
| 43 | + |
| 44 | + @Test |
| 45 | + public void fooBeansShouldDependOnBarBeanNames() { |
| 46 | + this.contextRunner |
| 47 | + .withUserConfiguration(FooDependsOnBarNamePostProcessor.class, FooBarFactoryBeanConfiguration.class) |
| 48 | + .run(this::assertThatFooDependsOnBar); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void fooBeansShouldDependOnBarBeanTypes() { |
| 53 | + this.contextRunner |
| 54 | + .withUserConfiguration(FooDependsOnBarTypePostProcessor.class, FooBarFactoryBeanConfiguration.class) |
| 55 | + .run(this::assertThatFooDependsOnBar); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void fooBeansShouldDependOnBarBeanNamesParentContext() { |
| 60 | + try (AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext( |
| 61 | + FooBarFactoryBeanConfiguration.class)) { |
| 62 | + this.contextRunner.withUserConfiguration(FooDependsOnBarNamePostProcessor.class).withParent(parentContext) |
| 63 | + .run(this::assertThatFooDependsOnBar); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void fooBeansShouldDependOnBarBeanTypesParentContext() { |
| 69 | + try (AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext( |
| 70 | + FooBarFactoryBeanConfiguration.class)) { |
| 71 | + this.contextRunner.withUserConfiguration(FooDependsOnBarTypePostProcessor.class).withParent(parentContext) |
| 72 | + .run(this::assertThatFooDependsOnBar); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void assertThatFooDependsOnBar(AssertableApplicationContext context) { |
| 77 | + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); |
| 78 | + assertThat(getBeanDefinition("foo", beanFactory).getDependsOn()).containsExactly("bar", "barFactoryBean"); |
| 79 | + assertThat(getBeanDefinition("fooFactoryBean", beanFactory).getDependsOn()).containsExactly("bar", |
| 80 | + "barFactoryBean"); |
| 81 | + } |
| 82 | + |
| 83 | + private BeanDefinition getBeanDefinition(String beanName, ConfigurableListableBeanFactory beanFactory) { |
| 84 | + try { |
| 85 | + return beanFactory.getBeanDefinition(beanName); |
| 86 | + } |
| 87 | + catch (NoSuchBeanDefinitionException ex) { |
| 88 | + BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory(); |
| 89 | + if (parentBeanFactory instanceof ConfigurableListableBeanFactory) { |
| 90 | + return getBeanDefinition(beanName, (ConfigurableListableBeanFactory) parentBeanFactory); |
| 91 | + } |
| 92 | + throw ex; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static class Foo { |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + static class Bar { |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + @Configuration |
| 105 | + static class FooBarFactoryBeanConfiguration { |
| 106 | + |
| 107 | + @Bean |
| 108 | + public FooFactoryBean fooFactoryBean() { |
| 109 | + return new FooFactoryBean(); |
| 110 | + } |
| 111 | + |
| 112 | + @Bean |
| 113 | + public BarFactoryBean barFactoryBean() { |
| 114 | + return new BarFactoryBean(); |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + @Configuration |
| 120 | + static class FooBarConfiguration { |
| 121 | + |
| 122 | + @Bean |
| 123 | + public Bar bar() { |
| 124 | + return new Bar(); |
| 125 | + } |
| 126 | + |
| 127 | + @Bean |
| 128 | + public Foo foo() { |
| 129 | + return new Foo(); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + static class FooDependsOnBarTypePostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { |
| 135 | + |
| 136 | + protected FooDependsOnBarTypePostProcessor() { |
| 137 | + super(Foo.class, FooFactoryBean.class, Bar.class, BarFactoryBean.class); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + static class FooDependsOnBarNamePostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { |
| 143 | + |
| 144 | + protected FooDependsOnBarNamePostProcessor() { |
| 145 | + super(Foo.class, FooFactoryBean.class, "bar", "barFactoryBean"); |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + static class FooFactoryBean implements FactoryBean<Foo> { |
| 151 | + |
| 152 | + @Override |
| 153 | + public Foo getObject() { |
| 154 | + return new Foo(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public Class<?> getObjectType() { |
| 159 | + return Foo.class; |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + static class BarFactoryBean implements FactoryBean<Bar> { |
| 165 | + |
| 166 | + @Override |
| 167 | + public Bar getObject() { |
| 168 | + return new Bar(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public Class<?> getObjectType() { |
| 173 | + return Bar.class; |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments