nl.ivonet.beanunit
Class SimplePojoContractAsserter

java.lang.Object
  extended by nl.ivonet.beanunit.Asserter
      extended by nl.ivonet.beanunit.SimplePojoContractAsserter

public final class SimplePojoContractAsserter
extends Asserter

Test utility class that makes easy work of testing default behavior pojos (beans).

The assertBasicGetterSetterBehavior methods will try to assert that the normal Bean (POJO) specifications and rules are upheld.

Getters should exactly return what setters set. Write methods should have read methods and vice versa.

The assertEqualsHashCode method will try to assert that the rules around these methods are upheld.

If equals has been overridden the hashCode method must also be overridden and vise versa. If an object is equal the hashcode must also be the same for both objects, etc.

Author:
Ivo Woltring

Method Summary
static
<T> void
assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest)
           
static
<T> void
assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest, java.util.Map<java.lang.String,java.lang.Object> properties)
           
static
<T> void
assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest, java.lang.String property)
          Tests that the getter and setter methods for property work in a basic fashion, which is that the getter returns the exact same object as set by the setter method.
static
<T> void
assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest, java.lang.String property, java.lang.Object argument)
          The method that does the actual assertion work.
static
<T> void
assertBasicGetterSetterBehaviorWithBlacklist(java.lang.Class<T> classUnderTest, java.lang.String... propertyNames)
           
static
<T> void
assertEqualsHashCode(java.lang.Class<T> classUnderTest, java.lang.String... excludedProperties)
          Tests all the flows of the overridden equals and hashCode methods of a class.
 
Methods inherited from class nl.ivonet.beanunit.Asserter
deregisterType, registerTypeAndDefaultArgument, resetToDefaultTypes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assertBasicGetterSetterBehavior

public static <T> void assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest,
                                                       java.lang.String property)
Tests that the getter and setter methods for property work in a basic fashion, which is that the getter returns the exact same object as set by the setter method. FindBugz says this is not correct but I don't see the bad in it :-)

Uses a default argument for basic collection types, primitive types, Dates, java.sql.Dates, and Timestamps. See Asserter.TYPE_ARGUMENTS.

Parameters:
classUnderTest - the object on which to invoke the getter and setter
property - the property name, e.g. "firstName"

assertBasicGetterSetterBehavior

public static <T> void assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest,
                                                       java.lang.String property,
                                                       java.lang.Object argument)
The method that does the actual assertion work.

It checks the getter and setter behavior of the property against the argument.

Parameters:
classUnderTest - the object on which to invoke the getter and setter
property - the property name, e.g. "firstName"
argument - the property value, i.e. the value the setter will be invoked with

assertBasicGetterSetterBehavior

public static <T> void assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest,
                                                       java.util.Map<java.lang.String,java.lang.Object> properties)
Parameters:
classUnderTest - the object on which to invoke the getter and setter
properties - map of property names to argument values
See Also:
SimplePojoContractAsserter#assertBasicGetterSetterBehavior(Class, String, Object)} method. Only difference is that here we accept a map containing property name/value pairs. Use this to test a bunch of property accessors at once. Note that the values in the map can be null, and in that case we'll try to supply a default argument.

assertBasicGetterSetterBehavior

public static <T> void assertBasicGetterSetterBehavior(java.lang.Class<T> classUnderTest)
Parameters:
classUnderTest - the object on which to invoke the getter and setter
See Also:
SimplePojoContractAsserter#assertBasicGetterSetterBehavior(Class, String, Object)} method. No items are blacklisted.

assertBasicGetterSetterBehaviorWithBlacklist

public static <T> void assertBasicGetterSetterBehaviorWithBlacklist(java.lang.Class<T> classUnderTest,
                                                                    java.lang.String... propertyNames)
Parameters:
classUnderTest - the object on which to invoke the getter and setter
propertyNames - the list of property names that should not be tested
See Also:
SimplePojoContractAsserter#assertBasicGetterSetterBehavior(Class, String, Object)} method. Big difference here is that we try to automatically introspect the target object, finding read/write properties, and automatically testing the getter and setter. Note specifically that read-only properties are ignored, as there is no way for us to know how to set the value (since there isn't a public setter).

Any property names contained in the blacklist will be skipped.


assertEqualsHashCode

public static <T> void assertEqualsHashCode(java.lang.Class<T> classUnderTest,
                                            java.lang.String... excludedProperties)
Tests all the flows of the overridden equals and hashCode methods of a class.

- it ensures that if A == B, B == A also - it tests the equals and hasCode methods for all the separate writable properties - it only tests if the equals method and the hashCode method are both overridden - it fails of only one is overridden - The both methods need to be overridden at the same level (not java.lang.Object)

You can exclude properties by adding them to the method argument list.

Type Parameters:
T - the type of the class to test
Parameters:
classUnderTest - the implementation.class
excludedProperties - string representation of all the properties excluded from the equals test , e.g. "firstName"


Copyright © 2011 IvoNet. All Rights Reserved.