Package io.vertigo.core.lang
Class Assertion
- java.lang.Object
-
- io.vertigo.core.lang.Assertion
-
public final class Assertion extends Object
Assertions help us to build better code, with more robustness. Assertions have been introduced by B.Meyer in a language called Eiffel. An assertion allows you to design by contract. Each time an assertion fails, an specific exception is thrown. The following assertions help you to test if - an object is noT null => isNotNull - a string is not blank (and not null) =>isNotBlank - an expression is true or false =>isTrue or isFalse You can have a condition before running an assertion => when That's usefull when you want to test a pattern of an object that can be null. This assertion should be written in a fluent style to group all the assertions into a single block of code. Assertion can define a message and args. "hello {0}, an error occured on '{1}'", "foo", "bar" returns hello foo, an error occured on 'bar' You can use the simple quote ' inside the message.- Author:
- pchretien, fconstantin
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Assertion
check()
Assertion
isFalse(boolean test, String msg, Object... params)
Checks if a boolean expression is False Throws an IllegalStateException with a pretty message if not.Assertion
isNotBlank(String str)
Checks if a string is not blank (and not empty).Assertion
isNotBlank(String str, String msg, Object... params)
Checks if a string is not blank (and not empty).Assertion
isNotNull(Object o)
Checks if an object is not null.Assertion
isNotNull(Object o, String msg, Object... params)
Checks if an object is not null.Assertion
isNull(Object o)
Checks if an object is null.Assertion
isNull(Object o, String msg, Object... params)
Checks if an object is null.Assertion
isTrue(boolean test, String msg, Object... params)
Checks if a boolean expression is True Throws an IllegalStateException with a pretty message if not.Assertion
isValid(Supplier<Assertion> assertionSupplier)
Checks if an assertion supplied is valid.Assertion
when(boolean condition, Supplier<Assertion> assertionSupplier)
Evaluates an assertion when a condition is fulfilled.
-
-
-
Method Detail
-
check
public static Assertion check()
-
when
public Assertion when(boolean condition, Supplier<Assertion> assertionSupplier)
Evaluates an assertion when a condition is fulfilled.- Parameters:
condition
- the condition to check the assertionassertionSupplier
- the assertion to check when the condition is fulfilled- Returns:
- the current assertion
-
isNotNull
public Assertion isNotNull(Object o)
Checks if an object is not null. Throws the famous NullPointerException if not.- Parameters:
o
- the object- Returns:
- the current assertion
-
isNotNull
public Assertion isNotNull(Object o, String msg, Object... params)
Checks if an object is not null. Throws the famous NullPointerException with a pretty message if not.- Parameters:
o
- the objectmsg
- the messageparams
- the params of the message- Returns:
- the current assertion
-
isNull
public Assertion isNull(Object o)
Checks if an object is null. Throws an IllegalStateException if not- Parameters:
o
- the object- Returns:
- the current assertion
-
isNull
public Assertion isNull(Object o, String msg, Object... params)
Checks if an object is null. Throws an illegalStateException with a pretty message if not.- Parameters:
o
- the objectmsg
- the messageparams
- the params of the message- Returns:
- the current assertion
-
isTrue
public Assertion isTrue(boolean test, String msg, Object... params)
Checks if a boolean expression is True Throws an IllegalStateException with a pretty message if not.- Parameters:
test
- the boolean expressionmsg
- the messageparams
- the params of the message- Returns:
- the current assertion
-
isFalse
public Assertion isFalse(boolean test, String msg, Object... params)
Checks if a boolean expression is False Throws an IllegalStateException with a pretty message if not.- Parameters:
test
- the boolean expressionmsg
- the messageparams
- the params of the message- Returns:
- the current assertion
-
isNotBlank
public Assertion isNotBlank(String str)
Checks if a string is not blank (and not empty). Throws an IllegalArgumentException if not.- Parameters:
str
- the string- Returns:
- the current assertion
-
isNotBlank
public Assertion isNotBlank(String str, String msg, Object... params)
Checks if a string is not blank (and not empty). Throws an IllegalArgumentException with a pretty message if not.- Parameters:
str
- the stringmsg
- the messageparams
- the params of the message- Returns:
- the current assertion
-
-