Search in sources :

Example 11 with ElementReader

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

the class AbstractEncodedTest method readWriteReadRef.

public <T> T readWriteReadRef(String file, Class<T> typeClass) throws XMLStreamException {
    Context context = getContext();
    AegisType type = mapping.getType(typeClass);
    assertNotNull("no type found for " + typeClass.getName());
    // read file
    ElementReader reader = new ElementReader(getClass().getResourceAsStream(file));
    T value = typeClass.cast(type.readObject(reader, context));
    reader.getXMLStreamReader().close();
    // write value to element
    Element element = writeRef(value);
    // reread value from element
    value = typeClass.cast(readRef(element));
    return value;
}
Also used : MapNamespaceContext(org.apache.cxf.helpers.MapNamespaceContext) Context(org.apache.cxf.aegis.Context) AegisContext(org.apache.cxf.aegis.AegisContext) AegisType(org.apache.cxf.aegis.type.AegisType) Element(org.w3c.dom.Element) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader)

Example 12 with ElementReader

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

the class SoapArrayTypeTest method testStructArray.

@Test
public void testStructArray() throws Exception {
    Context context = getContext();
    // b:address[2]
    ElementReader reader = new ElementReader(getClass().getResourceAsStream("arrayStructs.xml"));
    Address[] addresses = (Address[]) createArrayType(Address[].class).readObject(reader, context);
    reader.getXMLStreamReader().close();
    AbstractEncodedTest.validateShippingAddress(addresses[0]);
    AbstractEncodedTest.validateBillingAddress(addresses[1]);
    // round trip tests
    addresses = readWriteReadRef("arrayStructs.xml", Address[].class);
    AbstractEncodedTest.validateShippingAddress(addresses[0]);
    AbstractEncodedTest.validateBillingAddress(addresses[1]);
}
Also used : Context(org.apache.cxf.aegis.Context) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test)

Example 13 with ElementReader

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

the class SoapArrayTypeTest method testPartiallyTransmitted.

@Test
public void testPartiallyTransmitted() throws Exception {
    Context context = getContext();
    // xsd:int[5] offset="[2]"
    ElementReader reader = new ElementReader(getClass().getResourceAsStream("arrayPartiallyTransmitted.xml"));
    int[] numbers = (int[]) createArrayType(int[].class).readObject(reader, context);
    reader.getXMLStreamReader().close();
    assertArrayEquals(new int[] { 0, 0, 3, 4, 0 }, numbers);
    // round trip tests
    numbers = readWriteReadRef("arrayPartiallyTransmitted.xml", int[].class);
    assertArrayEquals(new int[] { 0, 0, 3, 4, 0 }, numbers);
}
Also used : Context(org.apache.cxf.aegis.Context) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test)

Example 14 with ElementReader

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

the class SoapArrayTypeTest method testUrTypeArray.

@Test
public void testUrTypeArray() throws Exception {
    Context context = getContext();
    // ur-type[4] nested elements have xsi:type
    ElementReader reader = new ElementReader(getClass().getResourceAsStream("arrayUrType1.xml"));
    Object[] objects = (Object[]) createArrayType(Object[].class).readObject(reader, context);
    reader.getXMLStreamReader().close();
    assertArrayEquals(new Object[] { 42, (float) 42.42, "Forty Two" }, objects);
    // ur-type[4] nested element name have a global schema type
    reader = new ElementReader(getClass().getResourceAsStream("arrayUrType2.xml"));
    objects = (Object[]) createArrayType(Object[].class).readObject(reader, context);
    reader.getXMLStreamReader().close();
    assertArrayEquals(Arrays.asList(objects).toString(), new Object[] { 42, new BigDecimal("42.42"), "Forty Two" }, objects);
}
Also used : Context(org.apache.cxf.aegis.Context) BigDecimal(java.math.BigDecimal) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test)

Example 15 with ElementReader

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

the class StructTypeTest method testComplexStruct.

@Test
public void testComplexStruct() throws Exception {
    // Test reading
    ElementReader reader = new ElementReader(getClass().getResourceAsStream("struct3.xml"));
    PurchaseOrder po = (PurchaseOrder) purchaseOrderType.readObject(reader, getLocalContext());
    validatePurchaseOrder(po);
    reader.getXMLStreamReader().close();
    // Test reading - no namespace on nested elements
    reader = new ElementReader(getClass().getResourceAsStream("struct4.xml"));
    po = (PurchaseOrder) purchaseOrderType.readObject(reader, getLocalContext());
    validatePurchaseOrder(po);
    reader.getXMLStreamReader().close();
    // Test writing
    Element element = writeRef(po);
    validatePurchaseOrder(element);
    // Test reading - no namespace on nested elements, xsi:nil (CXF-2695)
    reader = new ElementReader(getClass().getResourceAsStream("struct5.xml"));
    po = (PurchaseOrder) purchaseOrderType.readObject(reader, getLocalContext());
    validatePurchaseOrder(po, true);
    reader.getXMLStreamReader().close();
}
Also used : Element(org.w3c.dom.Element) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test)

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