Permalink
Browse files

HV-1541 Adding programmatic definition for ISBN constraint

- Adding new ISBNDef for programmatic constraint definition.
- Adding simple tests to make sure that ISBNDef works.
  • Loading branch information...
marko-bekhta authored and gsmet committed Nov 26, 2017
1 parent 5ba6668 commit 91c4ed35b04c9a6a98ba9038aee514f94618fc6b
@@ -0,0 +1,25 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.cfg.defs;
import org.hibernate.validator.cfg.ConstraintDef;
import org.hibernate.validator.constraints.ISBN;
/**
* @author Marko Bekhta
*/
public class ISBNDef extends ConstraintDef<ISBNDef, ISBN> {
public ISBNDef() {
super( ISBN.class );
}
public ISBNDef type(ISBN.Type type) {
addParameter( "type", type );
return this;
}
}
@@ -6,9 +6,23 @@
*/
package org.hibernate.validator.test.internal.constraintvalidators.hv;
import static java.lang.annotation.ElementType.FIELD;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertNoViolations;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf;
import static org.hibernate.validator.testutils.ValidatorUtil.getConfiguration;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.cfg.defs.ISBNDef;
import org.hibernate.validator.constraints.ISBN;
import org.hibernate.validator.internal.constraintvalidators.hv.ISBNValidator;
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
@@ -103,6 +117,25 @@ public void invalidISBN13() throws Exception {
assertInvalidISBN( "978-0-55555555555555" );
}
@Test
public void testProgrammaticDefinition() throws Exception {
HibernateValidatorConfiguration config = getConfiguration( HibernateValidator.class );
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type( Book.class )
.property( "isbn", FIELD )
.constraint( new ISBNDef().type( ISBN.Type.ISBN13 ) );
config.addMapping( mapping );
Validator validator = config.buildValidatorFactory().getValidator();
Set<ConstraintViolation<Book>> constraintViolations = validator.validate( new Book( "978-0-54560-495-6" ) );
assertNoViolations( constraintViolations );
constraintViolations = validator.validate( new Book( "978-0-54560-495-7" ) );
assertThat( constraintViolations ).containsOnlyViolations(
violationOf( ISBN.class )
);
}
private void assertValidISBN(String isbn) {
assertTrue( validator.isValid( isbn, null ), isbn + " should be a valid ISBN" );
}
@@ -116,4 +149,13 @@ private ISBN initializeAnnotation(ISBN.Type type) {
descriptorBuilder.setAttribute( "type", type );
return descriptorBuilder.build().getAnnotation();
}
private static class Book {
private final String isbn;
private Book(String isbn) {
this.isbn = isbn;
}
}
}

0 comments on commit 91c4ed3

Please sign in to comment.