From 77d187eefc8596ef8203eb827486099cc8b27835 Mon Sep 17 00:00:00 2001 From: Chas Honton Date: Sat, 23 Apr 2016 18:25:06 -0700 Subject: [PATCH] LANG-1134: New methods for lang3.Validate This closes #87 from github. --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/Validate.java | 771 +++++++++++++++++- .../apache/commons/lang3/ValidateTest.java | 759 ++++++++++++++++- 3 files changed, 1495 insertions(+), 36 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 8043fb944..9a2560759 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + New methods for lang3.Validate Fix for incorrect comment on StringUtils.containsIgnoreCase method Fix typo on appendIfMissing javadoc Add tests for missed branches in DateUtils diff --git a/src/main/java/org/apache/commons/lang3/Validate.java b/src/main/java/org/apache/commons/lang3/Validate.java index f83089549..2073d3f2d 100644 --- a/src/main/java/org/apache/commons/lang3/Validate.java +++ b/src/main/java/org/apache/commons/lang3/Validate.java @@ -45,6 +45,20 @@ import java.util.regex.Pattern; */ public class Validate { + private static final String DEFAULT_NOT_NAN_EX_MESSAGE = + "The validated value is not a number"; + private static final String DEFAULT_FINITE_EX_MESSAGE = + "The value is invalid: %f"; + private static final String DEFAULT_GREATER_EX_MESSAGE = + "The value %s is not greater than %s"; + private static final String DEFAULT_GREATER_OR_EQUAL_EX_MESSAGE = + "The value %s is not greater than or equal to %s"; + private static final String DEFAULT_SMALLER_EX_MESSAGE = + "The value %s is not smaller than %s"; + private static final String DEFAULT_SMALLER_OR_EQUAL_EX_MESSAGE = + "The value %s is not smaller than or equal to %s"; + private static final String DEFAULT_DIFFERENT_EX_MESSAGE = + "The value %s is invalid"; private static final String DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE = "The value %s is not in the specified exclusive range of %s to %s"; private static final String DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE = @@ -875,6 +889,757 @@ public class Validate { } } + // notNaN + //--------------------------------------------------------------------------------- + + /** + *

Validates that the specified argument is not {@code NaN}; otherwise + * throwing an exception.

+ * + *
Validate.notNaN(myDouble);
+ * + *

The message of the exception is "The validated value is not a + * number".

+ * + * @param value the value to validate + * @throws IllegalArgumentException if the value is not a number + * @see #notNaN(double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void notNaN(final double value) { + notNaN(value, DEFAULT_NOT_NAN_EX_MESSAGE); + } + + /** + *

Validates that the specified argument is not {@code NaN}; otherwise + * throwing an exception with the specified message.

+ * + *
Validate.notNaN(myDouble, "The value must be a number");
+ * + * @param value the value to validate + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if the value is not a number + * @see #notNaN(double) + * + * @since 3.5 + */ + public static void notNaN(final double value, final String message, final Object... values) { + if (Double.isNaN(value)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // finite + //--------------------------------------------------------------------------------- + + /** + *

Validates that the specified argument is not infinite or {@code NaN}; + * otherwise throwing an exception.

+ * + *
Validate.finite(myDouble);
+ * + *

The message of the exception is "The value is invalid: %f".

+ * + * @param value the value to validate + * @throws IllegalArgumentException if the value is infinite or {@code NaN} + * @see #finite(double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void finite(final double value) { + finite(value, DEFAULT_FINITE_EX_MESSAGE, value); + } + + /** + *

Validates that the specified argument is not infinite or {@code NaN}; + * otherwise throwing an exception with the specified message.

+ * + *
Validate.finite(myDouble, "The argument must contain a numeric value");
+ * + * @param value the value to validate + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if the value is infinite or {@code NaN} + * @see #finite(double) + * + * @since 3.5 + */ + public static void finite(final double value, final String message, final Object... values) { + if (Double.isNaN(value) || Double.isInfinite(value)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // greater + //--------------------------------------------------------------------------------- + + /** + *

Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception.

+ * + *
Validate.greaterObj(myObject, refObject);
+ * + *

The message of the exception is "The value {@code value} is not + * greater than {@code min}".

