com.advancedpwr.record.xstream
Class XstreamRecorder

java.lang.Object
  extended by com.advancedpwr.record.ClassWriter
      extended by com.advancedpwr.record.AbstractRecorder
          extended by com.advancedpwr.record.xstream.XstreamRecorder
All Implemented Interfaces:
ObjectRecorder

public class XstreamRecorder
extends AbstractRecorder

An ObjectRecorder that records the state of an object tree using the XStream project. The object tree is serialized to XML at recording time and Java factory class is created to rehydrate the object tree at test time.

Recording example:

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

        XstreamRecorder recorder = new XstreamRecorder();
        recorder.setDestination( "recordings" );
        recorder.record( person );
 

The above example will record the object tree of the "person" instance as a Java class:

        public Person buildPerson()
        {
                XStream xstream = new XStream();
                InputStream in = getClass().getResourceAsStream( "/com/examples/generated/PersonFactory.xml" );
                return (Person)xstream.fromXML( in );
        }
 

To reconstruct the instance of "person" in a unit test:

        Person person = new PersonFactory().buildPerson();
 

The XstreamRecorder is capable of recording arbitrary object trees and is "instance aware". This class requires the XStream 1.3.1 jar and the objenesis 1.2 jar.

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

Field Summary
protected  FileWriter fieldXmlFileWriter
           
 
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
XstreamRecorder()
           
 
Method Summary
protected  Set<Class> classes()
           
 FileWriter getXmlFileWriter()
           
<T> T
record(T inObject)
           
protected  String returnType()
           
protected  void writeFactoryClass()
           
protected  void writeObjectBuilderMethod()
           
protected  void writeXstream()
           
protected  File xmlFile()
           
protected  String xmlPath()
           
 
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, getObject, getPackageName, getPrintWriter, getSuperClass, newLine, openBrace, packageName, setClassName, setDescriptor, setObject, 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

fieldXmlFileWriter

protected FileWriter fieldXmlFileWriter
Constructor Detail

XstreamRecorder

public XstreamRecorder()
Method Detail

record

public <T> T record(T inObject)

writeXstream

protected void writeXstream()

writeFactoryClass

protected void writeFactoryClass()

getXmlFileWriter

public FileWriter getXmlFileWriter()

xmlFile

protected File xmlFile()

classes

protected Set<Class> classes()
Specified by:
classes in class ClassWriter

writeObjectBuilderMethod

protected void writeObjectBuilderMethod()
Specified by:
writeObjectBuilderMethod in class ClassWriter

xmlPath

protected String xmlPath()

returnType

protected String returnType()


Copyright © 2011 Advanced Power Co. All Rights Reserved.