Search in sources :

Example 6 with DecimalMax

use of jakarta.validation.constraints.DecimalMax 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 7 with DecimalMax

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

the class MaxValidatorForNumberTest method testIsValidDecimalMaxExclusive.

@Test
@TestForIssue(jiraKey = "HV-256")
public void testIsValidDecimalMaxExclusive() {
    boolean inclusive = false;
    ConstraintAnnotationDescriptor.Builder<DecimalMax> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMax.class);
    descriptorBuilder.setAttribute("value", "15.0E0");
    descriptorBuilder.setAttribute("inclusive", inclusive);
    descriptorBuilder.setMessage("{validator.max}");
    DecimalMax m = descriptorBuilder.build().getAnnotation();
    testDecimalMax(m, false);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMax(jakarta.validation.constraints.DecimalMax) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 8 with DecimalMax

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

the class MaxValidatorForNumberTest method testIsValidDecimalMax.

@Test
public void testIsValidDecimalMax() {
    ConstraintAnnotationDescriptor.Builder<DecimalMax> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMax.class);
    descriptorBuilder.setAttribute("value", "15.0E0");
    descriptorBuilder.setMessage("{validator.max}");
    DecimalMax m = descriptorBuilder.build().getAnnotation();
    testDecimalMax(m, true);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMax(jakarta.validation.constraints.DecimalMax) Test(org.testng.annotations.Test)

Example 9 with DecimalMax

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

the class MaxValidatorForStringTest method testIsValidDecimalMax.

@Test
public void testIsValidDecimalMax() {
    ConstraintAnnotationDescriptor.Builder<DecimalMax> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMax.class);
    descriptorBuilder.setAttribute("value", "15.0E0");
    descriptorBuilder.setMessage("{validator.max}");
    DecimalMax m = descriptorBuilder.build().getAnnotation();
    DecimalMaxValidatorForCharSequence constraint = new DecimalMaxValidatorForCharSequence();
    constraint.initialize(m);
    testMaxValidator(constraint, true);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMax(jakarta.validation.constraints.DecimalMax) DecimalMaxValidatorForCharSequence(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForCharSequence) Test(org.testng.annotations.Test)

Example 10 with DecimalMax

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

the class MaxValidatorForNumberTest method testIsValidDecimalMax1.

@Test
public void testIsValidDecimalMax1() {
    ConstraintAnnotationDescriptor.Builder<DecimalMax> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMax.class);
    descriptorBuilder.setAttribute("value", Integer.toString(Integer.MAX_VALUE - 1));
    DecimalMax m = descriptorBuilder.build().getAnnotation();
    DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
    constraint.initialize(m);
    assertFalse(constraint.isValid(Double.POSITIVE_INFINITY, null));
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMaxValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForNumber) DecimalMax(jakarta.validation.constraints.DecimalMax) Test(org.testng.annotations.Test)

Aggregations

DecimalMax (jakarta.validation.constraints.DecimalMax)12 Test (org.testng.annotations.Test)8 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)7 DecimalMaxValidatorForNumber (org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForNumber)4 DecimalMin (jakarta.validation.constraints.DecimalMin)2 Max (jakarta.validation.constraints.Max)2 Pattern (jakarta.validation.constraints.Pattern)2 Size (jakarta.validation.constraints.Size)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DecimalMaxValidatorForCharSequence (org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForCharSequence)2 TestForIssue (org.hibernate.validator.testutil.TestForIssue)2 Valid (jakarta.validation.Valid)1 Validator (jakarta.validation.Validator)1 Digits (jakarta.validation.constraints.Digits)1 Min (jakarta.validation.constraints.Min)1 Negative (jakarta.validation.constraints.Negative)1 NegativeOrZero (jakarta.validation.constraints.NegativeOrZero)1 NotNull (jakarta.validation.constraints.NotNull)1