|
|
|
@ -21,9 +21,13 @@ import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
|
|
|
import javax.validation.constraints.NotNull; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.hamcrest.Matchers; |
|
|
|
import org.junit.After; |
|
|
|
import org.junit.After; |
|
|
|
|
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.boot.TestUtils; |
|
|
|
import org.springframework.boot.TestUtils; |
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
|
|
@ -31,6 +35,7 @@ import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.context.annotation.ImportResource; |
|
|
|
import org.springframework.context.annotation.ImportResource; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import org.springframework.validation.BindException; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
@ -44,6 +49,9 @@ public class EnableConfigurationPropertiesTests { |
|
|
|
|
|
|
|
|
|
|
|
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
|
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Rule |
|
|
|
|
|
|
|
public ExpectedException expected = ExpectedException.none(); |
|
|
|
|
|
|
|
|
|
|
|
@After |
|
|
|
@After |
|
|
|
public void close() { |
|
|
|
public void close() { |
|
|
|
System.clearProperty("name"); |
|
|
|
System.clearProperty("name"); |
|
|
|
@ -111,6 +119,26 @@ public class EnableConfigurationPropertiesTests { |
|
|
|
assertEquals("foo", this.context.getBean(TestProperties.class).name); |
|
|
|
assertEquals("foo", this.context.getBean(TestProperties.class).name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testExceptionOnValidation() { |
|
|
|
|
|
|
|
this.context.register(ExceptionIfInvalidTestConfiguration.class); |
|
|
|
|
|
|
|
TestUtils.addEnviroment(this.context, "name:foo"); |
|
|
|
|
|
|
|
this.expected.expectCause(Matchers.<Throwable> instanceOf(BindException.class)); |
|
|
|
|
|
|
|
this.context.refresh(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testNoExceptionOnValidation() { |
|
|
|
|
|
|
|
this.context.register(NoExceptionIfInvalidTestConfiguration.class); |
|
|
|
|
|
|
|
TestUtils.addEnviroment(this.context, "name:foo"); |
|
|
|
|
|
|
|
this.context.refresh(); |
|
|
|
|
|
|
|
assertEquals( |
|
|
|
|
|
|
|
1, |
|
|
|
|
|
|
|
this.context |
|
|
|
|
|
|
|
.getBeanNamesForType(NoExceptionIfInvalidTestProperties.class).length); |
|
|
|
|
|
|
|
assertEquals("foo", this.context.getBean(TestProperties.class).name); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNestedPropertiesBinding() { |
|
|
|
public void testNestedPropertiesBinding() { |
|
|
|
this.context.register(NestedConfiguration.class); |
|
|
|
this.context.register(NestedConfiguration.class); |
|
|
|
@ -264,6 +292,16 @@ public class EnableConfigurationPropertiesTests { |
|
|
|
protected static class IgnoreNestedTestConfiguration { |
|
|
|
protected static class IgnoreNestedTestConfiguration { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@EnableConfigurationProperties(ExceptionIfInvalidTestProperties.class) |
|
|
|
|
|
|
|
protected static class ExceptionIfInvalidTestConfiguration { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@EnableConfigurationProperties(NoExceptionIfInvalidTestProperties.class) |
|
|
|
|
|
|
|
protected static class NoExceptionIfInvalidTestConfiguration { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
@EnableConfigurationProperties(DerivedProperties.class) |
|
|
|
@EnableConfigurationProperties(DerivedProperties.class) |
|
|
|
protected static class DerivedConfiguration { |
|
|
|
protected static class DerivedConfiguration { |
|
|
|
@ -379,6 +417,38 @@ public class EnableConfigurationPropertiesTests { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ConfigurationProperties |
|
|
|
|
|
|
|
protected static class ExceptionIfInvalidTestProperties extends TestProperties { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@NotNull |
|
|
|
|
|
|
|
private String description; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getDescription() { |
|
|
|
|
|
|
|
return this.description; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setDescription(String description) { |
|
|
|
|
|
|
|
this.description = description; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ConfigurationProperties(exceptionIfInvalid = false) |
|
|
|
|
|
|
|
protected static class NoExceptionIfInvalidTestProperties extends TestProperties { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@NotNull |
|
|
|
|
|
|
|
private String description; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getDescription() { |
|
|
|
|
|
|
|
return this.description; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setDescription(String description) { |
|
|
|
|
|
|
|
this.description = description; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected static class MoreProperties { |
|
|
|
protected static class MoreProperties { |
|
|
|
private String name; |
|
|
|
private String name; |
|
|
|
|
|
|
|
|
|
|
|
|