OMFactory factory = metaFactory.getOMFactory();
// Programmatically create the message
OMElement orgRoot = factory.createOMElement("root", null);
OMElement orgChild1 = factory.createOMElement("child1", null, orgRoot);
DataSource ds = new RandomDataSource(54321, 4096);
orgChild1.addChild(factory.createOMText(new DataHandler(ds), true));
// Create a child with a large text content and insert it after the binary node.
// If we don't do this, then the root part may be buffered entirely by the parser,
// and the test would not be effective.
OMElement orgChild2 = factory.createOMElement("child2", null, orgRoot);
String s = RandomUtils.randomString(128*1024);
orgChild2.setText(s);
// Serialize the message
OMOutputFormat format = new OMOutputFormat();
format.setDoOptimize(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
orgRoot.serialize(baos, format);
// Parse the message
OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory,
StAXParserConfiguration.NON_COALESCING,
new Attachments(new ByteArrayInputStream(baos.toByteArray()),
format.getContentType()));
OMElement root = builder.getDocumentElement();
OMElement child1 = (OMElement)root.getFirstOMChild();
OMText text = (OMText)child1.getFirstOMChild();
assertTrue(text.isBinary());
// Access the DataHandler
DataHandler dh = (DataHandler)text.getDataHandler();
IOTestUtils.compareStreams(ds.getInputStream(), dh.getInputStream());
OMElement child2 = (OMElement)child1.getNextOMSibling();
assertFalse(child2.isComplete());
assertEquals(s, child2.getText());
}