Consistent ordering of Assert variants

Issue: SPR-15196
This commit is contained in:
Juergen Hoeller 2017-02-16 15:32:07 +01:00
parent 0922943c12
commit f90cd7705f
2 changed files with 231 additions and 219 deletions

View File

@ -56,6 +56,22 @@ import java.util.function.Supplier;
*/ */
public abstract class Assert { public abstract class Assert {
/**
* Assert a boolean expression, throwing an {@code IllegalStateException}
* if the expression evaluates to {@code false}.
* <p>Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException}
* on an assertion failure.
* <pre class="code">Assert.state(id == null, "The id property must not already be initialized");</pre>
* @param expression a boolean expression
* @param message the exception message to use if the assertion fails
* @throws IllegalStateException if {@code expression} is {@code false}
*/
public static void state(boolean expression, String message) {
if (!expression) {
throw new IllegalStateException(message);
}
}
/** /**
* Assert a boolean expression, throwing an {@code IllegalStateException} * Assert a boolean expression, throwing an {@code IllegalStateException}
* if the expression evaluates to {@code false}. * if the expression evaluates to {@code false}.
@ -77,22 +93,6 @@ public abstract class Assert {
} }
} }
/**
* Assert a boolean expression, throwing an {@code IllegalStateException}
* if the expression evaluates to {@code false}.
* <p>Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException}
* on an assertion failure.
* <pre class="code">Assert.state(id == null, "The id property must not already be initialized");</pre>
* @param expression a boolean expression
* @param message the exception message to use if the assertion fails
* @throws IllegalStateException if {@code expression} is {@code false}
*/
public static void state(boolean expression, String message) {
if (!expression) {
throw new IllegalStateException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #state(boolean, String)} * @deprecated as of 4.3.7, in favor of {@link #state(boolean, String)}
*/ */
@ -101,6 +101,20 @@ public abstract class Assert {
state(expression, "[Assertion failed] - this state invariant must be true"); state(expression, "[Assertion failed] - this state invariant must be true");
} }
/**
* Assert a boolean expression, throwing an {@code IllegalArgumentException}
* if the expression evaluates to {@code false}.
* <pre class="code">Assert.isTrue(i &gt; 0, "The value must be greater than zero");</pre>
* @param expression a boolean expression
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if {@code expression} is {@code false}
*/
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert a boolean expression, throwing an {@code IllegalArgumentException} * Assert a boolean expression, throwing an {@code IllegalArgumentException}
* if the expression evaluates to {@code false}. * if the expression evaluates to {@code false}.
@ -119,20 +133,6 @@ public abstract class Assert {
} }
} }
/**
* Assert a boolean expression, throwing an {@code IllegalArgumentException}
* if the expression evaluates to {@code false}.
* <pre class="code">Assert.isTrue(i &gt; 0, "The value must be greater than zero");</pre>
* @param expression a boolean expression
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if {@code expression} is {@code false}
*/
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #isTrue(boolean, String)} * @deprecated as of 4.3.7, in favor of {@link #isTrue(boolean, String)}
*/ */
@ -141,6 +141,19 @@ public abstract class Assert {
isTrue(expression, "[Assertion failed] - this expression must be true"); isTrue(expression, "[Assertion failed] - this expression must be true");
} }
/**
* Assert that an object is {@code null}.
* <pre class="code">Assert.isNull(value, "The value must be null");</pre>
* @param object the object to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object is not {@code null}
*/
public static void isNull(Object object, String message) {
if (object != null) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that an object is {@code null}. * Assert that an object is {@code null}.
* <pre class="code"> * <pre class="code">
@ -158,19 +171,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that an object is {@code null}.
* <pre class="code">Assert.isNull(value, "The value must be null");</pre>
* @param object the object to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object is not {@code null}
*/
public static void isNull(Object object, String message) {
if (object != null) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #isNull(Object, String)} * @deprecated as of 4.3.7, in favor of {@link #isNull(Object, String)}
*/ */
@ -179,6 +179,19 @@ public abstract class Assert {
isNull(object, "[Assertion failed] - the object argument must be null"); isNull(object, "[Assertion failed] - the object argument must be null");
} }
/**
* Assert that an object is not {@code null}.
* <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
* @param object the object to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object is {@code null}
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that an object is not {@code null}. * Assert that an object is not {@code null}.
* <pre class="code"> * <pre class="code">
@ -196,19 +209,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that an object is not {@code null}.
* <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
* @param object the object to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object is {@code null}
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #notNull(Object, String)} * @deprecated as of 4.3.7, in favor of {@link #notNull(Object, String)}
*/ */
@ -217,6 +217,21 @@ public abstract class Assert {
notNull(object, "[Assertion failed] - this argument is required; it must not be null"); notNull(object, "[Assertion failed] - this argument is required; it must not be null");
} }
/**
* Assert that the given String is not empty; that is,
* it must not be {@code null} and not the empty String.
* <pre class="code">Assert.hasLength(name, "Name must not be empty");</pre>
* @param text the String to check
* @param message the exception message to use if the assertion fails
* @see StringUtils#hasLength
* @throws IllegalArgumentException if the text is empty
*/
public static void hasLength(String text, String message) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that the given String is not empty; that is, * Assert that the given String is not empty; that is,
* it must not be {@code null} and not the empty String. * it must not be {@code null} and not the empty String.
@ -236,21 +251,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that the given String is not empty; that is,
* it must not be {@code null} and not the empty String.
* <pre class="code">Assert.hasLength(name, "Name must not be empty");</pre>
* @param text the String to check
* @param message the exception message to use if the assertion fails
* @see StringUtils#hasLength
* @throws IllegalArgumentException if the text is empty
*/
public static void hasLength(String text, String message) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #hasLength(String, String)} * @deprecated as of 4.3.7, in favor of {@link #hasLength(String, String)}
*/ */
@ -260,6 +260,21 @@ public abstract class Assert {
"[Assertion failed] - this String argument must have length; it must not be null or empty"); "[Assertion failed] - this String argument must have length; it must not be null or empty");
} }
/**
* Assert that the given String contains valid text content; that is, it must not
* be {@code null} and must contain at least one non-whitespace character.
* <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
* @param text the String to check
* @param message the exception message to use if the assertion fails
* @see StringUtils#hasText
* @throws IllegalArgumentException if the text does not contain valid text content
*/
public static void hasText(String text, String message) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that the given String contains valid text content; that is, it must not * Assert that the given String contains valid text content; that is, it must not
* be {@code null} and must contain at least one non-whitespace character. * be {@code null} and must contain at least one non-whitespace character.
@ -279,21 +294,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that the given String contains valid text content; that is, it must not
* be {@code null} and must contain at least one non-whitespace character.
* <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
* @param text the String to check
* @param message the exception message to use if the assertion fails
* @see StringUtils#hasText
* @throws IllegalArgumentException if the text does not contain valid text content
*/
public static void hasText(String text, String message) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #hasText(String, String)} * @deprecated as of 4.3.7, in favor of {@link #hasText(String, String)}
*/ */
@ -303,6 +303,21 @@ public abstract class Assert {
"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
} }
/**
* Assert that the given text does not contain the given substring.
* <pre class="code">Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre>
* @param textToSearch the text to search
* @param substring the substring to find within the text
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the text contains the substring
*/
public static void doesNotContain(String textToSearch, String substring, String message) {
if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
textToSearch.contains(substring)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that the given text does not contain the given substring. * Assert that the given text does not contain the given substring.
* <pre class="code"> * <pre class="code">
@ -322,21 +337,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that the given text does not contain the given substring.
* <pre class="code">Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre>
* @param textToSearch the text to search
* @param substring the substring to find within the text
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the text contains the substring
*/
public static void doesNotContain(String textToSearch, String substring, String message) {
if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
textToSearch.contains(substring)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #doesNotContain(String, String, String)} * @deprecated as of 4.3.7, in favor of {@link #doesNotContain(String, String, String)}
*/ */
@ -346,6 +346,20 @@ public abstract class Assert {
() -> "[Assertion failed] - this String argument must not contain the substring [" + substring + "]"); () -> "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
} }
/**
* Assert that an array contains elements; that is, it must not be
* {@code null} and must contain at least one element.
* <pre class="code">Assert.notEmpty(array, "The array must contain elements");</pre>
* @param array the array to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object array is {@code null} or contains no elements
*/
public static void notEmpty(Object[] array, String message) {
if (ObjectUtils.isEmpty(array)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that an array contains elements; that is, it must not be * Assert that an array contains elements; that is, it must not be
* {@code null} and must contain at least one element. * {@code null} and must contain at least one element.
@ -364,20 +378,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that an array contains elements; that is, it must not be
* {@code null} and must contain at least one element.
* <pre class="code">Assert.notEmpty(array, "The array must contain elements");</pre>
* @param array the array to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object array is {@code null} or contains no elements
*/
public static void notEmpty(Object[] array, String message) {
if (ObjectUtils.isEmpty(array)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #notEmpty(Object[], String)} * @deprecated as of 4.3.7, in favor of {@link #notEmpty(Object[], String)}
*/ */
@ -386,6 +386,24 @@ public abstract class Assert {
notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
} }
/**
* Assert that an array contains no {@code null} elements.
* <p>Note: Does not complain if the array is empty!
* <pre class="code">Assert.noNullElements(array, "The array must contain non-null elements");</pre>
* @param array the array to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object array contains a {@code null} element
*/
public static void noNullElements(Object[] array, String message) {
if (array != null) {
for (Object element : array) {
if (element == null) {
throw new IllegalArgumentException(message);
}
}
}
}
/** /**
* Assert that an array contains no {@code null} elements. * Assert that an array contains no {@code null} elements.
* <p>Note: Does not complain if the array is empty! * <p>Note: Does not complain if the array is empty!
@ -408,24 +426,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that an array contains no {@code null} elements.
* <p>Note: Does not complain if the array is empty!
* <pre class="code">Assert.noNullElements(array, "The array must contain non-null elements");</pre>
* @param array the array to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object array contains a {@code null} element
*/
public static void noNullElements(Object[] array, String message) {
if (array != null) {
for (Object element : array) {
if (element == null) {
throw new IllegalArgumentException(message);
}
}
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #noNullElements(Object[], String)} * @deprecated as of 4.3.7, in favor of {@link #noNullElements(Object[], String)}
*/ */
@ -434,6 +434,21 @@ public abstract class Assert {
noNullElements(array, "[Assertion failed] - this array must not contain any null elements"); noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
} }
/**
* Assert that a collection contains elements; that is, it must not be
* {@code null} and must contain at least one element.
* <pre class="code">Assert.notEmpty(collection, "Collection must contain elements");</pre>
* @param collection the collection to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the collection is {@code null} or
* contains no elements
*/
public static void notEmpty(Collection<?> collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that a collection contains elements; that is, it must not be * Assert that a collection contains elements; that is, it must not be
* {@code null} and must contain at least one element. * {@code null} and must contain at least one element.
@ -453,21 +468,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that a collection contains elements; that is, it must not be
* {@code null} and must contain at least one element.
* <pre class="code">Assert.notEmpty(collection, "Collection must contain elements");</pre>
* @param collection the collection to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the collection is {@code null} or
* contains no elements
*/
public static void notEmpty(Collection<?> collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #notEmpty(Collection, String)} * @deprecated as of 4.3.7, in favor of {@link #notEmpty(Collection, String)}
*/ */
@ -477,6 +477,20 @@ public abstract class Assert {
"[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
} }
/**
* Assert that a Map contains entries; that is, it must not be {@code null}
* and must contain at least one entry.
* <pre class="code">Assert.notEmpty(map, "Map must contain entries");</pre>
* @param map the map to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
*/
public static void notEmpty(Map<?, ?> map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* Assert that a Map contains entries; that is, it must not be {@code null} * Assert that a Map contains entries; that is, it must not be {@code null}
* and must contain at least one entry. * and must contain at least one entry.
@ -495,20 +509,6 @@ public abstract class Assert {
} }
} }
/**
* Assert that a Map contains entries; that is, it must not be {@code null}
* and must contain at least one entry.
* <pre class="code">Assert.notEmpty(map, "Map must contain entries");</pre>
* @param map the map to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
*/
public static void notEmpty(Map<?, ?> map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(message);
}
}
/** /**
* @deprecated as of 4.3.7, in favor of {@link #notEmpty(Map, String)} * @deprecated as of 4.3.7, in favor of {@link #notEmpty(Map, String)}
*/ */
@ -517,25 +517,6 @@ public abstract class Assert {
notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
} }
/**
* Assert that the provided object is an instance of the provided class.
* <pre class="code">
* Assert.instanceOf(Foo.class, foo, () -&gt; "Processing " + Foo.class.getSimpleName() + ":");
* </pre>
* @param type the type to check against
* @param obj the object to check
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails. See {@link #isInstanceOf(Class, Object, String)} for details.
* @throws IllegalArgumentException if the object is not an instance of type
* @since 5.0
*/
public static void isInstanceOf(Class<?> type, Object obj, Supplier<String> messageSupplier) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
}
}
/** /**
* Assert that the provided object is an instance of the provided class. * Assert that the provided object is an instance of the provided class.
* <pre class="code">Assert.instanceOf(Foo.class, foo, "Foo expected");</pre> * <pre class="code">Assert.instanceOf(Foo.class, foo, "Foo expected");</pre>
@ -555,6 +536,25 @@ public abstract class Assert {
} }
} }
/**
* Assert that the provided object is an instance of the provided class.
* <pre class="code">
* Assert.instanceOf(Foo.class, foo, () -&gt; "Processing " + Foo.class.getSimpleName() + ":");
* </pre>
* @param type the type to check against
* @param obj the object to check
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails. See {@link #isInstanceOf(Class, Object, String)} for details.
* @throws IllegalArgumentException if the object is not an instance of type
* @since 5.0
*/
public static void isInstanceOf(Class<?> type, Object obj, Supplier<String> messageSupplier) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
}
}
/** /**
* Assert that the provided object is an instance of the provided class. * Assert that the provided object is an instance of the provided class.
* <pre class="code">Assert.instanceOf(Foo.class, foo);</pre> * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
@ -566,25 +566,6 @@ public abstract class Assert {
isInstanceOf(type, obj, ""); isInstanceOf(type, obj, "");
} }
/**
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
* <pre class="code">
* Assert.isAssignable(Number.class, myClass, () -&gt; "Processing " + myAttributeName + ":");
* </pre>
* @param superType the super type to check against
* @param subType the sub type to check
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails. See {@link #isAssignable(Class, Class, String)} for details.
* @throws IllegalArgumentException if the classes are not assignable
* @since 5.0
*/
public static void isAssignable(Class<?> superType, Class<?> subType, Supplier<String> messageSupplier) {
notNull(superType, "Super type to check against must not be null");
if (subType == null || !superType.isAssignableFrom(subType)) {
assignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
}
}
/** /**
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}. * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
* <pre class="code">Assert.isAssignable(Number.class, myClass, "Number expected");</pre> * <pre class="code">Assert.isAssignable(Number.class, myClass, "Number expected");</pre>
@ -604,6 +585,25 @@ public abstract class Assert {
} }
} }
/**
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
* <pre class="code">
* Assert.isAssignable(Number.class, myClass, () -&gt; "Processing " + myAttributeName + ":");
* </pre>
* @param superType the super type to check against
* @param subType the sub type to check
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails. See {@link #isAssignable(Class, Class, String)} for details.
* @throws IllegalArgumentException if the classes are not assignable
* @since 5.0
*/
public static void isAssignable(Class<?> superType, Class<?> subType, Supplier<String> messageSupplier) {
notNull(superType, "Super type to check against must not be null");
if (subType == null || !superType.isAssignableFrom(subType)) {
assignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
}
}
/** /**
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}. * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
* <pre class="code">Assert.isAssignable(Number.class, myClass);</pre> * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>

View File

@ -43,6 +43,37 @@ public class AssertTests {
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();
@Test
public void stateWithMessage() {
Assert.state(true, "enigma");
}
@Test
public void stateWithFalseExpressionAndMessage() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("enigma");
Assert.state(false, "enigma");
}
@Test
public void stateWithMessageSupplier() {
Assert.state(true, () -> "enigma");
}
@Test
public void stateWithFalseExpressionAndMessageSupplier() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("enigma");
Assert.state(false, () -> "enigma");
}
@Test
public void stateWithFalseExpressionAndNullMessageSupplier() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage(equalTo(null));
Assert.state(false, (Supplier<String>) null);
}
@Test @Test
public void isTrueWithMessage() { public void isTrueWithMessage() {
Assert.isTrue(true, "enigma"); Assert.isTrue(true, "enigma");
@ -637,23 +668,4 @@ public class AssertTests {
Assert.state(false, "enigma"); Assert.state(false, "enigma");
} }
@Test
public void stateWithMessageSupplier() {
Assert.state(true, () -> "enigma");
}
@Test
public void stateWithFalseExpressionAndMessageSupplier() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("enigma");
Assert.state(false, () -> "enigma");
}
@Test
public void stateWithFalseExpressionAndNullMessageSupplier() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage(equalTo(null));
Assert.state(false, (Supplier<String>) null);
}
} }