com.advancedpwr.record.mock
Class MockBehaviorRecorder

java.lang.Object
  extended by com.advancedpwr.record.ClassWriter
      extended by com.advancedpwr.record.AbstractRecorder
          extended by com.advancedpwr.record.BeanRecorder
              extended by com.advancedpwr.record.mock.MockBehaviorRecorder
All Implemented Interfaces:
ObjectRecorder

public class MockBehaviorRecorder
extends BeanRecorder

An ObjectRecorder that records the behavior of an object tree as a Java class file. The resulting factory class uses EasyMock mock objects to mock the expected behavior of methods called on objects throughout the object tree.

Recording example as a JUnit test:

        public void testRecordJavadocExample()
        {
                MockBehaviorRecorder recorder = new MockBehaviorRecorder();
                recorder.setDestination( "recordings" );

                Person person = new Person();

                Person dad = new Person();
                dad.setName( "dad" );
                person.setDad( dad );

                person = recorder.record( person );

                person.setName( "Joe" );
                assertEquals( "Joe", person.getName() );
                assertEquals( "dad", person.getDad().getName() );
                assertEquals( "dad", person.getDad().getName() );

                Person mom = new Person();
                mom.setName( "mom" );
                person.setMom( mom );

                recorder.endRecording();

        }
 

Note that the "person" instance is swapped with an instrumented instance of the "person" object on the line

 person = recorder.record( person );
 

The above example will record the behavior of the "person" instance as a tree of mock objects in the Java class:

        public class PersonFactory extends MockFactory
        {

                protected Person person;

                public Person buildPerson()
                {
                        if ( person != null ) 
                        {
                                return person;
                        }
                        person = createStrictMock( Person.class );
                        person.setName( "Joe" );
                        expect( person.getName() ).andReturn( "Joe" );
                        expect( person.getDad() ).andReturn( buildPerson_3_1() );
                        expect( person.getDad() ).andReturn( buildPerson_3_1() );
                        person.setMom( buildPerson_5_1() );
                        replay( person );
                        return person;
                }

                protected Person person_3_1;

                protected Person buildPerson_3_1()
                {
                        if ( person_3_1 != null ) 
                        {
                                return person_3_1;
                        }
                        person_3_1 = createStrictMock( Person.class );
                        expect( person_3_1.getName() ).andReturn( "dad" );
                        expect( person_3_1.getName() ).andReturn( "dad" );
                        replay( person_3_1 );
                        return person_3_1;
                }

                protected Person person_5_1;

                protected Person buildPerson_5_1()
                {
                        if ( person_5_1 != null ) 
                        {
                                return person_5_1;
                        }
                        person_5_1 = createStrictMock( Person.class );
                        replay( person_5_1 );
                        return person_5_1;
                }
        }
 

To reconstruct the instance of "person" in a unit test and assert that all method calls are identical to the recorded behavior:

        public void testPlaybackJavadocExample()
        {
                Person person = new PersonFactory().buildPerson();

                person.setName( "Joe" );
                assertEquals( "Joe", person.getName() );
                assertEquals( "dad", person.getDad().getName() );
                assertEquals( "dad", person.getDad().getName() );

                Person mom = new Person();
                mom.setName( "mom" );
                person.setMom( mom );
        }
 

Note that the above unit test will fail because the mock objects are strictly performing checks on all objects they are interacting with in the object tree. In the playback test case, the instance of "mom" is not the same instance as the recorded test case, so the mock objects will complain. In order to make this test useful, it is necessary to edit the recorded PersonFactory class and change the line

 person.setMom( buildPerson_5_1() );
 

to

 person.setMom( anyObject( Person.class ) );
 

This may be a bug. If you disagree with this behavior, please open an issue on the Java Test Object Recorder site. The MockBehaviorRecorder is "instance aware" and supports Collection and Map objects. This class requires the EasyMock 3.0 jar and the cglib 2.2 jar.

Author:
Matthew Avery, mavery@advancedpwr.com on Jun 22, 2010

Field Summary
protected  boolean fieldNice
           
protected  Set<Class> fieldPreferredInterfaces
           
 
Fields inherited from class com.advancedpwr.record.BeanRecorder
fieldFactoryBuilder, fieldInstanceTree, fieldStopClasses
 
Fields inherited from class com.advancedpwr.record.AbstractRecorder
fieldDestinationDirectory, fieldJavaFileWriter
 
Fields inherited from class com.advancedpwr.record.ClassWriter
CLASS, fieldDescriptor, fieldObject, fieldPrintWriter, fieldSuperClass, IMPORT, PACKAGE, PRIVATE, PROTECTED, PUBLIC, SPACE, tabDepth
 
