patterntesting.runtime.junit
Class ObjectTester

Package class diagram package ObjectTester
java.lang.Object
  extended by patterntesting.runtime.junit.ObjectTester

public final class ObjectTester
extends Object

This is a utility class to check some important methods of a class like the. Object.equals(Object) or Object.hashCode() method. Before v1.1 the methods are named "checkEquals" or "checkCompareTo". Since 1.1 these methods will have now an "assert" prefix ("assertEquals" or "assertCompareTo").

Since:
1.0.3 (21.07.2010)
Author:
oliver

Method Summary
static void assertAll(Class<?> clazz)
          Starts all known checks like checkEquals(..), checks from the SerializableTester (if the given class is serializable) or from other classes.
static void assertAll(Collection<Class<?>> classes)
          Check all.
static void assertAll(Package pkg)
          Starts all known checks for all classes of the given package.
static void assertAll(Package pkg, Class<?>... excluded)
          Starts all known checks for all classes of the given package except for the "excluded" classes.
static void assertAllOfPackage(String packageName)
          Starts all known checks for all classes of the given package.
static void assertAllOfPackage(String packageName, Class<?>... excluded)
          Starts all known checks for all classes of the given package but not for the "excluded" classes.
static void assertAllOfPackage(String packageName, List<Class<?>> excluded)
          Starts all known checks for all classes of the given package but not for the "excluded" classes.
static void assertCompareTo(Object o1, Object o2)
          Check equality of the given objects by using the compareTo() method.
static void assertEquals(Class<?> clazz)
          This method will create two objects of the given class using the default constructor.
static void assertEquals(Cloneable obj)
          The given object will be cloned to get a copy of that object.
static void assertEquals(Collection<Class<?>> classes)
          Check for each class in the given collection if the equals() method is implemented correct.
static void assertEquals(Object o1, Object o2)
          Check equality of the given objects.
static void assertEquals(Package pkg)
          Check for each class in the given package if the equals() method is implemented correct.
static void assertEquals(Package pkg, Class<?>... excluded)
          Check for each class in the given package if the equals() method is implemented correct.
static void assertEquals(Serializable obj)
          The given object will be serialized and deserialized to get a copy of that object.
static void assertEqualsOfPackage(Package pkg, List<Class<?>> excluded)
          Check for each class in the given package if the equals() method is implemented correct.
static void assertEqualsOfPackage(String packageName)
          Check for each class in the given package if the equals() method is implemented correct.
static void assertEqualsOfPackage(String packageName, Class<?>... excluded)
          Check for each class in the given package if the equals() method is implemented correct.
static void assertEqualsOfPackage(String packageName, List<Class<?>> excluded)
          Check for each class in the given package if the equals() method is implemented correct.
static void assertEqualsWithClone(Class<?> clazz)
          This method will create two objects of the given class by trying to clone them in different ways.
static void assertToString(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(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(Class<?> clazz)
          Normally you should overwrite the toString() method for better logging and debugging.
static boolean hasToStringDefaultImpl(Object obj)
          Normally you should overwrite the toString() method for better logging and debugging.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assertEquals

public static void assertEquals(Object o1,
                                Object o2)
                         throws 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 object
o2 - the 2nd object
Throws:
AssertionError - if the check fails
Since:
1.1

assertEquals

public static void assertEquals(Serializable obj)
                         throws AssertionError,
                                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.

Parameters:
obj - the object
Throws:
AssertionError - if the check fails
NotSerializableException - if obj is not serializable
Since:
1.1

assertEquals

public static void assertEquals(Cloneable obj)
                         throws 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:
AssertionError - the assertion error
Since:
1.1

assertEquals

public static void assertEquals(Class<?> clazz)
                         throws AssertionError
This method will create two objects of the given class using the default constructor. So three preconditions must be true:
  1. the class must not be abstract
  2. there must be a (public) default constructor
  3. it must be Cloneable, Serializable or return always the same object
That a constructor creates equals objects is not true for all classes. For example the default constructor of the Date class will generate objects with different timestamps which are not equal. But most classes should meet the precondition.

Parameters:
clazz - the clazz
Throws:
AssertionError - if the check fails
Since:
1.1

assertEqualsWithClone

public static void assertEqualsWithClone(Class<?> clazz)
                                  throws AssertionError
This method will create two objects of the given class by trying to clone them in different ways. The last try to get a copy will be a cloning of all attributes. But this is normally not very significant because most (wrong) equals implementation depends on a simple comparison of the object reference.

Parameters:
clazz - the clazz
Throws:
AssertionError - if the check fails
Since:
1.1

assertEquals

public static void assertEquals(Collection<Class<?>> classes)
                         throws Failures
Check for each class in the given collection if the equals() method is implemented correct.

Parameters:
classes - the classes
Throws:
Failures - the collected assertion errors
Since:
1.1

assertEquals

public static void assertEquals(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 use assertEqualsOfPackage(String).

Parameters:
pkg - the package e.g. "patterntesting.runtime"
Since:
1.1
See Also:
assertEqualsOfPackage(String)

assertEquals

public static void assertEquals(Package pkg,
                                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 use assertEqualsOfPackage(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...)

assertEqualsOfPackage

public static void assertEqualsOfPackage(Package pkg,
                                         List<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 use assertEqualsOfPackage(String, List).

Parameters:
pkg - the package e.g. "patterntesting.runtime"
excluded - classes which should be excluded from the check
Since:
1.1
See Also:
assertEqualsOfPackage(String, List)

assertEqualsOfPackage

public static void assertEqualsOfPackage(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 by Package.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(String packageName,
                                         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(String packageName,
                                         List<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)

hasEqualsDeclared

public static boolean hasEqualsDeclared(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(Object o1,
                                   Object o2)
                            throws 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:
AssertionError - if the check fails
Since:
1.1
See Also:
ComparableTester.assertCompareTo(Comparable, Comparable)

assertToString

public static void assertToString(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(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(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 void assertAll(Class<?> clazz)
Starts all known checks like checkEquals(..), checks from the SerializableTester (if the given class is serializable) or from other classes.

Parameters:
clazz - the clazz to be checked.
Since:
1.1

assertAll

public static void assertAll(Collection<Class<?>> classes)
Check all.

Parameters:
classes - the classes to be checked
Since:
1.1

assertAll

public static void assertAll(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 use assertAllOfPackage(String).

Parameters:
pkg - the package e.g. "patterntesting.runtime"
Since:
1.1

assertAll

public static void assertAll(Package pkg,
                             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 use assertEqualsOfPackage(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(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(String packageName,
                                      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(String packageName,
                                      List<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:
assertEqualsOfPackage(String)


Copyright © 2002–2014 PatternTesting Team. All rights reserved.