nl.ivonet.beanunit
Class BuilderBeanAsserter

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

public class BuilderBeanAsserter
extends Asserter

Asserts "simple" beans that are constructed using the "Builder" pattern as documented by Joshua Bloch ("Effective Java" - Second Edition).

The Beans constructed this way are after construction immutable or at least that is the intention for using the builder. See http://www.javapractices.com/topic/TopicAction.do?Id=29 for good reasons to make objects immutable. Construction can be done in stages by "building" the object but after the final build we can only read the object.

The following assumptions are asserted: All properties that can be set on the builder should be reflected by the read method on the class to be build. All Builders should have a build() method (convention) By preference one should call the Builder class "Builder" but this is not mandatory.

Author:
Ivo Woltring

Constructor Summary
BuilderBeanAsserter()
           
 
Method Summary
static
<T> void
assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest)
          Tests a Bean created with a Builder.
static
<T,B> void
assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest, java.lang.Class<B> builderUnderTest)
          Tests a Bean created with a Builder.
static
<T,B> void
assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest, java.lang.Class<B> builderUnderTest, java.lang.String... exclusionProperties)
          Tests a Bean created with a Builder.
static
<T,B> void
assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest, java.lang.Class<B> builderUnderTest, java.lang.String buildMethodName, java.util.List<java.lang.String> excludedBuilderMethods, java.util.List<java.lang.String> excludedClassProperties)
          Tests a bean created by a builder.
static
<T> void
assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest, java.util.List<java.lang.String> exclusionMethodNames)
          Tests a Bean created with a Builder.
static
<T> void
assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest, java.lang.String... exclusionProperties)
          Tests a Bean created with a Builder.
static
<T,B> void
assertEqualsHashCode(java.lang.Class<T> classUnderTest, java.lang.Class<B> builderUnderTest, java.lang.String buildMethod, java.util.List<java.lang.String> excludedProperties)
          Tests all the flows of the overridden equals and hashCode methods of a class.
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
 

Constructor Detail

BuilderBeanAsserter

public BuilderBeanAsserter()
Method Detail

assertBuildObjectGetterBehavior

public static <T,B> void assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest,
                                                         java.lang.Class<B> builderUnderTest,
                                                         java.lang.String buildMethodName,
                                                         java.util.List<java.lang.String> excludedBuilderMethods,
                                                         java.util.List<java.lang.String> excludedClassProperties)
Tests a bean created by a builder.

Type Parameters:
T - the type of the classUnderTest
B - the type of the builder
Parameters:
classUnderTest - the class to test
builderUnderTest - the builder that creates the classUnderTest
buildMethodName - the name of the method that builds the classUnderTest
excludedBuilderMethods - properties to exclude from the test in the builder
excludedClassProperties - properties to exclude from the test in de class under test

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 - 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"

assertEqualsHashCode

public static <T,B> void assertEqualsHashCode(java.lang.Class<T> classUnderTest,
                                              java.lang.Class<B> builderUnderTest,
                                              java.lang.String buildMethod,
                                              java.util.List<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
builderUnderTest - the Builder class
buildMethod - the string name of the build method
excludedProperties - string representation of all the properties excluded from the equals test , e.g. "firstName"

assertBuildObjectGetterBehavior

public static <T> void assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest)
Tests a Bean created with a Builder.

This test assumes that the builder is called "Builder" and the build method is called "build"

Type Parameters:
T - the type of the classUnderTest
Parameters:
classUnderTest - the class to test

assertBuildObjectGetterBehavior

public static <T> void assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest,
                                                       java.lang.String... exclusionProperties)
Tests a Bean created with a Builder.

This method accepts the a different Builder name than the default.

Type Parameters:
T - the type of the classUnderTest
Parameters:
classUnderTest - the class to test
exclusionProperties - class under test properties to exclude from the test

assertBuildObjectGetterBehavior

public static <T,B> void assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest,
                                                         java.lang.Class<B> builderUnderTest)
Tests a Bean created with a Builder.

This method accepts the a different Builder name than the default.

Type Parameters:
T - the type of the classUnderTest
B - the type of the builder
Parameters:
classUnderTest - the class to test
builderUnderTest - the builder that creates the classUnderTest

assertBuildObjectGetterBehavior

public static <T,B> void assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest,
                                                         java.lang.Class<B> builderUnderTest,
                                                         java.lang.String... exclusionProperties)
Tests a Bean created with a Builder.

This method accepts the a different Builder name than the default.

Type Parameters:
T - the type of the classUnderTest
B - the type of the builder
Parameters:
classUnderTest - the class to test
builderUnderTest - the builder that creates the classUnderTest
exclusionProperties - the properties to ignore on the class under test.

assertBuildObjectGetterBehavior

public static <T> void assertBuildObjectGetterBehavior(java.lang.Class<T> classUnderTest,
                                                       java.util.List<java.lang.String> exclusionMethodNames)
Tests a Bean created with a Builder.

This method accepts the a different Builder name than the default.

Type Parameters:
T - the type of the classUnderTest
Parameters:
classUnderTest - the class to test
exclusionMethodNames - the properties to ignore on the builder under test.


Copyright © 2011 IvoNet. All Rights Reserved.