Search in sources :

Example 11 with RandomDataSource

use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.

the class TestCloneNodeBinary method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    DataSource ds = new RandomDataSource(666L, 1000);
    Text text = (Text) factory.createOMText(new DataHandler(ds), false);
    String base64 = text.getData();
    assertTrue(base64.length() > 0);
    assertEquals(base64, ((Text) text.cloneNode(true)).getData());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) Text(org.w3c.dom.Text) DataHandler(javax.activation.DataHandler) DataSource(javax.activation.DataSource) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource)

Example 12 with RandomDataSource

use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.

the class TestGetNodeValueBinary method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    DataSource ds = new RandomDataSource(666L, 1000);
    Text text = (Text) factory.createOMText(new DataHandler(ds), false);
    String nodeValue = text.getNodeValue();
    assertThat(nodeValue).isNotNull();
    IOTestUtils.compareStreams(ds.getInputStream(), new ByteArrayInputStream(Base64.decodeBase64(nodeValue)));
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) ByteArrayInputStream(java.io.ByteArrayInputStream) Text(org.w3c.dom.Text) DataHandler(javax.activation.DataHandler) DataSource(javax.activation.DataSource) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource)

Example 13 with RandomDataSource

use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.

the class DataSourceRegistryTest method test.

public void test() throws Exception {
    RandomDataSource ds = new RandomDataSource(1000);
    DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds);
    try {
        // We must be able to connect to the URL after converting it to a String
        URL url = new URL(registration.getURL().toString());
        URLConnection connection = url.openConnection();
        IOTestUtils.compareStreams(connection.getInputStream(), "actual", ds.getInputStream(), "expected");
    } finally {
        registration.unregister();
    }
}
Also used : RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSourceRegistration(org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 14 with RandomDataSource

use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.

the class AttachmentsTest method testDataHandlerStreaming.

/**
 * Tests that a call to {@link DataHandlerExt#readOnce()} on a {@link DataHandler} returned by
 * the {@link Attachments} object streams the content of the MIME part.
 *
 * @throws Exception
 */
public void testDataHandlerStreaming() throws Exception {
    // Note: We are only interested in the MimeMultipart, but we need to create a
    // MimeMessage to be able to calculate the correct content type
    MimeMessage message = new MimeMessage((Session) null);
    final MimeMultipart mp = new MimeMultipart("related");
    // Prepare the "SOAP" part
    MimeBodyPart bp1 = new MimeBodyPart();
    // Obviously this is not SOAP, but this is irrelevant for this test
    bp1.setText("<root/>", "utf-8", "xml");
    bp1.addHeader("Content-Transfer-Encoding", "binary");
    bp1.addHeader("Content-ID", "part1@apache.org");
    mp.addBodyPart(bp1);
    // Create an attachment that is larger than the maximum heap
    DataSource dataSource = new RandomDataSource((int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE));
    MimeBodyPart bp2 = new MimeBodyPart();
    bp2.setDataHandler(new DataHandler(dataSource));
    bp2.addHeader("Content-Transfer-Encoding", "binary");
    bp2.addHeader("Content-ID", "part2@apache.org");
    mp.addBodyPart(bp2);
    message.setContent(mp);
    // Compute the correct content type
    message.saveChanges();
    // We use a pipe (with a producer running in a separate thread) because obviously we can't
    // store the multipart in memory.
    final PipedOutputStream pipeOut = new PipedOutputStream();
    PipedInputStream pipeIn = new PipedInputStream(pipeOut);
    Thread producerThread = new Thread(new Runnable() {

        public void run() {
            try {
                try {
                    mp.writeTo(pipeOut);
                } finally {
                    pipeOut.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    producerThread.start();
    try {
        // We configure Attachments to buffer MIME parts in memory. If the part content is not
        // streamed, then this will result in an OOM error.
        Attachments attachments = new Attachments(pipeIn, message.getContentType());
        DataHandlerExt dh = (DataHandlerExt) attachments.getDataHandler("part2@apache.org");
        IOTestUtils.compareStreams(dataSource.getInputStream(), dh.readOnce());
    } finally {
        pipeIn.close();
    }
}
Also used : RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) PipedOutputStream(java.io.PipedOutputStream) DataHandlerExt(org.apache.axiom.attachments.lifecycle.DataHandlerExt) DataHandler(javax.activation.DataHandler) PipedInputStream(java.io.PipedInputStream) MIMEException(org.apache.axiom.mime.MIMEException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SizeAwareDataSource(org.apache.axiom.ext.activation.SizeAwareDataSource) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)14 DataHandler (javax.activation.DataHandler)10 DataSource (javax.activation.DataSource)10 OMFactory (org.apache.axiom.om.OMFactory)9 OMElement (org.apache.axiom.om.OMElement)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStreamReader (java.io.InputStreamReader)3 OutputStream (java.io.OutputStream)3 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 QName (javax.xml.namespace.QName)3 MemoryBlob (org.apache.axiom.blob.MemoryBlob)3 OMText (org.apache.axiom.om.OMText)3 Charset (java.nio.charset.Charset)2 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)2 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)2 WrappedTextNodeOMDataSourceFromDataSource (org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource)2 Text (org.w3c.dom.Text)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1