Test for placeholder-specified default value within @Value

Issue: SPR-14025
This commit is contained in:
Juergen Hoeller 2016-03-11 15:00:01 +01:00
parent 0b1639d963
commit b4de66ff9a
1 changed files with 50 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ import static org.junit.Assert.*;
* and @Value fields in the same configuration class are mutually exclusive. * and @Value fields in the same configuration class are mutually exclusive.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
*/ */
public class ConfigurationClassWithPlaceholderConfigurerBeanTests { public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
@ -49,7 +50,7 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
* cannot be declared in the same configuration class that has a @Value * cannot be declared in the same configuration class that has a @Value
* field in need of placeholder replacement. It's an obvious chicken-and-egg issue. * field in need of placeholder replacement. It's an obvious chicken-and-egg issue.
* The solution is to do as {@link #valueFieldsAreProcessedWhenPlaceholderConfigurerIsSegregated()} * The solution is to do as {@link #valueFieldsAreProcessedWhenPlaceholderConfigurerIsSegregated()}
* does and segragate the two bean definitions across configuration classes. * does and segregate the two bean definitions across configuration classes.
*/ */
@Ignore @Test @Ignore @Test
public void valueFieldsAreNotProcessedWhenPlaceholderConfigurerIsIntegrated() { public void valueFieldsAreNotProcessedWhenPlaceholderConfigurerIsIntegrated() {
@ -75,32 +76,44 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
TestBean testBean = ctx.getBean(TestBean.class); TestBean testBean = ctx.getBean(TestBean.class);
assertThat(testBean.getName(), equalTo("foo")); assertThat(testBean.getName(), equalTo("foo"));
} }
}
@Configuration @Test
class ConfigWithValueField { public void valueFieldsResolveToPlaceholderSpecifiedDefaultValue() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithValueField.class);
ctx.register(ConfigWithPlaceholderConfigurer.class);
ctx.refresh();
@Value("${test.name}") TestBean testBean = ctx.getBean(TestBean.class);
assertThat(testBean.getName(), equalTo("bar"));
}
@Configuration
static class ConfigWithValueField {
@Value("${test.name:bar}")
private String name; private String name;
@Bean @Bean
public ITestBean testBean() { public ITestBean testBean() {
return new TestBean(this.name); return new TestBean(this.name);
} }
} }
@Configuration
class ConfigWithPlaceholderConfigurer { @Configuration
static class ConfigWithPlaceholderConfigurer {
@Bean @Bean
public PropertySourcesPlaceholderConfigurer ppc() { public PropertySourcesPlaceholderConfigurer ppc() {
return new PropertySourcesPlaceholderConfigurer(); return new PropertySourcesPlaceholderConfigurer();
} }
}
}
@Configuration @Configuration
class ConfigWithValueFieldAndPlaceholderConfigurer { static class ConfigWithValueFieldAndPlaceholderConfigurer {
@Value("${test.name}") @Value("${test.name}")
private String name; private String name;
@ -114,5 +127,6 @@ class ConfigWithValueFieldAndPlaceholderConfigurer {
public PropertySourcesPlaceholderConfigurer ppc() { public PropertySourcesPlaceholderConfigurer ppc() {
return new PropertySourcesPlaceholderConfigurer(); return new PropertySourcesPlaceholderConfigurer();
} }
}
} }