Search in sources :

Example 6 with Pattern

use of jakarta.validation.constraints.Pattern in project eclipselink by eclipse-ee4j.

the class BeanValidationBindingsTestCase method testFacetCustomizationsAndCustomFacets.

/**
 * Tests customizations, i.e. facet customizations + custom facets, i.e.
 * Future, Past, AssertTrue, AssertFalse.
 */
public void testFacetCustomizationsAndCustomFacets() throws Throwable {
    pkg = "cs";
    xjcGenerateJavaSourcesWithCustomizations(CUSTOMIZED_SCHEMA_PATH);
    compileGeneratedSources(createCompileList(pkg));
    Class<?> custom = loadCompiledClasses(createLoadList(pkg))[0];
    Field generic = custom.getDeclaredField("generic");
    Size s = generic.getAnnotation(Size.class);
    assertEquals(s.message(), "Hello, world!");
    assertEquals(s.groups()[0], BindingTeam.class);
    assertEquals(s.groups()[1], RocketTeam.class);
    assertEquals(s.max(), 4);
    Pattern p1 = generic.getAnnotation(Pattern.List.class).value()[0];
    Pattern p2 = generic.getAnnotation(Pattern.List.class).value()[1];
    assertEquals(p1.message(), p2.message());
    assertTrue(Arrays.equals(p1.groups(), p2.groups()));
    assertEquals(p1.message(), "Hello.");
    assertEquals(p1.groups()[0], Object.class);
    assertEquals(p1.regexp(), "10");
    Future f = generic.getAnnotation(Future.class);
    assertEquals(f.message(), "Welcome to the Future!");
    assertEquals(f.groups()[0], BindingTeam.class);
    Past p = generic.getAnnotation(Past.class);
    assertEquals(p.message(), "Farewell from the ancestors.");
    assertEquals(p.groups()[0], Ancestors.class);
    AssertTrue at = generic.getAnnotation(AssertTrue.class);
    assertEquals(at.message(), "True fan of the team!");
    assertEquals(at.groups()[0], BindingTeam.class);
    AssertFalse af = generic.getAnnotation(AssertFalse.class);
    assertEquals(af.message(), "false");
    assertEquals(af.groups()[0], Object.class);
    // To test a user custom annotation.
    CustomAnnotation xmlKey = generic.getAnnotation(CustomAnnotation.class);
    assertNotNull(xmlKey);
}
Also used : Field(java.lang.reflect.Field) Pattern(jakarta.validation.constraints.Pattern) Size(jakarta.validation.constraints.Size) Past(jakarta.validation.constraints.Past) AssertTrue(jakarta.validation.constraints.AssertTrue) Future(jakarta.validation.constraints.Future) AssertFalse(jakarta.validation.constraints.AssertFalse)

Example 7 with Pattern

use of jakarta.validation.constraints.Pattern in project eclipselink by eclipse-ee4j.

the class BeanValidationBindingsTestCase method testAllFacetsAndAnnotations.

/**
 * Tests that the XJC detects all facets and generates their respective
 * annotations correctly.
 */
