Package org.tudalgo.algoutils.reflect
Class ClassTester<T>
java.lang.Object
org.tudalgo.algoutils.reflect.ClassTester<T>
A class tester which tests properties of a class.
-
Constructor Summary
ConstructorsConstructorDescriptionClassTester(Class<T> clazz) Constructs and initializes a class tester.ClassTester(String packageName, String className) Constructs and initializes a class tester.ClassTester(String packageName, String className, double similarity) Constructs and initializes a class tester.ClassTester(String packageName, String className, double similarity, int accessModifier) Constructs and initializes a class tester.ClassTester(String packageName, String className, double similarity, int accessModifier, Class<? super T> superClass, ArrayList<IdentifierMatcher> implementsInterfaces) Constructs and initializes a class tester.ClassTester(String packageName, String className, double similarity, int accessModifier, Class<? super T> superClass, List<IdentifierMatcher> implementsInterfaces, T classInstance) Constructs and initializes a class tester. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddImplementsInterface(String interfaceName) Adds an interface matcher used for the test class.voidaddImplementsInterface(String interfaceName, double similarity) Adds an interface matcher used for the test class.voidaddImplementsInterface(IdentifierMatcher interfaceMatcher) Adds an interface matcher used for the test class.voidTests whether the access modifier count is correct and fails it with a predefined message if it fails.voidTests whether the class instance of the tested class is notnull.static voidassertClassNotNull(Class<?> theClass, String className) Tests whether the class instance is notnulland fails with a proper message if it isnull.voidTests thattheClassis notnulland fails with the predefined message if it cannot be resolved.voidassertConstructorValid(Constructor<T> constructor, int accessModifier, ArrayList<ParameterMatcher> parameters) Tests whether a constructor was declared correctly.voidassertConstructorValid(Constructor<T> constructor, int accessModifier, ParameterMatcher... parameters) Tests whether a constructor was declared correctly.voidTests whether there are no interface extension.voidassertEnumConstants(String[] expectedConstants) Tests whether the enum constants with the specified names exists.voidassertFieldEquals(Field field, Object expected) Tests whether a field has a certain content.voidassertFieldEquals(Field field, Object expected, String additionalMessage) Tests whether the specified field has a specified content.voidassertHasGetter(Field attribute, ParameterMatcher... parameters) Tests whether the specified field has a Getter-Method.voidassertHasSetter(Field attribute) Tests whether the specified field has a Setter-Method.voidassertHasSetter(Field attribute, Object testValue) Tests whether the specified field has a Setter-Method.voidTests whether all described interfaces by the specified matchers are being extended.voidassertImplementsInterfaces(List<IdentifierMatcher> implementsInterfaces) Tests whether all described interfaces by the specified matchers are being extended.voidTests whether the test class is an enum.static voidassertIsEnum(Class<?> theClass, String className) Tests whether the class instance is an enum.voidTests whether the test class is an interface.static voidassertIsInterface(Class<?> theClass, String className) Tests whether the class instance is an interface.voidTests whether the test class is a plain class.static voidassertIsPlainClass(Class<?> theClass, String className) Tests whether the class instance is a plain class.Tests whether this class is a spy.voidTests whether the super classes fo the test classes matches the specified super classes.Resolves the class if necessary.Makes the class a spy if not done already.Returns instance of a class tester with the needed configuration for spoon.booleanReturnstrueiftheClassis not null.booleanReturnstrueif the class instance of the tested class is notnull.Resolves a class with the current class name and similarity.findClass(double similarity) Resolves a class with the specified similarity.Resolves a class with the specified name and similarity.static <T> TfindInstance(Class<? super T> clazz, String className) Resolves an instance of a given class (even abstract).static <T> Class<? extends T>generateDerivedClass(Class<T> clazz, String className, String derivedClassName) Generates a derived class from a specified class.intReturns the expected access modifier.Returns all fields of the test class and its super classes.getAllFields(Class<?> clazz) Returns all fields of a class and its super classes recursively.Returns the class matcher used on the test class.Returns class instance of the tested class.Generates a predefined class not found message.static StringgetClassNotFoundMessage(String className) Generates a predefined class not found message.static ObjectgetDefaultValue(Class<?> type) Returns the default value for the specified type.static StringgetEnumConstantMissingMessage(String constantName) Generates a predefined message for a missing enum constant.static <T> Enum<?>getEnumValue(Class<Enum<?>> enumClass, String expectedName, double similarity) Returns the specified enum value.Enum<?>getEnumValue(String expectedName, double similarity) Returns a specific enum value.static ObjectgetFieldValue(Object instance, Field field) Returns the content of a field.getFieldValue(Field field) Returns the field content.Returns the implemented interfaces of the tested class.static StringgetInterfaceNotImplementedMessage(String interfaceName) Generates a predefined interface not found message.org.mockito.MockingDetailsReturns theMockingDetailsoftheClass.Returns a new test class instance.Returns a new test class instance.Enum<?>Returns a random enum constant.static Enum<?>getRandomEnumConstant(Class<Enum<?>> enumClass, String enumClassName) Returns a random enum constant from the available enum constants.static ObjectgetRandomValue(Class<?> type) Returns a random value for the specified type.spoon.LaunchergetSpoon()Returns the spoon launcher which allows source code analysis and transformation.Returns the super class of the tested class.Returns the test classes if it is already resolved.booleanis_mock()ReturnstrueiftheClassis mocked.booleanis_spy()ReturnstrueiftheClassis a spy.static <T> TlegacyFindInstance(Class<? super T> clazz, String className) Resolves an instance of a given class (even abstract).resolve()Resolves the class and instance and stores them.resolveAttribute(AttributeMatcher matcher) Resolves a field (attribute) with a specified matcher.Finds the class to test and stores it.resolveConstructor(List<ParameterMatcher> parameters) Resolves a constructor with the specified parameters.resolveConstructor(ParameterMatcher... parameters) Resolves a constructor with the parameters.Resolves a test class instance.Resolves the class and instance and stores them.Resolve a real test class instance.voidsetAccessModifier(int accessModifier) Sets the expected access modifier the specified value.voidsetClassIdentifier(IdentifierMatcher classIdentifier) Sets the class matcher used on the test class to the new value.voidsetClassInstance(T classInstance) Sets class instance of the tested class to the specified value.static voidSets the field to the specified value.voidSets a field value to the specified value.setFieldRandom(Field field) Sets a field value to a random value.static voidsetFieldTyped(Field field, Object obj, Object value) Sets the typed field to the specified value.voidsetImplementsInterfaces(List<IdentifierMatcher> implementsInterfaces) Sets the implemented class of the tested class to the specified value.voidsetSpoon(spoon.Launcher spoon) Sets the spoon launcher which allows source code analysis and transformation to the specified value.voidsetSuperClass(Class<? super T> superClass) Sets the super class of the tested class to the specified value.voidsetTheClass(Class<T> theClass) Sets the test class instance to the specified class instance.verify()Tests whether the Class is declared correctly.verify(double minSimilarity) Tests whether the Class is declared correctly.
-
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 testedclassName- the name of the class that should be testedsimilarity- the maximum allowed name similarity to matchaccessModifier- the expected access modifiersuperClass- the expected super classimplementsInterfaces- the matcher used for the interfaces that are expected to be implementedclassInstance- 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 testedclassName- the name of the class that should be testedsimilarity- the maximum allowed name similarity to matchaccessModifier- the expected access modifiersuperClass- the expected super classimplementsInterfaces- the matcher used for the interfaces that are expected to be implemented
-
ClassTester
Constructs and initializes a class tester.- Parameters:
packageName- the package name of the class to be testedclassName- the name of the class that should be testedsimilarity- the maximum allowed name similarity to matchaccessModifier- the expected access modifier
-
ClassTester
Constructs and initializes a class tester.- Parameters:
packageName- the package name of the class to be testedclassName- the name of the class that should be testedsimilarity- the maximum allowed name similarity to match
-
ClassTester
Constructs and initializes a class tester.- Parameters:
packageName- the package name of the class to be testedclassName- the name of the class that should be tested
-
ClassTester
Constructs and initializes a class tester.- Parameters:
clazz- the class to be tested
-
-
Method Details
-
getAllFields
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
Returns all fields of the test class and its super classes.- Returns:
- all fields of the test class and its super classes
-
getClassNotFoundMessage
Generates a predefined class not found message.- Parameters:
className- the class name used for the message- Returns:
- a predefined class not found message
-
getClassNotFoundMessage
Generates a predefined class not found message.- Returns:
- a predefined class not found message
-
getInterfaceNotImplementedMessage
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
Tests whether the class instance is notnulland fails with a proper message if it isnull.- Parameters:
theClass- theClassclassName- the Class Name for the error Message
-
getEnumConstantMissingMessage
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
Returns a random enum constant from the available enum constants.- Parameters:
enumClass- the enum instance to retrieve its enumenumClassName- the name of the enumeration class- Returns:
- the random enum constant
-
getRandomEnumConstant
Returns a random enum constant.- Returns:
- a random enum constant
-
getDefaultValue
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
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 instanceclassName- the source class namederivedClassName- the name for the derived class- Returns:
- the generated derived class
-
findInstance
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 instanceclassName- the class name- Returns:
- the resolved instance
-
legacyFindInstance
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 instanceclassName- 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 contentobj- the object instance containing the fieldvalue- the new value of the field- Throws:
IllegalAccessException- if thisFieldobject is enforcing Java language access control and the underlying field is either inaccessible or final; or if thisFieldobject 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
Sets the field to the specified value.- Parameters:
instance- the object instance containing the fieldfield- the field instance to change its contentvalue- the new value of the field
-
setField
Sets a field value to the specified value.- Parameters:
field- the field to modifyvalue- the new value of the field
-
getFieldValue
Returns the content of a field.- Parameters:
instance- the object instance containing the fieldfield- the field to retrieve its content- Returns:
- the content of a field.
-
getFieldValue
Returns the field content.- Parameters:
field- the field to access- Returns:
- the field content
-
setFieldRandom
Sets a field value to a random value.- Parameters:
field- the field to modify
-
assertFieldEquals
Tests whether the specified field has a specified content.- Parameters:
field- the field to checkexpected- the expected content of the fieldadditionalMessage- the additional message if the test fails
-
assertFieldEquals
Tests whether a field has a certain content.- Parameters:
field- the field to checkexpected- the expected content
-
getImplementsInterfaces
Returns the implemented interfaces of the tested class.- Returns:
- the implemented interfaces of the tested class
-
setImplementsInterfaces
Sets the implemented class of the tested class to the specified value.- Parameters:
implementsInterfaces- the new implemented class of the test class
-
getSuperClass
Returns the super class of the tested class.- Returns:
- the super class of the tested class
-
setSuperClass
Sets the super class of the tested class to the specified value.- Parameters:
superClass- the new super class of the test class
-
addImplementsInterface
Adds an interface matcher used for the test class.- Parameters:
interfaceMatcher- the interface matcher to add
-
addImplementsInterface
Adds an interface matcher used for the test class.- Parameters:
interfaceName- the name interface matcher to addsimilarity- the maximum allowed similarity
-
addImplementsInterface
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
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
Resolves a field (attribute) with a specified matcher.- Parameters:
matcher- the matcher used to resolve the attribute (field)- Returns:
- the resolved field
-
assertHasGetter
Tests whether the specified field has a Getter-Method.- Parameters:
attribute- the field to checkparameters- the parameter matcher to match the field
-
assertHasSetter
Tests whether the specified field has a Setter-Method.- Parameters:
attribute- the field to checktestValue- the test value to test the Setter-Method
-
assertHasSetter
Tests whether the specified field has a Setter-Method.- Parameters:
attribute- the field to check
-
assertImplementsInterfaces
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()ReturnstrueiftheClassis not null.- Returns:
trueiftheClassis not null
-
getMockingDetails
public org.mockito.MockingDetails getMockingDetails()Returns theMockingDetailsoftheClass.- Returns:
- the
MockingDetailsoftheClass
-
is_mock
public boolean is_mock()ReturnstrueiftheClassis mocked.- Returns:
trueiftheClassis mocked- See Also:
-
MockingDetails.isMock()
-
is_spy
public boolean is_spy()ReturnstrueiftheClassis a spy.- Returns:
trueiftheClassis a spy- See Also:
-
MockingDetails.isSpy()
-
assureSpied
Makes the class a spy if not done already.- Returns:
- this class tester
-
assertSpied
Tests whether this class is a spy.- Returns:
- this class tester
-
assertClassResolved
public void assertClassResolved()Tests thattheClassis notnulland fails with the predefined message if it cannot be resolved. -
verify
Tests whether the Class is declared correctly.- Returns:
- this class tester
-
verify
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
Returns the test classes if it is already resolved.- Returns:
- the test classes if it is already resolved
-
setTheClass
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
Returns class instance of the tested class.- Returns:
- class instance of the tested class
-
setClassInstance
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()Returnstrueif the class instance of the tested class is notnull.- Returns:
trueif the class instance of the tested class is notnull
-
assertclassInstanceResolved
public void assertclassInstanceResolved()Tests whether the class instance of the tested class is notnull. -
assertEnumConstants
Tests whether the enum constants with the specified names exists.- Parameters:
expectedConstants- the name of the enum constants to check
-
getClassIdentifier
Returns the class matcher used on the test class.- Returns:
- the class matcher used on the test class
-
setClassIdentifier
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
Resolves a class with the specified name and similarity.- Parameters:
packageName- the package name of the classclassName- the name of the class to resolvesimilarity- The minimum required similarity- Returns:
- the resolved Class With the given name and similarity
-
findClass
Resolves a class with the current class name and similarity.- Returns:
- the resolved class with the current class name and similarity.
-
findClass
Resolves a class with the specified similarity.- Parameters:
similarity- the minimum required similarity- Returns:
- the resolved class with the specified similarity
-
resolveClass
Finds the class to test and stores it.- Returns:
- this class tester
-
assureClassResolved
Resolves the class if necessary. (We do not care about fields being made accessible here)- Returns:
- this class tester
-
resolve
Resolves the class and instance and stores them.- Returns:
- this class tester
-
resolveReal
Resolves the class and instance and stores them.- Returns:
- this class tester
-
resolveInstance
Resolves a test class instance. (even abstract)- Returns:
- the test class instance
-
getNewInstance
Returns a new test class instance.- Returns:
- the new test class instance
-
getNewRealInstance
Returns a new test class instance.- Returns:
- the new test class instance
-
resolveRealInstance
Resolve a real test class instance.- Returns:
- this class tester
-
resolveConstructor
Resolves a constructor with the specified parameters.- Parameters:
parameters- the parameters of the constructor- Returns:
- the best matched constructor
-
resolveConstructor
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 checkaccessModifier- the expected access modifier countparameters- 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 checkaccessModifier- the expected access modifier countparameters- 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 classexpectedName- the name of the enum classsimilarity- the min similarity of the name- Returns:
- the specified enum value
-
getEnumValue
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
Tests whether the class instance is an interface.- Parameters:
theClass- the class instance to checkclassName- the name of the class
-
assertIsInterface
public void assertIsInterface()Tests whether the test class is an interface. -
assertIsEnum
Tests whether the class instance is an enum.- Parameters:
theClass- the class instance to checkclassName- 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
Tests whether the class instance is a plain class.- Parameters:
theClass- the class instance to checkclassName- the name of the class
-