LANG-1134: New methods for lang3.Validate

This closes #87 from github.
This commit is contained in:
Chas Honton 2016-04-23 18:25:06 -07:00
parent 2e3fa5c253
commit 77d187eefc
3 changed files with 1495 additions and 36 deletions

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.5" date="tba" description="tba"> <release version="3.5" date="tba" description="tba">
<action issue="LANG-1134" type="add" dev="chas" due-to="Alan Smithee">New methods for lang3.Validate</action>
<action issue="LANG-1222" type="fix" dev="ggregory" due-to="Adam J.">Fix for incorrect comment on StringUtils.containsIgnoreCase method</action> <action issue="LANG-1222" type="fix" dev="ggregory" due-to="Adam J.">Fix for incorrect comment on StringUtils.containsIgnoreCase method</action>
<action issue="LANG-1221" type="fix" dev="ggregory" due-to="Pierre Templier">Fix typo on appendIfMissing javadoc</action> <action issue="LANG-1221" type="fix" dev="ggregory" due-to="Pierre Templier">Fix typo on appendIfMissing javadoc</action>
<action issue="LANG-1220" type="add" dev="kinow" due-to="Casey Scarborough">Add tests for missed branches in DateUtils</action> <action issue="LANG-1220" type="add" dev="kinow" due-to="Casey Scarborough">Add tests for missed branches in DateUtils</action>

View File

