Class MethodTester

java.lang.Object
org.tudalgo.algoutils.reflect.MethodTester

public class MethodTester extends Object
A method tester which tests properties of a class.
  • Field Details

    • allowSuperClass

      public boolean allowSuperClass
      Indicator if super class check are allowed.
  • Constructor Details

    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType, List<ParameterMatcher> parameters, boolean allowSuperClass)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
      similarity - the minimum matching similarity
      accessModifier - the expected access modifier count
      returnType - the expected return type of the method
      parameters - the expected parameters of the method
      allowSuperClass - the indicator @param allowSuperClass the indicator if super class check are allowed
    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType, List<ParameterMatcher> parameters)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
      similarity - the minimum matching similarity
      accessModifier - the expected access modifier count
      returnType - the expected return type of the method
      parameters - the expected parameters of the method
    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType, ArrayList<ParameterMatcher> parameters, boolean allowSuperClass, boolean looseReturnTypeChecking)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
      similarity - the minimum matching similarity
      accessModifier - the expected access modifier count
      returnType - the expected return type of the method
      parameters - the expected parameters of the method
      allowSuperClass - the indicator if super class check are allowed
    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
      similarity - the minimum matching similarity
      accessModifier - the expected access modifier count
      returnType - the expected return type of the method
    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
      similarity - the minimum matching similarity
      accessModifier - the expected access modifier count
    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName, double similarity, Class<?> returnType, ArrayList<ParameterMatcher> parameters)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
      similarity - the minimum matching similarity
      returnType - the expected return type of the method
      parameters - the expected parameters of the method
    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName, double similarity)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
      similarity - the minimum matching similarity
    • MethodTester

      public MethodTester(ClassTester<?> classTester, String methodName)
      Constructs and initializes a method tester.
      Parameters:
      classTester - the class tester used to invoke the method
      methodName - the expected method name to test
  • Method Details

    • getInvalidReturnTypeMessage

      public static String getInvalidReturnTypeMessage(String methodName)
      Generates a predefined message for an invalid return type.
      Parameters:
      methodName - the method name used in the message
      Returns:
      a predefined message for an invalid return type.
    • getShouldNotHaveParameterMessage

      public static String getShouldNotHaveParameterMessage(String methodName)
      Generates a predefined should not have parameter message.
      Parameters:
      methodName - the method name used in the message
      Returns:
      a predefined should not have parameter message
    • countMatchingParameters

      public static int countMatchingParameters(List<ParameterMatcher> expectedParameters, List<Parameter> actualParameters, boolean ignoreNames)
      Counts the matching parameters.
      Parameters:
      expectedParameters - the expected parameters
      actualParameters - the actual Parameters
      ignoreNames - the indicator whether to ignore parameter names
      Returns:
      the number of matched parameters
    • countMatchingParameters

      public static int countMatchingParameters(Method m, String methodName, List<ParameterMatcher> parameters, boolean ignoreNames)
      Counts the matching parameters.
      Parameters:
      m - the actual method with the parameters
      methodName - the name of the method
      parameters - the expected parameters
      ignoreNames - the indicator whether to ignore parameter names
      Returns:
      the number of matched parameters
    • assertParametersMatch

      public static void assertParametersMatch(List<ParameterMatcher> expectedParameters, List<Parameter> actualParameters, boolean ignoreNames)
      Tests whether the method parameters match.
      Parameters:
      expectedParameters - the expected parameters
      actualParameters - the actual parameters
      ignoreNames - the indicator whether to ignore parameter names
    • assertParametersMatch

      public static void assertParametersMatch(Method m, String methodName, List<ParameterMatcher> parameters, boolean ignoreNames)
      Tests whether the method parameters match.
      Parameters:
      m - the actual method to verify
      methodName - the expected name of the method
      parameters - the expected parameter
      ignoreNames - the indicator whether to ignore parameter names
    • assertParametersMatch

      public void assertParametersMatch()
      Tests whether the method parameters matches with the expected parameters.
    • assertMethodNotNull

      public static void assertMethodNotNull(Method m, String name)
      Tests whether the specified method is not null.
      Parameters:
      m - the method to check
      name - the expected method name of the method to check
    • getClassTesterNullMessage

      public static String getClassTesterNullMessage(String methodName)
      Generates a predefined class tester null message.
      Parameters:
      methodName - the expected method name
      Returns:
      a predefined class tester null message
    • safeArrayToString

      public static String safeArrayToString(Object... array)
      Represents a safe string as an array.
      Parameters:
      array - the array to convert
      Returns:
      the string representation of the array
    • getAllMethods

      public static List<Method> getAllMethods(Class<?> clazz)
      Returns all fields from a class and its super classes recursively.
      Parameters:
      clazz - the class to search
      Returns:
      all fields from a class and its super classes
    • getClassTester

      public ClassTester<?> getClassTester()
      Returns the class tester used to invoke the method.
      Returns:
      the class tester used to invoke the method
    • setClassTester

      public void setClassTester(ClassTester<?> classTester)
      Sets the class tester used to invoke the method to the specified value.
      Parameters:
      classTester - the new class tester value
    • getMethodIdentifier

      public IdentifierMatcher getMethodIdentifier()
      Returns the method identifier containing the name of the method and the similarity to accept alternative identifiers.
      Returns:
      method identifier containing the name of the method and the similarity to accept alternative identifiers
    • setMethodIdentifier

      public void setMethodIdentifier(IdentifierMatcher methodIdentifier)
      Set the method identifier containing the name of the method and the similarity to accept alternative identifiers to the specified value.
      Parameters:
      methodIdentifier - the new method identifier
    • getReturnType

      public Class<?> getReturnType()
      Returns the expected class type of the test method.
      Returns:
      the expected class type of the test method
    • setReturnType

      public void setReturnType(Class<?> returnType)
      Sets the expected class type of the test method to the specified value.
      Parameters:
      returnType - the new return type
    • isLooseReturnTypeChecking

      public boolean isLooseReturnTypeChecking()
      Returns true if subtype are allowed for the return type.
      Returns:
      true if subtype are allowed for the return type
    • setLooseReturnTypeChecking

      public void setLooseReturnTypeChecking(boolean looseReturnTypeChecking)
      Sets the boolean value whether to allow subtypes for the return type.
      Parameters:
      looseReturnTypeChecking - the indicator whether to allow it or not
    • assertReturnType

      public void assertReturnType()
      Tests whether the actual return type matches the expected return type.
    • verify

      public MethodTester verify()
      Verifies that the method was declared correctly.
      Returns:
      this method tester
    • getParameters

      public List<ParameterMatcher> getParameters()
      Returns the expected parameters.
      Returns:
      the expected parameters
    • setParameters

      public void setParameters(List<ParameterMatcher> parameters)
      Sets the expected parameters to the specified value.
      Parameters:
      parameters - the new expected parameters
    • getTheMethod

      public Method getTheMethod()
      Returns the method that should be tested.
      Returns:
      the method that should be tested
    • setTheMethod

      public void setTheMethod(Method theMethod)
      Sets the method that should be tested to the specified value.
      Parameters:
      theMethod - the new method to be tested
    • addParameter

      public void addParameter(ParameterMatcher... parameterMatchers)
      Adds an expected parameters' matcher to this tester.
      Parameters:
      parameterMatchers - the parameters' matcher to add
    • addParameter

      public void addParameter(Class<?> type, String name, double similarity)
      Adds an expected parameters' matcher to this tester.
      Parameters:
      type - the type of the expected parameter type
      name - the name of the parameter to match
      similarity - the minimum required similarity
    • addParameter

      public void addParameter(Class<?> type)
      Adds an expected parameter matchers to this tester.
      Parameters:
      type - the type of the expected parameter type
    • getMethodNotFoundMessage

      public static String getMethodNotFoundMessage(String methodName)
      Generates a predefined method not found message.
      Parameters:
      methodName - the name of the method used for the message
      Returns:
      a predefined method not found message
    • getMethodNotFoundMessage

      public String getMethodNotFoundMessage()
      Generates a predefined method not found message.
      Returns:
      a predefined method not found message
    • methodResolved

      public boolean methodResolved()
      Returns true if test method is not null.
      Returns:
      true if test method is not null
    • assertMethodResolved

      public void assertMethodResolved()
      Tests whether the test method could be resolved.
    • assertClassTesterNotNull

      public void assertClassTesterNotNull()
      Tests whether the class tester used to invoke the method is not null.
    • classResolved

      public boolean classResolved()
      Returns true if the class tester used to invoke the method is not null and can be resolved.
      Returns:
      true if the class tester used to invoke the method is not null and can be resolved
    • assertClassResolved

      public void assertClassResolved()
      Tests whether the class tester used to invoke the method can be resolved.
    • invokeable

      public boolean invokeable()
      Returns true if the method can be invoked, more formally returns true if the class tester used to invoke the method, the method itself can be resolved.
      Returns:
      true if the method can be invoked.
    • assertInvokeable

      public void assertInvokeable()
      Tests whether the method can be invoked.
      See Also:
    • invoke

      public Object invoke(Object... params)
      Invokes test method using the class tester.
      Parameters:
      params - the parameters of the method that should be invoked
      Returns:
      the return value of the method after its invocation
    • getInvocations

      public List<org.mockito.invocation.Invocation> getInvocations()
      Gets the invocations of the test method.
      Returns:
      the invocations of the test method
    • getInvocationCount

      public int getInvocationCount()
      Gets the number of invocations. (How often Has the test method been invoked?)
      Returns:
      the number of invocations
    • getRandomParams

      public Object[] getRandomParams()
      Returns the random valid parameter values.
      Returns:
      the random valid parameter values
    • invokeWithRandomParams

      public Object invokeWithRandomParams()
      Invokes the test method with random parameters.
      Returns:
      the return value of the method after its invocation
    • assertConstructsNotUsed

      public void assertConstructsNotUsed(List<Class<? extends spoon.reflect.code.CtCodeElement>> disallowedConstructs)
      Tests whether none of the blacklisted constructs where used in the constructor.
      Parameters:
      disallowedConstructs - the not allowed constructs in the constructor
    • assertReturnValueEquals

      public void assertReturnValueEquals(Object expected, Object... params)
      Tests whether the return value after the invocation with the specified parameters are correct.
      Parameters:
      expected - the expected return value
      params - the parameters used for the invocation
    • assertReturnValueEquals

      public void assertReturnValueEquals(Object expected, String additionalMessage, Object... params)
      Tests whether the return value after the invocation with the specified parameters are correct.
      Parameters:
      expected - the expected return value
      additionalMessage - the additional message if the test fails
      params - the parameters used for the invocation
    • getAccessModifier

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

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

      public void assertAccessModifier()
      Tests whether the actual access modifier count matches with the expected one.
    • resolveMethod

      public Method resolveMethod(Class<?> theClass, String methodName, double similarity, List<ParameterMatcher> parameters, boolean allowSuperClass)
      Resolves the method with the tolerances, more formally the method is searched first using its name. If multiple overloads are found then the method with the most matching parameters according to countMatchingParameters(Method, String, List, boolean) is chosen.
      Parameters:
      theClass - the class to search
      methodName - the expected method name
      similarity - the minimum required similarity
      parameters - the expected parameters
      allowSuperClass - the indicator whether to search in super classes as well
      Returns:
      the resolved method
      See Also:
    • resolveMethod

      public Method resolveMethod(Class<?> theClass, String methodName, double similarity, List<ParameterMatcher> parameters)
      Resolves the method with the tolerances, more formally the method is searched first using its name. If multiple overloads are found then the method with the most matching parameters according to countMatchingParameters(Method, String, List, boolean) is chosen.
      Parameters:
      theClass - the class to search
      methodName - the expected method name
      similarity - the minimum required similarity
      parameters - the expected parameters
      Returns:
      the resolved method
      See Also:
    • resolveMethod

      public Method resolveMethod(double similarity)
      Resolves the method with the tolerances, more formally the method is searched first using its name. If multiple overloads are found then the method with the most matching parameters according to countMatchingParameters(Method, String, List, boolean) is chosen.
      Parameters:
      similarity - the minimum required similarity
      Returns:
      the resolved method
      See Also:
    • resolveMethod

      public Method resolveMethod()
      Resolves the method with the tolerances, more formally the method is searched first using its name. If multiple overloads are found then the method with the most matching parameters according to countMatchingParameters(Method, String, List, boolean) is chosen.
      Returns:
      the resolved method
      See Also:
    • assureMethodResolved

      public MethodTester assureMethodResolved()
      Assures that the method has been resolved.
      Returns:
      this method tester