Search in sources :

Example 31 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project camel by apache.

the class ModelSanityCheckerTest method testSanity.

public void testSanity() throws Exception {
    Set<Class<?>> classes = discoverJaxbClasses();
    assertNotNull(classes);
    assertTrue("There should be > 140 classes, was: " + classes.size(), classes.size() > 140);
    // check each class is okay
    for (Class<?> clazz : classes) {
        // skip ProcessorDefinition as its special
        if (clazz == ProcessorDefinition.class) {
            continue;
        }
        // skip RouteDefinition as its special
        if (clazz == RouteDefinition.class) {
            continue;
        }
        // check each declared field in the class
        for (Field field : clazz.getDeclaredFields()) {
            LOG.debug("Class {} has field {}", clazz.getName(), field.getName());
            // does the field have a jaxb annotation?
            boolean attribute = field.getAnnotation(XmlAttribute.class) != null;
            boolean element = field.getAnnotation(XmlElement.class) != null;
            boolean elementRef = field.getAnnotation(XmlElementRef.class) != null;
            // only one of those 3 is allowed, so check that we don't have 2+ of them
            if ((attribute && element) || (attribute && elementRef) || (element && elementRef)) {
                fail("Class " + clazz.getName() + " has field " + field.getName() + " which has 2+ annotations that are not allowed together.");
            }
            // check getter/setter
            if (attribute || element || elementRef) {
                // check for getter/setter
                Method getter = IntrospectionSupport.getPropertyGetter(clazz, field.getName());
                Method setter = IntrospectionSupport.getPropertySetter(clazz, field.getName());
                assertNotNull("Getter " + field.getName() + " on class " + clazz.getName() + " is missing", getter);
                assertNotNull("Setter " + field.getName() + " on class " + clazz.getName() + " is missing", setter);
            }
        }
        // we do not expect any JAXB annotations on methods
        for (Method method : clazz.getDeclaredMethods()) {
            LOG.debug("Class {} has method {}", clazz.getName(), method.getName());
            // special for OptionalIdentifiedDefinition as it has setter, so we should skip it
            if (clazz.getCanonicalName().equals(OptionalIdentifiedDefinition.class.getCanonicalName())) {
                continue;
            }
            // does the method have a jaxb annotation?
            boolean attribute = method.getAnnotation(XmlAttribute.class) != null;
            boolean element = method.getAnnotation(XmlElement.class) != null;
            boolean elementRef = method.getAnnotation(XmlElementRef.class) != null;
            assertFalse("Class " + clazz.getName() + " has method " + method.getName() + " should not have @XmlAttribute annotation", attribute);
            assertFalse("Class " + clazz.getName() + " has method " + method.getName() + " should not have @XmlElement annotation", element);
            assertFalse("Class " + clazz.getName() + " has method " + method.getName() + " should not have @XmlElementRef annotation", elementRef);
        }
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) Field(java.lang.reflect.Field) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlElement(javax.xml.bind.annotation.XmlElement) Method(java.lang.reflect.Method)

Example 32 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method mixedHandlingJustText.

/**
     * {@link XmlMixed} handling
     * In the places we use XmlMixed, we typically have either just text or just elements
     * This tests where we have just text.
     */
@Test
public void mixedHandlingJustText() throws Exception {
    String textStr = "text string";
    List<Object> elems = Lists.newArrayList();
    MixedTester jaxb = new MixedTester();
    elems.add(textStr);
    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 has just text] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    MixedTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedTester.class);
    Assert.assertEquals("roundtrippedX [Mixed has just text] num elems", 1, roundtrippedX.getElems().size());
    Assert.assertEquals("roundtrippedX [Mixed has just text] str", textStr, (String) roundtrippedX.getElems().get(0));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB [Mixed has just text] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    MixedTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedTester.class);
    Assert.assertEquals("roundtripped [Mixed has just text] num elems", 1, roundtripped.getElems().size());
    Assert.assertEquals("roundtripped [Mixed has just text] str", textStr, (String) roundtripped.getElems().get(0));
}
Also used : MixedTester(com.zimbra.soap.jaxb.MixedTester) 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) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 33 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method multipleDispositionCONTENTAttributes.

