Class Preconditions
- java.lang.Object
-
- org.junit.platform.commons.util.Preconditions
-
@API(status=INTERNAL, since="1.0") public final class Preconditions extends Object
Collection of utilities for asserting preconditions for method and constructor arguments.Each method in this class throws a
PreconditionViolationException
if the precondition is violated.DISCLAIMER
These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!
- Since:
- 1.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
condition(boolean predicate, String message)
Assert that the suppliedpredicate
istrue
.static void
condition(boolean predicate, Supplier<String> messageSupplier)
Assert that the suppliedpredicate
istrue
.static <T> T[]
containsNoNullElements(T[] array, String message)
Assert that the supplied array contains nonull
elements.static <T> T[]
containsNoNullElements(T[] array, Supplier<String> messageSupplier)
Assert that the supplied array contains nonull
elements.static <T extends Collection<?>>
TcontainsNoNullElements(T collection, String message)
Assert that the supplied collection contains nonull
elements.static <T extends Collection<?>>
TcontainsNoNullElements(T collection, Supplier<String> messageSupplier)
Assert that the supplied collection contains nonull
elements.static String
notBlank(String str, String message)
Assert that the suppliedString
is not blank.static String
notBlank(String str, Supplier<String> messageSupplier)
Assert that the suppliedString
is not blank.static <T> T[]
notEmpty(T[] array, String message)
Assert that the supplied array is neithernull
nor empty.static <T> T[]
notEmpty(T[] array, Supplier<String> messageSupplier)
Assert that the supplied array is neithernull
nor empty.static <T extends Collection<?>>
TnotEmpty(T collection, String message)
Assert that the suppliedCollection
is neithernull
nor empty.static <T extends Collection<?>>
TnotEmpty(T collection, Supplier<String> messageSupplier)
Assert that the suppliedCollection
is neithernull
nor empty.static <T> T
notNull(T object, String message)
Assert that the suppliedObject
is notnull
.static <T> T
notNull(T object, Supplier<String> messageSupplier)
Assert that the suppliedObject
is notnull
.
-
-
-
Method Detail
-
notNull
public static <T> T notNull(T object, String message) throws PreconditionViolationException
Assert that the suppliedObject
is notnull
.- Parameters:
object
- the object to checkmessage
- precondition violation message- Returns:
- the supplied object as a convenience
- Throws:
PreconditionViolationException
- if the supplied object isnull
- See Also:
notNull(Object, Supplier)
-
notNull
public static <T> T notNull(T object, Supplier<String> messageSupplier) throws PreconditionViolationException
Assert that the suppliedObject
is notnull
.- Parameters:
object
- the object to checkmessageSupplier
- precondition violation message supplier- Returns:
- the supplied object as a convenience
- Throws:
PreconditionViolationException
- if the supplied object isnull
- See Also:
condition(boolean, Supplier)
-
notEmpty
public static <T> T[] notEmpty(T[] array, String message) throws PreconditionViolationException
Assert that the supplied array is neithernull
nor empty.WARNING: this method does NOT check if the supplied array contains any
null
elements.- Parameters:
array
- the array to checkmessage
- precondition violation message- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException
- if the supplied array isnull
or empty- See Also:
containsNoNullElements(Object[], String)
,condition(boolean, String)
-
notEmpty
public static <T> T[] notEmpty(T[] array, Supplier<String> messageSupplier) throws PreconditionViolationException
Assert that the supplied array is neithernull
nor empty.WARNING: this method does NOT check if the supplied array contains any
null
elements.- Parameters:
array
- the array to checkmessageSupplier
- precondition violation message supplier- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException
- if the supplied array isnull
or empty- See Also:
containsNoNullElements(Object[], String)
,condition(boolean, String)
-
notEmpty
public static <T extends Collection<?>> T notEmpty(T collection, String message) throws PreconditionViolationException
Assert that the suppliedCollection
is neithernull
nor empty.WARNING: this method does NOT check if the supplied collection contains any
null
elements.- Parameters:
collection
- the collection to checkmessage
- precondition violation message- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException
- if the supplied collection isnull
or empty- See Also:
containsNoNullElements(Collection, String)
,condition(boolean, String)
-
notEmpty
public static <T extends Collection<?>> T notEmpty(T collection, Supplier<String> messageSupplier) throws PreconditionViolationException
Assert that the suppliedCollection
is neithernull
nor empty.WARNING: this method does NOT check if the supplied collection contains any
null
elements.- Parameters:
collection
- the collection to checkmessageSupplier
- precondition violation message supplier- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException
- if the supplied collection isnull
or empty- See Also:
containsNoNullElements(Collection, String)
,condition(boolean, String)
-
containsNoNullElements
public static <T> T[] containsNoNullElements(T[] array, String message) throws PreconditionViolationException
Assert that the supplied array contains nonull
elements.WARNING: this method does NOT check if the supplied array is
null
or empty.- Parameters:
array
- the array to checkmessage
- precondition violation message- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException
- if the supplied array contains anynull
elements- See Also:
notNull(Object, String)
-
containsNoNullElements
public static <T> T[] containsNoNullElements(T[] array, Supplier<String> messageSupplier) throws PreconditionViolationException
Assert that the supplied array contains nonull
elements.WARNING: this method does NOT check if the supplied array is
null
or empty.- Parameters:
array
- the array to checkmessageSupplier
- precondition violation message supplier- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException
- if the supplied array contains anynull
elements- See Also:
notNull(Object, String)
-
containsNoNullElements
public static <T extends Collection<?>> T containsNoNullElements(T collection, String message) throws PreconditionViolationException
Assert that the supplied collection contains nonull
elements.WARNING: this method does NOT check if the supplied collection is
null
or empty.- Parameters:
collection
- the collection to checkmessage
- precondition violation message- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException
- if the supplied collection contains anynull
elements- See Also:
notNull(Object, String)
-
containsNoNullElements
public static <T extends Collection<?>> T containsNoNullElements(T collection, Supplier<String> messageSupplier) throws PreconditionViolationException
Assert that the supplied collection contains nonull
elements.WARNING: this method does NOT check if the supplied collection is
null
or empty.- Parameters:
collection
- the collection to checkmessageSupplier
- precondition violation message supplier- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException
- if the supplied collection contains anynull
elements- See Also:
notNull(Object, String)
-
notBlank
public static String notBlank(String str, String message) throws PreconditionViolationException
Assert that the suppliedString
is not blank.A
String
is blank if it isnull
or consists only of whitespace characters.- Parameters:
str
- the string to checkmessage
- precondition violation message- Returns:
- the supplied string as a convenience
- Throws:
PreconditionViolationException
- if the supplied string is blank- See Also:
notBlank(String, Supplier)
-
notBlank
public static String notBlank(String str, Supplier<String> messageSupplier) throws PreconditionViolationException
Assert that the suppliedString
is not blank.A
String
is blank if it isnull
or consists only of whitespace characters.- Parameters:
str
- the string to checkmessageSupplier
- precondition violation message supplier- Returns:
- the supplied string as a convenience
- Throws:
PreconditionViolationException
- if the supplied string is blank- See Also:
StringUtils.isNotBlank(String)
,condition(boolean, Supplier)
-
condition
public static void condition(boolean predicate, String message) throws PreconditionViolationException
Assert that the suppliedpredicate
istrue
.- Parameters:
predicate
- the predicate to checkmessage
- precondition violation message- Throws:
PreconditionViolationException
- if the predicate isfalse
- See Also:
condition(boolean, Supplier)
-
condition
public static void condition(boolean predicate, Supplier<String> messageSupplier) throws PreconditionViolationException
Assert that the suppliedpredicate
istrue
.- Parameters:
predicate
- the predicate to checkmessageSupplier
- precondition violation message supplier- Throws:
PreconditionViolationException
- if the predicate isfalse
-
-