Constructor Summary
MockBehaviorRecorder()
           
 
Method Summary
 void addPreferredInterface(Class inClass)
           
protected  boolean canInstrument(Object result)
           
protected  boolean canInstrumentArray(Object result)
           
protected  boolean canInstrumentClass(Object result)
           
protected  Set<Class> classes()
           
protected  BehaviorInstanceTree createBehaviorInstanceTree(Object object)
           
protected  InstanceTree createInstanceTree(Object object)
           
protected  MethodBuilderFactory createMethodBuilderFactory()
           
protected
<T>
createProxyFactory(Class<T> inClass)
           
protected  void debug(Object inObject)
           
 Object endRecording()
          End monitoring of the object being recorded and trigger the writing of the factory file.
protected  Set<Class> getPreferredInterfaces()
           
protected  boolean hasPreferedInterface(Class inClass)
           
protected  void initializeInstanceTree(Object inObject)
           
protected
<T> T
instrument(T inObject)
           
protected
<T> T[]
instrumentedArray(T[] inArray)
           
protected  boolean isEnhanced(Class objectClass)
           
protected  boolean isFinal(Object result)
           
 boolean isNice()
           
protected  boolean isPrimitive(Object result)
           
protected  boolean isPublic(Object result)
           
protected  BehaviorInstanceTree newInstanceTree(Object inObject)
           
protected  Class preferredInterface(Class inClass)
           
<T> T
record(T inObject)
           
 void setNice(boolean nice)
          Create "nice" EasyMock objects rather than the default strict mocks.
 
Methods inherited from class com.advancedpwr.record.BeanRecorder
addBuilderFactory, closeFile, createMethodBuilder, getFactoryBuilder, getInstanceTree, getObject, getPrintWriter, getStopClasses, setObject, stopDescent, writeObjectBuilderMethod
 
Methods inherited from class com.advancedpwr.record.AbstractRecorder
close, createDefaultDescriptor, createFileWriter, getDestination, getJavaFileWriter, javaFile, objectClass, packagePath, parentDirectory, setDestination, setDestination
 
Methods inherited from class com.advancedpwr.record.ClassWriter
closeBrace, extendClass, getClassName, getDescriptor, getPackageName, getSuperClass, newLine, openBrace, packageName, setClassName, setDescriptor, setSuperClass, setWriter, tab, tabs, write, writeClassDeclaration, writeImports, writeLine, writeObject, writePackage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fieldPreferredInterfaces

protected Set<Class> fieldPreferredInterfaces

fieldNice

protected boolean fieldNice
Constructor Detail

MockBehaviorRecorder

public MockBehaviorRecorder()
Method Detail

getPreferredInterfaces

protected Set<Class> getPreferredInterfaces()

addPreferredInterface

public void addPreferredInterface(Class inClass)

classes

protected Set<Class> classes()
Overrides:
classes in class BeanRecorder

record

public <T> T record(T inObject)
Specified by:
record in interface ObjectRecorder
Overrides:
record in class BeanRecorder

debug

protected void debug(Object inObject)

instrument

protected <T> T instrument(T inObject)

createProxyFactory

protected <T>  createProxyFactory(Class<T> inClass)

hasPreferedInterface

protected boolean hasPreferedInterface(Class inClass)

preferredInterface

protected Class preferredInterface(Class inClass)

isEnhanced

protected boolean isEnhanced(Class objectClass)

canInstrument

protected boolean canInstrument(Object result)

canInstrumentClass

protected boolean canInstrumentClass(Object result)

isFinal

protected boolean isFinal(Object result)

isPrimitive

protected boolean isPrimitive(Object result)

isPublic

protected boolean isPublic(Object result)

canInstrumentArray

protected boolean canInstrumentArray(Object result)

instrumentedArray

protected <T> T[] instrumentedArray(T[] inArray)

newInstanceTree

protected BehaviorInstanceTree newInstanceTree(Object inObject)

initializeInstanceTree

protected void initializeInstanceTree(Object inObject)

endRecording

public Object endRecording()
End monitoring of the object being recorded and trigger the writing of the factory file.

Returns:
- Returns the original, un-instrumented object.

createMethodBuilderFactory

protected MethodBuilderFactory createMethodBuilderFactory()
Overrides:
createMethodBuilderFactory in class BeanRecorder

createInstanceTree

protected InstanceTree createInstanceTree(Object object)
Overrides:
createInstanceTree in class BeanRecorder

createBehaviorInstanceTree

protected BehaviorInstanceTree createBehaviorInstanceTree(Object object)

isNice

public boolean isNice()

setNice

public void setNice(boolean nice)
Create "nice" EasyMock objects rather than the default strict mocks.

Parameters:
nice -


Copyright © 2011 Advanced Power Co. All Rights Reserved.