Search in sources :

Example 1 with PatternValidator

use of org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator in project hibernate-validator by hibernate.

the class PatternValidatorTest method testIsValidForEmptyStringRegexp.

@Test
public void testIsValidForEmptyStringRegexp() {
    ConstraintAnnotationDescriptor.Builder<Pattern> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", "|^.*foo$");
    descriptorBuilder.setMessage("pattern does not match");
    Pattern p = descriptorBuilder.build().getAnnotation();
    PatternValidator constraint = new PatternValidator();
    constraint.initialize(p);
    assertTrue(constraint.isValid(null, null));
    assertTrue(constraint.isValid("", null));
    assertFalse(constraint.isValid("bla bla", null));
    assertTrue(constraint.isValid("foo", null));
    assertTrue(constraint.isValid("a b c foo", null));
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) PatternValidator(org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator) Test(org.testng.annotations.Test)

Example 2 with PatternValidator

use of org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator in project hibernate-validator by hibernate.

the class PatternValidatorTest method testIsValidForCharSequence.

@Test
@TestForIssue(jiraKey = "HV-502")
public void testIsValidForCharSequence() {
    ConstraintAnnotationDescriptor.Builder<Pattern> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", "char sequence");
    Pattern p = descriptorBuilder.build().getAnnotation();
    PatternValidator constraint = new PatternValidator();
    constraint.initialize(p);
    assertTrue(constraint.isValid(new MyCustomStringImpl("char sequence"), null));
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) PatternValidator(org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator) MyCustomStringImpl(org.hibernate.validator.testutil.MyCustomStringImpl) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with PatternValidator

use of org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator in project hibernate-validator by hibernate.

the class PatternValidatorTest method testIsValid.

@Test
public void testIsValid() {
    ConstraintAnnotationDescriptor.Builder<Pattern> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", "foobar");
    descriptorBuilder.setMessage("pattern does not match");
    Pattern p = descriptorBuilder.build().getAnnotation();
    PatternValidator constraint = new PatternValidator();
    constraint.initialize(p);
    assertTrue(constraint.isValid(null, null));
    assertFalse(constraint.isValid("", null));
    assertFalse(constraint.isValid("bla bla", null));
    assertFalse(constraint.isValid("This test is not foobar", null));
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) PatternValidator(org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator) Test(org.testng.annotations.Test)

Example 4 with PatternValidator

use of org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator in project hibernate-validator by hibernate.

the class PatternValidatorTest method testInvalidRegularExpression.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testInvalidRegularExpression() {
    ConstraintAnnotationDescriptor.Builder<Pattern> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", "(unbalanced parentheses");
    descriptorBuilder.setMessage("pattern does not match");
    Pattern p = descriptorBuilder.build().getAnnotation();
    PatternValidator constraint = new PatternValidator();
    constraint.initialize(p);
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) PatternValidator(org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator) Test(org.testng.annotations.Test)

Aggregations

Pattern (jakarta.validation.constraints.Pattern)4 PatternValidator (org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator)4 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)4 Test (org.testng.annotations.Test)4 MyCustomStringImpl (org.hibernate.validator.testutil.MyCustomStringImpl)1 TestForIssue (org.hibernate.validator.testutil.TestForIssue)1