use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.
the class StructTypeTest method testSimpleStruct.
@Test
public void testSimpleStruct() throws Exception {
// Test reading
ElementReader reader = new ElementReader(getClass().getResourceAsStream("struct1.xml"));
Address address = (Address) addressType.readObject(reader, getLocalContext());
validateShippingAddress(address);
reader.getXMLStreamReader().close();
// Test reading - no namespace on nested elements
reader = new ElementReader(getClass().getResourceAsStream("struct2.xml"));
address = (Address) addressType.readObject(reader, getLocalContext());
validateShippingAddress(address);
reader.getXMLStreamReader().close();
// Test writing
Element element = writeObjectToElement(addressType, address, getLocalContext());
validateShippingAddress(element);
}
use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.
the class EnumTypeTest method testTypeWithJaxbAnnotations.
/**
* {@link https://issues.apache.org/jira/browse/CXF-7188}
*/
@Test
public void testTypeWithJaxbAnnotations() throws Exception {
AegisType type = tm.getTypeCreator().createType(JaxbTestEnum.class);
Element element = writeObjectToElement(type, JaxbTestEnum.VALUE1, getContext());
assertEquals("Value1", element.getTextContent());
XMLStreamReader xreader = StaxUtils.createXMLStreamReader(element);
ElementReader reader = new ElementReader(xreader);
Object value = type.readObject(reader, getContext());
assertEquals(JaxbTestEnum.VALUE1, value);
}
use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.
the class EnumTypeTest method testType.
@Test
public void testType() throws Exception {
EnumType type = new EnumType();
type.setTypeClass(SmallEnum.class);
type.setSchemaType(new QName("urn:test", "test"));
tm.register(type);
Element element = writeObjectToElement(type, SmallEnum.VALUE1, getContext());
assertEquals("VALUE1", element.getTextContent());
XMLStreamReader xreader = StaxUtils.createXMLStreamReader(element);
ElementReader reader = new ElementReader(xreader);
Object value = type.readObject(reader, getContext());
assertEquals(SmallEnum.VALUE1, value);
}
use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.
the class DocumentType method readObject.
@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
try {
XMLStreamReader reader = ((ElementReader) mreader).getXMLStreamReader();
// we need to eat the surrounding element.
reader.nextTag();
Object tree = StaxUtils.read(null, new FragmentStreamReader(reader), true);
// eat the end tag.
reader.nextTag();
return tree;
} catch (XMLStreamException e) {
throw new DatabindingException("Could not parse xml.", e);
}
}
use of org.apache.cxf.aegis.xml.stax.ElementReader in project cxf by apache.
the class BeanTest method testBean.
@Test
public void testBean() throws Exception {
defaultContext();
BeanType type = new BeanType();
type.setTypeClass(SimpleBean.class);
type.setTypeMapping(mapping);
type.setSchemaType(new QName("urn:Bean", "bean"));
// Test reading
ElementReader reader = new ElementReader(getResourceAsStream("bean1.xml"));
SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
assertEquals("bleh", bean.getBleh());
assertEquals("howdy", bean.getHowdy());
reader.getXMLStreamReader().close();
// Test reading with extra elements
reader = new ElementReader(getResourceAsStream("bean2.xml"));
bean = (SimpleBean) type.readObject(reader, getContext());
assertEquals("bleh", bean.getBleh());
assertEquals("howdy", bean.getHowdy());
reader.getXMLStreamReader().close();
// test <bleh/> element
reader = new ElementReader(getResourceAsStream("bean7.xml"));
bean = (SimpleBean) type.readObject(reader, getContext());
assertEquals("", bean.getBleh());
assertEquals("howdy", bean.getHowdy());
reader.getXMLStreamReader().close();
bean.setBleh("bleh");
// Test writing
Element element = writeObjectToElement(type, bean, getContext());
assertValid("/b:root/b:bleh[text()='bleh']", element);
assertValid("/b:root/b:howdy[text()='howdy']", element);
}
Aggregations