diff --git a/scm-core/pom.xml b/scm-core/pom.xml index ac86a33f4d..643c9d1705 100644 --- a/scm-core/pom.xml +++ b/scm-core/pom.xml @@ -194,6 +194,19 @@ hibernate-validator + + javax.el + javax.el-api + 3.0.0 + test + + + + org.glassfish + javax.el + 3.0.1-b11 + + diff --git a/scm-core/src/main/java/sonia/scm/web/api/DtoValidator.java b/scm-core/src/main/java/sonia/scm/web/api/DtoValidator.java index 9ac5d3ad16..2b81fe6c62 100644 --- a/scm-core/src/main/java/sonia/scm/web/api/DtoValidator.java +++ b/scm-core/src/main/java/sonia/scm/web/api/DtoValidator.java @@ -31,8 +31,12 @@ import javax.validation.Validator; import javax.validation.ValidatorFactory; import java.util.Set; -public class DtoValidator { - void validate(Object configuration) { +public final class DtoValidator { + + private DtoValidator() { + } + + public static void validate(Object configuration) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set> violations = validator.validate(configuration); diff --git a/scm-core/src/test/java/sonia/scm/web/api/DtoValidatorTest.java b/scm-core/src/test/java/sonia/scm/web/api/DtoValidatorTest.java new file mode 100644 index 0000000000..f3370bd252 --- /dev/null +++ b/scm-core/src/test/java/sonia/scm/web/api/DtoValidatorTest.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package sonia.scm.web.api; + +import org.junit.jupiter.api.Test; + +import javax.validation.ValidationException; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +class DtoValidatorTest { + + @Test + void shouldValidateInvalidBean() { + assertThrows( + ValidationException.class, + () -> DtoValidator.validate(new AnyQuestion(43)) + ); + } + + @Test + void shouldValidateValidBean() { + DtoValidator.validate(new AnyQuestion(42)); + } + + static class AnyQuestion { + @Min(42) + @Max(42) + int answer; + + public AnyQuestion(int answer) { + this.answer = answer; + } + } +} diff --git a/scm-core/src/test/resources/META-INF/validation.xml b/scm-core/src/test/resources/META-INF/validation.xml new file mode 100644 index 0000000000..521ef2b195 --- /dev/null +++ b/scm-core/src/test/resources/META-INF/validation.xml @@ -0,0 +1,34 @@ + + + + org.hibernate.validator.parameternameprovider.ReflectionParameterNameProvider + +