/**
     * <p>Demonstrates that CANNOT have more than one attribute with same name, even if using
     * {@link Element.Disposition.CONTENT} to force treating the attribute as an element in XML.</p>
     * Desired serialization to XML:
     * <pre>
     *     &lt;multi-content-attrs xmlns="urn:zimbraTest">
     *       &lt;soapURL>https://soap.example.test&lt;/soapURL>
     *     &lt;/multi-content-attrs>
     * </pre>
     * Desired serialization to JSON:
     * <pre>
     *     {
     *       "soapURL": "https://soap.example.test",
     *       "_jsns": "urn:zimbraTest"
     *     }
     * </pre>
     */
@Test
public void multipleDispositionCONTENTAttributes() throws Exception {
    final String httpSoap = "http://soap.example.test";
    final String httpsSoap = "https://soap.example.test";
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
    Element xmlElem = XMLElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
    jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
    jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
    xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
    xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
    logDebug("MultiContentAttrs XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    logDebug("MultiContentAttrs JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    Assert.assertEquals("XMLElement soapURL as attribute", httpsSoap, xmlElem.getAttribute(AccountConstants.E_SOAP_URL));
    Assert.assertEquals("XMLElement num soapURL elements", 1, xmlElem.listElements(AccountConstants.E_SOAP_URL).size());
    Assert.assertEquals("XMLElement soapURL as element", httpsSoap, xmlElem.getElement(AccountConstants.E_SOAP_URL).getText());
    Assert.assertEquals("JSONElement soapURL as attribute", httpsSoap, jsonElem.getAttribute(AccountConstants.E_SOAP_URL));
    // 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 soapURL elements", 0, jsonElem.listElements(AccountConstants.E_SOAP_URL).size());
}
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) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 34 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method zimbraOddKeyValuePairsAnnotation.

/**
     * Check that JSON can be deserialised into a JAXB object when some of the JSON represents Zimbra KeyValuePairs
     * (i.e. An "_attrs" array).
     * In this case, the target objects for each keyvalue pair do NOT use the defaults of "a" for the element name and
     * "n" for the attribute name in the XML form.
     * Desired XML :<br />
     * &lt;key-value-pairs-tester xmlns="urn:zimbraTest">
     *   &lt;oddElemName name="key1">value1&lt;/oddElemName>
     *   &lt;oddElemName name="key2">value2-a&lt;/oddElemName>
     *   &lt;oddElemName name="key2">value2-b&lt;/oddElemName>
     * &lt;/key-value-pairs-tester>
     *<br />
     * Desired JSON :<br />
     * {
     *   "_attrs": {
     *     "key1": "value1",
     *     "key2": [
     *       "value2-a",
     *       "value2-b"]
     *   },
     *   "_jsns": "urn:zimbraTest"
     * }
     */
@Test
public void zimbraOddKeyValuePairsAnnotation() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
    jsonElem.addKeyValuePair("key1", "value1", "oddElemName", "name");
    jsonElem.addKeyValuePair("key2", "value2-a", "oddElemName", "name");
    jsonElem.addKeyValuePair("key2", "value2-b", "oddElemName", "name");
    List<Attr> attrs = Lists.newArrayList();
    attrs.add(new Attr("key1", "value1"));
    attrs.add(new Attr("key2", "value2-a"));
    attrs.add(new Attr("key2", "value2-b"));
    OddKeyValuePairsTester jaxb = new OddKeyValuePairsTester(attrs);
    logDebug("XMLElement (from JAXB) ---> prettyPrint\n%1$s", JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false).prettyPrint());
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    String origJson = jsonJaxbElem.prettyPrint();
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", origJson);
    OddKeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, OddKeyValuePairsTester.class);
    List<com.zimbra.common.soap.Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
    Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
    Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
    List<Attr> kvps = roundtripped.getAttrList();
    Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
    jsonJaxbElem = JacksonUtil.jaxbToJSONElement(roundtripped);
    String finalJson = jsonJaxbElem.prettyPrint();
    if (!origJson.equals(finalJson)) {
        logDebug("JSONElement from roundtripped JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
        Assert.assertEquals("roundtripped JSON pretty print", origJson, finalJson);
    }
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) 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) XmlElemJsonAttr(com.zimbra.soap.jaxb.XmlElemJsonAttr) Attr(com.zimbra.soap.account.type.Attr) OddKeyValuePairsTester(com.zimbra.soap.jaxb.OddKeyValuePairsTester) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 35 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method contentList.

