/**
 * @author <a href="mailto:[email protected]">Emanuel Muckenhuber</a>
 * @version $Revision$
 */
public class AbstractComponentMapperTest extends BaseTestCase {

  /** The managed object factory. */
  private ManagedObjectFactory managedObjectFactory = ManagedObjectFactory.getInstance();

  /** The persistence factory. */
  private AbstractPersistenceFactory persistenceFactory = new AbstractPersistenceFactory();

  public AbstractComponentMapperTest(String name) {
    super(name);
  }

  protected ManagedObjectFactory getMOF() {
    return managedObjectFactory;
  }

  protected PersistenceFactory getPersistenceFactory() {
    return persistenceFactory;
  }

  protected void addComponentMapper(ComponentMapper mapper) {
    persistenceFactory.addComponentMapper(mapper);
  }

  protected PersistenceRoot restore(PersistenceRoot root) throws Exception {
    File file = File.createTempFile("test", null);
    serialize(root, file);
    return deserialize(file);
  }

  protected void serialize(PersistenceRoot moElement, File file) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(PersistenceRoot.class);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
    marshaller.marshal(moElement, file);
    marshaller.marshal(moElement, System.out);
  }

  protected PersistenceRoot deserialize(File file) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(PersistenceRoot.class);
    Unmarshaller unmarshaller = ctx.createUnmarshaller();
    return (PersistenceRoot) unmarshaller.unmarshal(file);
  }
}