use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.
the class TestIOExceptionInGetText method runTest.
@Override
protected void runTest() throws Throwable {
// Construct a stream that will throw an exception in the middle of a text node.
// We need to create a very large document, because some parsers (such as some
// versions of XLXP) have a large input buffer and would throw an exception already
// when the XMLStreamReader is created.
StringBuffer xml = new StringBuffer("<root>");
for (int i = 0; i < 100000; i++) {
xml.append('x');
}
InputStream in = new ExceptionInputStream(new ByteArrayInputStream(xml.toString().getBytes("ASCII")));
XMLStreamReader originalReader = StAXUtils.createXMLStreamReader(in);
InvocationCounter invocationCounter = new InvocationCounter();
XMLStreamReader reader = (XMLStreamReader) invocationCounter.createProxy(originalReader);
OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader).getDocumentElement();
try {
element.getNextOMSibling();
fail("Expected exception");
} catch (Exception ex) {
// Expected
}
assertTrue(invocationCounter.getInvocationCount() > 0);
invocationCounter.reset();
try {
element.getNextOMSibling();
fail("Expected exception");
} catch (Exception ex) {
// Expected
}
assertEquals(0, invocationCounter.getInvocationCount());
}
use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.
the class TestCreateStAXOMBuilderIncorrectState method runTest.
@Override
protected void runTest() throws Throwable {
XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<root>text</root>"));
// Position the reader on a CHARACTERS event
while (reader.getEventType() != XMLStreamReader.CHARACTERS) {
reader.next();
}
try {
OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader);
fail("Expected OMException");
} catch (OMException ex) {
// Expected
}
}
use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.
the class TestCreateStAXOMBuilderFromFragment method runTest.
@Override
protected void runTest() throws Throwable {
XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a><b>text</b></a>"));
// Position the reader on the event for <b>
while (reader.getEventType() != XMLStreamReader.START_ELEMENT || !reader.getLocalName().equals("b")) {
reader.next();
}
// Check that the builder only builds the part of the document corresponding to <b>text</b>
OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader).getDocumentElement();
assertEquals("b", element.getLocalName());
OMNode child = element.getFirstOMChild();
assertTrue(child instanceof OMText);
assertNull(element.getNextOMSibling());
// Check that the original reader is now positioned on the event just following </b>
assertEquals(XMLStreamReader.END_ELEMENT, reader.getEventType());
assertEquals("a", reader.getLocalName());
}
use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.
the class TestRootPartStreaming method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
// Programmatically create the message
OMElement orgRoot = factory.createOMElement("root", null);
for (int i = 0; i < 10000; i++) {
factory.createOMElement("child", null, orgRoot).setText("Some text content");
}
// Serialize the message as XOP even if there will be no attachment parts
OMOutputFormat format = new OMOutputFormat();
format.setDoOptimize(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
orgRoot.serialize(baos, format);
// Parse the message and monitor the number of bytes read
InstrumentedInputStream in = new InstrumentedInputStream(new ByteArrayInputStream(baos.toByteArray()));
OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, StAXParserConfiguration.DEFAULT, MultipartBody.builder().setInputStream(in).setContentType(format.getContentType()).build());
OMElement root = builder.getDocumentElement();
long count1 = in.getCount();
XMLStreamReader reader = root.getXMLStreamReader(false);
while (reader.hasNext()) {
reader.next();
}
long count2 = in.getCount();
// We expect that after requesting the document element, only a small part (corresponding to
// the size of the parser buffer) should have been read:
assertTrue(count1 < count2 / 2);
}
use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.
the class TestCommentEvent method runTest.
@Override
protected void runTest() throws Throwable {
OMXMLParserWrapper builder = builderFactory.getBuilder(metaFactory, new InputSource(new StringReader("<a><!--comment text--></a>")));
OMElement element = builder.getDocumentElement();
XMLStreamReader reader = element.getXMLStreamReader(cache);
assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
assertEquals(XMLStreamReader.COMMENT, reader.next());
assertEquals("comment text", reader.getText());
assertEquals("comment text", new String(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()));
StringBuffer text = new StringBuffer();
char[] buf = new char[5];
for (int sourceStart = 0; ; sourceStart += buf.length) {
int nCopied = reader.getTextCharacters(sourceStart, buf, 0, buf.length);
text.append(buf, 0, nCopied);
if (nCopied < buf.length) {
break;
}
}
assertEquals("comment text", text.toString());
element.close(false);
}
Aggregations