/**
     * Test for "List of strings" field annotated with {@link XmlElement}.
     * Desired JSON:
     * {
     *   "more": false,
     *   "total": 23,
     *   "dlm": [
     *     {
     *       "_content": "dlmember1@no.where"
     *     },
     *     {
     *       "_content": "dlmember2@no.where"
     *     },
     *     {
     *       "_content": "dlmember3@no.where"
     *     }],
     *   "_jsns": "urn:zimbraAccount"
     * }
     */
@Test
public void contentList() throws Exception {
    Element legacyElem = JSONElement.mFactory.createElement(AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
    legacyElem.addAttribute(AccountConstants.A_MORE, false);
    legacyElem.addAttribute(AccountConstants.A_TOTAL, 23);
    legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember1@no.where");
    legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember2@no.where");
    legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember3@no.where");
    logDebug("GetDistributionListMembersResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
    // GetDistributionListMembersResponse has:
    //      @XmlElement(name=AccountConstants.E_DLM, required=false)
    //      private List<String> dlMembers = Lists.newArrayList();
    GetDistributionListMembersResponse jaxb = new GetDistributionListMembersResponse();
    jaxb.setMore(false);
    jaxb.setTotal(23);
    jaxb.addDlMember("dlmember1@no.where");
    jaxb.addDlMember("dlmember2@no.where");
    jaxb.addDlMember("dlmember3@no.where");
    Element elem = JacksonUtil.jaxbToJSONElement(jaxb, AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
    logDebug("GetDistributionListMembersResponse JSONElement from JAXB ---> prettyPrint\n%1$s", elem.prettyPrint());
    List<Element> dlMembers = elem.listElements(AccountConstants.E_DLM);
    Assert.assertEquals("Number of dlMembers", 3, dlMembers.size());
    Element dlMem3 = dlMembers.get(2);
    Assert.assertEquals("dlMember 3", "dlmember3@no.where", dlMem3.getText());
    Assert.assertEquals("total", 23, elem.getAttributeInt(AccountConstants.A_TOTAL));
    Assert.assertEquals("more", false, elem.getAttributeBool(AccountConstants.A_MORE));
    Assert.assertEquals("prettyPrint", legacyElem.prettyPrint(), elem.prettyPrint());
}
Also used : GetDistributionListMembersResponse(com.zimbra.soap.account.message.GetDistributionListMembersResponse) 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) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

XmlElement (javax.xml.bind.annotation.XmlElement)51 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)26 Element (com.zimbra.common.soap.Element)25 JSONElement (com.zimbra.common.soap.Element.JSONElement)25 XMLElement (com.zimbra.common.soap.Element.XMLElement)25 Test (org.junit.Test)25 FilterTest (com.zimbra.soap.mail.type.FilterTest)24 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)8 Field (java.lang.reflect.Field)7 XmlElements (javax.xml.bind.annotation.XmlElements)7 KeyValuePair (com.zimbra.soap.type.KeyValuePair)5 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)5 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)3 Annotation (java.lang.annotation.Annotation)3 TypeMirror (javax.lang.model.type.TypeMirror)3 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)3 QName (javax.xml.namespace.QName)3 GetDistributionListResponse (com.zimbra.soap.account.message.GetDistributionListResponse)2