@ -45,6 +45,20 @@ import java.util.regex.Pattern;
*/ */
public class Validate { 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 = private static final String DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE =
"The value %s is not in the specified exclusive range of %s to %s"; "The value %s is not in the specified exclusive range of %s to %s";
private static final String DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE = private static final String DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE =
@ -875,6 +889,757 @@ public class Validate {
} }
} }
// notNaN
//---------------------------------------------------------------------------------
/**
* <p>Validates that the specified argument is not {@code NaN}; otherwise
* throwing an exception.</p>
*
* <pre>Validate.notNaN(myDouble);</pre>
*
* <p>The message of the exception is &quot;The validated value is not a
* number&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is not {@code NaN}; otherwise
* throwing an exception with the specified message.</p>
*
* <pre>Validate.notNaN(myDouble, "The value must be a number");</pre>
*
* @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
//---------------------------------------------------------------------------------
/**
* <p>Validates that the specified argument is not infinite or {@code NaN};
* otherwise throwing an exception.</p>
*
* <pre>Validate.finite(myDouble);</pre>
*
* <p>The message of the exception is &quot;The value is invalid: %f&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is not infinite or {@code NaN};
* otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.finite(myDouble, "The argument must contain a numeric value");</pre>
*
* @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
//---------------------------------------------------------------------------------
/**
* <p>Validates that the specified argument is strictly greater than a given
* reference; otherwise throwing an exception.</p>
*
* <pre>Validate.greaterObj(myObject, refObject);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* greater than {@code min}&quot;.</p>
*
* @param <T> 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 <T> void greaterObj(final Comparable<T> value, final T min) {
greaterObj(value, min, DEFAULT_GREATER_EX_MESSAGE, value, min);
}
/**
* <p>Validates that the specified argument is strictly greater than a given
* reference; otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.greaterObj(myObject, refObject, "The value must be greater than the reference");</pre>
*
* @param <T> 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 <T> void greaterObj(final Comparable<T> value, final T min, final String message, final Object... values) {
if (value.compareTo(min) <= 0) {
throw new IllegalArgumentException(String.format(message, values));
}
}
/**
* <p>Validates that the specified argument is strictly greater than a given
* reference; otherwise throwing an exception.</p>
*
* <pre>Validate.greater(myLong, 0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* greater than {@code min}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is strictly greater than a given
* reference; otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.greater(myLong, 0);</pre>
*
* @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));
}
}
/**
* <p>Validates that the specified argument is strictly greater than a given
* reference; otherwise throwing an exception.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.greater(myDouble, 0.0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* greater than {@code min}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is strictly greater than a given
* reference; otherwise throwing an exception with the specified message.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.greater(myDouble, 0.0);</pre>
*
* @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
//---------------------------------------------------------------------------------
/**
* <p>Validates that the specified argument is greater than, or equal to, a
* given reference; otherwise throwing an exception.</p>
*
* <pre>Validate.greaterOrEqualObj(myObject, refObject);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* greater than or equal to {@code min}&quot;.</p>
*
* @param <T> 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 <T> void greaterOrEqualObj(final Comparable<T> value, final T min) {
greaterOrEqualObj(value, min, DEFAULT_GREATER_OR_EQUAL_EX_MESSAGE, value, min);
}
/**
* <p>Validates that the specified argument is greater than, or equal to, a
* given reference; otherwise throwing an exception.</p>
*
* <pre>Validate.greaterOrEqualObj(myObject, refObject, "The value must be greater than the reference");</pre>
*
* @param <T> 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 <T> void greaterOrEqualObj(final Comparable<T> value, final T min, final String message, final Object... values) {
if (value.compareTo(min) < 0) {
throw new IllegalArgumentException(String.format(message, values));
}
}
/**
* <p>Validates that the specified argument is greater than, or equal to, a
* given reference; otherwise throwing an exception.</p>
*
* <pre>Validate.greaterOrEqual(myLong, 0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* greater than or equal to {@code min}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is greater than, or equal to, a
* given reference; otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.greaterOrEqual(myLong, 0);</pre>
*
* @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));
}
}
/**
* <p>Validates that the specified argument is greater than, or equal to, a
* given reference; otherwise throwing an exception.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.greaterOrEqual(myDouble, 0.0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* greater than or equal to {@code min}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is greater than, or equal to, a
* given reference; otherwise throwing an exception with the specified message.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.greaterOrEqual(myDouble, 0.0);</pre>
*
* @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
//---------------------------------------------------------------------------------
/**
* <p>Validates that the specified argument is strictly smaller than a given
* reference; otherwise throwing an exception.</p>
*
* <pre>Validate.smallerObj(myObject, refObject);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* smaller than {@code max}&quot;.</p>
*
* @param <T> 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 <T> void smallerObj(final Comparable<T> value, final T max) {
smallerObj(value, max, DEFAULT_SMALLER_EX_MESSAGE, value, max);
}
/**
* <p>Validates that the specified argument is strictly smaller than a given
* reference; otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.smallerObj(myObject, refObject, "The value must be greater than the reference");</pre>
*
* @param <T> 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 <T> void smallerObj(final Comparable<T> value, final T max, final String message, final Object... values) {
if (value.compareTo(max) >= 0) {
throw new IllegalArgumentException(String.format(message, values));
}
}
/**
* <p>Validates that the specified argument is strictly smaller than a given
* reference; otherwise throwing an exception.</p>
*
* <pre>Validate.smaller(myLong, 0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* smaller than {@code max}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is strictly smaller than a given
* reference; otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.smaller(myLong, 0);</pre>
*
* @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));
}
}
/**
* <p>Validates that the specified argument is strictly smaller than a given
* reference; otherwise throwing an exception.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.smaller(myDouble, 0.0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* smaller than {@code max}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is strictly smaller than a given
* reference; otherwise throwing an exception with the specified message.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.smaller(myDouble, 0.0);</pre>
*
* @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
//---------------------------------------------------------------------------------
/**
* <p>Validates that the specified argument is smaller than, or equal to, a
* given reference; otherwise throwing an exception.</p>
*
* <pre>Validate.smallerOrEqualObj(myObject, refObject);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* smaller than or equal to {@code max}&quot;.</p>
*
* @param <T> 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 <T> void smallerOrEqualObj(final Comparable<T> value, final T max) {
smallerOrEqualObj(value, max, DEFAULT_SMALLER_OR_EQUAL_EX_MESSAGE, value, max);
}
/**
* <p>Validates that the specified argument is smaller than, or equal to, a
* given reference; otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.smallerOrEqualObj(myObject, refObject, "The value must be greater than the reference");</pre>
*
* @param <T> 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 <T> void smallerOrEqualObj(final Comparable<T> value, final T max, final String message, final Object... values) {
if (value.compareTo(max) > 0) {
throw new IllegalArgumentException(String.format(message, values));
}
}
/**
* <p>Validates that the specified argument is smaller than, or equal to, a
* given reference; otherwise throwing an exception.</p>
*
* <pre>Validate.smallerOrEqual(myLong, 0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* smaller than or equal to {@code max}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is smaller than, or equal to, a
* given reference; otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.smallerOrEqual(myLong, 0);</pre>
*
* @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));
}
}
/**
* <p>Validates that the specified argument is smaller than, or equal to, a
* given reference; otherwise throwing an exception.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.smallerOrEqual(myDouble, 0.0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is not
* smaller than or equal to {@code max}&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is smaller than, or equal to, a
* given reference; otherwise throwing an exception with the specified message.</p>
*
* <p>If {@code min} or {@code value} is {@code NaN}, the test will fail and
* the exception will be thrown.</p>
*
* <pre>Validate.smallerOrEqual(myDouble, 0.0);</pre>
*
* @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
//---------------------------------------------------------------------------------
/**
* <p>Validates that the specified argument is different from a given value
* (reference); otherwise throwing an exception.</p>
*
* <p>Two objects are considered different if
* {@code value.compareTo(reference) != 0}</p>
*
* <pre>Validate.differentObj(myObject, refObject);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is
* invalid&quot;.</p>
*
* @param <T> 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 <T> void differentObj(final Comparable<T> value, final T reference) {
differentObj(value, reference, DEFAULT_DIFFERENT_EX_MESSAGE, value);
}
/**
* <p>Validates that the specified argument is different from a given value
* (reference); otherwise throwing an exception with the specified message.</p>
*
* <p>Two objects are considered different if
* {@code value.compareTo(reference) != 0}</p>
*
* <pre>Validate.differentObj(myObject, refObject, "The value is invalid");</pre>
*
* @param <T> 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 <T> void differentObj(final Comparable<T> value, final T reference, final String message, final Object... values) {
if (value.compareTo(reference) == 0) {
throw new IllegalArgumentException(String.format(message, values));
}
}
/**
* <p>Validates that the specified argument is not equal to a given value
* (reference); otherwise throwing an exception.</p>
*
* <pre>Validate.different(myLong, 0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is
* invalid&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is not equal to a given value
* (reference); otherwise throwing an exception with the specified message.</p>
*
* <pre>Validate.different(myLong, 0, "The value is invalid");</pre>
*
* @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));
}
}
/**
* <p>Validates that the specified argument is not equal to a given value
* (reference); otherwise throwing an exception.</p>
*
* <p>If {@code value} or {@code reference} is {@code NaN}, no exception will be thrown.</p>
*
* <pre>Validate.different(myDouble, 0.0);</pre>
*
* <p>The message of the exception is &quot;The value {@code value} is
* invalid&quot;.</p>
*
* @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);
}
/**
* <p>Validates that the specified argument is not equal to a given value
* (reference); otherwise throwing an exception with the specified message.</p>
*
* <p>If {@code value} or {@code reference} is {@code NaN}, no exception will be thrown.</p>
*
* <pre>Validate.different(myDouble, 0.0, "The value is invalid");</pre>
*
* @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 // inclusiveBetween
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
@ -1156,7 +1921,7 @@ public class Validate {
/** /**
* Validates that the argument is an instance of the specified class, if not throws an exception. * Validates that the argument is an instance of the specified class, if not throws an exception.
* *
* <p>This method is useful when validating according to an arbitrary class</p> * <p>This method is useful when validating according to an arbitrary class</p>
* *
* <pre>Validate.isInstanceOf(OkClass.class, object);</pre> * <pre>Validate.isInstanceOf(OkClass.class, object);</pre>
@ -1207,7 +1972,7 @@ public class Validate {
/** /**
* Validates that the argument can be converted to the specified class, if not, throws an exception. * Validates that the argument can be converted to the specified class, if not, throws an exception.
* *
* <p>This method is useful when validating that there will be no casting errors.</p> * <p>This method is useful when validating that there will be no casting errors.</p>
* *
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre> * <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
@ -1231,7 +1996,7 @@ public class Validate {
/** /**
* Validates that the argument can be converted to the specified class, if not throws an exception. * Validates that the argument can be converted to the specified class, if not throws an exception.
* *
* <p>This method is useful when validating if there will be no casting errors.</p> * <p>This method is useful when validating if there will be no casting errors.</p>
* *
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre> * <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>

View File

@ -41,7 +41,7 @@ import org.junit.Test;
* Unit tests {@link org.apache.commons.lang3.Validate}. * Unit tests {@link org.apache.commons.lang3.Validate}.
*/ */
public class ValidateTest { public class ValidateTest {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@Test @Test
public void testIsTrue1() { public void testIsTrue1() {
@ -114,7 +114,7 @@ public class ValidateTest {
} catch (final NullPointerException ex) { } catch (final NullPointerException ex) {
assertEquals("The validated object is null", ex.getMessage()); assertEquals("The validated object is null", ex.getMessage());
} }
final String str = "Hi"; final String str = "Hi";
final String testStr = Validate.notNull(str); final String testStr = Validate.notNull(str);
assertSame(str, testStr); assertSame(str, testStr);
@ -131,7 +131,7 @@ public class ValidateTest {
} catch (final NullPointerException ex) { } catch (final NullPointerException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
final String str = "Hi"; final String str = "Hi";
final String testStr = Validate.notNull(str, "Message"); final String testStr = Validate.notNull(str, "Message");
assertSame(str, testStr); assertSame(str, testStr);
@ -154,7 +154,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("The validated array is empty", ex.getMessage()); assertEquals("The validated array is empty", ex.getMessage());
} }
final String[] array = new String[] {"hi"}; final String[] array = new String[] {"hi"};
final String[] test = Validate.notEmpty(array); final String[] test = Validate.notEmpty(array);
assertSame(array, test); assertSame(array, test);
@ -176,7 +176,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
final String[] array = new String[] {"hi"}; final String[] array = new String[] {"hi"};
final String[] test = Validate.notEmpty(array, "Message"); final String[] test = Validate.notEmpty(array, "Message");
assertSame(array, test); assertSame(array, test);
@ -201,7 +201,7 @@ public class ValidateTest {
} }
coll.add(Integer.valueOf(8)); coll.add(Integer.valueOf(8));
Validate.notEmpty(coll); Validate.notEmpty(coll);
final Collection<Integer> test = Validate.notEmpty(coll); final Collection<Integer> test = Validate.notEmpty(coll);
assertSame(coll, test); assertSame(coll, test);
} }
@ -224,7 +224,7 @@ public class ValidateTest {
} }
coll.add(Integer.valueOf(8)); coll.add(Integer.valueOf(8));
Validate.notEmpty(coll, "MSG"); Validate.notEmpty(coll, "MSG");
final Collection<Integer> test = Validate.notEmpty(coll, "Message"); final Collection<Integer> test = Validate.notEmpty(coll, "Message");
assertSame(coll, test); assertSame(coll, test);
} }
@ -248,7 +248,7 @@ public class ValidateTest {
} }
map.put("ll", Integer.valueOf(8)); map.put("ll", Integer.valueOf(8));
Validate.notEmpty(map); Validate.notEmpty(map);
final Map<String, Integer> test = Validate.notEmpty(map); final Map<String, Integer> test = Validate.notEmpty(map);
assertSame(map, test); assertSame(map, test);
} }
@ -271,7 +271,7 @@ public class ValidateTest {
} }
map.put("ll", Integer.valueOf(8)); map.put("ll", Integer.valueOf(8));
Validate.notEmpty(map, "MSG"); Validate.notEmpty(map, "MSG");
final Map<String, Integer> test = Validate.notEmpty(map, "Message"); final Map<String, Integer> test = Validate.notEmpty(map, "Message");
assertSame(map, test); assertSame(map, test);
} }
@ -293,7 +293,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("The validated character sequence is empty", ex.getMessage()); assertEquals("The validated character sequence is empty", ex.getMessage());
} }
final String str = "Hi"; final String str = "Hi";
final String testStr = Validate.notEmpty(str); final String testStr = Validate.notEmpty(str);
assertSame(str, testStr); assertSame(str, testStr);
@ -315,7 +315,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
final String str = "Hi"; final String str = "Hi";
final String testStr = Validate.notEmpty(str, "Message"); final String testStr = Validate.notEmpty(str, "Message");
assertSame(str, testStr); assertSame(str, testStr);
@ -556,7 +556,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("The validated array contains null element at index: 1", ex.getMessage()); assertEquals("The validated array contains null element at index: 1", ex.getMessage());
} }
array = new String[] {"a", "b"}; array = new String[] {"a", "b"};
final String[] test = Validate.noNullElements(array); final String[] test = Validate.noNullElements(array);
assertSame(array, test); assertSame(array, test);
@ -580,7 +580,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
array = new String[] {"a", "b"}; array = new String[] {"a", "b"};
final String[] test = Validate.noNullElements(array, "Message"); final String[] test = Validate.noNullElements(array, "Message");
assertSame(array, test); assertSame(array, test);
@ -607,7 +607,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("The validated collection contains null element at index: 1", ex.getMessage()); assertEquals("The validated collection contains null element at index: 1", ex.getMessage());
} }
coll.set(1, "b"); coll.set(1, "b");
final List<String> test = Validate.noNullElements(coll); final List<String> test = Validate.noNullElements(coll);
assertSame(coll, test); assertSame(coll, test);
@ -633,7 +633,7 @@ public class ValidateTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
coll.set(1, "b"); coll.set(1, "b");
final List<String> test = Validate.noNullElements(coll, "Message"); final List<String> test = Validate.noNullElements(coll, "Message");
assertSame(coll, test); assertSame(coll, test);
@ -670,7 +670,7 @@ public class ValidateTest {
} catch (final IndexOutOfBoundsException ex) { } catch (final IndexOutOfBoundsException ex) {
assertEquals("Broken: ", ex.getMessage()); assertEquals("Broken: ", ex.getMessage());
} }
final String[] strArray = new String[] {"Hi"}; final String[] strArray = new String[] {"Hi"};
final String[] test = Validate.noNullElements(strArray, "Message"); final String[] test = Validate.noNullElements(strArray, "Message");
assertSame(strArray, test); assertSame(strArray, test);
@ -693,7 +693,7 @@ public class ValidateTest {
} catch (final IndexOutOfBoundsException ex) { } catch (final IndexOutOfBoundsException ex) {
assertEquals("The validated array index is invalid: 2", ex.getMessage()); assertEquals("The validated array index is invalid: 2", ex.getMessage());
} }
final String[] strArray = new String[] {"Hi"}; final String[] strArray = new String[] {"Hi"};
final String[] test = Validate.noNullElements(strArray); final String[] test = Validate.noNullElements(strArray);
assertSame(strArray, test); assertSame(strArray, test);
@ -720,7 +720,7 @@ public class ValidateTest {
} catch (final IndexOutOfBoundsException ex) { } catch (final IndexOutOfBoundsException ex) {
assertEquals("Broken: ", ex.getMessage()); assertEquals("Broken: ", ex.getMessage());
} }
final List<String> strColl = Arrays.asList(new String[] {"Hi"}); final List<String> strColl = Arrays.asList(new String[] {"Hi"});
final List<String> test = Validate.validIndex(strColl, 0, "Message"); final List<String> test = Validate.validIndex(strColl, 0, "Message");
assertSame(strColl, test); assertSame(strColl, test);
@ -745,7 +745,7 @@ public class ValidateTest {
} catch (final IndexOutOfBoundsException ex) { } catch (final IndexOutOfBoundsException ex) {
assertEquals("The validated collection index is invalid: 2", ex.getMessage()); assertEquals("The validated collection index is invalid: 2", ex.getMessage());
} }
final List<String> strColl = Arrays.asList(new String[] {"Hi"}); final List<String> strColl = Arrays.asList(new String[] {"Hi"});
final List<String> test = Validate.validIndex(strColl, 0); final List<String> test = Validate.validIndex(strColl, 0);
assertSame(strColl, test); assertSame(strColl, test);
@ -770,7 +770,7 @@ public class ValidateTest {
} catch (final IndexOutOfBoundsException ex) { } catch (final IndexOutOfBoundsException ex) {
assertEquals("Broken: ", ex.getMessage()); assertEquals("Broken: ", ex.getMessage());
} }
final String input = "Hi"; final String input = "Hi";
final String test = Validate.validIndex(input, 0, "Message"); final String test = Validate.validIndex(input, 0, "Message");
assertSame(input, test); assertSame(input, test);
@ -793,12 +793,12 @@ public class ValidateTest {
} catch (final IndexOutOfBoundsException ex) { } catch (final IndexOutOfBoundsException ex) {
assertEquals("The validated character sequence index is invalid: 2", ex.getMessage()); assertEquals("The validated character sequence index is invalid: 2", ex.getMessage());
} }
final String input = "Hi"; final String input = "Hi";
final String test = Validate.validIndex(input, 0); final String test = Validate.validIndex(input, 0);
assertSame(input, test); assertSame(input, test);
} }
@Test @Test
public void testMatchesPattern() public void testMatchesPattern()
{ {
@ -814,7 +814,7 @@ public class ValidateTest {
assertEquals("The string hi does not match the pattern [0-9]*", e.getMessage()); assertEquals("The string hi does not match the pattern [0-9]*", e.getMessage());
} }
} }
@Test @Test
public void testMatchesPattern_withMessage() public void testMatchesPattern_withMessage()
{ {
@ -830,7 +830,700 @@ public class ValidateTest {
assertEquals("Does not match", e.getMessage()); 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 @Test
public void testInclusiveBetween() 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()); assertEquals("The value 6 is not in the specified inclusive range of 0 to 5", e.getMessage());
} }
} }
@Test @Test
public void testInclusiveBetween_withMessage() public void testInclusiveBetween_withMessage()
{ {
@ -906,7 +1599,7 @@ public class ValidateTest {
assertEquals("Error", e.getMessage()); assertEquals("Error", e.getMessage());
} }
} }
@Test @Test
public void testExclusiveBetween() 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()); assertEquals("The value 5 is not in the specified exclusive range of 0 to 5", e.getMessage());
} }
} }
@Test @Test
public void testExclusiveBetween_withMessage() public void testExclusiveBetween_withMessage()
{ {
@ -1020,7 +1713,7 @@ public class ValidateTest {
Validate.isInstanceOf(String.class, "hi"); Validate.isInstanceOf(String.class, "hi");
Validate.isInstanceOf(Integer.class, 1); Validate.isInstanceOf(Integer.class, 1);
} }
@Test @Test
public void testIsInstanceOfExceptionMessage() { public void testIsInstanceOfExceptionMessage() {
try { try {
@ -1030,7 +1723,7 @@ public class ValidateTest {
assertEquals("Expected type: java.util.List, actual: java.lang.String", e.getMessage()); assertEquals("Expected type: java.util.List, actual: java.lang.String", e.getMessage());
} }
} }
@Test @Test
public void testIsInstanceOf_withMessage() { public void testIsInstanceOf_withMessage() {
Validate.isInstanceOf(String.class, "hi", "Error"); Validate.isInstanceOf(String.class, "hi", "Error");
@ -1042,7 +1735,7 @@ public class ValidateTest {
assertEquals("Error", e.getMessage()); assertEquals("Error", e.getMessage());
} }
} }
@Test @Test
public void testIsInstanceOf_withMessageArgs() { public void testIsInstanceOf_withMessageArgs() {
Validate.isInstanceOf(String.class, "hi", "Error %s=%s", "Name", "Value"); 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()); assertEquals("Error interface java.util.List=null", e.getMessage());
} }
} }
@Test @Test
public void testIsAssignable() { public void testIsAssignable() {
Validate.isAssignableFrom(CharSequence.class, String.class); Validate.isAssignableFrom(CharSequence.class, String.class);
Validate.isAssignableFrom(AbstractList.class, ArrayList.class); Validate.isAssignableFrom(AbstractList.class, ArrayList.class);
} }
@Test @Test
public void testIsAssignableExceptionMessage() { public void testIsAssignableExceptionMessage() {
try { try {
@ -1082,7 +1775,7 @@ public class ValidateTest {
assertEquals("Cannot assign a java.lang.String to a java.util.List", e.getMessage()); assertEquals("Cannot assign a java.lang.String to a java.util.List", e.getMessage());
} }
} }
@Test @Test
public void testIsAssignable_withMessage() { public void testIsAssignable_withMessage() {
Validate.isAssignableFrom(CharSequence.class, String.class, "Error"); Validate.isAssignableFrom(CharSequence.class, String.class, "Error");