/*
* 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 21 下载量 182 浏览量
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留言
资源推荐
资源详情
资源评论
收起资源包目录
spring-boot-2.2.6.RELEASE.zip (6249个子文件)
_spring 454B
spring-boot-features.adoc 355KB
howto.adoc 140KB
production-ready-features.adoc 82KB
using-spring-boot.adoc 49KB
deployment.adoc 36KB
getting-started.adoc 29KB
appendix-configuration-metadata.adoc 29KB
spring-boot-cli.adoc 15KB
build-tool-plugins.adoc 13KB
appendix-executable-jar-format.adoc 11KB
README.adoc 11KB
CONTRIBUTING.adoc 10KB
packaging.adoc 10KB
README.adoc 8KB
attributes.adoc 8KB
documentation-overview.adoc 7KB
managing-dependencies.adoc 5KB
running.adoc 4KB
reacting.adoc 4KB
loggers.adoc 4KB
getting-started.adoc 4KB
integrating-with-actuator.adoc 3KB
health.adoc 3KB
caches.adoc 3KB
index.adoc 2KB
mappings.adoc 2KB
index.adoc 2KB
CODE_OF_CONDUCT.adoc 2KB
metrics.adoc 2KB
appendix-application-properties.adoc 2KB
sessions.adoc 2KB
publishing.adoc 2KB
README.adoc 2KB
README.adoc 2KB
env.adoc 1KB
integrationgraph.adoc 1KB
auditevents.adoc 1KB
SUPPORT.adoc 1KB
threaddump.adoc 1KB
info.adoc 1KB
appendix-auto-configuration-classes.adoc 1KB
logfile.adoc 985B
configprops.adoc 839B
conditions.adoc 797B
scheduledtasks.adoc 796B
liquibase.adoc 737B
httptrace.adoc 732B
flyway.adoc 707B
shutdown.adoc 706B
heapdump.adoc 698B
beans.adoc 659B
appendix-dependency-versions.adoc 638B
appendix-test-auto-configuration.adoc 556B
README.adoc 529B
prometheus.adoc 504B
README.adoc 440B
legal.adoc 325B
README.adoc 220B
index.apt 2KB
custom-layout.apt 2KB
gradlew.bat 2KB
spring.bat 2KB
build-project-windows.bat 222B
javax.cache.spi.CachingProvider 127B
certificate-location 1KB
mvnw.cmd 6KB
mvnw.cmd 6KB
org.springframework.boot.cli.command.CommandFactory 51B
org.springframework.boot.cli.CommandFactory 33B
commit-msg 29B
org.springframework.boot.cli.compiler.CompilerAutoConfiguration 1KB
jvm.config 9B
setup.cql 201B
bootstrap.min.css 141KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
bootstrap.min.css 81KB
spring-javadoc.css 13KB
spring-javadoc.css 13KB
style.css 333B
application.css 110B
test.css 5B
custom.css 2B
application.custom 0B
Dockerfile 519B
Dockerfile 513B
Dockerfile 513B
Dockerfile 402B
Dockerfile 281B
Dockerfile 281B
.editorconfig 100B
excluded 1B
spring.factories 12KB
共 6249 条
- 1
- 2
- 3
- 4
- 5
- 6
- 63
资源评论
Z小繁
- 粉丝: 5349
- 资源: 64
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功