Class ObjectTester
- java.lang.Object
-
- patterntesting.runtime.junit.AbstractTester
-
- patterntesting.runtime.junit.ObjectTester
-
public final class ObjectTester extends AbstractTester
This is a utility class to check some important methods of a class like theObject.equals(Object)
orObject.hashCode()
method. Before v1.1 the methods are named "checkEquals" or "checkCompareTo". Since 1.1 these methods have now an "assert" prefix ("assertEquals" or "assertCompareTo").- Since:
- 1.0.3 (21.07.2010)
- Author:
- oliver
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> void
assertAll(java.lang.Class<? extends T> clazz)
Starts all known checks like checkEquals(..), checks from the SerializableTester (if the given class is serializable) or from other classes.static void
assertAll(java.lang.Package pkg)
Starts all known checks for all classes of the given package.static void
assertAll(java.lang.Package pkg, java.lang.Class<?>... excluded)
Starts all known checks for all classes of the given package except for the "excluded" classes.static <T> void
assertAll(java.util.Collection<java.lang.Class<? extends T>> classes)
Check all.static void
assertAllOfPackage(java.lang.String packageName)
Starts all known checks for all classes of the given package.static void
assertAllOfPackage(java.lang.String packageName, java.lang.Class<?>... excluded)
Starts all known checks for all classes of the given package but not for the "excluded" classes.static void
assertAllOfPackage(java.lang.String packageName, java.util.regex.Pattern... excluded)
Starts all known checks for all classes of the given package but not for the "excluded" classes.static void
assertCompareTo(java.lang.Object o1, java.lang.Object o2)
Check equality of the given objects by using the compareTo() method.static void
assertEquals(java.io.Serializable obj)
The given object will be serialized and deserialized to get a copy of that object.static void
assertEquals(java.lang.Class<?> clazz)
This method will create two objects of the given class using the default constructor.static void
assertEquals(java.lang.Cloneable obj)
The given object will be cloned to get a copy of that object.static void
assertEquals(java.lang.Object o1, java.lang.Object o2)
Check equality of the given objects.static void
assertEquals(java.lang.Package pkg)
Check for each class in the given package if the equals() method is implemented correct.static void
assertEquals(java.lang.Package pkg, java.lang.Class<?>... excluded)
Check for each class in the given package if the equals() method is implemented correct.static void
assertEquals(java.lang.Package pkg, java.util.regex.Pattern... excluded)
Check for each class in the given package if the equals() method is implemented correct.static <T> void
assertEquals(java.util.Collection<java.lang.Class<? extends T>> classes)
Check for each class in the given collection if the equals() method is implemented correct.static void
assertEqualsOfPackage(java.lang.String packageName)
Check for each class in the given package if the equals() method is implemented correct.static void
assertEqualsOfPackage(java.lang.String packageName, java.lang.Class<?>... excluded)
Check for each class in the given package if the equals() method is implemented correct.static void
assertEqualsOfPackage(java.lang.String packageName, java.util.regex.Pattern... excluded)
Check for each class in the given package if the equals() method is implemented correct.static void
assertNotEquals(java.lang.Object a, java.lang.Object b)
Checks if the two given objects are really not equals.static void
assertToString(java.lang.Object obj)
If a object is only partially initalized it sometimes can happen, that calling the toString() method will result in a NullPointerException.static boolean
hasEqualsDeclared(java.lang.Class<?> clazz)
If you want to know if a class (or one of its super classes, except object) has overwritten the equals method you can use this method here.static boolean
hasToStringDefaultImpl(java.lang.Class<?> clazz)
Normally you should overwrite the toString() method for better logging and debugging.static boolean
hasToStringDefaultImpl(java.lang.Object obj)
Normally you should overwrite the toString() method for better logging and debugging.-
Methods inherited from class patterntesting.runtime.junit.AbstractTester
removeClasses, removeClasses
-
-
-
-
Method Detail
-
assertEquals
public static void assertEquals(java.lang.Object o1, java.lang.Object o2) throws java.lang.AssertionError
Check equality of the given objects. They must be equals otherwise an AssertionError will be thrown. And if A == B also B == A must be true (commutative law, i.e. it is a symmetrical operation).If two objects are equals they must have also the same hash code (but not the other way around). This condition is also checked here.
Often programmers forget that the
Object.equals(Object)
method can be called with null as argument and should return false as result. So this case is also tested here.- Parameters:
o1
- the 1st objecto2
- the 2nd object- Throws:
java.lang.AssertionError
- if the check fails- Since:
- 1.1
-
assertNotEquals
public static void assertNotEquals(java.lang.Object a, java.lang.Object b) throws java.lang.AssertionError
Checks if the two given objects are really not equals. The following conditions should be fullfilled: if (a != b) then also (b != a) should be true. This is tested here.- Parameters:
a
- the ab
- the b- Throws:
java.lang.AssertionError
- the assertion error- Since:
- 1.5
-
assertEquals
public static void assertEquals(java.io.Serializable obj) throws java.io.NotSerializableException
The given object will be serialized and deserialized to get a copy of that object. The copy must be equals to the original object.If the check fails (e.g. if copy and original object are not equals) an
AssertionError
will be thrown.- Parameters:
obj
- the object- Throws:
java.io.NotSerializableException
- if obj is not serializable- Since:
- 1.1
-
assertEquals
public static void assertEquals(java.lang.Cloneable obj) throws java.lang.AssertionError
The given object will be cloned to get a copy of that object. The copy must be equals to the original object.- Parameters:
obj
- the obj- Throws:
java.lang.AssertionError
- the assertion error- Since:
- 1.1
-
assertEquals
public static void assertEquals(java.lang.Class<?> clazz) throws java.lang.AssertionError
This method will create two objects of the given class using the default constructor. So three preconditions must be true:- the class must not be abstract
- there must be a (public) default constructor
- it must be Cloneable, Serializable or return always the same object
- Parameters:
clazz
- the clazz- Throws:
java.lang.AssertionError
- if the check fails- Since:
- 1.1
-
assertEquals
public static <T> void assertEquals(java.util.Collection<java.lang.Class<? extends T>> classes) throws Failures
Check for each class in the given collection if the equals() method is implemented correct.- Type Parameters:
T
- the generic type- Parameters:
classes
- the classes- Throws:
Failures
- the collected assertion errors- Since:
- 1.1
-
assertEquals
public static void assertEquals(java.lang.Package pkg)
Check for each class in the given package if the equals() method is implemented correct.To get a name of a package call
Package.getPackage(String)
. But be sure that you can't get null as result. In this case useassertEqualsOfPackage(String)
.- Parameters:
pkg
- the package e.g. "patterntesting.runtime"- Since:
- 1.1
- See Also:
assertEqualsOfPackage(String)
-
assertEquals
public static void assertEquals(java.lang.Package pkg, java.lang.Class<?>... excluded)
Check for each class in the given package if the equals() method is implemented correct.To get a name of a package call
Package.getPackage(String)
. But be sure that you can't get null as result. In this case useassertEqualsOfPackage(String, Class...)
.- Parameters:
pkg
- the package e.g. "patterntesting.runtime"excluded
- classes which are excluded from the check- Since:
- 1.1
- See Also:
assertEqualsOfPackage(String, Class...)
-
assertEquals
public static void assertEquals(java.lang.Package pkg, java.util.regex.Pattern... excluded)
Check for each class in the given package if the equals() method is implemented correct. E.g. if your unit test classes ends all with "...Test" and you want to remove them from the check you can callObjectTester.assertEquals(pkg, Pattern.compile(".*Test"));
- Parameters:
pkg
- the packageexcluded
- class pattern which should be excluded from the check- Since:
- 1.6
-
assertEqualsOfPackage
public static void assertEqualsOfPackage(java.lang.String packageName)
Check for each class in the given package if the equals() method is implemented correct.This method does the same as
assertEquals(Package)
but was introduced byPackage.getPackage(String)
sometimes return null if no class of this package is loaded.- Parameters:
packageName
- the package name e.g. "patterntesting.runtime"- Since:
- 1.1
- See Also:
assertEquals(Package)
-
assertEqualsOfPackage
public static void assertEqualsOfPackage(java.lang.String packageName, java.lang.Class<?>... excluded)
Check for each class in the given package if the equals() method is implemented correct.- Parameters:
packageName
- the package name e.g. "patterntesting.runtime"excluded
- classes which should be excluded from the check- Since:
- 1.1
- See Also:
assertEqualsOfPackage(String)
-
assertEqualsOfPackage
public static void assertEqualsOfPackage(java.lang.String packageName, java.util.regex.Pattern... excluded)
Check for each class in the given package if the equals() method is implemented correct. E.g. if your unit test classes ends all with "...Test" and you want to remove them from the check you can callObjectTester.assertEqualsOfPackage("my.package", Pattern.compile(".*Test"));
- Parameters:
packageName
- the package name e.g. "patterntesting.runtime"excluded
- class pattern which should be excluded from the check- Since:
- 1.6
-
hasEqualsDeclared
public static boolean hasEqualsDeclared(java.lang.Class<?> clazz)
If you want to know if a class (or one of its super classes, except object) has overwritten the equals method you can use this method here.- Parameters:
clazz
- the clazz- Returns:
- true, if successful
-
assertCompareTo
public static void assertCompareTo(java.lang.Object o1, java.lang.Object o2) throws java.lang.AssertionError
Check equality of the given objects by using the compareTo() method. Because casting an object to the expected Comparable is awesome we provide this additional method here- Parameters:
o1
- the first object (must be of type Comparable)o2
- the second object (must be of type Comparable)- Throws:
java.lang.AssertionError
- if the check fails- Since:
- 1.1
- See Also:
ComparableTester.assertCompareTo(Comparable, Comparable)
-
assertToString
public static void assertToString(java.lang.Object obj)
If a object is only partially initalized it sometimes can happen, that calling the toString() method will result in a NullPointerException. This should not happen so there are several check methods available where you can proof it.- Parameters:
obj
- the object to be checked- Since:
- 1.1
-
hasToStringDefaultImpl
public static boolean hasToStringDefaultImpl(java.lang.Object obj)
Normally you should overwrite the toString() method for better logging and debugging. This is the method to check it.- Parameters:
obj
- the object to be checked- Returns:
- true, if object has default implementation
-
hasToStringDefaultImpl
public static boolean hasToStringDefaultImpl(java.lang.Class<?> clazz)
Normally you should overwrite the toString() method for better logging and debugging. This is the method to check it.- Parameters:
clazz
- the clazz- Returns:
- true, if object has default implementation
-
assertAll
public static <T> void assertAll(java.lang.Class<? extends T> clazz)
Starts all known checks like checkEquals(..), checks from the SerializableTester (if the given class is serializable) or from other classes.- Type Parameters:
T
- the generic type- Parameters:
clazz
- the clazz to be checked.- Since:
- 1.1
-
assertAll
public static <T> void assertAll(java.util.Collection<java.lang.Class<? extends T>> classes)
Check all.- Type Parameters:
T
- the generic type- Parameters:
classes
- the classes to be checked- Since:
- 1.1
-
assertAll
public static void assertAll(java.lang.Package pkg)
Starts all known checks for all classes of the given package.To get a name of a package call
Package.getPackage(String)
. But be sure that you can't get null as result. In this case useassertAllOfPackage(String)
.- Parameters:
pkg
- the package e.g. "patterntesting.runtime"- Since:
- 1.1
-
assertAll
public static void assertAll(java.lang.Package pkg, java.lang.Class<?>... excluded)
Starts all known checks for all classes of the given package except for the "excluded" classes.To get a name of a package call
Package.getPackage(String)
. But be sure that you can't get null as result. In this case useassertEqualsOfPackage(String, Class...)
.- Parameters:
pkg
- the package e.g. "patterntesting.runtime"excluded
- classes which are excluded from the check- Since:
- 1.1
- See Also:
assertAllOfPackage(String, Class...)
-
assertAllOfPackage
public static void assertAllOfPackage(java.lang.String packageName)
Starts all known checks for all classes of the given package.- Parameters:
packageName
- the package e.g. "patterntesting.runtime"- Since:
- 1.1
-
assertAllOfPackage
public static void assertAllOfPackage(java.lang.String packageName, java.lang.Class<?>... excluded)
Starts all known checks for all classes of the given package but not for the "excluded" classes.- Parameters:
packageName
- the package name e.g. "patterntesting.runtime"excluded
- classes which should be excluded from the check- Since:
- 1.1
- See Also:
assertAllOfPackage(String)
-
assertAllOfPackage
public static void assertAllOfPackage(java.lang.String packageName, java.util.regex.Pattern... excluded)
Starts all known checks for all classes of the given package but not for the "excluded" classes. E.g. if your unit test classes ends all with "...Test" and you want to remove them from the check you can callObjectTester.assertEqualsOfPackage("my.package", Pattern.compile(".*Test"));
- Parameters:
packageName
- the package name e.g. "patterntesting.runtime"excluded
- class pattern which should be excluded from the check- Since:
- 1.6
-
-