You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gary Gregory <ga...@gmail.com> on 2016/04/25 23:38:20 UTC
Fwd: [lang] LANG-1134: New methods for lang3.Validate This closes #87
from github.
This feels to me like it all belongs in [validator]. IMO, we should not
fiddle with this class in [lang] and point to [validator].
Gary
---------- Forwarded message ----------
From: <ch...@apache.org>
Date: Sat, Apr 23, 2016 at 6:30 PM
Subject: [lang] LANG-1134: New methods for lang3.Validate This closes #87
from github.
To: commits@commons.apache.org
Repository: commons-lang
Updated Branches:
refs/heads/master 2e3fa5c25 -> 77d187eef
LANG-1134: New methods for lang3.Validate
This closes #87 from github.
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/77d187ee
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/77d187ee
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/77d187ee
Branch: refs/heads/master
Commit: 77d187eefc8596ef8203eb827486099cc8b27835
Parents: 2e3fa5c
Author: Chas Honton <ch...@apache.org>
Authored: Sat Apr 23 18:25:06 2016 -0700
Committer: Chas Honton <ch...@apache.org>
Committed: Sat Apr 23 18:28:49 2016 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/Validate.java | 771 ++++++++++++++++++-
.../org/apache/commons/lang3/ValidateTest.java | 759 +++++++++++++++++-
3 files changed, 1495 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/77d187ee/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8043fb9..9a25607 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,6 +22,7 @@
<body>
<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-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>
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/77d187ee/src/main/java/org/apache/commons/lang3/Validate.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/Validate.java
b/src/main/java/org/apache/commons/lang3/Validate.java
index f830895..2073d3f 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
+
//---------------------------------------------------------------------------------
+
+ /**
+ * <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 "The validated value is not
a
+ * number".</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 "The value is invalid:
%f".</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 "The value {@code value} is
not
+ * greater than {@code min}".</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 "The value {@code value} is
not
+ * greater than {@code min}".</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 "The value {@code value} is
not
+ * greater than {@code min}".</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 "The value {@code value} is
not
+ * greater than or equal to {@code min}".</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 "The value {@code value} is
not
+ * greater than or equal to {@code min}".</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 "The value {@code value} is
not
+ * greater than or equal to {@code min}".</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 "The value {@code value} is
not
+ * smaller than {@code max}".</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 "The value {@code value} is
not
+ * smaller than {@code max}".</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 "The value {@code value} is
not
+ * smaller than {@code max}".</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 "The value {@code value} is
not
+ * smaller than or equal to {@code max}".</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 "The value {@code value} is
not
+ * smaller than or equal to {@code max}".</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 "The value {@code value} is
not
+ * smaller than or equal to {@code max}".</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 "The value {@code value} is
+ * invalid".</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 "The value {@code value} is
+ * invalid".</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 "The value {@code value} is
+ * invalid".</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
//---------------------------------------------------------------------------------
@@ -1156,7 +1921,7 @@ public class Validate {
/**
* 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>
*
* <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.
- *
+ *
* <p>This method is useful when validating that there will be no
casting errors.</p>
*
* <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.
- *
+ *
* <p>This method is useful when validating if there will be no
casting errors.</p>
*
* <pre>Validate.isAssignableFrom(SuperClass.class,
object.getClass());</pre>
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/77d187ee/src/test/java/org/apache/commons/lang3/ValidateTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java
b/src/test/java/org/apache/commons/lang3/ValidateTest.java
index 954179c..d577ee6 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<Integer> 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<Integer> 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<String, Integer> 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<String, Integer> 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<String> 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<String> 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<String> strColl = Arrays.asList(new String[] {"Hi"});
final List<String> 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<String> strColl = Arrays.asList(new String[] {"Hi"});
final List<String> 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");
--
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory