diff --git a/spring-bootstrap-service/pom.xml b/spring-bootstrap-service/pom.xml index dfb133bb5a3..b2919042914 100644 --- a/spring-bootstrap-service/pom.xml +++ b/spring-bootstrap-service/pom.xml @@ -57,13 +57,7 @@ org.slf4j - slf4j-log4j12 - true - test - - - log4j - log4j + slf4j-jdk14 true test diff --git a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/EndpointsProperties.java b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/EndpointsProperties.java index 2eab907dab2..ff11ad0a630 100644 --- a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/EndpointsProperties.java +++ b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/EndpointsProperties.java @@ -15,7 +15,9 @@ */ package org.springframework.bootstrap.service.properties; +import javax.validation.Valid; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; import org.springframework.bootstrap.context.annotation.ConfigurationProperties; @@ -28,14 +30,19 @@ import org.springframework.bootstrap.context.annotation.ConfigurationProperties; @ConfigurationProperties(name = "endpoints", ignoreUnknownFields = false) public class EndpointsProperties { + @Valid private Endpoint varz = new Endpoint("/varz"); + @Valid private Endpoint healthz = new Endpoint("/healthz"); + @Valid private Endpoint error = new Endpoint("/error"); + @Valid private Endpoint shutdown = new Endpoint("/shutdown"); + @Valid private Endpoint trace = new Endpoint("/trace"); public Endpoint getVarz() { @@ -61,6 +68,7 @@ public class EndpointsProperties { public static class Endpoint { @NotNull + @Pattern(regexp = "/[^/]*", message = "Path must start with /") private String path; public Endpoint() { diff --git a/spring-bootstrap-service/src/test/java/org/springframework/bootstrap/service/properties/EndpointsPropertiesTests.java b/spring-bootstrap-service/src/test/java/org/springframework/bootstrap/service/properties/EndpointsPropertiesTests.java new file mode 100644 index 00000000000..914c103be23 --- /dev/null +++ b/spring-bootstrap-service/src/test/java/org/springframework/bootstrap/service/properties/EndpointsPropertiesTests.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012-2013 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 + * + * http://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.bootstrap.service.properties; + +import org.junit.Test; +import org.springframework.validation.BindException; +import org.springframework.validation.Errors; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Externalized configuration for endpoints (e.g. paths) + * + * @author Dave Syer + * + */ +public class EndpointsPropertiesTests { + + private EndpointsProperties properties = new EndpointsProperties(); + + @Test + public void testDefaultPathValid() throws Exception { + assertEquals("/error", this.properties.getError().getPath()); + Errors errors = validate(this.properties); + assertFalse(errors.hasErrors()); + } + + @Test + public void testQueryPathValid() throws Exception { + Errors errors = validate(new EndpointsProperties.Endpoint("/foo?bar")); + assertFalse(errors.hasErrors()); + } + + @Test + public void testEmptyPathInvalid() throws Exception { + Errors errors = validate(new EndpointsProperties.Endpoint("")); + assertTrue(errors.hasErrors()); + } + + @Test + public void testDoubleSlashInvalid() throws Exception { + Errors errors = validate(new EndpointsProperties.Endpoint("//foo")); + assertTrue(errors.hasErrors()); + } + + @Test + public void testEmptyPathInProperties() throws Exception { + this.properties.getError().setPath(""); + Errors errors = validate(this.properties); + assertTrue(errors.hasErrors()); + } + + /** + * @return + */ + private Errors validate(Object target) { + BindException errors = new BindException(target, "properties"); + LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); + validator.afterPropertiesSet(); + validator.validate(target, errors); + return errors; + } + +}