This commit is contained in:
Phillip Webb
2015-08-03 11:03:32 -07:00
parent 5c1f700c3a
commit 891dd5a0f6
25 changed files with 139 additions and 129 deletions

View File

@@ -34,7 +34,7 @@ public class SampleProperties {
private Integer port = 8080;
public String getHost() {
return host;
return this.host;
}
public void setHost(String host) {
@@ -42,10 +42,11 @@ public class SampleProperties {
}
public Integer getPort() {
return port;
return this.port;
}
public void setPort(Integer port) {
this.port = port;
}
}

View File

@@ -35,10 +35,9 @@ public class SamplePropertiesValidator implements Validator {
public void validate(Object o, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "host", "host.empty");
ValidationUtils.rejectIfEmpty(errors, "port", "port.empty");
SampleProperties properties = (SampleProperties) o;
if (properties.getHost() != null &&
!pattern.matcher(properties.getHost()).matches()) {
if (properties.getHost() != null
&& !this.pattern.matcher(properties.getHost()).matches()) {
errors.rejectValue("host", "Invalid host");
}
}

View File

@@ -52,8 +52,8 @@ public class SamplePropertyValidationApplication {
}
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(SamplePropertyValidationApplication.class)
.profiles("app").run(args);
new SpringApplicationBuilder(SamplePropertyValidationApplication.class).profiles(
"app").run(args);
}
}

View File

@@ -20,7 +20,6 @@ import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.EnvironmentTestUtils;
@@ -43,16 +42,15 @@ public class SamplePropertyValidationApplicationTests {
@After
public void closeContext() {
context.close();
this.context.close();
}
@Test
public void bindValidProperties() {
this.context.register(SamplePropertyValidationApplication.class);
EnvironmentTestUtils.addEnvironment(this.context,
"sample.host:192.168.0.1", "sample.port:9090");
EnvironmentTestUtils.addEnvironment(this.context, "sample.host:192.168.0.1",
"sample.port:9090");
this.context.refresh();
SampleProperties properties = this.context.getBean(SampleProperties.class);
assertEquals("192.168.0.1", properties.getHost());
assertEquals(Integer.valueOf(9090), properties.getPort());
@@ -61,32 +59,29 @@ public class SamplePropertyValidationApplicationTests {
@Test
public void bindInvalidHost() {
this.context.register(SamplePropertyValidationApplication.class);
EnvironmentTestUtils.addEnvironment(this.context,
"sample.host:xxxxxx", "sample.port:9090");
thrown.expect(BeanCreationException.class);
thrown.expectMessage("xxxxxx");
EnvironmentTestUtils.addEnvironment(this.context, "sample.host:xxxxxx",
"sample.port:9090");
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("xxxxxx");
this.context.refresh();
}
@Test
public void bindNullHost() {
this.context.register(SamplePropertyValidationApplication.class);
thrown.expect(BeanCreationException.class);
thrown.expectMessage("null");
thrown.expectMessage("host");
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("null");
this.thrown.expectMessage("host");
this.context.refresh();
}
@Test
public void validatorOnlyCalledOnSupportedClass() {
this.context.register(SamplePropertyValidationApplication.class);
this.context.register(ServerProperties.class); // our validator will not apply here
EnvironmentTestUtils.addEnvironment(this.context,
"sample.host:192.168.0.1", "sample.port:9090");
this.context.register(ServerProperties.class); // our validator will not apply
EnvironmentTestUtils.addEnvironment(this.context, "sample.host:192.168.0.1",
"sample.port:9090");
this.context.refresh();
SampleProperties properties = this.context.getBean(SampleProperties.class);
assertEquals("192.168.0.1", properties.getHost());
assertEquals(Integer.valueOf(9090), properties.getPort());