Class TestUtils

java.lang.Object
org.tquadrat.foundation.testutil.TestUtils

@API(status=STABLE, since="0.0.5") public final class TestUtils extends Object
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"

UML Diagram for "org.tquadrat.foundation.testutil.TestUtils"

UML Diagram for "org.tquadrat.foundation.testutil.TestUtils"
  • Field Details

  • Constructor Details

    • TestUtils

      private TestUtils()
      No instance allowed for this class.
  • Method Details

    • getLiveThreads

      @API(status=STABLE, since="0.0.5") public static final Collection<Thread> getLiveThreads()
      Returns all threads that are currently alive.
      Returns:
      The living threads.
    • isAssertionOn

      @API(status=STABLE, since="0.0.5") public static final boolean 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 and org.tquadrat.test is not amongst these, or org.tquadrat.test is explicitly disabled with -da or -disableassertions, this method will return false. But even it may return true, it is possible that assertions are still not activated for some packages.
      Returns:
      true if assertions are activated for the package org.tquadrat.foundation.test and hopefully also for any other package, false otherwise.
    • isEmpty

      @API(status=STABLE, since="0.1.0") public static final boolean isEmpty(CharSequence input)
      Tests if the given String is null or the empty String.
      Parameters:
      input - The String to test.
      Returns:
      true if the given String reference is null or the empty String.
      Since:
      0.1.0
    • isEmptyOrBlank

      @API(status=STABLE, since="0.1.0") public static final boolean isEmptyOrBlank(CharSequence input)
      Tests if the given String is null, the empty String, or just containing whitespace.
      Parameters:
      input - The String to test.
      Returns:
      true if the given String reference is not null and not the empty String.
      Since:
      0.1.0
      See Also:
    • isNotEmpty

      @API(status=STABLE, since="0.1.0") public static final boolean isNotEmpty(CharSequence input)
      Tests if the given String is not null and not the empty String.
      Parameters:
      input - The String to test.
      Returns:
      true if the given String reference is not null 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 not null, 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 not null 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 uses AccessibleObject.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 uses AccessibleObject.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 object
      excludeFields - A Collection 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 uses AccessibleObject.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
      excludeFields - An array of String field names to exclude from testing; may be null.
      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 uses AccessibleObject.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 the testTransients parameter is set to true, 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 object
      testTransients - 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 uses AccessibleObject.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 the testTransients parameter is set to true, 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. A null superclass is treated as Object.
      Parameters:
      lhs - this object.
      rhs - The other object
      testTransients - true whether to include transient fields, false otherwise.
      reflectUpToClass - The superclass to reflect up to (inclusive), may be null
      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 uses AccessibleObject.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 the testTransients parameter is set to true, 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. A null superclass is treated as Object.
      Parameters:
      lhs - this object.
      rhs - The other object
      testTransients - true whether to include transient fields, false otherwise.
      reflectUpToClass - The superclass to reflect up to (inclusive), may be null
      excludeFields - An array of String field names to exclude from testing; may be null.
      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

      @API(status=STABLE, since="0.0.5") public static final <T> T requireNonNull(T a)
      Checks if the given value a is null and throws a NullPointerException if it is null; calls Objects.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 is null.
    • requireNonNull

      @API(status=STABLE, since="0.0.5") public static final <T> T requireNonNull(T a, String message)
      Checks if the given value a is null and throws a NullPointerException with the specified message if it is null. Calls Objects.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 is null.
    • requireNonNullArgument

      @API(status=STABLE, since="0.0.5") public static final <T> T requireNonNullArgument(T a, String name)
      Checks if the given argument a is null and throws a NullPointerException if it is null.
      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 or a is null.
    • requireNotEmptyArgument

      @API(status=STABLE, since="0.0.5") public static final <T> T requireNotEmptyArgument(T a, String name)
      Checks if the given argument a is null or empty and throws a NullPointerException if it is null, or a IllegalArgumentException if it is empty.

      Only Strings, arrays, Collections, and Maps will be checked on being empty.

      Because the interface Enumeration does not provide an API for the check on emptiness (hasMoreElements() will return false after all elements have been taken from the Enumeration instance), the result for arguments of this type has to be taken with caution.

      For instances of Stream, the method Stream.findAny() is used to determine if the stream has elements. Using count() 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 be null.
      name - The name of the argument; this is used for the error message.
      Returns:
      The argument if it is not null.
      Throws:
      NullPointerException - name or a is null.
      IllegalArgumentException - name or a 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

      @API(status=STABLE, since="0.0.5") public static final String toString(Object object)
      Converts the given argument object into a String, usually by calling its toString() method. If the value of the argument is null, the text "null" will be returned instead. Arrays will be converted to a string through calling the respective toString() method from Arrays (this distinguishes this implementation from {link java.util.Objects#toString(Object, String)}). Values of type Date or Calendar will be translated based on the default locale - whatever that is.
      Parameters:
      object - The object; may be null.
      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 argument object into a String, usually by calling its toString() method. If the value of the argument is null, the text provided as the nullDefault argument will be returned instead. Arrays will be converted to a string through calling the respective toString() method from Arrays (this distinguishes this implementation from {link java.util.Objects#toString(Object, String)}). Values of type Date or Calendar will be translated based on the default locale - whatever that is.
      Parameters:
      object - The object; may be null.
      nullDefault - The text that should be returned if object is null.
      Returns:
      The object's string representation.
      See Also: