/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.properties;
import java.beans.PropertyEditorSupport;
import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.context.properties.bind.BindException;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.stereotype.Component;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link ConfigurationProperties @ConfigurationProperties}-annotated beans.
* Covers {@link EnableConfigurationProperties @EnableConfigurationProperties},
* {@link ConfigurationPropertiesBindingPostProcessorRegistrar},
* {@link ConfigurationPropertiesBindingPostProcessor} and
* {@link ConfigurationPropertiesBinder}.
*
* @author Dave Syer
* @author Christian Dupuis
* @author Phillip Webb
* @author Stephane Nicoll
* @author Madhura Bhave
*/
@ExtendWith(OutputCaptureExtension.class)
class ConfigurationPropertiesTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@AfterEach
void cleanup() {
this.context.close();
System.clearProperty("name");
System.clearProperty("nested.name");
System.clearProperty("nested_name");
}
@Test
void loadShouldBind() {
load(BasicConfiguration.class, "name=foo");
assertThat(this.context.getBeanNamesForType(BasicProperties.class)).hasSize(1);
assertThat(this.context.containsBean(BasicProperties.class.getName())).isTrue();
assertThat(this.context.getBean(BasicProperties.class).name).isEqualTo("foo");
}
@Test
void loadShouldBindNested() {
load(NestedConfiguration.class, "name=foo", "nested.name=bar");
assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1);
assertThat(this.context.getBean(NestedProperties.class).name).isEqualTo("foo");
assertThat(this.context.getBean(NestedProperties.class).nested.name).isEqualTo("bar");
}
@Test
void loadWhenUsingSystemPropertiesShouldBind() {
System.setProperty("name", "foo");
load(BasicConfiguration.class);
assertThat(this.context.getBeanNamesForType(BasicProperties.class)).hasSize(1);
assertThat(this.context.getBean(BasicProperties.class).name).isEqualTo("foo");
}
@Test
void loadWhenUsingSystemPropertiesShouldBindNested() {
System.setProperty("name", "foo");
System.setProperty("nested.name", "bar");
load(NestedConfiguration.class);
assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1);
assertThat(this.context.getBean(NestedProperties.class).name).isEqualTo("foo");
assertThat(this.context.getBean(NestedProperties.class).nested.name).isEqualTo("bar");
}
@Test
void loadWhenHasIgnoreUnknownFieldsFalseAndNoUnknownFieldsShouldBind() {
removeSystemProperties();
load(IgnoreUnknownFieldsFalseConfiguration.class, "name=foo");
IgnoreUnknownFieldsFalseProperties bean = this.context.getBean(IgnoreUnknownFieldsFalseProperties.class);
assertThat(((BasicProperties) bean).name).isEqualTo("foo");
}
@Test
void loadWhenHasIgnoreUnknownFieldsFalseAndUnknownFieldsShouldFail() {
removeSystemProperties();
assertThatExceptionOfType(ConfigurationPropertiesBindException.class)
.isThrownBy(() -> load(IgnoreUnknownFieldsFalseConfiguration.class, "name=foo", "bar=baz"))
.withCauseInstanceOf(BindException.class);
}
@Test
void loadWhenHasIgnoreInvalidFieldsTrueAndInvalidFieldsShouldBind() {
load(IgnoreInvalidFieldsFalseProperties.class, "com.example.bar=spam");
IgnoreInvalidFieldsFalseProperties bean = this.context.getBean(IgnoreInvalidFieldsFalseProperties.class);
assertThat(bean.getBar()).isEqualTo(0);
}
@Test
void loadWhenHasPrefixShouldBind() {
load(PrefixConfiguration.class, "spring.foo.name=foo");
PrefixProperties bean = this.context.getBean(PrefixProperties.class);
没有合适的资源?快使用搜索试试~ 我知道了~
spring-boot-2.2.6.RELEASE.zip

共6249个文件
java:4929个
xml:345个
properties:172个

需积分: 50 20 下载量 171 浏览量
2020-04-04
14:06:46
上传
评论
收藏 10.99MB ZIP 举报
温馨提示
spring-boot最新正式版源码spring-boot-2.2.6.RELEASE.zip,github太慢特上传此处,方便大家学习使用。其他资源亦可从我的资源页获取https://download.csdn.net/user/u010887744/uploads,如果没找到你想要的可以前往公众号zxiaofan留言
资源推荐
资源详情
资源评论














收起资源包目录





































































































共 6249 条
- 1
- 2
- 3
- 4
- 5
- 6
- 63
资源评论


Z小繁
- 粉丝: 5210
- 资源: 64
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
