Search in sources :

Example 1 with WrapperAbsentIfEmpty

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

the class JaxbToJsonTest method wrapperAbsentIfEmpty.

/**
 * How to achieve a wrapped list where no element for the wrapper will appear in the XML if the list is empty.
 * This no longer works!  Fortunately, looks like we don't use any classes similar to WrapperAbsentIfEmpty
 * at the moment.
 * For JUDASPRIEST/Java 7 setNumbers is called with 2 integers.  For main/Java 8
 * it is called with an empty list.
 *     WrapperAbsentIfEmpty.setNumbers(List<Integer>) line: 51
 *     WrapperAbsentIfEmpty$JaxbAccessorM_getNumbers_setNumbers_java_util_List.set(Object, Object) line: 60
 *     Lister$CollectionLister<BeanT,T>.startPacking(BeanT, Accessor<BeanT,T>) line: 298
 *     Lister$CollectionLister<BeanT,T>.startPacking(Object, Accessor) line: 269
 *     Scope<BeanT,PropT,ItemT,PackT>.start(Accessor<BeanT,PropT>, Lister<BeanT,PropT,ItemT,PackT>) line: 142
 *     ArrayERProperty$ItemsLoader.startElement(UnmarshallingContext$State, TagName) line: 119
 *     UnmarshallingContext._startElement(TagName) line: 501
 *     UnmarshallingContext.startElement(TagName) line: 480
 *     InterningXmlVisitor.startElement(TagName) line: 75
 *     SAXConnector.startElement(String, String, String, Attributes) line: 150
 *     DOMScanner.visit(Element) line: 244
 *     DOMScanner.visit(Node) line: 281
 *     DOMScanner.visit(Element) line: 250
 *     DOMScanner.scan(Element) line: 127
 *     UnmarshallerImpl.unmarshal0(Node, JaxBeanInfo) line: 369
 *     UnmarshallerImpl.unmarshal(Node, Class<T>) line: 348
 *     JaxbUtil.rawW3cDomDocToJaxb(Document, Class<?>, boolean) line: 1420
 *     JaxbUtil.w3cDomDocToJaxb(Document, Class<?>, boolean) line: 1392
 *     JaxbUtil.elementToJaxb(Element, Class<?>) line: 1438
 *     JaxbToJsonTest.wrapperAbsentIfEmpty() line: 1516
 */
@Ignore("Bug:95509 - this is hypothetical functionality that isn't currently used, so low priority")
@Test
public void wrapperAbsentIfEmpty() throws Exception {
    WrapperAbsentIfEmpty jaxb = new WrapperAbsentIfEmpty();
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    WrapperAbsentIfEmpty roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrapperAbsentIfEmpty.class);
    logDebug("JSONElement from JAXB (empty numbers)--> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertNull("original getNumbers (empty list)", jaxb.getNumbers());
    Assert.assertNull("JSON roundtripped getNumbers (empty list)", roundtripped.getNumbers());
    Assert.assertEquals("JSON for empty Numbers", "{\n  \"_jsns\": \"urn:zimbraTest\"\n}", jsonJaxbElem.prettyPrint());
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement from JAXB (empty numbers) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    WrapperAbsentIfEmpty roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, WrapperAbsentIfEmpty.class);
    Assert.assertNull("XML roundtripped getNumbers (empty list)", roundtrippedX.getNumbers());
    Assert.assertEquals("XML for empty Numbers", "<wrapper-absent-if-empty xmlns=\"urn:zimbraTest\"/>", xmlElem.prettyPrint());
    jaxb.addNumber(314159);
    jaxb.addNumber(42);
    jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrapperAbsentIfEmpty.class);
    logDebug("JSONElement from JAXB (empty numbers)--> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("original size of Numbers (2 numbers)", 2, jaxb.getNumbers().size());
    Assert.assertNotNull("XML roundtripped getNumbers (when 2 numbers in list)", roundtrippedX.getNumbers());
    Assert.assertEquals("JSON roundtripped size of Numbers (2 numbers)", 2, roundtripped.getNumbers().size());
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement from JAXB (empty numbers) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, WrapperAbsentIfEmpty.class);
    Assert.assertEquals("XML roundtripped size of Numbers (2 numbers)", 2, roundtrippedX.getNumbers().size());
    Assert.assertEquals("XML when have 2 numbers", String.format("%s\n  %s\n    %s\n    %s\n  %s\n%s", "<wrapper-absent-if-empty xmlns=\"urn:zimbraTest\">", "<numbers>", "<number>314159</number>", "<number>42</number>", "</numbers>", "</wrapper-absent-if-empty>"), xmlElem.prettyPrint());
}
Also used : 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) WrapperAbsentIfEmpty(com.zimbra.soap.jaxb.WrapperAbsentIfEmpty) Ignore(org.junit.Ignore) 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 WrapperAbsentIfEmpty (com.zimbra.soap.jaxb.WrapperAbsentIfEmpty)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1