Package org.tquadrat.foundation.testutil
Class TestUtils
java.lang.Object
org.tquadrat.foundation.testutil.TestUtils
Some methods that are useful in the context of testing.
- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: TestUtils.java 1105 2024-02-28 12:58:46Z tquadrat $
- Since:
- 0.1.0
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.testutil.TestUtils"
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The empty string.static final String[]
An empty array ofString
instances.private static boolean
The flag that tracks the assertion on/off status for this package.static final String
A String containing the sequence "null".static final String
The system property for the code of country for the current user: "user.country". -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final Collection
<Thread> Returns all threads that are currently alive.static final boolean
Checks whether JDK assertion is currently activated, meaning that the program was started with the command line flags-ea
or-enableassertions
.static final boolean
isEmpty
(CharSequence input) Tests if the given String isnull
or the empty String.static final boolean
isEmptyOrBlank
(CharSequence input) Tests if the given String isnull
, the empty String, or just containing whitespace.static final boolean
isNotEmpty
(CharSequence input) Tests if the given String is notnull
and not the empty String.static final boolean
isNotEmptyOrBlank
(CharSequence input) Tests if the given String is notnull
, not the empty String, and that it contains other characters than just whitespace.static final boolean
reflectionEquals
(Object lhs, Object rhs) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields.static final boolean
reflectionEquals
(Object lhs, Object rhs, boolean testTransients) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields.static final boolean
reflectionEquals
(Object lhs, Object rhs, boolean testTransients, Class<?> reflectUpToClass) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields.static final boolean
reflectionEquals
(Object lhs, Object rhs, boolean testTransients, Class<?> reflectUpToClass, String[] excludeFields) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields.static final boolean
reflectionEquals
(Object lhs, Object rhs, String[] excludeFields) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields.static final boolean
reflectionEquals
(Object lhs, Object rhs, Collection<String> excludeFields) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields.static final <T> T
requireNonNull
(T a) Checks if the given valuea
isnull
and throws aNullPointerException
if it isnull
; callsObjects.requireNonNull(Object)
internally.static final <T> T
requireNonNull
(T a, String message) Checks if the given valuea
isnull
and throws aNullPointerException
with the specified message if it isnull
.static final <T> T
requireNonNullArgument
(T a, String name) static final <T> T
requireNotEmptyArgument
(T a, String name) Checks if the given argumenta
isnull
or empty and throws aNullPointerException
if it isnull
, or aIllegalArgumentException
if it is empty.
Only Strings, arrays,Collection
s, andMap
s will be checked on being empty.
Because the interfaceEnumeration
does not provide an API for the check on emptiness (hasMoreElements()
will returnfalse
after all elements have been taken from theEnumeration
instance), the result for arguments of this type has to be taken with caution.
For instances ofStream
, the methodStream.findAny()
is used to determine if the stream has elements.private static boolean
testReflective
(Object lhs, Object rhs, Class<?> testClass, boolean useTransients, Collection<String> excludeFields) Tests the fields on the given instances on equal.static final String
static final String
-
Field Details
-
PROPERTY_USER_COUNTRY
The system property for the code of country for the current user: "user.country".- See Also:
-
m_AssertionOn
The flag that tracks the assertion on/off status for this package. -
EMPTY_STRING
The empty string.- See Also:
-
EMPTY_String_ARRAY
An empty array ofString
instances. -
NULL_STRING
A String containing the sequence "null".
-
-
Constructor Details
-
TestUtils
private TestUtils()No instance allowed for this class.
-
-
Method Details
-
getLiveThreads
Returns all threads that are currently alive.- Returns:
- The living threads.
-
isAssertionOn
Checks whether JDK assertion is currently activated, meaning that the program was started with the command line flags-ea
or-enableassertions
. If assertions are activated for some selected packages only andorg.tquadrat.test
is not amongst these, ororg.tquadrat.test
is explicitly disabled with-da
or-disableassertions
, this method will returnfalse
. But even it may returntrue
, it is possible that assertions are still not activated for some packages.- Returns:
true
if assertions are activated for the packageorg.tquadrat.foundation.test
and hopefully also for any other package,false
otherwise.
-
isEmpty
Tests if the given String isnull
or the empty String.- Parameters:
input
- The String to test.- Returns:
true
if the given String reference isnull
or the empty String.- Since:
- 0.1.0
-
isEmptyOrBlank
Tests if the given String isnull
, the empty String, or just containing whitespace.- Parameters:
input
- The String to test.- Returns:
true
if the given String reference is notnull
and not the empty String.- Since:
- 0.1.0
- See Also:
-
isNotEmpty
Tests if the given String is notnull
and not the empty String.- Parameters:
input
- The String to test.- Returns:
true
if the given String reference is notnull
and not the empty String.- Since:
- 0.1.0
-
isNotEmptyOrBlank
@API(status=STABLE, since="0.1.0") public static final boolean isNotEmptyOrBlank(CharSequence input) Tests if the given String is notnull
, not the empty String, and that it contains other characters than just whitespace.- Parameters:
input
- The String to test.- Returns:
true
if the given String reference is notnull
and not the empty String, and it contains other characters than just whitespace.- Since:
- 0.1.0
- See Also:
-
reflectionEquals
@API(status=STABLE, since="0.0.5") public static final boolean reflectionEquals(Object lhs, Object rhs) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.
Transient members will be not be tested, as they are likely derived fields, and not part of the value of the object instance.
Static fields will not be tested. Superclass fields will be included.- Parameters:
lhs
-this
object.rhs
- The other object- Returns:
true
if the two objects have tested equals,false
otherwise.- Authors:
- Apache Software Foundation, Steve Downey (steve.downey@netfolio.com), Gary Gregory, Pete Gieser, Arun Mammen Thomas
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
-
reflectionEquals
@API(status=STABLE, since="0.0.5") public static final boolean reflectionEquals(Object lhs, Object rhs, Collection<String> excludeFields) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields. This means that it will throw a security exception if run under a security manager and the permissions are not set up correctly. It is also not as efficient as testing explicitly.
Transient members will be not be tested, as they are likely derived fields, and not part of the value of the object instance.
Static fields will not be tested. Superclass fields will be included.- Parameters:
lhs
-this
object.rhs
- The other objectexcludeFields
- ACollection
of String field names to exclude from testing.- Returns:
true
if the two objects have tested equals,false
otherwise.- Authors:
- Apache Software Foundation, Steve Downey (steve.downey@netfolio.com), Gary Gregory, Pete Gieser, Arun Mammen Thomas
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
-
reflectionEquals
@API(status=STABLE, since="0.0.5") public static final boolean reflectionEquals(Object lhs, Object rhs, String[] excludeFields) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.
Transient members will be not be tested, as they are likely derived fields, and not part of the value of the object instance.
Static fields will not be tested. Superclass fields will be included.- Parameters:
lhs
-this
object.rhs
- The other objectexcludeFields
- An array of String field names to exclude from testing; may benull
.- Returns:
true
if the two objects have tested equals,false
otherwise.- Authors:
- Apache Software Foundation, Steve Downey (steve.downey@netfolio.com), Gary Gregory, Pete Gieser, Arun Mammen Thomas
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
-
reflectionEquals
@API(status=STABLE, since="0.0.5") public static final boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.
If thetestTransients
parameter is set totrue
, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of the object instance.
Static fields will not be tested. Superclass fields will be included.- Parameters:
lhs
-this
object.rhs
- The other objecttestTransients
-true
whether to include transient fields,false
otherwise.- Returns:
true
if the two objects have tested equals,false
otherwise.- Authors:
- Apache Software Foundation, Steve Downey (steve.downey@netfolio.com), Gary Gregory, Pete Gieser, Arun Mammen Thomas
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
-
reflectionEquals
@API(status=STABLE, since="0.0.5") public static final boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients, Class<?> reflectUpToClass) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.
If thetestTransients
parameter is set totrue
, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of the object instance.
Static fields will not be tested. Superclass fields will be appended up to and including the specified superclass. Anull
superclass is treated asObject
.- Parameters:
lhs
-this
object.rhs
- The other objecttestTransients
-true
whether to include transient fields,false
otherwise.reflectUpToClass
- The superclass to reflect up to (inclusive), may benull
- Returns:
true
if the two objects have tested equals,false
otherwise.- Authors:
- Apache Software Foundation, Steve Downey (steve.downey@netfolio.com), Gary Gregory, Pete Gieser, Arun Mammen Thomas
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
-
reflectionEquals
@API(status=STABLE, since="0.0.5") public static final boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients, Class<?> reflectUpToClass, String[] excludeFields) This method uses reflection to determine if the two objects are equal.
It usesAccessibleObject.setAccessible(boolean)
to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.
If thetestTransients
parameter is set totrue
, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of the object instance.
Static fields will not be tested. Superclass fields will be appended up to and including the specified superclass. Anull
superclass is treated asObject
.- Parameters:
lhs
-this
object.rhs
- The other objecttestTransients
-true
whether to include transient fields,false
otherwise.reflectUpToClass
- The superclass to reflect up to (inclusive), may benull
excludeFields
- An array of String field names to exclude from testing; may benull
.- Returns:
true
if the two objects have tested equals,false
otherwise.- Authors:
- Apache Software Foundation, Steve Downey (steve.downey@netfolio.com), Gary Gregory, Pete Gieser, Arun Mammen Thomas
- Modified by:
- Thomas Thrien (thomas.thrien@tquadrat.org)
-
requireNonNull
Checks if the given valuea
isnull
and throws aNullPointerException
if it isnull
; callsObjects.requireNonNull(Object)
internally.- Type Parameters:
T
- The type of the value to check.- Parameters:
a
- The value to check.- Returns:
- The value if it is not
null
. - Throws:
NullPointerException
-a
isnull
.
-
requireNonNull
Checks if the given valuea
isnull
and throws aNullPointerException
with the specified message if it isnull
. CallsObjects.requireNonNull(Object, String)
internally.- Type Parameters:
T
- The type of the value to check.- Parameters:
a
- The value to check.message
- The message that is set to the thrown exception.- Returns:
- The value if it is not
null
. - Throws:
NullPointerException
-a
isnull
.
-
requireNonNullArgument
@API(status=STABLE, since="0.0.5") public static final <T> T requireNonNullArgument(T a, String name) - Type Parameters:
T
- The type of the argument to check.- Parameters:
a
- The argument to check.name
- The name of the argument; this is used for the error message.- Returns:
- The argument if it is not
null
. - Throws:
IllegalArgumentException
-name
is empty.NullPointerException
-name
ora
isnull
.
-
requireNotEmptyArgument
@API(status=STABLE, since="0.0.5") public static final <T> T requireNotEmptyArgument(T a, String name) Checks if the given argumenta
isnull
or empty and throws aNullPointerException
if it isnull
, or aIllegalArgumentException
if it is empty.
Only Strings, arrays,Collection
s, andMap
s will be checked on being empty.
Because the interfaceEnumeration
does not provide an API for the check on emptiness (hasMoreElements()
will returnfalse
after all elements have been taken from theEnumeration
instance), the result for arguments of this type has to be taken with caution.
For instances ofStream
, the methodStream.findAny()
is used to determine if the stream has elements. Usingcount()
may have negative impact on performance if the argument has a large amount of elements.- Type Parameters:
T
- The type of the argument to check.- Parameters:
a
- The argument to check; may benull
.name
- The name of the argument; this is used for the error message.- Returns:
- The argument if it is not
null
. - Throws:
NullPointerException
-name
ora
isnull
.IllegalArgumentException
-name
ora
is empty.
-
testReflective
private static boolean testReflective(Object lhs, Object rhs, Class<?> testClass, boolean useTransients, Collection<String> excludeFields) Tests the fields on the given instances on equal.- Parameters:
lhs
- The left-hand object.rhs
- The right-hand object.testClass
- The class that defines the details.useTransients
-true
if to test transient fields also,false
otherwise.excludeFields
- Set of field names to exclude from testing.- Returns:
true
if all relevant fields are equal,false
otherwise.
-
toString
Converts the given argumentobject
into aString
, usually by calling itstoString()
method. If the value of the argument isnull
, the text "null" will be returned instead. Arrays will be converted to a string through calling the respectivetoString()
method fromArrays
(this distinguishes this implementation from {link java.util.Objects#toString(Object, String)}). Values of typeDate
orCalendar
will be translated based on the default locale - whatever that is.- Parameters:
object
- The object; may benull
.- Returns:
- The object's string representation.
- See Also:
-
toString
@API(status=STABLE, since="0.0.5") public static final String toString(Object object, String nullDefault) Converts the given argumentobject
into aString
, usually by calling itstoString()
method. If the value of the argument isnull
, the text provided as thenullDefault
argument will be returned instead. Arrays will be converted to a string through calling the respectivetoString()
method fromArrays
(this distinguishes this implementation from {link java.util.Objects#toString(Object, String)}). Values of typeDate
orCalendar
will be translated based on the default locale - whatever that is.- Parameters:
object
- The object; may benull
.nullDefault
- The text that should be returned ifobject
isnull
.- Returns:
- The object's string representation.
- See Also:
-