use of com.zimbra.soap.jaxb.ElementRefTester in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method elementRefHandling.
/**
* XmlElementRef handling. Note that slightly counter-intuitively, any name specified is ignored (unless
* type=JAXBElement.class)
* <pre>
* @XmlElementRef(name="ignored-name-root-elem-name-used-instead", type=StringAttribIntValue.class)
* private StringAttribIntValue byRef;
* </pre>
* Desired JSON :
* {
* "string-attr-int-value": [{
* "attr1": "my string",
* "_content": 321
* }],
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void elementRefHandling() throws Exception {
String str = "my string";
int num = 321;
ElementRefTester jaxb = new ElementRefTester();
StringAttribIntValue inner = new StringAttribIntValue(str, num);
jaxb.setByRef(inner);
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());
ElementRefTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, ElementRefTester.class);
ElementRefTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, ElementRefTester.class);
StringAttribIntValue rtByRef = roundtripped.getByRef();
StringAttribIntValue rtXmlByRef = roundtrippedX.getByRef();
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());
}
Aggregations