Search in sources :

Example 1 with AnyTester

use of com.zimbra.soap.jaxb.AnyTester in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method anyElementHandling.

/**
 * {@link XmlAnyElement} handling
 * <pre>
 *     @XmlAnyElement
 *     private List<org.w3c.dom.Element> elems = Lists.newArrayList();
 * </pre>
 */
@Test
public void anyElementHandling() throws Exception {
    String given = "Given information";
    List<Object> elems = Lists.newArrayList();
    AnyTester jaxb = new AnyTester();
    DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = builder.newDocument();
    org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
    elem.setAttribute("myAttr", "myValue");
    org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
    child.setTextContent("Purple beans");
    elem.appendChild(child);
    org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
    child2.setAttribute("name", "Kate");
    child2.setAttribute("age", "23");
    elem.appendChild(child2);
    elems.add(elem);
    org.w3c.dom.Element elem2 = doc.createElementNS("urn:wooky", "may");
    elem2.setAttribute("fourth", "be with you");
    jaxb.setGiven(given);
    jaxb.setElems(Lists.newArrayList(elem, elem2));
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    AnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyTester.class);
    Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
    Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
    org.w3c.dom.Element w3ce = roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX elem name", "alien", w3ce.getLocalName());
    logDebug("STRING from JAXB ---> prettyPrint\n%1$s", getZimbraJsonJaxbString(jaxb));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    AnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyTester.class);
    Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
    Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
    org.w3c.dom.Element rtElem = roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped elem name", "alien", rtElem.getTagName());
    Assert.assertEquals("roundtripped elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
Also used : MixedAnyTester(com.zimbra.soap.jaxb.MixedAnyTester) AnyTester(com.zimbra.soap.jaxb.AnyTester) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 AnyTester (com.zimbra.soap.jaxb.AnyTester)1 MixedAnyTester (com.zimbra.soap.jaxb.MixedAnyTester)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 Test (org.junit.Test)1