use of javax.xml.stream.XMLOutputFactory in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallIntoStaxStreamWriter.
@Test
public void testMarshallIntoStaxStreamWriter() throws Exception {
GreetMe obj = new GreetMe();
obj.setRequestType("Hello");
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
FixNamespacesXMLStreamWriter writer = new FixNamespacesXMLStreamWriter(opFactory.createXMLStreamWriter(baos));
assertNull(writer.getMarshaller());
Marshaller m = context.createMarshaller();
JAXBEncoderDecoder.marshall(m, obj, part, writer);
assertEquals(m, writer.getMarshaller());
writer.flush();
writer.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
XMLInputFactory ipFactory = XMLInputFactory.newInstance();
XMLEventReader reader = ipFactory.createXMLEventReader(bais);
Unmarshaller um = context.createUnmarshaller();
Object val = um.unmarshal(reader, GreetMe.class);
assertTrue(val instanceof JAXBElement);
val = ((JAXBElement<?>) val).getValue();
assertTrue(val instanceof GreetMe);
assertEquals(obj.getRequestType(), ((GreetMe) val).getRequestType());
}
Aggregations