public void testAllFacetsAndAnnotations() throws Throwable {
    pkg = "rs";
    Class<?>[] c = roundTrip(RICH_SCHEMA_PATH, pkg);
    Class<?> Main = c[0];
    Class<?> Numbers = c[1];
    Class<?> NumberWithHiddenValueAttribute = c[2];
    Class<?> Strings = c[4];
    XmlElement xmlElement;
    Size size;
    DecimalMax decimalMax;
    DecimalMin decimalMin;
    Pattern.List patternList;
    Pattern pattern;
    /* Main.class */
    Field numbers = Main.getDeclaredField("numbers");
    assertNotNull(numbers.getAnnotation(Valid.class));
    assertNotNull(numbers.getAnnotation(NotNull.class));
    xmlElement = numbers.getAnnotation(XmlElement.class);
    assertFalse(xmlElement.nillable());
    Field strings = Main.getDeclaredField("strings");
    size = strings.getAnnotation(Size.class);
    assertTrue(size.min() == 1 && size.max() == 2);
    assertNotNull(strings.getAnnotation(Valid.class));
    assertNotNull(strings.getAnnotation(NotNull.class));
    xmlElement = strings.getAnnotation(XmlElement.class);
    assertFalse(xmlElement.nillable());
    Field unsignedByte = Main.getDeclaredField("unsignedByte");
    decimalMax = unsignedByte.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "255");
    assertTrue(decimalMax.inclusive());
    decimalMin = unsignedByte.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "0");
    assertTrue(decimalMin.inclusive());
    Field byteArray = Main.getDeclaredField("byteArray");
    size = byteArray.getAnnotation(Size.class);
    assertTrue(size.max() == 18);
    Field someCollection = Main.getDeclaredField("someCollection");
    size = someCollection.getAnnotation(Size.class);
    assertTrue(size.min() == 1);
    assertNotNull(someCollection.getAnnotation(Valid.class));
    assertNotNull(someCollection.getAnnotation(NotNull.class));
    Field optionalElement = Main.getDeclaredField("optionalElement");
    size = optionalElement.getAnnotation(Size.class);
    assertTrue(size.min() == 0);
    assertNotNull(optionalElement.getAnnotation(Valid.class));
    assertNull(optionalElement.getAnnotation(NotNull.class));
    Field optionalElementWithSimpleType = Main.getDeclaredField("optionalElementWithSimpleType");
    assertNull(optionalElementWithSimpleType.getAnnotation(NotNull.class));
    decimalMin = optionalElementWithSimpleType.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "1");
    decimalMax = optionalElementWithSimpleType.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "31");
    /* Numbers.class */
    Field minInclusive = Numbers.getDeclaredField("minInclusive");
    decimalMin = minInclusive.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "1000");
    assertTrue(decimalMin.inclusive());
    Field maxInclusive = Numbers.getDeclaredField("maxInclusive");
    decimalMax = maxInclusive.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "1000");
    assertTrue(decimalMax.inclusive());
    Field minExclusive = Numbers.getDeclaredField("minExclusive");
    decimalMin = minExclusive.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "0");
    assertFalse(decimalMin.inclusive());
    Field maxExclusive = Numbers.getDeclaredField("maxExclusive");
    decimalMax = maxExclusive.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "1000");
    assertFalse(decimalMax.inclusive());
    Field minMaxExclusive = Numbers.getDeclaredField("minMaxExclusive");
    decimalMax = minMaxExclusive.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "9223372");
    assertFalse(decimalMax.inclusive());
    decimalMin = minMaxExclusive.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "0");
    assertFalse(decimalMin.inclusive());
    /* NumberWithHiddenValueAttribute.class */
    Field value = NumberWithHiddenValueAttribute.getDeclaredField("value");
    assertNotNull(value.getAnnotation(XmlValue.class));
    size = value.getAnnotation(Size.class);
    assertTrue(size.min() == 1 && size.max() == 5);
    Field code = NumberWithHiddenValueAttribute.getDeclaredField("code");
    assertNotNull(code.getAnnotation(NotNull.class));
    Field whatNumber = NumberWithHiddenValueAttribute.getDeclaredField("whatNumber");
    XmlAttribute xmlAttribute = whatNumber.getAnnotation(XmlAttribute.class);
    assertTrue(xmlAttribute.required());
    /* Strings.class */
    Field regexShorthands = Strings.getDeclaredField("regexShorthands");
    patternList = regexShorthands.getAnnotation(Pattern.List.class);
    Pattern[] patterns = patternList.value();
    assertEquals("[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[0].regexp());
    assertEquals("[^:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[1].regexp());
    assertEquals("[-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[2].regexp());
    assertEquals("[^-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[3].regexp());
    assertEquals("\\p{Nd}", patterns[4].regexp());
    assertEquals("\\P{Nd}", patterns[5].regexp());
    assertEquals("[\\u0009-\\u000D\\u0020\\u0085\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]", patterns[6].regexp());
    assertEquals("[^\\u0009-\\u000D\\u0020\\u0085\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]", patterns[7].regexp());
    assertEquals("[\\u0009\\u0020\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u202F\\u205F\\u3000]", patterns[8].regexp());
    assertEquals("[^\\u0009\\u0020\\u00A0\\u1680\\u180E\\u2000\\u2001-\\u200A\\u202F\\u205F\\u3000]", patterns[9].regexp());
    assertEquals("[^\\u000A-\\u000D\\u0085\\u2028\\u2029]", patterns[10].regexp());
    assertEquals("(?:(?>\\u000D\\u000A)|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029])", patterns[11].regexp());
    Field idType = Strings.getDeclaredField("idType");
    pattern = idType.getAnnotation(Pattern.class);
    assertEquals(pattern.regexp(), "[[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]-[:]][[-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]-[:]]*");
    assertTrue(idType.getAnnotation(Size.class).max() == 100);
    Field genericString = Strings.getDeclaredField("genericString");
    assertTrue(genericString.getAnnotation(Size.class).min() == 0);
    assertTrue(genericString.getAnnotation(Size.class).max() == 1024);
    Field maxLength = Strings.getDeclaredField("maxLength");
    assertTrue(maxLength.getAnnotation(Size.class).max() == 1024);
    Field minLength = Strings.getDeclaredField("minLength");
    assertTrue(minLength.getAnnotation(Size.class).min() == 0);
}
Also used : Pattern(jakarta.validation.constraints.Pattern) XmlAttribute(jakarta.xml.bind.annotation.XmlAttribute) Size(jakarta.validation.constraints.Size) DecimalMin(jakarta.validation.constraints.DecimalMin) NotNull(jakarta.validation.constraints.NotNull) Field(java.lang.reflect.Field) Valid(jakarta.validation.Valid) XmlElement(jakarta.xml.bind.annotation.XmlElement) XmlValue(jakarta.xml.bind.annotation.XmlValue) ArrayList(java.util.ArrayList) List(java.util.List) DecimalMax(jakarta.validation.constraints.DecimalMax)

Example 8 with Pattern

use of jakarta.validation.constraints.Pattern 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)

Example 9 with Pattern

use of jakarta.validation.constraints.Pattern in project hibernate-validator by hibernate.

the class AnnotationFactoryTest method createAnnotationProxyWithRequiredParameter.

@Test
public void createAnnotationProxyWithRequiredParameter() {
    AnnotationDescriptor.Builder<Pattern> descriptorBuilder = new AnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", ".*");
    Pattern pattern = descriptorBuilder.build().getAnnotation();
    assertEquals(".*", pattern.regexp(), "Wrong parameter value");
}
Also used : AnnotationDescriptor(org.hibernate.validator.internal.util.annotation.AnnotationDescriptor) Pattern(jakarta.validation.constraints.Pattern) Test(org.testng.annotations.Test)

Example 10 with Pattern

use of jakarta.validation.constraints.Pattern in project hibernate-validator by hibernate.

the class ConstraintHelperTest method testGetConstraintsFromMultiValueConstraint.

@Test
public void testGetConstraintsFromMultiValueConstraint() throws Exception {
    Engine engine = new Engine();
    Field field = engine.getClass().getDeclaredField("serialNumber");
    Annotation annotation = field.getAnnotation(Pattern.List.class);
    assertNotNull(annotation);
    List<Annotation> multiValueConstraintAnnotations = constraintHelper.getConstraintsFromMultiValueConstraint(annotation);
    assertTrue(multiValueConstraintAnnotations.size() == 2, "There should be two constraint annotations");
    assertTrue(multiValueConstraintAnnotations.get(0) instanceof Pattern, "Wrong constraint annotation");
    assertEquals(((Pattern) multiValueConstraintAnnotations.get(0)).regexp(), "^[A-Z0-9-]+$");
    assertTrue(multiValueConstraintAnnotations.get(1) instanceof Pattern, "Wrong constraint annotation");
    assertEquals(((Pattern) multiValueConstraintAnnotations.get(1)).regexp(), "^....-....-....$");
}
Also used : Field(java.lang.reflect.Field) Pattern(jakarta.validation.constraints.Pattern) Engine(org.hibernate.validator.test.internal.metadata.Engine) Annotation(java.lang.annotation.Annotation) Test(org.testng.annotations.Test)

Aggregations

Pattern (jakarta.validation.constraints.Pattern)11 Test (org.testng.annotations.Test)6 Size (jakarta.validation.constraints.Size)4 PatternValidator (org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator)4 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)4 Field (java.lang.reflect.Field)3 AssertFalse (jakarta.validation.constraints.AssertFalse)2 AssertTrue (jakarta.validation.constraints.AssertTrue)2 DecimalMax (jakarta.validation.constraints.DecimalMax)2 DecimalMin (jakarta.validation.constraints.DecimalMin)2 Max (jakarta.validation.constraints.Max)2 Min (jakarta.validation.constraints.Min)2 NotNull (jakarta.validation.constraints.NotNull)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Constraint (jakarta.validation.Constraint)1 Valid (jakarta.validation.Valid)1 ValidationException (jakarta.validation.ValidationException)1 Digits (jakarta.validation.constraints.Digits)1 Email (jakarta.validation.constraints.Email)1