|
| 1 | +package integration; |
| 2 | + |
| 3 | +import static java.util.Arrays.asList; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | + |
| 6 | +import java.util.Collections; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.experimental.runners.Enclosed; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 15 | +import org.springframework.context.annotation.Bean; |
| 16 | +import org.springframework.context.annotation.PropertySource; |
| 17 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 18 | + |
| 19 | +import info.novatec.testit.webtester.config.Configuration; |
| 20 | +import info.novatec.testit.webtester.config.ConfigurationBuilder; |
| 21 | +import info.novatec.testit.webtester.config.ConfigurationExporter; |
| 22 | +import info.novatec.testit.webtester.config.adapters.LocalFileConfigurationAdapter; |
| 23 | +import info.novatec.testit.webtester.config.builders.BaseConfigurationBuilder; |
| 24 | +import info.novatec.testit.webtester.spring4.config.ConfigurationBuilderFactoryBean; |
| 25 | +import info.novatec.testit.webtester.spring4.config.DefaultSpringConfigurationFactoryBean; |
| 26 | +import info.novatec.testit.webtester.spring4.config.PrototypeConfigurationBuilderFactoryBean; |
| 27 | +import info.novatec.testit.webtester.spring4.config.adapters.SpringEnvironmentConfigurationAdapter; |
| 28 | + |
| 29 | + |
| 30 | +@RunWith(Enclosed.class) |
| 31 | +public class AdapterIntegrationTest { |
| 32 | + |
| 33 | + @RunWith(SpringJUnit4ClassRunner.class) |
| 34 | + @SpringApplicationConfiguration(classes = ManualAdapterDefinitionTest.TestConfiguration.class) |
| 35 | + public static class ManualAdapterDefinitionTest { |
| 36 | + |
| 37 | + @Autowired |
| 38 | + Configuration configuration; |
| 39 | + |
| 40 | + /** |
| 41 | + * This test verifies that properties from the configuration are correctly |
| 42 | + * resolved against the spring environment and the configuration is |
| 43 | + * correctly adapted if a property could be resolved against the |
| 44 | + * environment. |
| 45 | + */ |
| 46 | + @Test |
| 47 | + public void propertiesFromSpringContextOverrideExistingValues() { |
| 48 | + assertThat(configuration.getStringProperty("test.property.key1")).hasValue("value 1 from spring environment"); |
| 49 | + assertThat(configuration.getStringProperty("test.property.key2")).hasValue("value 2 from webtester"); |
| 50 | + } |
| 51 | + |
| 52 | + @org.springframework.context.annotation.Configuration |
| 53 | + @PropertySource("classpath:spring-environment.properties") |
| 54 | + public static class TestConfiguration { |
| 55 | + |
| 56 | + @Bean |
| 57 | + public Configuration configuration() { |
| 58 | + BaseConfigurationBuilder builder = new BaseConfigurationBuilder(); |
| 59 | + builder.withAdapter(new LocalFileConfigurationAdapter()); |
| 60 | + builder.withAdapter(springEnvironmentConfigurationAdapter()); |
| 61 | + return builder.build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + public SpringEnvironmentConfigurationAdapter springEnvironmentConfigurationAdapter() { |
| 66 | + return new SpringEnvironmentConfigurationAdapter(); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + @RunWith(SpringJUnit4ClassRunner.class) |
| 74 | + @SpringApplicationConfiguration(classes = ConfigurationBuilderFactoryBeanTest.TestConfiguration.class) |
| 75 | + public static class ConfigurationBuilderFactoryBeanTest { |
| 76 | + |
| 77 | + public static final Logger log = LoggerFactory.getLogger(ConfigurationBuilderFactoryBeanTest.class); |
| 78 | + |
| 79 | + @Autowired |
| 80 | + ConfigurationBuilder builder; |
| 81 | + @Autowired |
| 82 | + ConfigurationBuilder additionalBuild; |
| 83 | + |
| 84 | + @Test |
| 85 | + public void propertiesFromSpringContextOverrideExistingValues() { |
| 86 | + Configuration configuration = builder.build(); |
| 87 | + assertThat(configuration.getStringProperty("test.property.key1")).hasValue("value 1 from spring environment"); |
| 88 | + assertThat(configuration.getStringProperty("test.property.key2")).hasValue("value 2 from webtester"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void builderIsASingleton() { |
| 93 | + assertThat(additionalBuild).isSameAs(builder); |
| 94 | + } |
| 95 | + |
| 96 | + @org.springframework.context.annotation.Configuration |
| 97 | + @PropertySource("classpath:spring-environment.properties") |
| 98 | + public static class TestConfiguration { |
| 99 | + |
| 100 | + @Bean |
| 101 | + public ConfigurationBuilderFactoryBean configurationBuilder() { |
| 102 | + ConfigurationBuilderFactoryBean bean = new ConfigurationBuilderFactoryBean(); |
| 103 | + bean.setAdapters(asList(new LocalFileConfigurationAdapter(), springEnvironmentConfigurationAdapter())); |
| 104 | + bean.setExporters(Collections.singletonList( |
| 105 | + ( ConfigurationExporter ) (key, value) -> log.debug("exported: {}={}", key, value))); |
| 106 | + return bean; |
| 107 | + } |
| 108 | + |
| 109 | + @Bean |
| 110 | + public SpringEnvironmentConfigurationAdapter springEnvironmentConfigurationAdapter() { |
| 111 | + return new SpringEnvironmentConfigurationAdapter(); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + @RunWith(SpringJUnit4ClassRunner.class) |
| 119 | + @SpringApplicationConfiguration(classes = PrototypeConfigurationBuilderFactoryBeanTest.TestConfiguration.class) |
| 120 | + public static class PrototypeConfigurationBuilderFactoryBeanTest { |
| 121 | + |
| 122 | + public static final Logger log = LoggerFactory.getLogger(PrototypeConfigurationBuilderFactoryBeanTest.class); |
| 123 | + |
| 124 | + @Autowired |
| 125 | + ConfigurationBuilder builder; |
| 126 | + @Autowired |
| 127 | + ConfigurationBuilder additionalBuild; |
| 128 | + |
| 129 | + @Test |
| 130 | + public void propertiesFromSpringContextOverrideExistingValues() { |
| 131 | + Configuration configuration = builder.build(); |
| 132 | + assertThat(configuration.getStringProperty("test.property.key1")).hasValue("value 1 from spring environment"); |
| 133 | + assertThat(configuration.getStringProperty("test.property.key2")).hasValue("value 2 from webtester"); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void builderIsNotASingleton() { |
| 138 | + assertThat(additionalBuild).isNotSameAs(builder); |
| 139 | + } |
| 140 | + |
| 141 | + @org.springframework.context.annotation.Configuration |
| 142 | + @PropertySource("classpath:spring-environment.properties") |
| 143 | + public static class TestConfiguration { |
| 144 | + |
| 145 | + @Bean |
| 146 | + public PrototypeConfigurationBuilderFactoryBean configurationBuilder() { |
| 147 | + PrototypeConfigurationBuilderFactoryBean bean = new PrototypeConfigurationBuilderFactoryBean(); |
| 148 | + bean.setAdapters(asList(new LocalFileConfigurationAdapter(), springEnvironmentConfigurationAdapter())); |
| 149 | + return bean; |
| 150 | + } |
| 151 | + |
| 152 | + @Bean |
| 153 | + public SpringEnvironmentConfigurationAdapter springEnvironmentConfigurationAdapter() { |
| 154 | + return new SpringEnvironmentConfigurationAdapter(); |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + @RunWith(SpringJUnit4ClassRunner.class) |
| 162 | + @SpringApplicationConfiguration(classes = DefaultSpringConfigurationFactoryBeanTest.TestConfiguration.class) |
| 163 | + public static class DefaultSpringConfigurationFactoryBeanTest { |
| 164 | + |
| 165 | + @Autowired |
| 166 | + Configuration configuration; |
| 167 | + @Autowired |
| 168 | + Configuration configurationBuild; |
| 169 | + |
| 170 | + @Test |
| 171 | + public void propertiesFromSpringContextOverrideExistingValues() { |
| 172 | + assertThat(configuration.getStringProperty("test.property.key1")).hasValue("value 1 from spring environment"); |
| 173 | + assertThat(configuration.getStringProperty("test.property.key2")).hasValue("value 2 from webtester"); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void configurationIsASingleton() { |
| 178 | + assertThat(configurationBuild).isSameAs(configuration); |
| 179 | + } |
| 180 | + |
| 181 | + @org.springframework.context.annotation.Configuration |
| 182 | + @PropertySource("classpath:spring-environment.properties") |
| 183 | + public static class TestConfiguration { |
| 184 | + |
| 185 | + @Bean |
| 186 | + public DefaultSpringConfigurationFactoryBean configurationBuilder() { |
| 187 | + return new DefaultSpringConfigurationFactoryBean(); |
| 188 | + } |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments