Search in sources :

Example 1 with XStreamMarshaller

use of org.hudsonci.utils.marshal.XStreamMarshaller in project hudson-2.x by hudson.

the class XReferenceStoreConverterTest method testChewDocument.

@Test
public void testChewDocument() throws Exception {
    XStream xs = new XStream();
    xs.autodetectAnnotations(true);
    XStreamMarshaller marshaller = new XStreamMarshaller(xs);
    XReferenceStore store = new FileXReferenceStore(marshaller, util.resolveFile("target/test-xref"));
    XReferenceStoreConverter converter = new XReferenceStoreConverter(store, xs.getMapper(), xs.getReflectionProvider());
    xs.registerConverter(converter);
    Document<Record> doc1 = new Document<Record>();
    Record rec1 = new Record("1");
    Entity ent1 = new Entity("1");
    rec1.set(ent1);
    assertNotNull(rec1.entity.holder);
    assertTrue(rec1.entity.holder instanceof XReference.InstanceHolder);
    XReference.InstanceHolder holder1 = (XReference.InstanceHolder) rec1.entity.holder;
    assertNotNull(holder1.instance);
    doc1.records.add(rec1);
    StringWriter buff = new StringWriter();
    marshaller.marshal(doc1, buff);
    System.out.println("XML:\n" + buff);
    Document<Record> doc2 = (Document<Record>) marshaller.unmarshal(new StringReader(buff.toString()));
    assertNotNull(doc2);
    assertNotNull(doc2.records);
    assertEquals(1, doc2.records.size());
    Record rec2 = doc2.records.get(0);
    assertNotNull(rec2.entity);
    System.out.println("ENT REF: " + rec2.entity);
    assertNotNull(rec2.entity.holder);
    assertTrue(rec2.entity.holder instanceof XReferenceStoreConverter.UnmarshalHolder);
    XReferenceStoreConverter.UnmarshalHolder holder2 = (XReferenceStoreConverter.UnmarshalHolder) rec2.entity.holder;
    assertNull(holder2.instance);
    Entity ent2 = rec2.get();
    System.out.println("ENT: " + ent2);
    assertNotNull(ent2);
    assertNotNull(holder2.instance);
    buff = new StringWriter();
    marshaller.marshal(doc2, buff);
    System.out.println("XML\n" + buff);
}
Also used : XStream(com.thoughtworks.xstream.XStream) XStreamMarshaller(org.hudsonci.utils.marshal.XStreamMarshaller) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

XStream (com.thoughtworks.xstream.XStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 XStreamMarshaller (org.hudsonci.utils.marshal.XStreamMarshaller)1 Test (org.junit.Test)1