// Construct the original message
DocumentBean object = new DocumentBean();
object.setId("123456");
object.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, object));
orgEnvelope.getBody().addChild(element);
// Serialize the message
OMOutputFormat format = new OMOutputFormat();
format.setDoOptimize(true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
orgEnvelope.serialize(out, format);
assertFalse(element.isExpanded());
// Parse the serialized message
Attachments att = new Attachments(new ByteArrayInputStream(out.toByteArray()), format.getContentType());
assertEquals(2, att.getAllContentIDs().length);
SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(att).getSOAPEnvelope();
OMElement contentElement = envelope.getBody().getFirstElement().getFirstChildWithName(
new QName("http://ws.apache.org/axiom/test/jaxb", "content"));
OMText content = (OMText)contentElement.getFirstOMChild();
assertTrue(content.isBinary());
assertTrue(content.isOptimized());
DataHandler dh = (DataHandler)content.getDataHandler();