diff --git a/spring-core/src/main/java/org/springframework/util/Assert.java b/spring-core/src/main/java/org/springframework/util/Assert.java index 828f527381..8f60f5988d 100644 --- a/spring-core/src/main/java/org/springframework/util/Assert.java +++ b/spring-core/src/main/java/org/springframework/util/Assert.java @@ -56,6 +56,22 @@ import java.util.function.Supplier; */ public abstract class Assert { + /** + * Assert a boolean expression, throwing an {@code IllegalStateException} + * if the expression evaluates to {@code false}. + *
Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException} + * on an assertion failure. + *
Assert.state(id == null, "The id property must not already be initialized");+ * @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} * 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}. - *
Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException} - * on an assertion failure. - *
Assert.state(id == null, "The id property must not already be initialized");- * @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)} */ @@ -101,6 +101,20 @@ public abstract class Assert { 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}. + *
Assert.isTrue(i > 0, "The value must be greater than zero");+ * @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} * 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}. - *
Assert.isTrue(i > 0, "The value must be greater than zero");- * @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)} */ @@ -141,6 +141,19 @@ public abstract class Assert { isTrue(expression, "[Assertion failed] - this expression must be true"); } + /** + * Assert that an object is {@code null}. + *
Assert.isNull(value, "The value must be null");+ * @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}. *
@@ -158,19 +171,6 @@ public abstract class Assert {
}
}
- /**
- * Assert that an object is {@code null}.
- * Assert.isNull(value, "The value must be null");
- * @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)}
*/
@@ -179,6 +179,19 @@ public abstract class Assert {
isNull(object, "[Assertion failed] - the object argument must be null");
}
+ /**
+ * Assert that an object is not {@code null}.
+ * Assert.notNull(clazz, "The class must not be null");
+ * @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}.
*
@@ -196,19 +209,6 @@ public abstract class Assert {
}
}
- /**
- * Assert that an object is not {@code null}.
- * Assert.notNull(clazz, "The class must not be null");
- * @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)}
*/
@@ -217,6 +217,21 @@ public abstract class Assert {
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.
+ * Assert.hasLength(name, "Name must not be empty");
+ * @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,
* 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.
- * Assert.hasLength(name, "Name must not be empty");
- * @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)}
*/
@@ -260,6 +260,21 @@ public abstract class Assert {
"[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.
+ * Assert.hasText(name, "'name' must not be empty");
+ * @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
* 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.
- * Assert.hasText(name, "'name' must not be empty");
- * @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)}
*/
@@ -303,6 +303,21 @@ public abstract class Assert {
"[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.
+ * Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
+ * @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.
*
@@ -322,21 +337,6 @@ public abstract class Assert {
}
}
- /**
- * Assert that the given text does not contain the given substring.
- * Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
- * @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)}
*/
@@ -346,6 +346,20 @@ public abstract class Assert {
() -> "[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.
+ * Assert.notEmpty(array, "The array must contain elements");
+ * @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
* {@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.
- * Assert.notEmpty(array, "The array must contain elements");
- * @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)}
*/
@@ -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");
}
+ /**
+ * Assert that an array contains no {@code null} elements.
+ * Note: Does not complain if the array is empty!
+ *
Assert.noNullElements(array, "The array must contain non-null elements");
+ * @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.
* 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.
- *
Note: Does not complain if the array is empty!
- *
Assert.noNullElements(array, "The array must contain non-null elements");
- * @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)}
*/
@@ -434,6 +434,21 @@ public abstract class Assert {
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.
+ * Assert.notEmpty(collection, "Collection must contain elements");
+ * @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
* {@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.
- * Assert.notEmpty(collection, "Collection must contain elements");
- * @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)}
*/
@@ -477,6 +477,20 @@ public abstract class Assert {
"[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.
+ * Assert.notEmpty(map, "Map must contain entries");
+ * @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}
* 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.
- * Assert.notEmpty(map, "Map must contain entries");
- * @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)}
*/
@@ -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");
}
- /**
- * Assert that the provided object is an instance of the provided class.
- *
- * Assert.instanceOf(Foo.class, foo, () -> "Processing " + Foo.class.getSimpleName() + ":");
- *
- * @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 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.instanceOf(Foo.class, foo, "Foo expected");
@@ -555,6 +536,25 @@ public abstract class Assert {
}
}
+ /**
+ * Assert that the provided object is an instance of the provided class.
+ *
+ * Assert.instanceOf(Foo.class, foo, () -> "Processing " + Foo.class.getSimpleName() + ":");
+ *
+ * @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 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.instanceOf(Foo.class, foo);
@@ -566,25 +566,6 @@ public abstract class Assert {
isInstanceOf(type, obj, "");
}
- /**
- * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
- *
- * Assert.isAssignable(Number.class, myClass, () -> "Processing " + myAttributeName + ":");
- *
- * @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 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.isAssignable(Number.class, myClass, "Number expected");
@@ -604,6 +585,25 @@ public abstract class Assert {
}
}
+ /**
+ * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
+ *
+ * Assert.isAssignable(Number.class, myClass, () -> "Processing " + myAttributeName + ":");
+ *
+ * @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 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.isAssignable(Number.class, myClass);
diff --git a/spring-core/src/test/java/org/springframework/util/AssertTests.java b/spring-core/src/test/java/org/springframework/util/AssertTests.java
index ebfc026506..cd59c40f2e 100644
--- a/spring-core/src/test/java/org/springframework/util/AssertTests.java
+++ b/spring-core/src/test/java/org/springframework/util/AssertTests.java
@@ -43,6 +43,37 @@ public class AssertTests {
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) null);
+ }
+
@Test
public void isTrueWithMessage() {
Assert.isTrue(true, "enigma");
@@ -637,23 +668,4 @@ public class AssertTests {
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) null);
- }
-
}