Package org.tudalgo.algoutils.reflect
Class MethodTester
java.lang.Object
org.tudalgo.algoutils.reflect.MethodTester
A method tester which tests properties of a class.
-
Field Summary
FieldsModifier and TypeFieldDescriptionbooleanIndicator if super class check are allowed. -
Constructor Summary
ConstructorsConstructorDescriptionMethodTester(ClassTester<?> classTester, String methodName) Constructs and initializes a method tester.MethodTester(ClassTester<?> classTester, String methodName, double similarity) Constructs and initializes a method tester.MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier) Constructs and initializes a method tester.MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType) Constructs and initializes a method tester.MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType, ArrayList<ParameterMatcher> parameters, boolean allowSuperClass, boolean looseReturnTypeChecking) Constructs and initializes a method tester.MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType, List<ParameterMatcher> parameters) Constructs and initializes a method tester.MethodTester(ClassTester<?> classTester, String methodName, double similarity, int accessModifier, Class<?> returnType, List<ParameterMatcher> parameters, boolean allowSuperClass) Constructs and initializes a method tester.MethodTester(ClassTester<?> classTester, String methodName, double similarity, Class<?> returnType, ArrayList<ParameterMatcher> parameters) Constructs and initializes a method tester. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddParameter(Class<?> type) Adds an expected parameter matchers to this tester.voidaddParameter(Class<?> type, String name, double similarity) Adds an expected parameters' matcher to this tester.voidaddParameter(ParameterMatcher... parameterMatchers) Adds an expected parameters' matcher to this tester.voidTests whether the actual access modifier count matches with the expected one.voidTests whether the class tester used to invoke the method can be resolved.voidTests whether the class tester used to invoke the method is notnull.voidassertConstructsNotUsed(List<Class<? extends spoon.reflect.code.CtCodeElement>> disallowedConstructs) Tests whether none of the blacklisted constructs where used in the constructor.voidTests whether the method can be invoked.static voidassertMethodNotNull(Method m, String name) Tests whether the specified method is notnull.voidTests whether the test method could be resolved.voidTests whether the method parameters matches with the expected parameters.static voidassertParametersMatch(Method m, String methodName, List<ParameterMatcher> parameters, boolean ignoreNames) Tests whether the method parameters match.static voidassertParametersMatch(List<ParameterMatcher> expectedParameters, List<Parameter> actualParameters, boolean ignoreNames) Tests whether the method parameters match.voidTests whether the actual return type matches the expected return type.voidassertReturnValueEquals(Object expected, Object... params) Tests whether the return value after the invocation with the specified parameters are correct.voidassertReturnValueEquals(Object expected, String additionalMessage, Object... params) Tests whether the return value after the invocation with the specified parameters are correct.Assures that the method has been resolved.booleanReturnstrueif the class tester used to invoke the method is notnulland can be resolved.static intcountMatchingParameters(Method m, String methodName, List<ParameterMatcher> parameters, boolean ignoreNames) Counts the matching parameters.static intcountMatchingParameters(List<ParameterMatcher> expectedParameters, List<Parameter> actualParameters, boolean ignoreNames) Counts the matching parameters.intReturns the expected access modifier count.getAllMethods(Class<?> clazz) Returns all fields from a class and its super classes recursively.ClassTester<?>Returns the class tester used to invoke the method.static StringgetClassTesterNullMessage(String methodName) Generates a predefined class testernullmessage.static StringgetInvalidReturnTypeMessage(String methodName) Generates a predefined message for an invalid return type.intGets the number of invocations.List<org.mockito.invocation.Invocation>Gets the invocations of the test method.Returns the method identifier containing the name of the method and the similarity to accept alternative identifiers.Generates a predefined method not found message.static StringgetMethodNotFoundMessage(String methodName) Generates a predefined method not found message.Returns the expected parameters.Object[]Returns the random valid parameter values.Class<?>Returns the expected class type of the test method.static StringgetShouldNotHaveParameterMessage(String methodName) Generates a predefined should not have parameter message.Returns the method that should be tested.Invokes test method using the class tester.booleanReturnstrueif the method can be invoked, more formally returnstrueif the class tester used to invoke the method, the method itself can be resolved.Invokes the test method with random parameters.booleanReturnstrueif subtype are allowed for the return type.booleanReturnstrueif test method is notnull.Resolves the method with the tolerances, more formally the method is searched first using its name.resolveMethod(double similarity) Resolves the method with the tolerances, more formally the method is searched first using its name.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.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.static StringsafeArrayToString(Object... array) Represents a safe string as an array.voidsetAccessModifier(int accessModifier) Sets the expected access modifier count to the new value.voidsetClassTester(ClassTester<?> classTester) Sets the class tester used to invoke the method to the specified value.voidsetLooseReturnTypeChecking(boolean looseReturnTypeChecking) Sets the boolean value whether to allow subtypes for the return type.voidsetMethodIdentifier(IdentifierMatcher methodIdentifier) Set the method identifier containing the name of the method and the similarity to accept alternative identifiers to the specified value.voidsetParameters(List<ParameterMatcher> parameters) Sets the expected parameters to the specified value.voidsetReturnType(Class<?> returnType) Sets the expected class type of the test method to the specified value.voidsetTheMethod(Method theMethod) Sets the method that should be tested to the specified value.verify()Verifies that the method was declared correctly.
-
Field Details
-
allowSuperClass
public boolean allowSuperClassIndicator 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 methodmethodName- the expected method name to testsimilarity- the minimum matching similarityaccessModifier- the expected access modifier countreturnType- the expected return type of the methodparameters- the expected parameters of the methodallowSuperClass- 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 methodmethodName- the expected method name to testsimilarity- the minimum matching similarityaccessModifier- the expected access modifier countreturnType- the expected return type of the methodparameters- 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 methodmethodName- the expected method name to testsimilarity- the minimum matching similarityaccessModifier- the expected access modifier countreturnType- the expected return type of the methodparameters- the expected parameters of the methodallowSuperClass- 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 methodmethodName- the expected method name to testsimilarity- the minimum matching similarityaccessModifier- the expected access modifier countreturnType- 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 methodmethodName- the expected method name to testsimilarity- the minimum matching similarityaccessModifier- 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 methodmethodName- the expected method name to testsimilarity- the minimum matching similarityreturnType- the expected return type of the methodparameters- the expected parameters of the method
-
MethodTester
Constructs and initializes a method tester.- Parameters:
classTester- the class tester used to invoke the methodmethodName- the expected method name to testsimilarity- the minimum matching similarity
-
MethodTester
Constructs and initializes a method tester.- Parameters:
classTester- the class tester used to invoke the methodmethodName- the expected method name to test
-
-
Method Details
-
getInvalidReturnTypeMessage
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
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 parametersactualParameters- the actual ParametersignoreNames- 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 parametersmethodName- the name of the methodparameters- the expected parametersignoreNames- 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 parametersactualParameters- the actual parametersignoreNames- 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 verifymethodName- the expected name of the methodparameters- the expected parameterignoreNames- the indicator whether to ignore parameter names
-
assertParametersMatch
public void assertParametersMatch()Tests whether the method parameters matches with the expected parameters. -
assertMethodNotNull
Tests whether the specified method is notnull.- Parameters:
m- the method to checkname- the expected method name of the method to check
-
getClassTesterNullMessage
Generates a predefined class testernullmessage.- Parameters:
methodName- the expected method name- Returns:
- a predefined class tester
nullmessage
-
safeArrayToString
Represents a safe string as an array.- Parameters:
array- the array to convert- Returns:
- the string representation of the array
-
getAllMethods
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
Returns the class tester used to invoke the method.- Returns:
- the class tester used to invoke the method
-
setClassTester
Sets the class tester used to invoke the method to the specified value.- Parameters:
classTester- the new class tester value
-
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
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
Returns the expected class type of the test method.- Returns:
- the expected class type of the test method
-
setReturnType
Sets the expected class type of the test method to the specified value.- Parameters:
returnType- the new return type
-
isLooseReturnTypeChecking
public boolean isLooseReturnTypeChecking()Returnstrueif subtype are allowed for the return type.- Returns:
trueif 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
Verifies that the method was declared correctly.- Returns:
- this method tester
-
getParameters
Returns the expected parameters.- Returns:
- the expected parameters
-
setParameters
Sets the expected parameters to the specified value.- Parameters:
parameters- the new expected parameters
-
getTheMethod
Returns the method that should be tested.- Returns:
- the method that should be tested
-
setTheMethod
Sets the method that should be tested to the specified value.- Parameters:
theMethod- the new method to be tested
-
addParameter
Adds an expected parameters' matcher to this tester.- Parameters:
parameterMatchers- the parameters' matcher to add
-
addParameter
Adds an expected parameters' matcher to this tester.- Parameters:
type- the type of the expected parameter typename- the name of the parameter to matchsimilarity- the minimum required similarity
-
addParameter
Adds an expected parameter matchers to this tester.- Parameters:
type- the type of the expected parameter type
-
getMethodNotFoundMessage
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
Generates a predefined method not found message.- Returns:
- a predefined method not found message
-
methodResolved
public boolean methodResolved()Returnstrueif test method is notnull.- Returns:
trueif test method is notnull
-
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 notnull. -
classResolved
public boolean classResolved()Returnstrueif the class tester used to invoke the method is notnulland can be resolved.- Returns:
trueif the class tester used to invoke the method is notnulland can be resolved
-
assertClassResolved
public void assertClassResolved()Tests whether the class tester used to invoke the method can be resolved. -
invokeable
public boolean invokeable()Returnstrueif the method can be invoked, more formally returnstrueif the class tester used to invoke the method, the method itself can be resolved.- Returns:
trueif the method can be invoked.
-
assertInvokeable
public void assertInvokeable()Tests whether the method can be invoked.- See Also:
-
invoke
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
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
Returns the random valid parameter values.- Returns:
- the random valid parameter values
-
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
Tests whether the return value after the invocation with the specified parameters are correct.- Parameters:
expected- the expected return valueparams- the parameters used for the invocation
-
assertReturnValueEquals
Tests whether the return value after the invocation with the specified parameters are correct.- Parameters:
expected- the expected return valueadditionalMessage- the additional message if the test failsparams- 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 tocountMatchingParameters(Method, String, List, boolean)is chosen.- Parameters:
theClass- the class to searchmethodName- the expected method namesimilarity- the minimum required similarityparameters- the expected parametersallowSuperClass- 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 tocountMatchingParameters(Method, String, List, boolean)is chosen.- Parameters:
theClass- the class to searchmethodName- the expected method namesimilarity- the minimum required similarityparameters- the expected parameters- Returns:
- the resolved method
- See Also:
-
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 tocountMatchingParameters(Method, String, List, boolean)is chosen.- Parameters:
similarity- the minimum required similarity- Returns:
- the resolved method
- See Also:
-
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 tocountMatchingParameters(Method, String, List, boolean)is chosen.- Returns:
- the resolved method
- See Also:
-
assureMethodResolved
Assures that the method has been resolved.- Returns:
- this method tester
-