Search in sources :

Example 1 with InstrumentedInputStream

use of org.apache.axiom.testutils.io.InstrumentedInputStream in project webservices-axiom by apache.

the class TestBuilderDetach method runTest.

@Override
protected void runTest() throws Throwable {
    MTOMSample sample = MTOMSample.SAMPLE1;
    InstrumentedInputStream in = new InstrumentedInputStream(sample.getInputStream());
    MultipartBody mb = MultipartBody.builder().setInputStream(in).setContentType(sample.getContentType()).build();
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb);
    SOAPEnvelope envelope = builder.getSOAPEnvelope();
    long countBeforeDetach = in.getCount();
    builder.detach();
    assertThat(in.getCount()).isGreaterThan(countBeforeDetach);
    assertThat(in.isClosed()).isFalse();
    int binaryCount = 0;
    for (Iterator<OMNode> it = envelope.getDescendants(false); it.hasNext(); ) {
        OMNode node = it.next();
        if (node instanceof OMText) {
            OMText text = (OMText) node;
            if (text.isBinary()) {
                IOTestUtils.compareStreams(sample.getPart(text.getContentID()), text.getDataHandler().getInputStream());
                binaryCount++;
            }
        }
    }
    assertThat(binaryCount).isGreaterThan(0);
    in.close();
}
Also used : OMNode(org.apache.axiom.om.OMNode) MultipartBody(org.apache.axiom.mime.MultipartBody) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) OMText(org.apache.axiom.om.OMText) MTOMSample(org.apache.axiom.ts.soap.MTOMSample) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 2 with InstrumentedInputStream

use of org.apache.axiom.testutils.io.InstrumentedInputStream in project webservices-axiom by apache.

the class TestBuild method runTest.

@Override
protected void runTest() throws Throwable {
    InstrumentedInputStream in = new InstrumentedInputStream(XMLSample.LARGE.getInputStream());
    OMDocument doc = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocument();
    assertFalse(doc.isComplete());
    long countBeforeBuild = in.getCount();
    doc.build();
    assertTrue(doc.isComplete());
    long countAfterBuild = in.getCount();
    assertTrue(countAfterBuild > countBeforeBuild);
    OMNode node = doc.getFirstOMChild();
    while (node != null) {
        node = node.getNextOMSibling();
    }
    assertEquals(countAfterBuild, in.getCount());
}
Also used : OMNode(org.apache.axiom.om.OMNode) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) OMDocument(org.apache.axiom.om.OMDocument)

Example 3 with InstrumentedInputStream

use of org.apache.axiom.testutils.io.InstrumentedInputStream 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);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 4 with InstrumentedInputStream

use of org.apache.axiom.testutils.io.InstrumentedInputStream in project webservices-axiom by apache.

the class TestReadFrom method runTest.

@Override
protected void runTest(WritableBlob blob) throws Throwable {
    Random random = new Random();
    byte[] data = new byte[size];
    random.nextBytes(data);
    InstrumentedInputStream in = new InstrumentedInputStream(new ByteArrayInputStream(data));
    assertThat(blob.readFrom(in)).isEqualTo(size);
    assertThat(in.isClosed()).isFalse();
    InputStream in2 = blob.getInputStream();
    try {
        assertThat(IOUtils.toByteArray(in2)).isEqualTo(data);
    } finally {
        in2.close();
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) InputStream(java.io.InputStream) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream)

Example 5 with InstrumentedInputStream

use of org.apache.axiom.testutils.io.InstrumentedInputStream in project webservices-axiom by apache.

the class TestCloseWithoutCaching method runTest.

@Override
protected void runTest() throws Throwable {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(baos, "UTF-8");
    writer.write("<root><a>");
    for (int i = 0; i < 20000; i++) {
        writer.write('a');
    }
    writer.write("</a></root>");
    writer.close();
    InstrumentedInputStream in = new InstrumentedInputStream(new ByteArrayInputStream(baos.toByteArray()));
    OMDocument doc = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocument();
    XMLStreamReader reader = doc.getXMLStreamReaderWithoutCaching();
    reader.next();
    reader.next();
    long count = in.getCount();
    reader.close();
    assertEquals(count, in.getCount());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) OMDocument(org.apache.axiom.om.OMDocument)

Aggregations

InstrumentedInputStream (org.apache.axiom.testutils.io.InstrumentedInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 OMDocument (org.apache.axiom.om.OMDocument)2 OMNode (org.apache.axiom.om.OMNode)2 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Random (java.util.Random)1 MultipartBody (org.apache.axiom.mime.MultipartBody)1 OMElement (org.apache.axiom.om.OMElement)1 OMFactory (org.apache.axiom.om.OMFactory)1 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)1 OMText (org.apache.axiom.om.OMText)1 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAPModelBuilder (org.apache.axiom.soap.SOAPModelBuilder)1 MTOMSample (org.apache.axiom.ts.soap.MTOMSample)1