Search in sources :

Example 1 with DigitsValidatorForNumber

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

the class DigitsValidatorForNumberTest method testNegativeFractionLength.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testNegativeFractionLength() {
    descriptorBuilder.setAttribute("integer", 1);
    descriptorBuilder.setAttribute("fraction", -1);
    Digits p = descriptorBuilder.build().getAnnotation();
    DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
    constraint.initialize(p);
}
Also used : DigitsValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber) Digits(jakarta.validation.constraints.Digits) Test(org.testng.annotations.Test)

Example 2 with DigitsValidatorForNumber

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

the class DigitsValidatorForNumberTest method testNegativeIntegerLength.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testNegativeIntegerLength() {
    descriptorBuilder.setAttribute("integer", -1);
    descriptorBuilder.setAttribute("fraction", 1);
    Digits p = descriptorBuilder.build().getAnnotation();
    DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
    constraint.initialize(p);
}
Also used : DigitsValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber) Digits(jakarta.validation.constraints.Digits) Test(org.testng.annotations.Test)

Example 3 with DigitsValidatorForNumber

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

the class DigitsValidatorForNumberTest method testIsValid.

@Test
public void testIsValid() {
    descriptorBuilder.setAttribute("integer", 5);
    descriptorBuilder.setAttribute("fraction", 2);
    Digits p = descriptorBuilder.build().getAnnotation();
    DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
    constraint.initialize(p);
    assertTrue(constraint.isValid(null, null));
    assertTrue(constraint.isValid(Byte.valueOf("0"), null));
    assertTrue(constraint.isValid(Double.valueOf("500.2"), null));
    assertTrue(constraint.isValid(new BigDecimal("-12345.12"), null));
    assertFalse(constraint.isValid(new BigDecimal("-123456.12"), null));
    assertFalse(constraint.isValid(new BigDecimal("-123456.123"), null));
    assertFalse(constraint.isValid(new BigDecimal("-12345.123"), null));
    assertFalse(constraint.isValid(new BigDecimal("12345.123"), null));
    assertTrue(constraint.isValid(Float.valueOf("-000000000.22"), null));
    assertFalse(constraint.isValid(Integer.valueOf("256874"), null));
    assertFalse(constraint.isValid(Double.valueOf("12.0001"), null));
}
Also used : DigitsValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber) Digits(jakarta.validation.constraints.Digits) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with DigitsValidatorForNumber

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

the class DigitsValidatorForNumberTest method testTrailingZerosAreTrimmed.

@Test
public void testTrailingZerosAreTrimmed() {
    descriptorBuilder.setAttribute("integer", 12);
    descriptorBuilder.setAttribute("fraction", 3);
    Digits p = descriptorBuilder.build().getAnnotation();
    DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
    constraint.initialize(p);
    assertTrue(constraint.isValid(0.001d, null));
    assertTrue(constraint.isValid(0.00100d, null));
    assertFalse(constraint.isValid(0.0001d, null));
}
Also used : DigitsValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber) Digits(jakarta.validation.constraints.Digits) Test(org.testng.annotations.Test)

Example 5 with DigitsValidatorForNumber

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

the class DigitsValidatorForNumberTest method testIsValidZeroLength.

@Test
public void testIsValidZeroLength() {
    descriptorBuilder.setAttribute("integer", 0);
    descriptorBuilder.setAttribute("fraction", 0);
    Digits p = descriptorBuilder.build().getAnnotation();
    DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
    constraint.initialize(p);
    assertTrue(constraint.isValid(null, null));
    assertFalse(constraint.isValid(Byte.valueOf("0"), null));
    assertFalse(constraint.isValid(Double.valueOf("500.2"), null));
}
Also used : DigitsValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber) Digits(jakarta.validation.constraints.Digits) Test(org.testng.annotations.Test)

Aggregations

Digits (jakarta.validation.constraints.Digits)5 DigitsValidatorForNumber (org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber)5 Test (org.testng.annotations.Test)5 BigDecimal (java.math.BigDecimal)1