|
| 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.context.properties; |
| 18 | + |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.TreeMap; |
| 22 | + |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import org.springframework.boot.context.properties.bind.AbstractBindHandler; |
| 27 | +import org.springframework.boot.context.properties.bind.BindContext; |
| 28 | +import org.springframework.boot.context.properties.bind.BindHandler; |
| 29 | +import org.springframework.boot.context.properties.bind.BindResult; |
| 30 | +import org.springframework.boot.context.properties.bind.Bindable; |
| 31 | +import org.springframework.boot.context.properties.source.ConfigurationPropertyName; |
| 32 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | +import org.springframework.context.annotation.Import; |
| 35 | +import org.springframework.test.context.support.TestPropertySourceUtils; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for {@link ConfigurationPropertiesBindHandlerAdvisor}. |
| 41 | + * |
| 42 | + * @author Phillip Webb |
| 43 | + */ |
| 44 | +public class ConfigurationPropertiesBindHandlerAdvisorTests { |
| 45 | + |
| 46 | + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 47 | + |
| 48 | + @After |
| 49 | + public void cleanup() { |
| 50 | + this.context.close(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void loadWithoutConfigurationPropertiesBindHandlerAdvisor() { |
| 55 | + load(WithoutConfigurationPropertiesBindHandlerAdvisor.class, |
| 56 | + "foo.bar.default.content-type=text/plain", |
| 57 | + "foo.bar.bindings.input.destination=d1", |
| 58 | + "foo.bar.bindings.input.content-type=text/xml", |
| 59 | + "foo.bar.bindings.output.destination=d2"); |
| 60 | + BindingServiceProperties properties = this.context |
| 61 | + .getBean(BindingServiceProperties.class); |
| 62 | + BindingProperties input = properties.getBindings().get("input"); |
| 63 | + assertThat(input.getDestination()).isEqualTo("d1"); |
| 64 | + assertThat(input.getContentType()).isEqualTo("text/xml"); |
| 65 | + BindingProperties output = properties.getBindings().get("output"); |
| 66 | + assertThat(output.getDestination()).isEqualTo("d2"); |
| 67 | + assertThat(output.getContentType()).isEqualTo("application/json"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void loadWithConfigurationPropertiesBindHandlerAdvisor() { |
| 72 | + load(WithConfigurationPropertiesBindHandlerAdvisor.class, |
| 73 | + "foo.bar.default.content-type=text/plain", |
| 74 | + "foo.bar.bindings.input.destination=d1", |
| 75 | + "foo.bar.bindings.input.content-type=text/xml", |
| 76 | + "foo.bar.bindings.output.destination=d2"); |
| 77 | + BindingServiceProperties properties = this.context |
| 78 | + .getBean(BindingServiceProperties.class); |
| 79 | + BindingProperties input = properties.getBindings().get("input"); |
| 80 | + assertThat(input.getDestination()).isEqualTo("d1"); |
| 81 | + assertThat(input.getContentType()).isEqualTo("text/xml"); |
| 82 | + BindingProperties output = properties.getBindings().get("output"); |
| 83 | + assertThat(output.getDestination()).isEqualTo("d2"); |
| 84 | + assertThat(output.getContentType()).isEqualTo("text/plain"); |
| 85 | + } |
| 86 | + |
| 87 | + private AnnotationConfigApplicationContext load(Class<?> configuration, |
| 88 | + String... inlinedProperties) { |
| 89 | + return load(new Class<?>[] { configuration }, inlinedProperties); |
| 90 | + } |
| 91 | + |
| 92 | + private AnnotationConfigApplicationContext load(Class<?>[] configuration, |
| 93 | + String... inlinedProperties) { |
| 94 | + this.context.register(configuration); |
| 95 | + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, |
| 96 | + inlinedProperties); |
| 97 | + this.context.refresh(); |
| 98 | + return this.context; |
| 99 | + } |
| 100 | + |
| 101 | + @Configuration |
| 102 | + @EnableConfigurationProperties(BindingServiceProperties.class) |
| 103 | + static class WithoutConfigurationPropertiesBindHandlerAdvisor { |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + @Configuration |
| 108 | + @EnableConfigurationProperties(BindingServiceProperties.class) |
| 109 | + @Import(DefaultValuesConfigurationPropertiesBindHandlerAdvisor.class) |
| 110 | + static class WithConfigurationPropertiesBindHandlerAdvisor { |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + static class DefaultValuesConfigurationPropertiesBindHandlerAdvisor |
| 115 | + implements ConfigurationPropertiesBindHandlerAdvisor { |
| 116 | + |
| 117 | + @Override |
| 118 | + public BindHandler apply(BindHandler bindHandler) { |
| 119 | + return new DefaultValuesBindHandler(bindHandler); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + static class DefaultValuesBindHandler extends AbstractBindHandler { |
| 125 | + |
| 126 | + private final Map<ConfigurationPropertyName, ConfigurationPropertyName> mappings; |
| 127 | + |
| 128 | + DefaultValuesBindHandler(BindHandler bindHandler) { |
| 129 | + super(bindHandler); |
| 130 | + this.mappings = new LinkedHashMap<>(); |
| 131 | + this.mappings.put(ConfigurationPropertyName.of("foo.bar.bindings"), |
| 132 | + ConfigurationPropertyName.of("foo.bar.default")); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public <T> Bindable<T> onStart(ConfigurationPropertyName name, Bindable<T> target, |
| 137 | + BindContext context) { |
| 138 | + ConfigurationPropertyName defaultName = getDefaultName(name); |
| 139 | + if (defaultName != null) { |
| 140 | + BindResult<T> result = context.getBinder().bind(defaultName, target); |
| 141 | + if (result.isBound()) { |
| 142 | + return target.withExistingValue(result.get()); |
| 143 | + } |
| 144 | + } |
| 145 | + return super.onStart(name, target, context); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + private ConfigurationPropertyName getDefaultName(ConfigurationPropertyName name) { |
| 150 | + for (Map.Entry<ConfigurationPropertyName, ConfigurationPropertyName> mapping : this.mappings |
| 151 | + .entrySet()) { |
| 152 | + ConfigurationPropertyName from = mapping.getKey(); |
| 153 | + ConfigurationPropertyName to = mapping.getValue(); |
| 154 | + if (name.getNumberOfElements() == from.getNumberOfElements() + 1 |
| 155 | + && from.isParentOf(name)) { |
| 156 | + return to; |
| 157 | + } |
| 158 | + } |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + @ConfigurationProperties("foo.bar") |
| 165 | + static class BindingServiceProperties { |
| 166 | + |
| 167 | + private Map<String, BindingProperties> bindings = new TreeMap<>(); |
| 168 | + |
| 169 | + public Map<String, BindingProperties> getBindings() { |
| 170 | + return this.bindings; |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + static class BindingProperties { |
| 176 | + |
| 177 | + private String destination; |
| 178 | + |
| 179 | + private String contentType = "application/json"; |
| 180 | + |
| 181 | + public String getDestination() { |
| 182 | + return this.destination; |
| 183 | + } |
| 184 | + |
| 185 | + public void setDestination(String destination) { |
| 186 | + this.destination = destination; |
| 187 | + } |
| 188 | + |
| 189 | + public String getContentType() { |
| 190 | + return this.contentType; |
| 191 | + } |
| 192 | + |
| 193 | + public void setContentType(String contentType) { |
| 194 | + this.contentType = contentType; |
| 195 | + } |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments