use of com.zimbra.soap.jaxb.MixedAnyTester in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method mixedAndAnyElementHandlingJustElement.
/**
* {@link XmlAnyElement} and {@link XmlMixed} handling
* In the places we use XmlMixed, we typically have either just text or just elements - this tests with elements
* that do NOT map to JAXB classes.
* Desired JSON:
* {
* "alien": {
* "myAttr": "myValue",
* "child": {
* "_content": "Purple beans"
* },
* "daughter": {
* "age": "23",
* "name": "Kate"
* },
* "_jsns": "urn:foreign"
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void mixedAndAnyElementHandlingJustElement() throws Exception {
List<Object> elems = Lists.newArrayList();
MixedAnyTester jaxb = new MixedAnyTester();
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);
jaxb.setElems(elems);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) [Mixed w3c element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
MixedAnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedAnyTester.class);
Assert.assertEquals("roundtrippedX [Mixed w3c element] num elems", 1, roundtrippedX.getElems().size());
org.w3c.dom.Element w3ce = (org.w3c.dom.Element) roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX [Mixed w3c element] elem name", "alien", w3ce.getLocalName());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB [Mixed w3c element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
MixedAnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedAnyTester.class);
Assert.assertEquals("roundtripped [Mixed w3c element] num elems", 1, roundtripped.getElems().size());
org.w3c.dom.Element rtElem = (org.w3c.dom.Element) roundtripped.getElems().get(0);
Assert.assertEquals("roundtripped [Mixed w3c element] elem name", "alien", rtElem.getTagName());
Assert.assertEquals("roundtripped [Mixed w3c element] elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
Aggregations