Class ClassTester<T>

java.lang.Object
org.tudalgo.algoutils.reflect.ClassTester<T>

public class ClassTester<T> extends Object
A class tester which tests properties of a class.
  • Constructor Details

    • ClassTester

      public ClassTester(String packageName, String className, double similarity, int accessModifier, Class<? super T> superClass, List<IdentifierMatcher> implementsInterfaces, T classInstance)
      Constructs and initializes a class tester.
      Parameters:
      packageName - the package name of the class to be tested
      className - the name of the class that should be tested
      similarity - the maximum allowed name similarity to match
      accessModifier - the expected access modifier
      superClass - the expected super class
      implementsInterfaces - the matcher used for the interfaces that are expected to be implemented
      classInstance - the class instance of the tested class
    • ClassTester

      public ClassTester(String packageName, String className, double similarity, int accessModifier, Class<? super T> superClass, ArrayList<IdentifierMatcher> implementsInterfaces)
      Constructs and initializes a class tester.
      Parameters:
      packageName - the package name of the class to be tested
      className - the name of the class that should be tested
      similarity - the maximum allowed name similarity to match
      accessModifier - the expected access modifier
      superClass - the expected super class
      implementsInterfaces - the matcher used for the interfaces that are expected to be implemented
    • ClassTester

      public ClassTester(String packageName, String className, double similarity, int accessModifier)
      Constructs and initializes a class tester.
      Parameters:
      packageName - the package name of the class to be tested
      className - the name of the class that should be tested
      similarity - the maximum allowed name similarity to match
      accessModifier - the expected access modifier
    • ClassTester

      public ClassTester(String packageName, String className, double similarity)
      Constructs and initializes a class tester.
      Parameters:
      packageName - the package name of the class to be tested
      className - the name of the class that should be tested
      similarity - the maximum allowed name similarity to match
    • ClassTester

      public ClassTester(String packageName, String className)
      Constructs and initializes a class tester.
      Parameters:
      packageName - the package name of the class to be tested
      className - the name of the class that should be tested
    • ClassTester

      public ClassTester(Class<T> clazz)
      Constructs and initializes a class tester.
      Parameters:
      clazz - the class to be tested
  • Method Details

    • getAllFields

      public static List<Field> getAllFields(Class<?> clazz)
      Returns all fields of a class and its super classes recursively.
      Parameters:
      clazz - the class to look up for its fields
      Returns:
      the found fields of a class and its super classes
    • getAllFields

      public List<Field> getAllFields()
      Returns all fields of the test class and its super classes.
      Returns:
      all fields of the test class and its super classes
    • getClassNotFoundMessage

      public static String getClassNotFoundMessage(String className)
      Generates a predefined class not found message.
      Parameters:
      className - the class name used for the message
      Returns:
      a predefined class not found message
    • getClassNotFoundMessage

      public String getClassNotFoundMessage()
      Generates a predefined class not found message.
      Returns:
      a predefined class not found message
    • getInterfaceNotImplementedMessage

      public static String getInterfaceNotImplementedMessage(String interfaceName)
      Generates a predefined interface not found message.
      Parameters:
      interfaceName - the name of the interface used for the message
      Returns:
      a predefined interface not found message
    • assertClassNotNull

      public static void assertClassNotNull(Class<?> theClass, String className)
      Tests whether the class instance is not null and fails with a proper message if it is null.
      Parameters:
      theClass - the Class
      className - the Class Name for the error Message
    • getEnumConstantMissingMessage

      public static String getEnumConstantMissingMessage(String constantName)
      Generates a predefined message for a missing enum constant.
      Parameters:
      constantName - the enum constant used for the message
      Returns:
      a predefined message for a missing enum constant
    • getRandomEnumConstant

      public static Enum<?> getRandomEnumConstant(Class<Enum<?>> enumClass, String enumClassName)
      Returns a random enum constant from the available enum constants.
      Parameters:
      enumClass - the enum instance to retrieve its enum
      enumClassName - the name of the enumeration class
      Returns:
      the random enum constant
    • getRandomEnumConstant

      public Enum<?> getRandomEnumConstant()
      Returns a random enum constant.
      Returns:
      a random enum constant
    • getDefaultValue

      public static Object getDefaultValue(Class<?> type)
      Returns the default value for the specified type.
      Parameters:
      type - the class type of the default type
      Returns:
      the default value for the specified type
    • getRandomValue

      public static Object getRandomValue(Class<?> type)
      Returns a random value for the specified type.
      Parameters:
      type - the type to generate a random value
      Returns:
      a random value for the specified type
    • generateDerivedClass

      public static <T> Class<? extends T> generateDerivedClass(Class<T> clazz, String className, String derivedClassName)
      Generates a derived class from a specified class.
      Type Parameters:
      T - the type of the class
      Parameters:
      clazz - the source class instance
      className - the source class name
      derivedClassName - the name for the derived class
      Returns:
      the generated derived class
    • findInstance

      public static <T> T findInstance(Class<? super T> clazz, String className)
      Resolves an instance of a given class (even abstract).
      Type Parameters:
      T - the type of the instance to resolve
      Parameters:
      clazz - the class to generate from the instance
      className - the class name
      Returns:
      the resolved instance
    • legacyFindInstance

      public static <T> T legacyFindInstance(Class<? super T> clazz, String className)
      Resolves an instance of a given class (even abstract).
      Type Parameters:
      T - the type of the instance to resolve
      Parameters:
      clazz - the class to generate from the instance
      className - the class name
      Returns:
      the resolved instance
    • setFieldTyped

      public static void setFieldTyped(Field field, Object obj, Object value) throws IllegalArgumentException, IllegalAccessException
      Sets the typed field to the specified value.
      Parameters:
      field - the field instance to change its content
      obj - the object instance containing the field
      value - the new value of the field
      Throws:
      IllegalAccessException - if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final; or if this Field object has no write access.
      IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails.
    • setField

      public static void setField(Object instance, Field field, Object value)
      Sets the field to the specified value.
      Parameters:
      instance - the object instance containing the field
      field - the field instance to change its content
      value - the new value of the field
    • setField

      public void setField(Field field, Object value)
      Sets a field value to the specified value.
      Parameters:
      field - the field to modify
      value - the new value of the field
    • getFieldValue

      public static Object getFieldValue(Object instance, Field field)
      Returns the content of a field.
      Parameters:
      instance - the object instance containing the field
      field - the field to retrieve its content
      Returns:
      the content of a field.
    • getFieldValue

      public Object getFieldValue(Field field)
      Returns the field content.
      Parameters:
      field - the field to access
      Returns:
      the field content
    • setFieldRandom

      public Object setFieldRandom(Field field)
      Sets a field value to a random value.
      Parameters:
      field - the field to modify
    • assertFieldEquals

      public void assertFieldEquals(Field field, Object expected, String additionalMessage)
      Tests whether the specified field has a specified content.
      Parameters:
      field - the field to check
      expected - the expected content of the field
      additionalMessage - the additional message if the test fails
    • assertFieldEquals

      public void assertFieldEquals(Field field, Object expected)
      Tests whether a field has a certain content.
      Parameters:
      field - the field to check
      expected - the expected content
    • getImplementsInterfaces

      public List<IdentifierMatcher> getImplementsInterfaces()
      Returns the implemented interfaces of the tested class.
      Returns:
      the implemented interfaces of the tested class
    • setImplementsInterfaces

      public void setImplementsInterfaces(List<IdentifierMatcher> implementsInterfaces)
      Sets the implemented class of the tested class to the specified value.
      Parameters:
      implementsInterfaces - the new implemented class of the test class
    • getSuperClass

      public Class<? super T> getSuperClass()
      Returns the super class of the tested class.
      Returns:
      the super class of the tested class
    • setSuperClass

      public void setSuperClass(Class<? super T> superClass)
      Sets the super class of the tested class to the specified value.
      Parameters:
      superClass - the new super class of the test class
    • addImplementsInterface

      public void addImplementsInterface(IdentifierMatcher interfaceMatcher)
      Adds an interface matcher used for the test class.
      Parameters:
      interfaceMatcher - the interface matcher to add
    • addImplementsInterface

      public void addImplementsInterface(String interfaceName, double similarity)
      Adds an interface matcher used for the test class.
      Parameters:
      interfaceName - the name interface matcher to add
      similarity - the maximum allowed similarity
    • addImplementsInterface

      public void addImplementsInterface(String interfaceName)
      Adds an interface matcher used for the test class.
      Parameters:
      interfaceName - the name interface matcher to add
    • getSpoon

      public spoon.Launcher getSpoon()
      Returns the spoon launcher which allows source code analysis and transformation.
      Returns:
      the spoon launcher which allows source code analysis and transformation
    • setSpoon

      public void setSpoon(spoon.Launcher spoon)
      Sets the spoon launcher which allows source code analysis and transformation to the specified value.
      Parameters:
      spoon - the new spoon launcher
    • assureSpoonLauncherModelsBuild

      public ClassTester<T> assureSpoonLauncherModelsBuild()
      Returns instance of a class tester with the needed configuration for spoon.
      Returns:
      instance of a class tester with the needed configuration for spoon
    • resolveAttribute

      public Field resolveAttribute(AttributeMatcher matcher)
      Resolves a field (attribute) with a specified matcher.
      Parameters:
      matcher - the matcher used to resolve the attribute (field)
      Returns:
      the resolved field
    • assertHasGetter

      public void assertHasGetter(Field attribute, ParameterMatcher... parameters)
      Tests whether the specified field has a Getter-Method.
      Parameters:
      attribute - the field to check
      parameters - the parameter matcher to match the field
    • assertHasSetter

      public void assertHasSetter(Field attribute, Object testValue)
      Tests whether the specified field has a Setter-Method.
      Parameters:
      attribute - the field to check
      testValue - the test value to test the Setter-Method
    • assertHasSetter

      public void assertHasSetter(Field attribute)
      Tests whether the specified field has a Setter-Method.
      Parameters:
      attribute - the field to check
    • assertImplementsInterfaces

      public void assertImplementsInterfaces(List<IdentifierMatcher> implementsInterfaces)
      Tests whether all described interfaces by the specified matchers are being extended.
      Parameters:
      implementsInterfaces - the matchers to match the criterion of extension
    • assertImplementsInterfaces

      public void assertImplementsInterfaces()
      Tests whether all described interfaces by the specified matchers are being extended.
    • assertDoesNotImplementAnyInterfaces

      public void assertDoesNotImplementAnyInterfaces()
      Tests whether there are no interface extension.
    • class_resolved

      public boolean class_resolved()
      Returns true if theClass is not null.
      Returns:
      true if theClass is not null
    • getMockingDetails

      public org.mockito.MockingDetails getMockingDetails()
      Returns the MockingDetails of theClass.
      Returns:
      the MockingDetails of theClass
    • is_mock

      public boolean is_mock()
      Returns true if theClass is mocked.
      Returns:
      true if theClass is mocked
      See Also:
      • MockingDetails.isMock()
    • is_spy

      public boolean is_spy()
      Returns true if theClass is a spy.
      Returns:
      true if theClass is a spy
      See Also:
      • MockingDetails.isSpy()
    • assureSpied

      public ClassTester<T> assureSpied()
      Makes the class a spy if not done already.
      Returns:
      this class tester
    • assertSpied

      public ClassTester<T> assertSpied()
      Tests whether this class is a spy.
      Returns:
      this class tester
    • assertClassResolved

      public void assertClassResolved()
      Tests that theClass is not null and fails with the predefined message if it cannot be resolved.
    • verify

      public ClassTester<T> verify()
      Tests whether the Class is declared correctly.
      Returns:
      this class tester
    • verify

      public ClassTester<T> verify(double minSimilarity)
      Tests whether the Class is declared correctly.
      Parameters:
      minSimilarity - the minimum required similarity
      Returns:
      this class tester
    • assertSuperclass

      public void assertSuperclass()
      Tests whether the super classes fo the test classes matches the specified super classes.
    • getTheClass

      public Class<T> getTheClass()
      Returns the test classes if it is already resolved.
      Returns:
      the test classes if it is already resolved
    • setTheClass

      public void setTheClass(Class<T> theClass)
      Sets the test class instance to the specified class instance.
      Parameters:
      theClass - the new test class instance
    • getAccessModifier

      public int getAccessModifier()
      Returns the expected access modifier.
      Returns:
      the expected access modifier
    • setAccessModifier

      public void setAccessModifier(int accessModifier)
      Sets the expected access modifier the specified value.
      Parameters:
      accessModifier - the new expected access modifier count
    • assertAccessModifier

      public void assertAccessModifier()
      Tests whether the access modifier count is correct and fails it with a predefined message if it fails.
    • getClassInstance

      public T getClassInstance()
      Returns class instance of the tested class.
      Returns:
      class instance of the tested class
    • setClassInstance

      public void setClassInstance(T classInstance)
      Sets class instance of the tested class to the specified value.
      Parameters:
      classInstance - the new value of the class instance of the tested class
    • classInstanceResolved

      public boolean classInstanceResolved()
      Returns true if the class instance of the tested class is not null.
      Returns:
      true if the class instance of the tested class is not null
    • assertclassInstanceResolved

      public void assertclassInstanceResolved()
      Tests whether the class instance of the tested class is not null.
    • assertEnumConstants

      public void assertEnumConstants(String[] expectedConstants)
      Tests whether the enum constants with the specified names exists.
      Parameters:
      expectedConstants - the name of the enum constants to check
    • getClassIdentifier

      public IdentifierMatcher getClassIdentifier()
      Returns the class matcher used on the test class.
      Returns:
      the class matcher used on the test class
    • setClassIdentifier

      public void setClassIdentifier(IdentifierMatcher classIdentifier)
      Sets the class matcher used on the test class to the new value.
      Parameters:
      classIdentifier - the new the class matcher used on the test class
    • findClass

      public Class<T> findClass(String packageName, String className, double similarity)
      Resolves a class with the specified name and similarity.
      Parameters:
      packageName - the package name of the class
      className - the name of the class to resolve
      similarity - The minimum required similarity
      Returns:
      the resolved Class With the given name and similarity
    • findClass

      public Class<T> findClass()
      Resolves a class with the current class name and similarity.
      Returns:
      the resolved class with the current class name and similarity.
    • findClass

      public Class<T> findClass(double similarity)
      Resolves a class with the specified similarity.
      Parameters:
      similarity - the minimum required similarity
      Returns:
      the resolved class with the specified similarity
    • resolveClass

      public ClassTester<T> resolveClass()
      Finds the class to test and stores it.
      Returns:
      this class tester
    • assureClassResolved

      public ClassTester<T> assureClassResolved()
      Resolves the class if necessary. (We do not care about fields being made accessible here)
      Returns:
      this class tester
    • resolve

      public ClassTester<T> resolve()
      Resolves the class and instance and stores them.
      Returns:
      this class tester
    • resolveReal

      public ClassTester<T> resolveReal()
      Resolves the class and instance and stores them.
      Returns:
      this class tester
    • resolveInstance

      public T resolveInstance()
      Resolves a test class instance. (even abstract)
      Returns:
      the test class instance
    • getNewInstance

      public T getNewInstance()
      Returns a new test class instance.
      Returns:
      the new test class instance
    • getNewRealInstance

      public T getNewRealInstance()
      Returns a new test class instance.
      Returns:
      the new test class instance
    • resolveRealInstance

      public ClassTester<T> resolveRealInstance()
      Resolve a real test class instance.
      Returns:
      this class tester
    • resolveConstructor

      public Constructor<T> resolveConstructor(List<ParameterMatcher> parameters)
      Resolves a constructor with the specified parameters.
      Parameters:
      parameters - the parameters of the constructor
      Returns:
      the best matched constructor
    • resolveConstructor

      public Constructor<T> resolveConstructor(ParameterMatcher... parameters)
      Resolves a constructor with the parameters.
      Parameters:
      parameters - the parameters of the constructor
      Returns:
      the best matched constructor
    • assertConstructorValid

      public void assertConstructorValid(Constructor<T> constructor, int accessModifier, ArrayList<ParameterMatcher> parameters)
      Tests whether a constructor was declared correctly.
      Parameters:
      constructor - the constructor to check
      accessModifier - the expected access modifier count
      parameters - the expected parameters of the constructor
    • assertConstructorValid

      public void assertConstructorValid(Constructor<T> constructor, int accessModifier, ParameterMatcher... parameters)
      Tests whether a constructor was declared correctly.
      Parameters:
      constructor - the constructor to check
      accessModifier - the expected access modifier count
      parameters - the expected parameters of the constructor
    • getEnumValue

      public static <T> Enum<?> getEnumValue(Class<Enum<?>> enumClass, String expectedName, double similarity)
      Returns the specified enum value.
      Type Parameters:
      T - the type of the enum class
      Parameters:
      enumClass - the enum class
      expectedName - the name of the enum class
      similarity - the min similarity of the name
      Returns:
      the specified enum value
    • getEnumValue

      public Enum<?> getEnumValue(String expectedName, double similarity)
      Returns a specific enum value.
      Parameters:
      expectedName - the enum class name to retrieve the value.
      similarity - the min similarity
      Returns:
      the specific enum value
    • assertIsInterface

      public static void assertIsInterface(Class<?> theClass, String className)
      Tests whether the class instance is an interface.
      Parameters:
      theClass - the class instance to check
      className - the name of the class
    • assertIsInterface

      public void assertIsInterface()
      Tests whether the test class is an interface.
    • assertIsEnum

      public static void assertIsEnum(Class<?> theClass, String className)
      Tests whether the class instance is an enum.
      Parameters:
      theClass - the class instance to check
      className - the name of the class
    • assertIsEnum

      public void assertIsEnum()
      Tests whether the test class is an enum.
    • assertIsPlainClass

      public void assertIsPlainClass()
      Tests whether the test class is a plain class.
    • assertIsPlainClass

      public static void assertIsPlainClass(Class<?> theClass, String className)
      Tests whether the class instance is a plain class.
      Parameters:
      theClass - the class instance to check
      className - the name of the class