+ * + * @param the type of the argument object + * @param value the object to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greaterObj(java.lang.Object, java.lang.Comparable, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greaterObj(final Comparable value, final T min) { + greaterObj(value, min, DEFAULT_GREATER_EX_MESSAGE, value, min); + } + + /** + *

Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception with the specified message.

+ * + *
Validate.greaterObj(myObject, refObject, "The value must be greater than the reference");
+ * + * @param the type of the argument object + * @param value the object to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greaterObj(java.lang.Object, java.lang.Comparable) + * + * @since 3.5 + */ + public static void greaterObj(final Comparable value, final T min, final String message, final Object... values) { + if (value.compareTo(min) <= 0) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception.

+ * + *
Validate.greater(myLong, 0);
+ * + *

The message of the exception is "The value {@code value} is not + * greater than {@code min}".

+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greater(final long value, final long min) { + greater(value, min, DEFAULT_GREATER_EX_MESSAGE, value, min); + } + + /** + *

Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception with the specified message.

+ * + *
Validate.greater(myLong, 0);
+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(long, long) + * + * @since 3.5 + */ + public static void greater(final long value, final long min, final String message, final Object... values) { + if (value <= min) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.greater(myDouble, 0.0);
+ * + *

The message of the exception is "The value {@code value} is not + * greater than {@code min}".

+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greater(final double value, final double min) { + greater(value, min, DEFAULT_GREATER_EX_MESSAGE, value, min); + } + + /** + *

Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception with the specified message.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.greater(myDouble, 0.0);
+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(double, double) + * + * @since 3.5 + */ + public static void greater(final double value, final double min, final String message, final Object... values) { + if (!(value > min)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // greaterOrEqual + //--------------------------------------------------------------------------------- + + /** + *

Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.

+ * + *
Validate.greaterOrEqualObj(myObject, refObject);
+ * + *

The message of the exception is "The value {@code value} is not + * greater than or equal to {@code min}".

+ * + * @param the type of the argument object + * @param value the object to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqualObj(java.lang.Object, java.lang.Comparable, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greaterOrEqualObj(final Comparable value, final T min) { + greaterOrEqualObj(value, min, DEFAULT_GREATER_OR_EQUAL_EX_MESSAGE, value, min); + } + + /** + *

Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.

+ * + *
Validate.greaterOrEqualObj(myObject, refObject, "The value must be greater than the reference");
+ * + * @param the type of the argument object + * @param value the object to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqualObj(java.lang.Object, java.lang.Comparable) + * + * @since 3.5 + */ + public static void greaterOrEqualObj(final Comparable value, final T min, final String message, final Object... values) { + if (value.compareTo(min) < 0) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.

+ * + *
Validate.greaterOrEqual(myLong, 0);
+ * + *

The message of the exception is "The value {@code value} is not + * greater than or equal to {@code min}".

+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greaterOrEqual(final long value, final long min) { + greaterOrEqual(value, min, DEFAULT_GREATER_OR_EQUAL_EX_MESSAGE, value, min); + } + + /** + *

Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.

+ * + *
Validate.greaterOrEqual(myLong, 0);
+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(long, long) + * + * @since 3.5 + */ + public static void greaterOrEqual(final long value, final long min, final String message, final Object... values) { + if (value < min) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.greaterOrEqual(myDouble, 0.0);
+ * + *

The message of the exception is "The value {@code value} is not + * greater than or equal to {@code min}".

+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greaterOrEqual(final double value, final double min) { + greaterOrEqual(value, min, DEFAULT_GREATER_OR_EQUAL_EX_MESSAGE, value, min); + } + + /** + *

Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.greaterOrEqual(myDouble, 0.0);
+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(double, double) + * + * @since 3.5 + */ + public static void greaterOrEqual(final double value, final double min, final String message, final Object... values) { + if (!(value >= min)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // smaller + //--------------------------------------------------------------------------------- + + /** + *

Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception.

+ * + *
Validate.smallerObj(myObject, refObject);
+ * + *

The message of the exception is "The value {@code value} is not + * smaller than {@code max}".

+ * + * @param the type of the argument object + * @param value the object to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smallerObj(java.lang.Object, java.lang.Comparable, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smallerObj(final Comparable value, final T max) { + smallerObj(value, max, DEFAULT_SMALLER_EX_MESSAGE, value, max); + } + + /** + *

Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception with the specified message.

+ * + *
Validate.smallerObj(myObject, refObject, "The value must be greater than the reference");
+ * + * @param the type of the argument object + * @param value the object to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smallerObj(java.lang.Object, java.lang.Comparable) + * + * @since 3.5 + */ + public static void smallerObj(final Comparable value, final T max, final String message, final Object... values) { + if (value.compareTo(max) >= 0) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception.

+ * + *
Validate.smaller(myLong, 0);
+ * + *

The message of the exception is "The value {@code value} is not + * smaller than {@code max}".

+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smaller(final long value, final long max) { + smaller(value, max, DEFAULT_SMALLER_EX_MESSAGE, value, max); + } + + /** + *

Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception with the specified message.

+ * + *
Validate.smaller(myLong, 0);
+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(long, long) + * + * @since 3.5 + */ + public static void smaller(final long value, final long max, final String message, final Object... values) { + if (value >= max) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.smaller(myDouble, 0.0);
+ * + *

The message of the exception is "The value {@code value} is not + * smaller than {@code max}".

+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smaller(final double value, final double max) { + smaller(value, max, DEFAULT_SMALLER_EX_MESSAGE, value, max); + } + + /** + *

Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception with the specified message.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.smaller(myDouble, 0.0);
+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(double, double) + * + * @since 3.5 + */ + public static void smaller(final double value, final double max, final String message, final Object... values) { + if (!(value < max)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // smallerOrEqual + //--------------------------------------------------------------------------------- + + /** + *

Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception.

+ * + *
Validate.smallerOrEqualObj(myObject, refObject);
+ * + *

The message of the exception is "The value {@code value} is not + * smaller than or equal to {@code max}".

+ * + * @param the type of the argument object + * @param value the object to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqualObj(java.lang.Object, java.lang.Comparable, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smallerOrEqualObj(final Comparable value, final T max) { + smallerOrEqualObj(value, max, DEFAULT_SMALLER_OR_EQUAL_EX_MESSAGE, value, max); + } + + /** + *

Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.

+ * + *
Validate.smallerOrEqualObj(myObject, refObject, "The value must be greater than the reference");
+ * + * @param the type of the argument object + * @param value the object to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqualObj(java.lang.Object, java.lang.Comparable) + * + * @since 3.5 + */ + public static void smallerOrEqualObj(final Comparable value, final T max, final String message, final Object... values) { + if (value.compareTo(max) > 0) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception.

+ * + *
Validate.smallerOrEqual(myLong, 0);
+ * + *

The message of the exception is "The value {@code value} is not + * smaller than or equal to {@code max}".

+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smallerOrEqual(final long value, final long max) { + smallerOrEqual(value, max, DEFAULT_SMALLER_OR_EQUAL_EX_MESSAGE, value, max); + } + + /** + *

Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.

+ * + *
Validate.smallerOrEqual(myLong, 0);
+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(long, long) + * + * @since 3.5 + */ + public static void smallerOrEqual(final long value, final long max, final String message, final Object... values) { + if (value > max) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.smallerOrEqual(myDouble, 0.0);
+ * + *

The message of the exception is "The value {@code value} is not + * smaller than or equal to {@code max}".

+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smallerOrEqual(final double value, final double max) { + smallerOrEqual(value, max, DEFAULT_SMALLER_OR_EQUAL_EX_MESSAGE, value, max); + } + + /** + *

Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.

+ * + *

If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.

+ * + *
Validate.smallerOrEqual(myDouble, 0.0);
+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(double, double) + * + * @since 3.5 + */ + public static void smallerOrEqual(final double value, final double max, final String message, final Object... values) { + if (!(value <= max)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // different + //--------------------------------------------------------------------------------- + + /** + *

Validates that the specified argument is different from a given value + * (reference); otherwise throwing an exception.

+ * + *

Two objects are considered different if + * {@code value.compareTo(reference) != 0}

+ * + *
Validate.differentObj(myObject, refObject);
+ * + *

The message of the exception is "The value {@code value} is + * invalid".

+ * + * @param the type of the argument object + * @param value the object to validate + * @param reference the reference value + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void differentObj(final Comparable value, final T reference) { + differentObj(value, reference, DEFAULT_DIFFERENT_EX_MESSAGE, value); + } + + /** + *

Validates that the specified argument is different from a given value + * (reference); otherwise throwing an exception with the specified message.

+ * + *

Two objects are considered different if + * {@code value.compareTo(reference) != 0}

+ * + *
Validate.differentObj(myObject, refObject, "The value is invalid");
+ * + * @param the type of the argument object + * @param value the object to validate + * @param reference the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void differentObj(final Comparable value, final T reference, final String message, final Object... values) { + if (value.compareTo(reference) == 0) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception.

+ * + *
Validate.different(myLong, 0);
+ * + *

The message of the exception is "The value {@code value} is + * invalid".

+ * + * @param value the value to validate + * @param reference the reference value + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final long value, final long reference) { + different(value, reference, DEFAULT_DIFFERENT_EX_MESSAGE, value); + } + + /** + *

Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception with the specified message.

+ * + *
Validate.different(myLong, 0, "The value is invalid");
+ * + * @param value the value to validate + * @param reference the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final long value, final long reference, final String message, final Object... values) { + if (value == reference) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception.

+ * + *

If {@code value} or {@code reference} is {@code NaN}, no exception will be thrown.

+ * + *
Validate.different(myDouble, 0.0);
+ * + *

The message of the exception is "The value {@code value} is + * invalid".

+ * + * @param value the value to validate + * @param reference the reference value + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final double value, final double reference) { + different(value, reference, DEFAULT_DIFFERENT_EX_MESSAGE, value); + } + + /** + *

Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception with the specified message.

+ * + *

If {@code value} or {@code reference} is {@code NaN}, no exception will be thrown.

+ * + *
Validate.different(myDouble, 0.0, "The value is invalid");
+ * + * @param value the value to validate + * @param reference the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final double value, final double reference, final String message, final Object... values) { + if (value == reference) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + // inclusiveBetween //--------------------------------------------------------------------------------- @@ -1156,7 +1921,7 @@ public class Validate { /** * Validates that the argument is an instance of the specified class, if not throws an exception. - * + * *

This method is useful when validating according to an arbitrary class

* *
Validate.isInstanceOf(OkClass.class, object);
@@ -1207,7 +1972,7 @@ public class Validate { /** * Validates that the argument can be converted to the specified class, if not, throws an exception. - * + * *

This method is useful when validating that there will be no casting errors.

* *
Validate.isAssignableFrom(SuperClass.class, object.getClass());
@@ -1231,7 +1996,7 @@ public class Validate { /** * Validates that the argument can be converted to the specified class, if not throws an exception. - * + * *

This method is useful when validating if there will be no casting errors.

* *
Validate.isAssignableFrom(SuperClass.class, object.getClass());
diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index 954179c7e..d577ee65e 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -41,7 +41,7 @@ import org.junit.Test; * Unit tests {@link org.apache.commons.lang3.Validate}. */ public class ValidateTest { - + //----------------------------------------------------------------------- @Test public void testIsTrue1() { @@ -114,7 +114,7 @@ public class ValidateTest { } catch (final NullPointerException ex) { assertEquals("The validated object is null", ex.getMessage()); } - + final String str = "Hi"; final String testStr = Validate.notNull(str); assertSame(str, testStr); @@ -131,7 +131,7 @@ public class ValidateTest { } catch (final NullPointerException ex) { assertEquals("MSG", ex.getMessage()); } - + final String str = "Hi"; final String testStr = Validate.notNull(str, "Message"); assertSame(str, testStr); @@ -154,7 +154,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("The validated array is empty", ex.getMessage()); } - + final String[] array = new String[] {"hi"}; final String[] test = Validate.notEmpty(array); assertSame(array, test); @@ -176,7 +176,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } - + final String[] array = new String[] {"hi"}; final String[] test = Validate.notEmpty(array, "Message"); assertSame(array, test); @@ -201,7 +201,7 @@ public class ValidateTest { } coll.add(Integer.valueOf(8)); Validate.notEmpty(coll); - + final Collection test = Validate.notEmpty(coll); assertSame(coll, test); } @@ -224,7 +224,7 @@ public class ValidateTest { } coll.add(Integer.valueOf(8)); Validate.notEmpty(coll, "MSG"); - + final Collection test = Validate.notEmpty(coll, "Message"); assertSame(coll, test); } @@ -248,7 +248,7 @@ public class ValidateTest { } map.put("ll", Integer.valueOf(8)); Validate.notEmpty(map); - + final Map test = Validate.notEmpty(map); assertSame(map, test); } @@ -271,7 +271,7 @@ public class ValidateTest { } map.put("ll", Integer.valueOf(8)); Validate.notEmpty(map, "MSG"); - + final Map test = Validate.notEmpty(map, "Message"); assertSame(map, test); } @@ -293,7 +293,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("The validated character sequence is empty", ex.getMessage()); } - + final String str = "Hi"; final String testStr = Validate.notEmpty(str); assertSame(str, testStr); @@ -315,7 +315,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } - + final String str = "Hi"; final String testStr = Validate.notEmpty(str, "Message"); assertSame(str, testStr); @@ -556,7 +556,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("The validated array contains null element at index: 1", ex.getMessage()); } - + array = new String[] {"a", "b"}; final String[] test = Validate.noNullElements(array); assertSame(array, test); @@ -580,7 +580,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } - + array = new String[] {"a", "b"}; final String[] test = Validate.noNullElements(array, "Message"); assertSame(array, test); @@ -607,7 +607,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("The validated collection contains null element at index: 1", ex.getMessage()); } - + coll.set(1, "b"); final List test = Validate.noNullElements(coll); assertSame(coll, test); @@ -633,7 +633,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } - + coll.set(1, "b"); final List test = Validate.noNullElements(coll, "Message"); assertSame(coll, test); @@ -670,7 +670,7 @@ public class ValidateTest { } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } - + final String[] strArray = new String[] {"Hi"}; final String[] test = Validate.noNullElements(strArray, "Message"); assertSame(strArray, test); @@ -693,7 +693,7 @@ public class ValidateTest { } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated array index is invalid: 2", ex.getMessage()); } - + final String[] strArray = new String[] {"Hi"}; final String[] test = Validate.noNullElements(strArray); assertSame(strArray, test); @@ -720,7 +720,7 @@ public class ValidateTest { } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } - + final List strColl = Arrays.asList(new String[] {"Hi"}); final List test = Validate.validIndex(strColl, 0, "Message"); assertSame(strColl, test); @@ -745,7 +745,7 @@ public class ValidateTest { } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated collection index is invalid: 2", ex.getMessage()); } - + final List strColl = Arrays.asList(new String[] {"Hi"}); final List test = Validate.validIndex(strColl, 0); assertSame(strColl, test); @@ -770,7 +770,7 @@ public class ValidateTest { } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } - + final String input = "Hi"; final String test = Validate.validIndex(input, 0, "Message"); assertSame(input, test); @@ -793,12 +793,12 @@ public class ValidateTest { } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated character sequence index is invalid: 2", ex.getMessage()); } - + final String input = "Hi"; final String test = Validate.validIndex(input, 0); assertSame(input, test); } - + @Test public void testMatchesPattern() { @@ -814,7 +814,7 @@ public class ValidateTest { assertEquals("The string hi does not match the pattern [0-9]*", e.getMessage()); } } - + @Test public void testMatchesPattern_withMessage() { @@ -830,7 +830,700 @@ public class ValidateTest { assertEquals("Does not match", e.getMessage()); } } - + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + + @Test + public void testNotNaN1() { + Validate.notNaN(0.0); + Validate.notNaN(Double.POSITIVE_INFINITY); + Validate.notNaN(Double.NEGATIVE_INFINITY); + try { + Validate.notNaN(Double.NaN); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The validated value is not a number", ex.getMessage()); + } + } + + @Test + public void testNotNaN2() { + Validate.notNaN(0.0, "MSG"); + Validate.notNaN(Double.POSITIVE_INFINITY, "MSG"); + Validate.notNaN(Double.NEGATIVE_INFINITY, "MSG"); + try { + Validate.notNaN(Double.NaN, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + + @Test + public void testFinite1() { + Validate.finite(0.0); + try { + Validate.finite(Double.POSITIVE_INFINITY); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value is invalid: Infinity", ex.getMessage()); + } + try { + Validate.finite(Double.NEGATIVE_INFINITY); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value is invalid: -Infinity", ex.getMessage()); + } + try { + Validate.finite(Double.NaN); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value is invalid: NaN", ex.getMessage()); + } + } + + @Test + public void testFinite2() { + Validate.finite(0.0, "MSG"); + try { + Validate.finite(Double.POSITIVE_INFINITY, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.finite(Double.NEGATIVE_INFINITY, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.finite(Double.NaN, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + + @Test + public void testGreaterObject1() { + Validate.greaterObj("c", "b"); + try { + Validate.greaterObj("b", "b"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value b is not greater than b", ex.getMessage()); + } + try { + Validate.greaterObj("a", "b"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value a is not greater than b", ex.getMessage()); + } + } + + @Test + public void testGreaterObject2() { + Validate.greaterObj("c", "b", "MSG"); + try { + Validate.greaterObj("b", "b", "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greaterObj("a", "b", "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testGreaterLong1() { + Validate.greater(1, 0); + try { + Validate.greater(0, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0 is not greater than 0", ex.getMessage()); + } + try { + Validate.greater(-1, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value -1 is not greater than 0", ex.getMessage()); + } + } + + @Test + public void testGreaterLong2() { + Validate.greater(1, 0, "MSG"); + try { + Validate.greater(0, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greater(-1, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testGreaterDouble1() { + Validate.greater(1.0, 0.0); + Validate.greater(Double.POSITIVE_INFINITY, 0.0); + try { + Validate.greater(0.0, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0.0 is not greater than 0.0", ex.getMessage()); + } + try { + Validate.greater(-1.0, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value -1.0 is not greater than 0.0", ex.getMessage()); + } + try { + Validate.greater(Double.NEGATIVE_INFINITY, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value -Infinity is not greater than 0.0", ex.getMessage()); + } + try { + Validate.greater(Double.NaN, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value NaN is not greater than 0.0", ex.getMessage()); + } + try { + Validate.greater(0.0, Double.NaN); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0.0 is not greater than NaN", ex.getMessage()); + } + } + + @Test + public void testGreaterDouble2() { + Validate.greater(1.0, 0.0, "MSG"); + Validate.greater(Double.POSITIVE_INFINITY, 0.0, "MSG"); + try { + Validate.greater(0.0, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greater(-1.0, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greater(Double.NEGATIVE_INFINITY, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greater(Double.NaN, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greater(0.0, Double.NaN, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + + @Test + public void testGreaterOrEqualObject1() { + Validate.greaterOrEqualObj("c", "b"); + Validate.greaterOrEqualObj("b", "b"); + try { + Validate.greaterOrEqualObj("a", "b"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value a is not greater than or equal to b", ex.getMessage()); + } + } + + @Test + public void testGreaterOrEqualObject2() { + Validate.greaterOrEqualObj("c", "b", "MSG"); + Validate.greaterOrEqualObj("b", "b", "MSG"); + try { + Validate.greaterOrEqualObj("a", "b", "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testGreaterOrEqualLong1() { + Validate.greaterOrEqual(1, 0); + Validate.greaterOrEqual(0, 0); + try { + Validate.greaterOrEqual(-1, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value -1 is not greater than or equal to 0", ex.getMessage()); + } + } + + @Test + public void testGreaterOrEqualLong2() { + Validate.greaterOrEqual(1, 0, "MSG"); + Validate.greaterOrEqual(0, 0, "MSG"); + try { + Validate.greaterOrEqual(-1, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testGreaterOrEqualDouble1() { + Validate.greaterOrEqual(1.0, 0.0); + Validate.greaterOrEqual(Double.POSITIVE_INFINITY, 0.0); + Validate.greaterOrEqual(0.0, 0.0); + try { + Validate.greaterOrEqual(-1.0, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value -1.0 is not greater than or equal to 0.0", ex.getMessage()); + } + try { + Validate.greaterOrEqual(Double.NEGATIVE_INFINITY, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value -Infinity is not greater than or equal to 0.0", ex.getMessage()); + } + try { + Validate.greaterOrEqual(Double.NaN, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value NaN is not greater than or equal to 0.0", ex.getMessage()); + } + try { + Validate.greaterOrEqual(0.0, Double.NaN); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0.0 is not greater than or equal to NaN", ex.getMessage()); + } + try { + Validate.greaterOrEqual(Double.NaN, Double.NaN); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value NaN is not greater than or equal to NaN", ex.getMessage()); + } + } + + @Test + public void testGreaterOrEqualDouble2() { + Validate.greaterOrEqual(1.0, 0.0, "MSG"); + Validate.greaterOrEqual(Double.POSITIVE_INFINITY, 0.0, "MSG"); + Validate.greaterOrEqual(0.0, 0.0, "MSG"); + + try { + Validate.greaterOrEqual(-1.0, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greaterOrEqual(Double.NEGATIVE_INFINITY, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greaterOrEqual(Double.NaN, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greaterOrEqual(0.0, Double.NaN, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.greaterOrEqual(Double.NaN, Double.NaN, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + + @Test + public void testSmallerObject1() { + Validate.smallerObj("a", "b"); + try { + Validate.smallerObj("b", "b"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value b is not smaller than b", ex.getMessage()); + } + try { + Validate.smallerObj("c", "b"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value c is not smaller than b", ex.getMessage()); + } + } + + @Test + public void testSmallerObject2() { + Validate.smallerObj("a", "b", "MSG"); + try { + Validate.smallerObj("b", "b", "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smallerObj("c", "b", "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testSmallerLong1() { + Validate.smaller(-1, 0); + try { + Validate.smaller(0, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0 is not smaller than 0", ex.getMessage()); + } + try { + Validate.smaller(1, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 1 is not smaller than 0", ex.getMessage()); + } + } + + @Test + public void testSmallerLong2() { + Validate.smaller(-1, 0, "MSG"); + try { + Validate.smaller(0, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smaller(1, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testSmallerDouble1() { + Validate.smaller(-1.0, 0.0); + Validate.smaller(Double.NEGATIVE_INFINITY, 0.0); + try { + Validate.smaller(0.0, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0.0 is not smaller than 0.0", ex.getMessage()); + } + try { + Validate.smaller(1.0, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 1.0 is not smaller than 0.0", ex.getMessage()); + } + try { + Validate.smaller(Double.POSITIVE_INFINITY, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value Infinity is not smaller than 0.0", ex.getMessage()); + } + try { + Validate.smaller(Double.NaN, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value NaN is not smaller than 0.0", ex.getMessage()); + } + try { + Validate.smaller(0.0, Double.NaN); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0.0 is not smaller than NaN", ex.getMessage()); + } + } + + @Test + public void testSmallerDouble2() { + Validate.smaller(-1.0, 0.0, "MSG"); + Validate.smaller(Double.NEGATIVE_INFINITY, 0.0, "MSG"); + try { + Validate.smaller(0.0, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smaller(1.0, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smaller(Double.POSITIVE_INFINITY, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smaller(Double.NaN, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smaller(0.0, Double.NaN, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + + @Test + public void testSmallerOrEqualObject1() { + Validate.smallerOrEqualObj("a", "b"); + Validate.smallerOrEqualObj("b", "b"); + try { + Validate.smallerOrEqualObj("c", "b"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value c is not smaller than or equal to b", ex.getMessage()); + } + } + + @Test + public void testSmallerOrEqualObject2() { + Validate.smallerOrEqualObj("a", "b", "MSG"); + Validate.smallerOrEqualObj("b", "b", "MSG"); + try { + Validate.smallerOrEqualObj("c", "b", "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testSmallerOrEqualLong1() { + Validate.smallerOrEqual(-1, 0); + Validate.smallerOrEqual(0, 0); + try { + Validate.smallerOrEqual(1, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 1 is not smaller than or equal to 0", ex.getMessage()); + } + } + + @Test + public void testSmallerOrEqualLong2() { + Validate.smallerOrEqual(-1, 0, "MSG"); + Validate.smallerOrEqual(0, 0, "MSG"); + try { + Validate.smallerOrEqual(1, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testSmallerOrEqualDouble1() { + Validate.smallerOrEqual(-1.0, 0.0); + Validate.smallerOrEqual(Double.NEGATIVE_INFINITY, 0.0); + Validate.smallerOrEqual(0.0, 0.0); + try { + Validate.smallerOrEqual(1.0, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 1.0 is not smaller than or equal to 0.0", ex.getMessage()); + } + try { + Validate.smallerOrEqual(Double.POSITIVE_INFINITY, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value Infinity is not smaller than or equal to 0.0", ex.getMessage()); + } + try { + Validate.smallerOrEqual(Double.NaN, 0.0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value NaN is not smaller than or equal to 0.0", ex.getMessage()); + } + try { + Validate.smallerOrEqual(0.0, Double.NaN); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0.0 is not smaller than or equal to NaN", ex.getMessage()); + } + } + + @Test + public void testSmallerOrEqualDouble2() { + Validate.smallerOrEqual(-1.0, 0.0, "MSG"); + Validate.smallerOrEqual(Double.NEGATIVE_INFINITY, 0.0, "MSG"); + Validate.smallerOrEqual(0.0, 0.0, "MSG"); + try { + Validate.smallerOrEqual(1.0, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smallerOrEqual(Double.POSITIVE_INFINITY, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smallerOrEqual(Double.NaN, 0.0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.smallerOrEqual(0.0, Double.NaN, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + + @Test + public void testDifferentObject1() { + Validate.differentObj("b", "a"); + try { + Validate.differentObj("a", "a"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value a is invalid", ex.getMessage()); + } + } + + @Test + public void testDifferentObject2() { + Validate.differentObj("b", "a", "MSG"); + try { + Validate.differentObj("a", "a", "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testDifferentLong1() { + Validate.different(1, 0); + try { + Validate.different(0, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0 is invalid", ex.getMessage()); + } + } + + @Test + public void testDifferentLong2() { + Validate.different(1, 0, "MSG"); + try { + Validate.different(0, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + @Test + public void testDifferentDouble1() { + Validate.different(1.0, 0.0); + Validate.different(Double.NaN, 0.0); + Validate.different(1.0, Double.NaN); + Validate.different(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); + try { + Validate.different(0, 0); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value 0 is invalid", ex.getMessage()); + } + try { + Validate.different(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("The value Infinity is invalid", ex.getMessage()); + } + } + + @Test + public void testDifferentDouble2() { + Validate.different(1.0, 0.0, "MSG"); + Validate.different(Double.NaN, 0.0, "MSG"); + Validate.different(1.0, Double.NaN, "MSG"); + Validate.different(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, "MSG"); + try { + Validate.different(0, 0, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + try { + Validate.different(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, "MSG"); + fail("Expecting IllegalArgumentException"); + } catch (final IllegalArgumentException ex) { + assertEquals("MSG", ex.getMessage()); + } + } + + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- + @Test public void testInclusiveBetween() { @@ -842,7 +1535,7 @@ public class ValidateTest { assertEquals("The value 6 is not in the specified inclusive range of 0 to 5", e.getMessage()); } } - + @Test public void testInclusiveBetween_withMessage() { @@ -906,7 +1599,7 @@ public class ValidateTest { assertEquals("Error", e.getMessage()); } } - + @Test public void testExclusiveBetween() { @@ -924,7 +1617,7 @@ public class ValidateTest { assertEquals("The value 5 is not in the specified exclusive range of 0 to 5", e.getMessage()); } } - + @Test public void testExclusiveBetween_withMessage() { @@ -1020,7 +1713,7 @@ public class ValidateTest { Validate.isInstanceOf(String.class, "hi"); Validate.isInstanceOf(Integer.class, 1); } - + @Test public void testIsInstanceOfExceptionMessage() { try { @@ -1030,7 +1723,7 @@ public class ValidateTest { assertEquals("Expected type: java.util.List, actual: java.lang.String", e.getMessage()); } } - + @Test public void testIsInstanceOf_withMessage() { Validate.isInstanceOf(String.class, "hi", "Error"); @@ -1042,7 +1735,7 @@ public class ValidateTest { assertEquals("Error", e.getMessage()); } } - + @Test public void testIsInstanceOf_withMessageArgs() { Validate.isInstanceOf(String.class, "hi", "Error %s=%s", "Name", "Value"); @@ -1066,13 +1759,13 @@ public class ValidateTest { assertEquals("Error interface java.util.List=null", e.getMessage()); } } - + @Test public void testIsAssignable() { Validate.isAssignableFrom(CharSequence.class, String.class); Validate.isAssignableFrom(AbstractList.class, ArrayList.class); } - + @Test public void testIsAssignableExceptionMessage() { try { @@ -1082,7 +1775,7 @@ public class ValidateTest { assertEquals("Cannot assign a java.lang.String to a java.util.List", e.getMessage()); } } - + @Test public void testIsAssignable_withMessage() { Validate.isAssignableFrom(CharSequence.class, String.class, "Error");