Search in sources :

Example 6 with ElementReader

use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.

the class BeanTest method testAttributeMap.

@Test
public void testAttributeMap() throws Exception {
    defaultContext();
    BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean");
    info.mapAttribute("howdy", new QName("urn:Bean", "howdy"));
    info.mapAttribute("bleh", new QName("urn:Bean", "bleh"));
    info.setTypeMapping(mapping);
    BeanType type = new BeanType(info);
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:Bean", "bean"));
    ElementReader reader = new ElementReader(getResourceAsStream("bean4.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // Test writing
    Element element = writeObjectToElement(type, bean, getContext());
    assertValid("/b:root[@b:bleh='bleh']", element);
    assertValid("/b:root[@b:howdy='howdy']", element);
    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);
    XmlSchemaComplexType stype = (XmlSchemaComplexType) schema.getTypeByName("bean");
    boolean howdy = false;
    boolean bleh = false;
    for (int x = 0; x < stype.getAttributes().size(); x++) {
        XmlSchemaObject o = stype.getAttributes().get(x);
        if (o instanceof XmlSchemaAttribute) {
            XmlSchemaAttribute a = (XmlSchemaAttribute) o;
            if ("howdy".equals(a.getName())) {
                howdy = true;
            }
            if ("bleh".equals(a.getName())) {
                bleh = true;
            }
        }
    }
    assertTrue(howdy);
    assertTrue(bleh);
}
Also used : XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 7 with ElementReader

use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.

the class BeanTest method testUnmappedProperty.

@Test
public void testUnmappedProperty() throws Exception {
    defaultContext();
    String ns = "urn:Bean";
    BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, ns, false);
    QName name = new QName(ns, "howdycustom");
    info.mapElement("howdy", name);
    info.setTypeMapping(mapping);
    assertEquals("howdy", info.getPropertyDescriptorFromMappedName(name).getName());
    BeanType type = new BeanType(info);
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:Bean", "bean"));
    ElementReader reader = new ElementReader(getResourceAsStream("bean3.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("howdy", bean.getHowdy());
    assertNull(bean.getBleh());
    reader.getXMLStreamReader().close();
    Element element = writeObjectToElement(type, bean, getContext());
    assertInvalid("/b:root/b:bleh", element);
    assertValid("/b:root/b:howdycustom[text()='howdy']", element);
}
Also used : QName(javax.xml.namespace.QName) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 8 with ElementReader

use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.

the class BeanTest method testAttributeMapDifferentNS.

@Test
public void testAttributeMapDifferentNS() throws Exception {
    defaultContext();
    BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean");
    info.mapAttribute("howdy", new QName("urn:Bean2", "howdy"));
    info.mapAttribute("bleh", new QName("urn:Bean2", "bleh"));
    info.setTypeMapping(mapping);
    BeanType type = new BeanType(info);
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:Bean", "bean"));
    ElementReader reader = new ElementReader(getResourceAsStream("bean8.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // Test writing
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ElementWriter writer = new ElementWriter(bos, "root", "urn:Bean");
    type.writeObject(bean, writer, getContext());
    writer.close();
    writer.flush();
    bos.close();
    Document doc = StaxUtils.read(new ByteArrayInputStream(bos.toByteArray()));
    Element element = doc.getDocumentElement();
    addNamespace("b2", "urn:Bean2");
    assertValid("/b:root[@b2:bleh='bleh']", element);
    assertValid("/b:root[@b2:howdy='howdy']", element);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter) Document(org.w3c.dom.Document) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 9 with ElementReader

use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.

the class DynamicProxyTest method testDynamicProxyMissingAttribute.

@Test
public void testDynamicProxyMissingAttribute() throws Exception {
    BeanType type = new BeanType();
    type.setTypeClass(IMyInterface.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:MyInterface", "data"));
    ElementReader reader = new ElementReader(getResourceAsStream("MyInterface.xml"));
    IMyInterface data = (IMyInterface) type.readObject(reader, getContext());
    assertEquals("junk", data.getName());
    assertNull(data.getType());
}
Also used : QName(javax.xml.namespace.QName) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Example 10 with ElementReader

use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.

the class DynamicProxyTest method testDynamicProxyNonStandardGetter.

@Test
public void testDynamicProxyNonStandardGetter() throws Exception {
    BeanType type = new BeanType();
    type.setTypeClass(IMyInterface.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:MyInterface", "data"));
    ElementReader reader = new ElementReader(getResourceAsStream("MyInterface.xml"));
    IMyInterface data = (IMyInterface) type.readObject(reader, getContext());
    try {
        data.getNameById(0);
        fail(IllegalAccessError.class + " should be thrown.");
    } catch (IllegalAccessError e) {
    // do nothing
    }
    try {
        data.get();
        fail(IllegalAccessError.class + " should be thrown.");
    } catch (IllegalAccessError e) {
    // do nothing
    }
}
Also used : QName(javax.xml.namespace.QName) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Aggregations

ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)34 Test (org.junit.Test)26 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)16 QName (javax.xml.namespace.QName)13 Element (org.w3c.dom.Element)13 Context (org.apache.cxf.aegis.Context)11 XmlElement (javax.xml.bind.annotation.XmlElement)7 AegisType (org.apache.cxf.aegis.type.AegisType)7 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)7 SimpleBean (org.apache.cxf.aegis.services.SimpleBean)6 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 AegisContext (org.apache.cxf.aegis.AegisContext)3 DatabindingException (org.apache.cxf.aegis.DatabindingException)3 BigDecimal (java.math.BigDecimal)2 MapNamespaceContext (org.apache.cxf.helpers.MapNamespaceContext)2 XmlSchema (org.apache.ws.commons.schema.XmlSchema)2 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1