use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method exerciseZimbraJsonAttribute.
/**
* <p>Exercise annotation {@link ZimbraJsonAttribute} which is needed in JAXB in addition to {@link XmlElement} or
* {@link XmlElementRef} annotations to provide a field which as an analog of something like:
* <pre>
* jsonElem.addAttribute("xml-elem-json-attr", "XML elem but JSON attribute", Element.Disposition.CONTENT);
* </pre>
* Desired XML serialization:
* <pre>
* <XmlElemJsonAttr xmlns="urn:zimbraTest">
* <xml-elem-json-attr>XML elem but JSON attribute</xml-elem-json-attr>
* <classic-elem>elem for both XML and JSON</classic-elem>
* </XmlElemJsonAttr>
* </pre>
* Desired JSON serialization:
* <pre>
* {
* "xml-elem-json-attr": "XML elem but JSON attribute",
* "classic-elem": [{
* "_content": "elem for both XML and JSON"
* }],
* "_jsns": "urn:zimbraTest"
* }
* </pre>
*/
@Test
public void exerciseZimbraJsonAttribute() throws Exception {
final String str1 = "XML elem but JSON attribute";
final String str2 = "elem for both XML and JSON";
Element jsonElem = JSONElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
Element xmlElem = XMLElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
jsonElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
jsonElem.addElement("classic-elem").setText(str2);
xmlElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
xmlElem.addElement("classic-elem").setText(str2);
logDebug("XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
XmlElemJsonAttr jaxb = new XmlElemJsonAttr(str1, str2);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("JSONElement and JSONElement from JAXB", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
XmlElemJsonAttr roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, XmlElemJsonAttr.class);
Assert.assertEquals("roundtripped xml-elem-json-attr", str1, roundtripped.getXmlElemJsonAttr());
Assert.assertEquals("roundtripped classic-elem", str2, roundtripped.getDefaultElem());
Assert.assertEquals("JSONElement xml-elem-json-attr as attribute", str1, jsonElem.getAttribute("xml-elem-json-attr"));
// Note difference from XMLElement - for JSON this is Always an attribute but for XML it can be treated as an element
Assert.assertEquals("JSONElement num xml-elem-json-attr elements", 0, jsonElem.listElements("xml-elem-json-attr").size());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method elementRefsHandling.
/**
* XmlElementRefs handling
* Desired JSON :
* {
* "string-attr-int-value": [{
* "attr1": "my string",
* "_content": 321
* }],
* "enumEttribs": [{
* "fold1": "chat",
* "fold2": "remote folder"
* }],
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void elementRefsHandling() throws Exception {
String str = "my string";
int num = 321;
List<Object> elems = Lists.newArrayList();
ElementRefsTester jaxb = new ElementRefsTester();
StringAttribIntValue inner = new StringAttribIntValue(str, num);
elems.add(inner);
EnumAttribs ea = new EnumAttribs();
ea.setFold1(ViewEnum.CHAT);
ea.setFold2(ViewEnum.REMOTE_FOLDER);
elems.add(ea);
jaxb.setElems(elems);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
ElementRefsTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, ElementRefsTester.class);
ElementRefsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, ElementRefsTester.class);
Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
StringAttribIntValue rtByRef = (StringAttribIntValue) roundtripped.getElems().get(0);
StringAttribIntValue rtXmlByRef = (StringAttribIntValue) roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX str", str, rtXmlByRef.getAttrib1());
Assert.assertEquals("roundtrippedX num", num, rtXmlByRef.getMyValue());
Assert.assertEquals("roundtripped str", str, rtByRef.getAttrib1());
Assert.assertEquals("roundtripped num", num, rtByRef.getMyValue());
EnumAttribs rtea = (EnumAttribs) roundtripped.getElems().get(1);
Assert.assertEquals("roundtripped fold1", ViewEnum.CHAT, rtea.getFold1());
Assert.assertEquals("roundtripped fold2", ViewEnum.REMOTE_FOLDER, rtea.getFold2());
}
use of javax.xml.bind.annotation.XmlElement 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());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method wrappedEnumElemList.
/**
* Testing form:
* {@code @XmlElementWrapper(name="wrapper", required=false)
* @XmlElement(name="enum-entry", required=false)
* private List<ViewEnum> entries = Lists.newArrayList();}
* Desired JSON :
* {
* "wrapper": [{
* "enum-entry": [
* {
* "_content": "appointment"
* },
* {
* "_content": "document"
* }]
* }],
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void wrappedEnumElemList() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("wrapped-enum-elem-list", "urn:zimbraTest"));
Element wrapElem = jsonElem.addElement("wrapper");
wrapElem.addElement("enum-entry").addText(ViewEnum.APPOINTMENT.toString());
wrapElem.addElement("enum-entry").addText(ViewEnum.DOCUMENT.toString());
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
WrappedEnumElemList tstr = new WrappedEnumElemList();
tstr.addEntry(ViewEnum.APPOINTMENT);
tstr.addEntry(ViewEnum.DOCUMENT);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
WrappedEnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrappedEnumElemList.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Element wElem = jsonJaxbElem.getElement("wrapper");
List<Element> jsonElems = wElem.listElements();
List<ViewEnum> entries = roundtripped.getEntries();
Assert.assertEquals("jsonElems num", 2, jsonElems.size());
Assert.assertEquals("entries num", 2, entries.size());
Assert.assertTrue("has APPOINTMENT", entries.contains(ViewEnum.APPOINTMENT));
Assert.assertTrue("has DOCUMENT", entries.contains(ViewEnum.DOCUMENT));
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method mixedHandlingWithJaxbAndNoText.
/**
* {@link XmlMixed} handling
* In the places we use XmlMixed, we have either just text or just elements
* This tests where we have something that maps to a JAXB object.
* Side note: Also tests out case for {@link XmlElementRef} where name is derived from root element.
*/
@Test
public void mixedHandlingWithJaxbAndNoText() throws Exception {
String str = "my string";
int num = 321;
List<Object> elems = Lists.newArrayList();
MixedTester jaxb = new MixedTester();
StringAttribIntValue inner = new StringAttribIntValue(str, num);
elems.add(inner);
jaxb.setElems(elems);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) [Mixed has element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
MixedTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedTester.class);
Assert.assertEquals("roundtrippedX num elems", 1, roundtrippedX.getElems().size());
StringAttribIntValue rtXmlByRef = (StringAttribIntValue) roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX [Mixed has element] str", str, rtXmlByRef.getAttrib1());
Assert.assertEquals("roundtrippedX [Mixed has element] num", num, rtXmlByRef.getMyValue());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB [Mixed has element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
MixedTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedTester.class);
Assert.assertEquals("roundtripped [Mixed has element] num elems", 1, roundtripped.getElems().size());
StringAttribIntValue rtByRef = (StringAttribIntValue) roundtripped.getElems().get(0);
Assert.assertEquals("roundtripped [Mixed has element] str", str, rtByRef.getAttrib1());
Assert.assertEquals("roundtripped [Mixed has element] num", num, rtByRef.getMyValue());
}
Aggregations