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());
}
Aggregations