diff --git a/src/main/java/org/apache/commons/validator/routines/BigIntegerValidator.java b/src/main/java/org/apache/commons/validator/routines/BigIntegerValidator.java index 1642d66cc..3535f4e95 100644 --- a/src/main/java/org/apache/commons/validator/routines/BigIntegerValidator.java +++ b/src/main/java/org/apache/commons/validator/routines/BigIntegerValidator.java @@ -145,7 +145,7 @@ public boolean maxValue(final BigInteger value, final long max) { * or equal to the minimum. */ public boolean minValue(final BigInteger value, final long min) { - return value.longValue() >= min; + return value.compareTo(BigInteger.valueOf(min)) >= 0; } /** diff --git a/src/test/java/org/apache/commons/validator/routines/BigIntegerValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/BigIntegerValidatorTest.java index 53ab62631..50002c181 100644 --- a/src/test/java/org/apache/commons/validator/routines/BigIntegerValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/BigIntegerValidatorTest.java @@ -84,7 +84,7 @@ void testBigIntegerAboveLongMaxValue() { assertTrue(resultAboveLong.compareTo(BigInteger.ZERO) > 0); assertTrue(resultAboveLong.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0); assertTrue(instance.minValue(resultAboveLong, Long.MIN_VALUE)); - assertFalse(instance.minValue(resultAboveLong, Long.MAX_VALUE)); + assertTrue(instance.minValue(resultAboveLong, Long.MAX_VALUE)); assertFalse(instance.maxValue(resultAboveLong, Long.MIN_VALUE)); assertFalse(instance.maxValue(resultAboveLong, Long.MAX_VALUE)); assertFalse(instance.isInRange(resultAboveLong, Long.MIN_VALUE, Long.MAX_VALUE)); @@ -105,7 +105,7 @@ void testBigIntegerBelowLongMinValue() { assertTrue(resultBelowLong.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0); assertTrue(resultBelowLong.compareTo(BigInteger.ZERO) < 0); assertTrue(resultBelowLong.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0); - assertTrue(instance.minValue(resultBelowLong, Long.MIN_VALUE)); + assertFalse(instance.minValue(resultBelowLong, Long.MIN_VALUE)); assertFalse(instance.minValue(resultBelowLong, Long.MAX_VALUE)); assertTrue(instance.maxValue(resultBelowLong, Long.MIN_VALUE)); assertTrue(instance.maxValue(resultBelowLong, Long.MAX_VALUE)); @@ -114,6 +114,22 @@ void testBigIntegerBelowLongMinValue() { assertEquals(BigDecimalValidator.getInstance().validate(belowLongStr, "#").toBigInteger(), resultBelowLong); } + /** + * Test minValue() against bounds for values outside the long range, using exact BigIntegers so the comparison is not affected by double rounding. + */ + @Test + void testMinValueOutsideLongRange() { + final BigIntegerValidator instance = BigIntegerValidator.getInstance(); + final BigInteger aboveMax = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE); + final BigInteger belowMin = BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE); + // aboveMax is greater than every long, so it is >= any long minimum + assertTrue(instance.minValue(aboveMax, Long.MAX_VALUE)); + assertTrue(instance.minValue(aboveMax, Long.MIN_VALUE)); + // belowMin is smaller than every long, so it is not >= any long minimum + assertFalse(instance.minValue(belowMin, Long.MIN_VALUE)); + assertFalse(instance.minValue(belowMin, Long.MAX_VALUE)); + } + /** * Test BigInteger Range/Min/Max */