com.advancedpwr.record.xstream
Class XstreamRecorder
java.lang.Object
com.advancedpwr.record.ClassWriter
com.advancedpwr.record.AbstractRecorder
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
| Fields inherited from class com.advancedpwr.record.ClassWriter |
CLASS, fieldDescriptor, fieldObject, fieldPrintWriter, fieldSuperClass, IMPORT, PACKAGE, PRIVATE, PROTECTED, PUBLIC, SPACE, tabDepth |
| 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 |
fieldXmlFileWriter
protected FileWriter fieldXmlFileWriter
XstreamRecorder
public XstreamRecorder()
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.